created 10/01/99; corrected 03/08/00
These exercises assume that your have read the review exercise for this chapter. All of the programming exercises are modifications of that program. Start by copying that program into Notepad.
Modify the PantryTester class so that it carries out a dialog with the user:
Welcome to Mother Hubbard's Pantry! The jams are: 1: Gooseberry 7/4/86 12 fl. oz. 2: Crab Apple 9/30/99 8 fl. oz. 3: Rhubarb 10/31/99 16 fl. oz. Enter your selection (1, 2, or 3): 1 Enter amount to spread: 2 Spreading 2 fluid ounces of Gooseberry The jams are: 1: Gooseberry 7/4/86 10 fl. oz. 2: Crab Apple 9/30/99 8 fl. oz. 3: Rhubarb 10/31/99 16 fl. oz. Enter your selection (1, 2, or 3): 2 Enter amount to spread: 25 Spreading 8 fluid ounces of Crab Apple The jams are: 1: Gooseberry 7/4/86 10 fl. oz. 2: Crab Apple 9/30/99 0 fl. oz. 3: Rhubarb 10/31/99 16 fl. oz. Enter your selection (1, 2, or 3): 2 Enter amount to spread: 9 No jam in the Jar! The jams are: 1: Gooseberry 7/4/86 10 fl. oz. 2: Crab Apple 9/30/99 0 fl. oz. 3: Rhubarb 10/31/99 16 fl. oz. Enter your selection (1, 2, or 3): -1 Good-by
The program should first initialize the jams and the pantry, then....
Use the Jam
and Pantry
classes from the Review exercise.
To do this:
Jam.class
, Pantry.class
and PantryTester.class
in your directory.Exercise1.java
that implements the user interaction.Exercise1.java
will import Jam.class
and Pantry.class
.
Exercise1.java
.
Exercise1.java
will look like this:
import java.io.*; import Jam; import Pantry; class Exercise1 { public static void main ( String[] args ) { Scan scan = new Scanner( System.in ); String inChars; Jam goose = new Jam( "Gooseberry", "7/4/86", 12 ); Jam apple = new Jam( "Crab Apple", "9/30/99", 8 ); Jam rhub = new Jam( "Rhubarb", "10/31/99", 16 ); . . . . . . . . . . } }
Click here to go back to the main menu.
Add new constructors to the Pantry Class: a constructor that takes one Jam parameter, and another that takes two Jam parameters. Unused instance variables in the Pantry object will be set to null.
Now modify the methods of the class to deal with null instance
variables:
print()
will test for null before printing
out a Jam.
select()
will have a return type of boolean
.
Return true
if the selection is available, otherwise false
.
Here is a PantryTester2 class that demonstrates the new code:
class PantryTester2 { public static void main ( String[] args ) { Jam goose = new Jam( "Gooseberry", "7/4/86", 12 ); Jam apple = new Jam( "Crab Apple", "9/30/99", 8 ); Pantry hubbard = new Pantry( goose, apple ); hubbard.print(); if ( hubbard.select(1) ) hubbard.spread(2); else System.out.println("Selection not available"); hubbard.print(); if ( hubbard.select(3) ) hubbard.spread(2); else System.out.println("Selection not available"); hubbard.print(); } }
When it is run, it prints the following:
1: Gooseberry 7/4/86 12 fl. oz. 2: Crab Apple 9/30/99 8 fl. oz. Spreading 2 fluid ounces of Gooseberry 1: Gooseberry 7/4/86 10 fl. oz. 2: Crab Apple 9/30/99 8 fl. oz. Selection not available 1: Gooseberry 7/4/86 10 fl. oz. 2: Crab Apple 9/30/99 8 fl. oz.
Of course, complete testing would involve a much larger PantryTester2.
Click here to go back to the main menu.replace()
method
Add more code to the program of exercise 2.
Write a method replace( Jam j, int slot )
for the Pantry
class
that replaces a particular jar of jam in the pantry with the object j
.
Here is a testing program:
class PantryTester3 { public static void main ( String[] args ) { Jam goose = new Jam( "Gooseberry", "7/4/86", 12 ); Jam apple = new Jam( "Crab Apple", "9/30/99", 8 ); Jam rhub = new Jam( "Rhubarb", "10/31/99", 16 ); Pantry hubbard = new Pantry( goose, apple ); hubbard.print(); if ( hubbard.select(3) ) hubbard.spread(2); else System.out.println("Selection not available"); hubbard.print(); hubbard.replace( rhub, 3 ); hubbard.print(); if ( hubbard.select(3) ) hubbard.spread(2); else System.out.println("Selection not available"); hubbard.print(); } }Click here to go back to the main menu.
Add a method to the Pantry class:
public void mixedFruit()
This method checks that each jar of jam in the pantry has 2 fluid ounces or less, and if so, replaces the first jar of jam with a jar of mixed fruit jam. The amount is the combined amount of the original three jars. The last two jars are set to null. (In other words, this method mixes the jam in all three jars to create a new jar that replaces the old jars.) Modify the testing program to use this new method. Here is sample output of a testing program:
1: Gooseberry 7/4/86 4 fl. oz. 2: Crab Apple 9/30/99 1 fl. oz. 3: Rhubarb 10/31/99 2 fl. oz. Spreading 2 fluid ounces of Gooseberry 1: Gooseberry 7/4/86 2 fl. oz. 2: Crab Apple 9/30/99 1 fl. oz. 3: Rhubarb 10/31/99 2 fl. oz. 1: Mixed Fruit 7/4/86 5 fl. oz.
Click here to go back to the main menu.
End of the Exercises