Beruflich Dokumente
Kultur Dokumente
Homework Set 7
Inheritance
P7A. Youve been hired to design the next generation electronic device. Write the class
definition for a new type of object of your own created design that extends
MyDevice. The class definition should:
a.
include a unique instance variable or variables and/or processor methods (to
justify it as a new subclass),
b.
include an upgraded multi-argument constructor to reflect the addition of the new
instance variable(s),
c.
add new getters and setters for each new instance variable(s), and
d.
include a toString() method that overrides the superclass toString()
method to include the values of all private instance variables in its output.
Include your new device in the ArrayList<MyDevice>. Traverse the
ArrayList<MyDevice> using either a for or for-each loop and call each objects
toString() method.
The ArrayList below includes seemingly different objects. However, since they all extend
MyDevice, they are all really considered MyDevice objects and can therefore be placed into the
ArrayList<MyDevice>; that is, they can all fit into the ArrayList of MyDevices.
The for loop below polymorphically processes (prints out) the all of the devices in the
ArrayList<MyDevice>. This means that the loop can treat all objects that are seemingly different
(poly-morphism many shapes) as MyDevice objects and call their respective toString()
methods.
polymorphisminaction!
for(MyDevicedevice:inventory)
{
Weneverrefertospecific
System.out.println(device);
phones,pods,pads,etc.Weonly
}//endfor
refertothemasadevice
Page 1
AP Computer Science HW Set 7 Programs
http://thecubscientist.com
/polymorphism/n.1.theactofreferringtoasubclassobject
usingasuperclassname,2.theactofreferringtoobjectsinthe
generalasopposedtothespecific.
P7B. Consider the Television class on the next page. Write a program that will include
the following:
a.
Write four new classes, Plasma, DLP, LCD, and LED that extend the
Television class shown below.
b.
For each of the above classes:
i.
Write a zero argument constructor for each of the above classes.
ii.
Write a new two argument constructor for each of these four classes that
take a String argument for the model of the Television and a double
for the cost of the television.
c. Write a driver program to create each one of these types of Televisions, including
a Television object and store them into an ArrayList of type Television. Use a
for-each loop to polymorphically call the toString() method for each
Television in the ArrayList. Sample output is shown below:
Page 2
AP Computer Science HW Set 7 Programs
http://thecubscientist.com
Page 3
AP Computer Science HW Set 7 Programs
http://thecubscientist.com
Page 4
AP Computer Science HW Set 7 Programs
http://thecubscientist.com
Page 5
AP Computer Science HW Set 7 Programs
http://thecubscientist.com
P7D. College Tuition! In this program, you will write an abstract class Student
and three concrete classes, UnderGrad and Graduate (that inherit from
Student) and PostGraduate that inherits from Graduate. The
requirements for each of the classes are shown below:
Part 1: Write the class definition for the abstract class Student. The class definition
should include private instance variables of type String to hold the students first
name, a String for his/her major and an int to hold the number of units taken.
Getter and setter methods for each of the variables should be included in the class
definition. It should also include an abstract method calculateTuition() as
shown below:
abstractpublicintcalculateTuition(intunits);
Part 2: Write a three-argument constructor in the Student class to initialize each
students name, major, and number of units taken.
Part 3: Write the class definitions for the UnderGrad, Graduate, and
PostGraduate classes. These concrete classes should override the abstract method
calculateTuition() according to the following rule:
UnderGrad tuition is calculated by multiplying the number of units by $250.
Graduate tuition is calculated by multiplying the number of units by $500.
Page 6
AP Computer Science HW Set 7 Programs
http://thecubscientist.com
Page 7
AP Computer Science HW Set 7 Programs
http://thecubscientist.com
Page 8
AP Computer Science HW Set 7 Programs
http://thecubscientist.com