Chapter 7 Test

advertisement
Chapter 7 Test
Assign: 1-30-09
Due: 1-30-09
Instructions: Choose one of the following items and write the
program. Include the additional info as comments in your program.
Include your name, date and pseudo code at the beginning of your
program. (Points will be deducted if this is not included)
Choice 1:
Write a class encapsulating the concept of coins, assuming that
coins have the following attributes:
a number of quarters
a number of dimes
a number of nickels
a number of pennies
Include a constructor, the accessors and mutators, and methods
toString and equals. Also code the following methods:
one returning the total amount of money in dollar notation
with two significant digits after the decimal point
others returning the money in quarters (for instance, 0.75
if there are three quarters), in dimes, in nickels, and in
pennies
Write the client class to test all the methods in your class.
Additional info:
Overloaded constructor:
Allows client to set beginning values for quarters, dimes, nickels, and
pennies
This constructor takes four parameters<BR>
Calls mutator methods to validate new values
newQuarters the number of quarters
newDimes the number of dimes
newNickels the number of nickels
newPennies the number of pennies
getQuarters method
return the number of quarters
Mutator method:
Allows client to set value of quarters
Prints an error message if the value is less than 0
setQuarters does not change the value of quarters if newQuarters has negative
value
newQuarters the new number of quarters
getDimes method
return number of dimes
Mutator method:
Allows client to set value of dimes
Prints an error message if the value is less than 0
setDimes does not change the value of dimes if newDimes has negative value
newdimes the new number of dimes
getNickels method
return the number of nickels
Mutator method:
Allows client to set value of nickels
Prints an error message if the value is less than 0
setNickels does not change the value of nickelsif newNickels has negative
value
newNickels the new number of nickels
getPennies method
return the number of pennies
Mutator method:
Allows client to set value of pennies
Prints an error message if the value is less than 0
setPennies does not change the value of pennies if newPennies has negative
value
newPennies the new number of pennies
toString
return the number of quarters, dimes, nickels, and pennies for the coins
equals method
Compares two Coins objects for the same field values
c another Coins object
return a boolean, true if this object has the same field values as the
parameter c
outputTotalAmount method
Outputs the total amount in $ notation
moneyFromQuarters method
Computes the dollar amount from quarters
return a double, the dollar money amount from quarters
moneyFromDimes method
Computes the dollar amount from dimes
return a double, the dollar money amount from dimes
moneyFromNickels method
Computes the dollar amount from nickels
return a double, the dollar money amount from nickels
moneyFromPennies method
Computes the dollar amount from pennies
return a double, the dollar money amount from pennies
Possible output should look like:
The number of quarters cannot be negative.
Value not changed.
The number of quarters of coins #1 is 4
The number of dimes of coins #1 is 3
The number of nickels of coins #1 is 2
The number of pennies of coins #1 is 3
Coins #2 is quarters: 0; dimes: 5; nickels: 3; pennies:4
From quarters: $0.00
From dimes: $0.50
From nickels: $0.15
From pennies: $0.04
Total amount: $0.69
Original coins #1 and #2 are different
Original coins #1 and modified coins #2 are identical
Process completed.
Choice 2:
Write a class encapsulating the concept of a rational number, assuming
a rational number has the following attributes:
an integer representing the numerator of the rational number
another integer representing the denominator of the rational
number
Include a constructor, the accessors and mutators, and methods toString
and equals. You should not allow the denominator to equal to 0; you
should give it the default value 1 in case the corresponding argument
of the constructor or a method is 0. Also include methods performing
multiplication of a rational number by another and addition of a
rational number to another, returning the resulting rational number in
both cases. Write a client class to test all the methods in your class.
Additional Information
Overloaded constructor:
Allows client to set beginning values for numerator and denominator
This constructor takes two parameters
Calls mutator methods to validate new values
newNumerator the numerator of the rational number
newDenominator the denominator of the rational number
getNumerator method
return the numerator of the rational number
Mutator method:
Allows client to set value of numerator
newNumerator the new number of numerator
getDenominator method
return the denominator of the rational number
Mutator method:
Allows client to set value of denominator
setDenominator sets the value of denominator to 1 if newDenominator has
value 0
newDenominator the new denominator
toString method
return the fraction represented by the rational number
equals method
Compares two Rational objects for the same field values
r another Rational object
return a boolean, true if this object has the same field values as the
parameter r
multiply method
Multiplies two Rational objects
r another Rational object
return a Rational, the result of the multiplication
add method
Adds two Rational objects
r another Rational object
return a Rational, the result of the addition
Possible Output should look like:
The numerator of rational #1 is 2
The denominator of rational #1 is 3
Rational #2 is 5/7
2/3 * 5/7 = 10/21
2/3 + 5/7 = 29/21
Original rational #1 and #2 are different
Original rational #1 and modified rational #2 are identical
Process completed.
Download