[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Voyeur Cams | Click for more| Home]

Inheritance and Super methods

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.
Voyeur Cams

Thread replies: 36
Thread images: 6

File: main.png (30KB, 615x698px) Image search: [iqdb] [SauceNao] [Google]
main.png
30KB, 615x698px
Hey guys.

I'm a second year SE student and I've been doing pretty well with my CS classes. But my prof slapped us with a inheritance example project due only a couple days after she assigned it to have it due before the thanksgiving break.

Anyway, I've had like no time to figure this shit out, and I'm stuck.

Basically, I have to have a superclass (called Person) that holds String variables name and birthYear, and two subclasses (Student and Instructor) that inherit those variables and add String major (for student) and String salary (for Instructor).

I have to create instances of each class and set their variables then print out those variables, including the inherited ones, through what I imagine is inherited methods too (super.print()).

I'd really appreciate help.
Sorry if this isn't exactly what /g/ is for, I haven't really ever posted here so I'm not sure if this is normal or not to do.

I already have some things done so I'll be bumping with my main.
>>
>>51496647
So what are you stuck on? It sounds to me like you understand what you need to do.
>>
Seems like you got the instances part down, and as I understand it, you now have to write 3 classes, right? Where exactly are you stuck? Did you already tried to write the superclass?
>>
File: person.png (20KB, 449x669px) Image search: [iqdb] [SauceNao] [Google]
person.png
20KB, 449x669px
this is the superclass Person.
I have a constructor for no parameters and constructor to make one with set variables (name and birthYear).

Print should be the one that gets extended and overridden because the next two classes have to print their extra variable, but I'm not sure how to do that.
>>
File: run problem 1.png (4KB, 362x153px) Image search: [iqdb] [SauceNao] [Google]
run problem 1.png
4KB, 362x153px
>>51496750
>>51496751
my first problem is that I have to use the toString method, but it prints out that random @code thing. I need it to print the variables of the classes.

If i could get that down I'd at least finish this out with a 70 or so since its the whole first part of this example we have to do.
>>
>>51496761
are you Indon? if yes i can provide you some assistance. if not, i'm sorry, i can't speak english very well.
>>
>>51496761
the assignment in your constructor is backwards.
>>51496804
write your own toString() method, like in your person class
>>
>>51496761
In your constructor, flip the variable order around. Right now you are assigning the value of the member to the parameter, which should be the other way around.

The print in the subclasses will look something like:
@Override
public String print() {
return super.print() + <subclass-specific output>;
}
>>
Also, no need to call .toString() explicitly in the System.out.println
>>
>>51496844
yeah, I thought I should write my own. But the professor says to use the toString() method - should I just make one called toString and use that?

>>51496851
so you mean instead do
name = fullName;
year = birthYear;
?
I'll show you guys the subclasses next, I'll take a screenshot.
>>
>>51496647
HARMFUL
A
R
M
F
U
L
>>
>>51496991
exactly, add an @Override so the compiler knows.
And yes first you write the variable that the value is getting assigned on , than the value you want to assign
>>
>>51497027
Also your current print method is what your toString should look like. So not much new to write.
>>
>>51496991
>But the professor says to use the toString() method - should I just make one called toString and use that?
Yes. All Java classes provide a default toString() method, (that's what makes the "[email protected]" output above,) but if you want special output you have to write your own.

>so you mean instead do
>name = fullName;
>year = birthYear;
>?
name = fullName;
birthYear = birthDate;

You're taking the parameters from the constructor and saving them in the instance of the class.
>>
>>51497066
yeah I changed it to that instead.

Now, to make the toString for the subclasses:
Right now it will only print their name and birthYear - how do I add the the extra subclass variable (in this case Major for the Student subclass)?

Will I just do
super.toString() + major;
?

Pic is my subclass Student (Instructor is the same but with Salary instead of Major as an instance variable).
>>
>>51496647
>SE student and I've been doing pretty well with my CS classes
>SE
>CS
What? They're two different degrees nigga.
>>
>>51497184
>Getting accounting degree
>Skip all math classes because "They're two different degrees nigga"
>?????
>Profit
>>
>>51496647
What is that shit?
C++?
>>
>>51497131
return super.toString() + ", " + major should do the trick.
>>
File: override works.png (4KB, 417x92px) Image search: [iqdb] [SauceNao] [Google]
override works.png
4KB, 417x92px
>>51497184
They make us take the Software Engineering classes alongside the Computer Science ones. I'm in 176/501B right now.

I got the override to work and print out the right variables now.

>>51497250
Javascript

>>51497253
yeah I had to add return since its not void
>>
>>51497249
Ok, made me lel.

>>51497262
I guess it's just a name thing then.
I'm a SE myself too B-) but my classes are named "Algorithms", "Calculus", etc.

What year are you family?
>>
>>51497262
Java, not Javascript. Two very different languages.
>>
>>51497304
Oh fuck, second year, right.
I missed it.
>>
>>51497304
>>51497358
yeah, second year. I think I'm headed for 5 years instead of 4 for a bachelors cause I've been taking 4 classes instead of 5 since its hard to commute and line up a lot of classes. I've got like a 40 minute drive daily and that sucks but I love the school. Also frees up time for me to work.

>>51497305
my bad
>>
the last problem i have is making an array of the supeclass Person, and filling it with the instances of person1, student1, and instructor1, and then printing their variables.

I can get it to print the superclass's variables (name and birthyear) but i'm not sure how to get it to print the variables specific to the subclasses (.getSalary and .getMajor)

After that I'm done and gotta do some lame UML charts but that shouldn't be that bad.
>>
>>51497560
why not use your toString method, it should display everything you want it to display.
>>
>>51497614
BRUH youre right
ahaha that just blew my mind idk why I didn't think of that. Brain fart I guess.

Thanks so much guys for all the help.
>>
please just drop out already.
>>
>>51497659
nerd
>>
>>51497262

Oh sheeeeit polymorphism.

It's amazing how many people still don't understand polymorphism in 2015. Everyone went, "hurr, OOP is dumb!" and 3/4 of them never even understood what OOP was about.

>>51496761

You need to improve your variable names. Usually languages develop sort of a defacto standard for differentiating member variables from constructor arguments.

In C++, you might use:

class Person {
string _name;
string _birthDate;

Person(string name, string birthDate) :
_name(name), _birthDate(birthDate)
{
// Do nothing
}
};


Windows C++ generally adopted the m_ prefix for member variables. In Pascal, I seem to recall that you had to refer to members with the "this." prefix, eliminating any name collision. In C#, you can use "this." to clear up confusion, so you could write "this.name = name;".
>>
>>51497922
yeah, I'm not too sure how to use "this"
I haven't had any problems not using it but I'm sure it could cause issues in a larger system with many different classes.

Are you saying where you used

string _name;
i would use
string this.name;
?

My professor hasn't ever deducted for not using "this" though.
>>
>>51498007
>I'm sure it could cause issues in a larger system with many different classes.

...no.

>Are you saying where you used
>string _name;
>i would use
>string this.name;
>?

Hell no. The fact that you even asked that is somewhat horrifying.

I'm suggesting something along these lines (pardon the imperfect Java syntax, I don't write Java):

private class Person {
private String name = "";
private String birthYear = "";

public Person(string name, string birthYear) {
this.name = name;
this.birthYear = birthYear;
}
...
}


Like I said, C# allows that, I assume Java probably does too. Either way, you need to clean your shit up. Is it name, or full name? Is it birth year or birth date? Get your variable names consistent and logical.
>>
>>51498062
oh, I guess I didn't understand what you meant.

We havent really done that in the 3 CS courses I've taken so far, even for examples in class the professors lead (ive had 2 different ones) we havent used "this".
>>
>>51498140

It's like you're just not getting it.

I'm fucking telling you to fix your inconsistent, weird variable names. I gave you the "this." as a method to make them 100% consistent without introducing any ambiguity.
>>
>>51498174
Not saying youre wrong at all

Just saying why I didn't get it, why it seemed weird to me.
I already changed it.
>>
install gentoo
Thread posts: 36
Thread images: 6


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]
Please support this website by donating Bitcoins to 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
If a post contains copyrighted or illegal content, please click on that post's [Report] button and fill out a post removal request
All trademarks and copyrights on this page are owned by their respective parties. Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site. This means that 4Archive shows an archive of their content. If you need information for a Poster - contact them.