Pick #1, 2, or 3 and also do 4 1. The purpose of this exercise is to

advertisement
Pick #1, 2, or 3 and also do 4
1. The purpose of this exercise is to use inheritance, an abstract class, abstract methods, and override the
toString() method. Save the classes you create as Account.java, Checking.java, Savings.java, and
AccountArray.java.
1. For the Account class, create:
a. two protected variables for the account number and the account balance.
b. two get methods - one for the account number and one for the account balance
c. two set methods - one for the account number and one for the balance.
d. a constructor that requires an account number and sets the balance to 0.
e. a method to override the toString() method which returns a String containing: Account
# = < account number> Account Balance = <account balance> where <account
number> is the object's account number and <account balance> is the object's account
balance.
f. an abstract computeInterest() method that takes one integer argument and returns a
double.
g. Do not allow the balance to be set to a negative number.
2. The Checking class will be a subclass of Account. For the Checking class, create:
3. a method to override the toString() method which returns a String containing:
"Checking" , the account number, and the account balance. Do not print the interest
earned as part of toString().
4. a constructor that takes an account number as an argument and calls the superclass
constructor passing this account number.
5. a method to implement the abstract computeInterest() method of the superclass. This
method takes one argument for the interest period and returns the interest earned. The
interest earned is 2 % of the balance over $700 times the interest period. The interest
period is passed as an argument to the computeInterest() method. For example, the
interest on $3000 at 2% for three years is (3000 - 700) times .02 times 3 or 138.
3. The Savings class will be a subclass of Account. For the Savings class, create:
a. an additional private variable to hold the interest rate.
b. a get and set method for the interest rate. Do not allow the interest rate to be set to a
negative number.
c. a method named toString() that overrides the toString() method of the Object class.
This method should return "Savings", the account number, the account balance and the
interest rate. Do not print the interest earned as part of toString().
d. a constructor with two arguments - the account number and the interest rate. This
constructor calls the superclass constructor passing the account number.
e. a method to implement the abstract computeInterest() method of the superclass. This
method takes one argument for the interest period and returns the interest earned. The
interest is calculated as (1 + interest rate)period times the balance minus the balance. The
interest period is passed as an argument to the computeInterest() method. For example,
the interest on $6000 at 2% for three years is (1.02)3 * 6000 - 6000 or 367.
4. For the AccountArray class, create:
a. a main() method.
b. an array to store 10 objects of either the Savings or Checking class. Use 2% (.02) for
the savings account rate. Use account numbers 100 through 109. Use initial balances of
1000 through 10000.
c. a for loop that will instantiate 5 objects of the Savings class and 5 objects of the
Checking class. Store these objects in a single array created above.
d. a for loop that prints the data in all 10 objects of the array using the overridden
toString() method and also prints the interest amount computed for each object. Use 3
for the period.
2. Instructions:
1. Write an Abstract Data Type called Animal. Include two instance variables in this class: a
String for name and an integer for age. Write a constructor method that takes two
arguments and a second constructor that takes no arguments. Write get and set methods
for each of the two instance variables. Write a toString method which displays the values of
the instance variables for any object of type Animal. Write a speak method that takes no
arguments, returns nothing and displays “Arg! Arg!” on the monitor.
2. Write a second class called Duck, a subclass of class Animal. Add an additional instance
variable to those inherited from Animal: an integer called feathers (this variable is used to
store the number of feathers that a particular Duck object has). Write a constructor that
takes three arguments and another constructor that takes no arguments. Provide set and
get methods for the feathers instance variable. Override the toString method to display the
values of all instance variables belonging to a Duck type of object. Override the speak
method to display a value of “Quack! Quack!”.
3. Write a third class called Cow, also a subclass of class Animal. Add an additional instance
variable to those inherited from Animal: an integer called spots (this variable is used to
store the number of spots feathers that a particular Cow object has). Write a constructor
that takes three arguments and another constructor that takes no arguments. Override the
toString method to display the values of all instance variables belonging to a Cow type of
object. Override the speak method to display a value of “Mooo! Mooo!”.
4. Finally, enter, compile and execute a Farm class shown below. Include an array of
animals to demonstrate polymorphism.
3. You will write 4 Java class files - FootballPlayer.java, OffensivePlayer.java,
DefensivePlayer.java, and QuarterBack.java.
a. The following are the specifications for the FootballPlayer class:
* Instance variables name(String), gamesPlayed(int), and numberOfInjuries(int)
* A constructor that takes 3 arguments (String, int, and int) and sets the instance
variables accordingly
* Methods include all getters and setters (accessors and mutators) as well as a display()
method with no return type that outputs the FootballPlayer's information
b. The following are the specifications for the OffensivePlayer class:
* This class should inherit from the FootballPlayer class
* Instance variables are totalYards(int), numberOfTDs(int)
* A constructor that takes 5 arguments (String, int, int, int, and int) and sets the instance
variables accordingly
* Methods include all getters and setters as well as a display() method with no return
type that outputs the OffensivePlayer's information (should call the super display method
as well)
c. The following are the specifications for the DefensivePlayer class:
* This class should inherit from the FootballPlayer class
* Instance variables are numberOfInterceptions(int), numberOfTackles(int)
* A constructor that takes 5 arguments (String, int, int, int, and int) and sets the instance
variables accordingly
* Methods include all getters and setters as well as a display() method with no return
type that outputs the DefensivePlayer's information (should call the super display
method as well)
d. The following are the specifications for the QuarterBack class:
* This class should inherit from the OffensivePlayer class
* Instance variables are interceptionsThrown(int), completionPercentage(double)
* A constructor that takes 7 arguments (String, int, int, int, int, int, double), i.e., the five
arguments for OffensivePlayer plus two more for the QuarterBack’s instance variables
,and sets the instance variables accordingly
* Methods include all getters and setters as well as a display() method with no return
type that outputs the QuarterBack's information (should call the super display method as
well)
e. Write a client called TestFootballPlayer.java and test that your classes have been
written correctly.
4.Composition:
Create a class called Name, include fields, first name, last name, middle name, nickname.
Test with a client.
Add a class called Buddy that includes a name, phone number, and a birthdate (use Date class).
Download