This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes A little help with my homework, explaining inheritance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "A little help with my homework, explaining inheritance" Watch "A little help with my homework, explaining inheritance" New topic
Author

A little help with my homework, explaining inheritance

Rob Freiberger
Greenhorn

Joined: Mar 13, 2005
Posts: 8
Hi,

I have homework due for my class and I got the swing design down but am really confused with the specifications of the homework. I'm still reading about inheritance so the assignment is a bit past my skill level. If someone could explain it in a simpler method that would be awesome!

Unfortunatly the case only meets once and week and the instructor is busy so it's hard to ask questions directly. Any comments are welcome.

Here's the program specifications.

*******************
*******************
*******************

Part I: Use inheritance to display a student record (continued)
Operation

� The user enters a college student�s identification number followed by the major and GPA. (this part I got)
� The application creates a college student object and displays the object to the screen. (this part done)

(Now the confusing part)
Specifications
� First, create a class that defines a student. This class should contain at least two instance variables for identification number and GPA. It should also contain two constructors�a default one and one that accepts two values for the instance variables. In addition to set and get methods, the class should override the toString method from the Object class.
� Next, create a class that defines a college student. To do this, the class should inherit the above class. In addition to the inherited instance variables, this class should contain at least one more instance variable that defines the college student�s major. This class should also contain a default constructor and one that accepts three values for the instance variables. The first statement in this constructor should call a constructor in the superclass. This class should also override the toString method.
� Code a driver class that carries out the operation of the project.
� Assume valid data is entered.

Again, any comments are more than welcome!

Thanks,
Rob
Ta Ri Ki Sun
Ranch Hand

Joined: Mar 26, 2002
Posts: 442
It seems to me that you're looking at the whole picture and worrying too much, it's much simpler when you look at all the rather specific little problems.
Can you perhaps go through all the steps mentioned and then ask when you get stuck on a specific problem? It will be much easier to assist you then without spoon feeding.
Even to deal with the inheritance only it would help to know what you don't understand, simply put though all students have an "identification number and GPA", so these are properties of the Student class. Next you have a college student, that is a student, so your class is defined as CollegeStudent extends Student. It inherits all the properties of the student, and in addition has a major, so add that property to CollegeStudent.
Lastly both of these classes override Object's toString method to give a String representation of the object, well Student does and CollegeStudent overrides Student's toString.
I'm not sure that was helpful at all but if you're more specific you'll get a more specific response.
Ashish Chopra
Ranch Hand

Joined: Nov 30, 2004
Posts: 134
Hi Rob,

The student class contains the attributes of a generic student, things like an ID & GPA. The college student subclass contains these properties as well as some properties special to it , like the major.

Now, your student class must have two constructors, a default one, and another one accepting two default values for the ID & GPA.

In your college class, you'll have a constructor that accepts three values(two for the attributes it has inherited from student class, and one for its own attribute). This would call the superclass' constructor and pass the two values that are ID & GPA. The third one it would assign to its local attribute (Major).

I am not clear as to why do you have to override the toString method. If you could shed some light on the same..

Hope I was able to help...


Quis Custodiet Ipsos Custodes<br /> <br />My blog: <a href="http://www.coherentrambling.blogspot.com" target="_blank" rel="nofollow">http://www.coherentrambling.blogspot.com</a>
Ko Ko Naing
Ranch Hand

Joined: Jun 08, 2002
Posts: 3178
Originally posted by Rob Freiberger:
� Next, create a class that defines a college student. To do this, the class should inherit the above class. In addition to the inherited instance variables, this class should contain at least one more instance variable that defines the college student�s major. This class should also contain a default constructor and one that accepts three values for the instance variables. The first statement in this constructor should call a constructor in the superclass. This class should also override the toString method.


Rob,
As a hint to this "Next" bullet, I designed an initial design for your college class for the constructors that you specified above.

public class CollegeStudent extends Student {
private String major;
public CollegeStudent() {
super();
}
public CollegeStudent(int id, float gpa, String major) {
super(id, gpa);
this.id = id;
this.gpa = gpa;
this.major = major;
}

public <type> getXxx(){...}
public void setXxx(<type> xxx){...}

public String toString() {
return "The String that we want to return";
}
}

Any enhancement on this inital class design is welcome to help Rob out. Rob, why don't you come up with your existing design so that ranchers can help you to improve according to your initial design.


Co-author of SCMAD Exam Guide, Author of JMADPlus
SCJP1.2, CCNA, SCWCD1.4, SCBCD1.3, SCMAD1.0, SCJA1.0, SCJP6.0
Jeff Jetton
Ranch Hand

Joined: Mar 29, 2005
Posts: 71
Originally posted by Ta Ri Ki Sun:
It seems to me that you're looking at the whole picture and worrying too much, it's much simpler when you look at all the rather specific little problems.


I agree.

Are you familiar with creating your own classes and intantiating them as object? You should have that down cold before you start worrying about inheritance.

So, if you haven't already, try just doing the Student class first. Get so that you can define your class, create a new object (or objects) based on that class, assign various ID and GPA values to it, print those values out, and so on.

Then, and only then, should you tackle the CollegeStudent class.

Which might lead you to wonder: Why do all this inheriting in the first place? Well, the idea here is that you need a special type of Student that has the ability to hold a Major. Now, in the dark old days before object-oriented programming, we would have to rewrite our Student class. But in Java, we simply need to create a new class (a subclass) that has the Student class as a "parent". We add just the new functionality to our subclass, and it magically gets all the functionality of the Student class "for free".

In theory, someone else might one day write a "ElementarySchoolStudent" class that also inherits from Student. It won't have a Major (that's just in your CollegeStudent subclass), but it might add the ability to store a SchoolBusRouteNumber, or it might have a method that sends a report card to the parents. Someone else might write an "AdultEducationStudent" that would have still other functionality... you get the idea.

And if it turns out that, for example, you one day need to store and track the GPA as a percentage instead of a 0.0-4.0 number, all you have to change is your parent class, Student. The subclasses will then automatically use this new way of dealing with the GPA--you won't have to change them at all.

- Jeff
Ko Ko Naing
Ranch Hand

Joined: Jun 08, 2002
Posts: 3178
Rob,
This sample chapter from Head First Java, Second Edition might help you in understanding the way to subclass a class and the reason we need OO. I am even currently reading that chapter for my SCJA preparation.
[ April 19, 2005: Message edited by: Ko Ko Naing ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A little help with my homework, explaining inheritance