CSH Practice Test Part II. Class Design

advertisement
CSH Practice Test
Part II. Class Design - BoxOfCandy
Create a class to represent a box of candy.
Fields:
The candy box is represented by a field called box which is an array of 4 ints. The array represents how
many of each type of candy is in the box. Position 0 in the array represents how many strawberry
candies are in the box. Position 1 represents how many vanilla candies are in the box. Position 2
represents how many caramel candies are in the box. Position 3 represents how many chocolate candies
are in the box. There is also a double field called box_value which represents how much the candy in the
box is currently worth. Strawberry and caramel candies are worth 0.50 each. Vanilla and chocolate
candies are worth 0.25 cents each.
Constructors:
The default constructor uses loops to create a box filled as follows:
4 strawberry candies
4 vanilla candies
6 caramel candies
6 chocolate candies
Be sure to add to the box_value field as you fill the array.
The 2nd constructor takes 4 ints as parameters to represent how much of each type of candy is in
the box.
Be sure to add to the box_value field as you fill the array.
Methods:
Create the following methods:
getHowManyStrawberryLeft – This method has no parameters and it returns how many Strawberry
candies are left.
getHowManyVanillaLeft – This method has no parameters and it returns how many Strawberry candies
are left.
getHowManyCaramelLeft – This method has no parameters and it returns how many Strawberry candies
are left.
getHowManyChocolateLeft – This method has no parameters and it returns how many Strawberry
candies are left.
getValueOfCandyInBox – This method has no parameters and it returns the value of the candy left in the
box.
eatStrawberrys – This method takes an int parameter for how many Strawberry candies are to be eaten
and it checks to see if there are that many Strawberry candies left. If there are not enough left, it prints
an error message. If there are enough then, the appropriate array slot is subtracted and the box_value is
reduced appropriately. Nothing is returned by this method.
eatVanillas – This method takes an int parameter for how many Vanilla candies are to be eaten and it
checks to see if there are that many Strawberry candies left. If there are not enough left, it prints an error
message. If there are enough then, the appropriate array slot is subtracted and the box_value is reduced
appropriately. Nothing is returned by this method.
eatCaramels – This method takes an int parameter for how many Caramel candies are to be eaten and it
checks to see if there are that many Strawberry candies left. If there are not enough left, it prints an error
message. If there are enough then, the appropriate array slot is subtracted and the box_value is reduced
appropriately. Nothing is returned by this method.
eatChocolates – This method takes an int parameter for how many Chocolate candies are to be eaten and
it checks to see if there are that many Strawberry candies left. If there are not enough left, it prints an
error message. If there are enough then, the appropriate array slot is subtracted and the box_value is
reduced appropriately. Nothing is returned by this method.
eatOneOfEach – This method takes no parameters. This method loops through the array and if there are
enough left it subtracts a given type of candy and reduces box_value. If there is not enough left of a given
type of candy, it prints "There are no more of those left." Nothing is returned by this method.
printStatusOfCandyBox – This method takes no parameters. This method loops through the array and
prints how many of each type of candy are left as numbers only (i.e. 2 5 0 3). It then also prints the
current value of the candy in the box. Nothing is returned by this method.
getNumCandiesInBox – This method loops through the array and using a local variable it totals the
number of candies in the box and returns this number.
getAveCandyValue – This method uses the box_value and the method getNumCandiesInBox and it
correctly calculates the average value of a candy in the box and returns it as a double.
Test Class: create a class TestBoxOfCandy which has no fields, no constructors, and a single
method.
public static void main(String[] args)
In this method, import the Scanner class.
In the main method:
Create a new Scanner object called myScanner
Ask the user how much of each type of candy using the nextInt method on the Scanner object and store
them in four local variables. Create a new BoxOfCandy named BC using the 2nd constructor and the four
local variables. Then print the status of BC. Eat one of each type of candy in BC. Then print the status of
BC again. Eat 2 vanilla candies. Print the status of BC one last time.
Download