Embed
Email

Inheritance

Document Sample

Shared by: liamei12345
Categories
Tags
Stats
views:
25
posted:
10/21/2011
language:
English
pages:
5
1. The Person class has two attributes, name and age. We want to create a subclass of Person called

CollegeKid, which represents someone who is attending college. Note the additional grade point

average attribute gpa in CollegeKid.



For this exercise, complete the constructor for the CollegeKid class. The constructor has three

parameters: a String parameter called n, an int parameter called a, and a double parameter called

g. We've provided a super call, which does some of the work of the constructor. Your code should

complete the constructor by assigning the value of g to the gpa attribute.







/**

* This class contains data and methods pertaining

* to a college kid. The datum contained in this class is:

* grade point average -- gpa. This class derives from Person.

*/



public class CollegeKid extends Person

{

private double gpa;



public CollegeKid (String n, int a, double g) {



super(n, a);



/* the rest here */







}



public double getGPA ()

{

return gpa;

}



public void setGPA (double g)

{

gpa = g;

}



} // end class

2. The Person class has a print method that writes a person's name and age to the console.

Thus, if we created a Person object with these values:



Person p = new Person("Buster", 22);





the call:





p.print();







would result in the following output to the console:

Name: Buster

Age: 22

We want to provide similar printed information information for objects in the subclass

CollegeKid. Remember that CollegeKid objects contain the additional datum gpa. If we

created a CollegeKid object with these values:



CollegeKid k = new CollegeKid("Zoe", 23, 3.75);



the call:



k.print();



should result in the following output to the console:

Name: Zoe

Age: 23

GPA: 3.75





For this exercise, enter code in the box below that will allow the print method in the

CollegeKid class to override print in the Person class, and thus write the above

specified output to the console. Note that your output must have the same format as in the

example above.





/**

* This class contains data and methods pertaining

* to a college kid. The datum contained in this class is:

* grade point average -- gpa. This class derives from Person.

*/



public class CollegeKid extends Person

{

private double gpa;







public double getGPA ()

{

return gpa;

}



public void setGPA (double g)

{

gpa = g;

}



public void print ( )

{

//code goes here



} // end method

} // end class



3. The Person class has two attributes, name and age. We want to create a subclass of

Person, called Oldie, that represents someone who is getting on in age, say about thirty years

old or older. Note the additional boolean-valued attribute called perryComoFan in Oldie. If

you are an Oldie, you are either a fan of Perry Como or you are not.

(OPTIONAL: If the name Perry Como doesn't ring a bell and you are curious, try a web search for

Perry).



For this exercise, write a complete constructor for the Oldie class. Your constructor should

have three parameters: a String parameter called n, an int parameter called a, and a

boolean parameter called p. Your code should initialize the name and age attributes to the

value of n and a respectively. Your constructor must also assign the value of p to the

perryComoFan attribute of the class.





/**

* This class contains data and methods pertaining

* to an oldie -- i.e. someone over the age of, oh say 30.

* The datum contained in this class is:

* whether or not this person is a Perry Como fan.

* This class derives from Person.

*/



public class Oldie extends Person

{

private boolean perryComoFan;



/* CONSTUCTOR CODE HERE */



public boolean isPerryComoFan ()

{

return perryComoFan;

}



public void setPerryComoFan (boolean f)

{

perryComoFan = f;

}



} // end class





4. The Person class has a print method that writes a person's name and age to the console.

Thus, if we create a Person object with these values:



Person p = new Person("Buster", 22);





then the call:



p.print();





will write the following output to the console:

Name: Buster

Age: 22

In the same manner, we want to print information about objects in the subclass Oldie.

Remember that Oldie objects contain the additional boolean datum perryComoFan. If we create

an Oldie object with these values:



Oldie k = new Oldie("Cornelius", 93, true);



then the call: k.print(); should result in the following output to the console:

Name: Cornelius

Age: 93

Perry Fan: true

For this exercise, enter code in the box below that will allow the print method in the

Oldie class to write the above specified output to the console. Note that your output must

have the same format as that given in the example above.

/**

* This class contains data and methods pertaining

* to an oldie -- i.e. someone over the age of, oh say 30.

* The datum contained in this class is:

* whether or not this person is a Perry Como fan.

* This class derives from Person.

*/



public class Oldie extends Person

{

private boolean perryComoFan;







public boolean isPerryComoFan ()

{

return perryComoFan;

}



public void setPerryComoFan (boolean f)

{

perryComoFan = f;

}



public void print ()

{

//Code goes here



} // end method

} // end class



5.


Related docs
Other docs by liamei12345
T14_Op_Exp_Mode_Class_Bus
Views: 0  |  Downloads: 0
Diagnostic principle_ rule in database
Views: 0  |  Downloads: 0
daet_result
Views: 0  |  Downloads: 0
Samplevoucher
Views: 0  |  Downloads: 0
TOMMY12
Views: 0  |  Downloads: 0
Copy_of_2010-2011School_Calendar
Views: 0  |  Downloads: 0
2011_Kits_Invite_Final_Results_web
Views: 0  |  Downloads: 0
Journal Holdings 2004 ENG
Views: 0  |  Downloads: 0
CS 10-080
Views: 1  |  Downloads: 0
DevelopmentalCodingWorkbook
Views: 0  |  Downloads: 0
By registering with docstoc.com you agree to our
privacy policy

You are almost ready to download!

You are almost ready to download!