Java program 4
ANSWERS MUST BE TYPED ON THIS ASSIGNMENT SHEET and please add detailed comments to each step in the coding process
Classes
1. (a) Create a Money class that stores a dollar and cent amount. Include in the class: (20 pts.)
a private instance variable named dollars of type int
a private instance variable named cents of type int
a default constructor that initizalizes to zero dollars and zero cents
an alternate constructor that is passed a specific dollar and cent amount
an alternate constructor that is passed only a dollar amount (i.e., sets cents to 0)
a copy constructor
getter methods and setter methods
a toString method that returns a string of the form $4.25
an add and subtract methods (that return a function value of type Money)
a lessThan and greaterThan methods
Implement the class so that all cent amounts are between 0 and 99. Therefore, implement the
alternate constructor that takes a cent parameter to throw an InvalidCentAmountException if a
cent amout more than 99 is passed it.Also, the add and subtract methods must returns a Money
object in which 0 <= cents <= 99.
< insert answer here>
(b) Write a section of code that prompts the user for two monetary amounts, and creates a
Money object for each named money1 and money2. (2 pts.)
< insert answer here>
(c) Give an instruction that creates a new Money object, named money3, by use of the copy
constructor of the Money class. (1 pt.)
< insert answer here>
(d) Give ONE print statement that displays the values of both money1 and money2 by implicit
call to the toString methods of the Money class, of the form (1 pt.)
Value of money1 = $xx.xx, value of money2 = $xx.xx
< insert answer here>
(e) Give one statement that assignment the result of the sum of money1 and money2 to
variable money4. (1 pt.)
< insert answer here>
(f) Give the code to compare money1 and money2 and display one of the following: (3 pts.)
the first amount entered is greater than the second amount
the first amount entered is less than the second amount
the two entered amounts are equal
< insert answer here>
(g) Create an array that can store up to 10 Money objects. Write code to add up all of the values in the
array and display the total. (6 pts.)
< insert answer here>
Composition
2. (a) Create a CustomerAccount class that contains a name (first/last name in one string), an account number (as a String), and a current balance (of type Money created in question 1). Include in the class, (20 pts.)
an alternate constructor that is passed a name and account number (with the initial
balance of the account set to $0)
getter methods for name, account number and current balance
a deposit method (that is passed an object of type Money and adds to the current balance)
a withdraw method (that reduces the current balance) by a given amout
a boolean freeChecking method that returns true if the current balance is greater than $1,000
Implement the withdraw method so that if the amount to withdraw is greater than the current balance,
an InsufficientFundsException is thrown.
< insert answer here>
(b) Write a section of code that prompts the user for their name and creates a new Account object containing their name and account number 1000 (with an initial balance of $0). The code should the continue to prompt the user for the following options:
1 Display account balance
2 Make a deposit
3 Make a withdrawl
The code must be written to catch (and handle) any InsufficientFundsException objects that may be
thrown. (15 pts)
< insert answer here>
Polymorphism (and Abstract Classes)
3. (a) Create an abstact class named RegularPolygon designed so that classes such as Square,
RegularPentagon, RegularHexagon, etc. can be can be derived. (Recall that a regular polygon is one
in which all sides are the same length.) Include in the class ONLY the following: (15 pts.)
a private variable named len_side of type int
an alternate constructor that is passed the length of the side
an abstract method getPerimeter (that returns the perimeter of the polygon)
< insert answer here>
(b) Create subclasses Square, RegularPentagon and RegularHexagon of the abstract RegularPolygon
class. (8 pts.)
< insert answer here>
(c) Write client code that declares an array of size 6 containing elements of type RegularPolygon, prompts the user for the type and length of side of 6 particular regular polygons (of type
square, pentagon, or hexagon), and determines and displays the sum of the perimeters of all of the 6 regular polygons. (8 pts.)
1
“0.
Discount Code: 0
: +1 0 0-0
0..