creation: 09/30/99; revised 03/07/00
This exercise will give you practice in composing objects out of other objects. First you will define the Jam class. Objects of this class represent jars of fruit preserves. Then several these jars will be placed in a Pantry.
1. Design of the Jam Class. Objects of this class will represent a jar of fruit preserves such as your grandmother made every Fall. Think of three values you would like to know about a jar of jam.
Now think of some methods that apply to a Jam object. It is almost always a good idea to have a way to print the data of an object:
This is a somewhat silly example, and it would not be unusual if you thought of different ways to fill these blanks.
2. Document the class. Now fill in the blanks for the documentation of the class:
3. Checking the Design. To check the design, write a small program that uses the class. Of course, the program can't be compiled and run until the class is written, but you can get a feel for if the class design is sensible by doing this.
You may have some doubts that this is a sensible program.
Perhaps it would be a good idea to have the spread()
method print out a message
each time it is used.
4. Fill in Instance Variables. Fill in the data type of each instance variable.
5. Complete the Constructor. The constructor will initialize the instance variables of the object being constructed.
6. Complete the Methods. If the user wants to spread more jam than is available, spread only the amount that is left in the jar.
7. Design of the Pantry class. Let us say that a pantry consists of three jars of jam of any type and size. The methods of a pantry will be to:
To use a jar of jam, one must first go to the pantry and select one.
8. Instance Variables and constructor. Decide on the instance variables. There will be a variable that refers to the currently selected jar of jam. When no jar is selected, it should refer to no object. Outsiders should not be able to directly change the variables.
class Pantry { // Instance Variables private jar1 ; private jar2 ; private jar3 ; private selected ; // Constructors Pantry( Jam jar1, Jam jar2, Jam jar3 ) { . jar1 = jar1 ; . jar2 = jar2 ; . jar3 = jar3 ; selected = ; } // Methods }
9. Implement print method. The print method will print each of the three jars in the pantry.
10. Implement the select() and spread() methods.
Entire Program, with testing class: You might wish to copy this program to NotePad, save it to a file, and to play with it.
class Jam { // Instance Variables String contents ; // type of fruit in the jar String date ; // date of canning int capacity ; // amount of jam in the jar // Constructors Jam( String contents, String date, int size ) { this . contents = contents ; this . date = date ; capacity = size; } // Methods public boolean empty () { return ( capacity== 0 ) ; } public void print () { System.out.println ( contents + " " + date + " " + capacity + " fl. oz." ) ; } public void spread ( int fluidOz) { if ( !empty() ) { if ( fluidOz <= capacity ) { System.out.println("Spreading " + fluidOz + " fluid ounces of " + contents ); capacity = capacity - fluidOz ; } else { System.out.println("Spreading " + capacity + " fluid ounces of " + contents ); capacity = 0 ; } } else System.out.println("No jam in the Jar!"); } } class Pantry { // Instance Variables private Jam jar1 ; private Jam jar2 ; private Jam jar3 ; private Jam selected ; // Constructors Pantry( Jam jar1, Jam jar2, Jam jar3 ) { this . jar1 = jar1 ; this . jar2 = jar2 ; this . jar3 = jar3 ; selected = null ; } // Methods public void print() { System.out.print("1: "); jar1 . print() ; System.out.print("2: "); jar2 . print() ; System.out.print("3: "); jar3 . print() ; } // assume that the user entered a correct selection, 1, 2, or 3 public void select( int jarNumber ) { if ( jarNumber == 1 ) selected = jar1 ; else if ( jarNumber == 2 ) selected = jar2 ; else selected = jar3 ; } // spread the selected jam public void spread( int oz ) { selected . spread( oz ) ; } } class PantryTester { 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", 3 ); Pantry hubbard = new Pantry( goose, apple, rhub ); hubbard.print(); hubbard.select(1); hubbard.spread(2); hubbard.print(); hubbard.select(3); hubbard.spread(4); hubbard.print(); } }When you run the program you will be rewarded with the output:
1: Gooseberry 7/4/86 12 fl. oz. 2: Crab Apple 9/30/99 8 fl. oz. 3: Rhubarb 10/31/99 3 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. 3: Rhubarb 10/31/99 3 fl. oz. Spreading 3 fluid ounces of Rhubarb 1: Gooseberry 7/4/86 10 fl. oz. 2: Crab Apple 9/30/99 8 fl. oz. 3: Rhubarb 10/31/99 0 fl. oz.
End of the Exercise. If you want to do it again, click on "Refresh" in your browser window. Click here to go back to the main menu.