Jump to content

  • Log In with Google      Sign In   
  • Create Account

NUCLEAR RABBIT

Member Since 12 Apr 2006
Offline Last Active Oct 07 2014 03:20 PM

#5185612 Polymorphism and Interfaces in Java

Posted by NUCLEAR RABBIT on 07 October 2014 - 02:23 PM

Hello,

 

So I recently learned about Interfaces and thought I had a good grasp, but I'm not sure why this example does what it does. It creates an interface object and assign it to an ArrayQueue<T>() object. Why not just write it as an ArrayQueue object to begin with, is there an advantage? I think my question relates more to polymorphism maybe, but I also thought that interfaces aren't objects that are initialized, just implemented from in another class.

 

Can anyone help clear the air? I would really appreciate any help! huh.png

Attached Thumbnails

  • Screen Shot 2014-10-07 at 1.15.22 PM.png



#5155138 Writing/Reading from files help needed! (C Programming)

Posted by NUCLEAR RABBIT on 21 May 2014 - 05:13 PM

hello, I am trying to write a program that makes 10 empty entries in my file, updating the first 3, and then asking the user which one to erase back to a blank entry, and then reprints the file contents again with the entry deleted. It seems to work okay ONLY if you delete the first entry, if you try to delete the 2nd or 3rd, the output goes all crazy! Any help would be greatly appreciated! :(

#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>

typedef struct person {
    char first_name[15];
    char last_name[15];
    char age[4];
    int accNum;
} Person;

int main(int argc, char * argv[])
{
    FILE * file = fopen("/Users/MYNAME/Desktop/person.txt", "r+");
    
    Person blankEntry = {"No First Name", "No Last Name", "0", 0};
    Person person1 = blankEntry;
    
    printf("Records:\n");
    for(int i = 1; i <= 10; i++) {
        fwrite(blankEntry.first_name, 1, sizeof(blankEntry.first_name), file);
        fwrite(blankEntry.last_name, 1, sizeof(blankEntry.last_name), file);
        fwrite(blankEntry.age, 1, sizeof(blankEntry.age), file);
        fprintf(file, "%d", i);
        
        printf("%d. %s\t\t\t%10s%10s\n", i, blankEntry.first_name, blankEntry.last_name, blankEntry.age);
    }
    
    printf("\n\nUpdating Record:\n");
    fseek(file, SEEK_SET, 0);
    for(int i = 1; i <= 3; i++) {
        printf("Enter in first name, last name, and age: ");
        scanf("%s %s %s", person1.first_name, person1.last_name, person1.age);
        
        fwrite(person1.first_name, 1, sizeof(person1.first_name), file);
        fwrite(person1.last_name, 1, sizeof(person1.last_name), file);
        fwrite(person1.age, 1, sizeof(person1.age), file);
        fprintf(file, "%d", i);
    }
    
    printf("\n\nRecords:\n");
    printf("First Name:\t\tLast Name:\t\tAge:\n");
    fseek(file, SEEK_SET, 0);
    for(int i = 1; i <= 10; i++) {
        fread(person1.first_name, 1, sizeof(person1.first_name), file);
        fread(person1.last_name, 1, sizeof(person1.last_name), file);
        fread(person1.age, 1, sizeof(person1.age), file);
        fscanf(file, "%d", &person1.accNum);
        
        printf("%i. %s\t\t%10s%10s\n", person1.accNum, person1.first_name, person1.last_name, person1.age);
    }
    
    int deleteKey;
    printf("\n\nEnter in account number to delete: ");
    scanf("%i", &deleteKey);
    
    fseek(file, (deleteKey - 1) * sizeof(struct person), SEEK_SET);
    fwrite(person1.first_name, 1, sizeof(person1.first_name), file);
    fwrite(person1.last_name, 1, sizeof(person1.last_name), file);
    fwrite(person1.age, 1, sizeof(person1.age), file);
    //fwrite(&blankEntry, sizeof(struct person), 1, file);
    
    printf("\n\nRecords:\n");
    printf("First Name:\t\tLast Name:\t\tAge:\n");
    fseek(file, SEEK_SET, 0);
    for(int i = 1; i <= 10; i++) {
        fread(person1.first_name, 1, sizeof(person1.first_name), file);
        fread(person1.last_name, 1, sizeof(person1.last_name), file);
        fread(person1.age, 1, sizeof(person1.age), file);
        fscanf(file, "%d", &person1.accNum);
        
        printf("%i. %s\t\t%10s%10s\n", person1.accNum, person1.first_name, person1.last_name, person1.age);
    }
    
    fclose(file);
    
    return 0;
}



#5135991 Recursion in C Programming: Confusion Begins

Posted by NUCLEAR RABBIT on 02 March 2014 - 09:55 PM

Hello, 

 

So I was working on a HW assignment in my C programming class, and we are learning about recursion. I guess the idea itself is pretty self explanatory, a function calling itself, but how it works is where I get lost! I got the assignment to work, but I'm not sure why what I coded worked. How does the function know what to return when there's 2 numbers inputted? It's just a function with 2 parameters and no variable changing so how does x and y get altered? I understood my previous assignment where we used recursion to return x to the nth power, but this one is a little less concrete with me.

 

Could anyone help me understand this a little more? Any help is greatly appreciated!

#include <stdio.h>

int gcd(int x, int y)
{
    // base case
    if(y == 0)
        return x;
    return gcd(y, x % y);
}

int main(int argc, const char * argv[])
{
    int x = 54;
    int y = 24;
    
    printf("GCD of %i and %i is %i\n", x, y, gcd(x,y));
    
    return 0;
}



#5132213 Struct Inheritance in C

Posted by NUCLEAR RABBIT on 17 February 2014 - 09:12 PM

The idea is that you'll be able to access and use a Manager as an Employee when you want to treat them as just another employee. For example, if you just wanted to do payroll, you don't care who's a manager or what their title is.

 

So you can create a function, for example, that takes an employee by pointer and prints out the payroll check, and you can pass it pointers to regular employees or pointers to managers by casting them as pointers to employees. The one function will handle both -- If you continue to access a Manager's Employee attributes through the Super member variable as you're doing in your code, then you'd need to separate functions.

 

You'd be doing functionally the same thing by passing the address of a manager's 'super' member variable -- but because its the first struct element, simply casting the Manager pointer to an Employee pointer achieves the same thing. All it says is "Treat this manager as a regular employee."

 

You're awesome!




#5132211 Struct Inheritance in C

Posted by NUCLEAR RABBIT on 17 February 2014 - 09:05 PM

What you have done is called "composition", not "inheritance". The good news is that composition is the right tool to use more often than inheritance.

The pointer cast is what makes inheritance look like inheritance: Some other part of the code can take a pointer to an Employee and do something with it, and if instead of giving it a pointer to an Employee you give it a pointer to a Manager (after casting), it would work just fine.

However, the main reason to use inheritance is not to build neat little hierarchies the way most OOP texts seem to suggest: The main reason is polymorphism, which means you can call a function that takes a pointer to Employee as an argument, and it might do something different depending on the specific type of employee that was passed. In C you would achieve this by making `Employee' have a function pointer as part of its data, so each derived type can provide an appropriate function to call there. (The mechanism that C++ uses is not exactly this; C++ compilers typically use a vtable instead (look it up).)

 

Sweet, thank you! Makes more sense now. And I'll will look into vtables 




#5132207 Struct Inheritance in C

Posted by NUCLEAR RABBIT on 17 February 2014 - 08:50 PM

Hello,

 

I am trying to make a struct type and make another struct built on top of the other struct and I got it working, but I'm not sure what the tutorial means when it says I need to do pointer casting. I know how, but not sure why I have to do this? I seem to have it going without the casting so I'm not sure why they mentioned it. Can anyone help me understand why? rolleyes.gif

 

my code:

#include <stdio.h>
#include <string.h>

typedef struct {
    float wage;
    char name[15];
} Employee;

typedef struct{
    Employee super;
    char title[15];
} Manager;

int main(int argc, const char * argv[])
{
    Manager manager1;
    strcpy(manager1.super.name, "Steve");
    strcpy(manager1.title, "Manager On Duty");
    
    Manager manager2;
    strcpy(manager2.super.name, "Jack");
    strcpy(manager2.title, "CEO");
    
    printf("Title:\t\t\t\tEmployee Name:\n%s\t\t%s\n", manager1.title, manager1.super.name);
    printf("%s\t\t\t\t\t%s\n", manager2.title, manager2.super.name);
    
    return 0;
}

tutorial code: (not same example)

typedef struct
{
    // base members

} Base;

typedef struct
{
    Base base;

    // derived members

} Derived;

then they say "As Derived starts with a copy of Base, you can do this:"

Base *b = (Base *b)d;

"Where d is an instance of Derived. So they are kind of polymorphic. But having virtual methods is another challenge - to do that, you'd need to have the equivalent of a vtable pointer in Base, containing function pointers to functions that accept Base as their first argument (which you could name this)"




#5128620 C If Statement Question

Posted by NUCLEAR RABBIT on 03 February 2014 - 09:58 PM

Hello! 

 

I am taking a C programming class and my teacher uses the "puts statement" to combine an if statements with the action if the condition is met. I saw too different examples of doing the same thing and was just wondering which method is more "standardized" and which method is not recommended using? It's nothing too serious, but it got me curious. thanks! 

 

PS - here's an example:

#include <stdio.h>

int main(int argc, const char * argv[])
{
    int grade;
    
    // gets user input
    printf("What is the students grade? ");
    scanf("%d", &grade);
    
    // alerts user if passed or failed
    (grade >= 60) ? printf("Passed!\n") : printf("Uh-ohhh...\n");
    // puts statement version
    puts(grade >= 60 ? "Passed!\n" : "Uh-ohhh...\n");
    
    // displays grade
    if(grade >= 90)
        printf("Grade = A\n");
    else if(grade >= 80)
        printf("Grade = B\n");
    else if(grade >= 70)
        printf("Grade  = C\n");
    else if(grade >= 60)
        printf("Grade = D\n");
    else
        printf("Student failed.\n");
    
    return 0;
}



PARTNERS