3.4 Jug Puzzle OOPs

Build your Jug

You should be able to construct jugs of any capacity, containing any valid volume of liquid. You will also need to be able to add and remove liquid from a jug. To start, do not concern yourself where the liquid will come from or go to, just be able to add without spilling and remove some or all of the liquid.

For the Jug class, identify

HAS-A: attributes

RESPONDS-TO: methods

constructors: special initialization code used when creating a new Jug() parameters will include the capacity and a volume of liquid.

methods:

toString(), the method that allows a user to fetch a String representation of an instance of a class (a.k.a. the object). The toString() method is a special method that can be defined in any class you create.

public String toString(){

Return “some string”;

}

add

remove

Check to see if your jug is well behaved:

  • can you make a jug of negative capacity?
  • can you make a jug that has a larger volume than capacity?
  • can you make a jug that has a negative currentVolume?
  • can you add a negative amount?
  • can you remove a negative amount?

Get two jugs to interact

2) Create a method to allow jugs to pourInto( ) one another jug.

public pourInto(Jug otherJug){

remove the amount that can be added to the otherJug

}

This is a new concept so once you have a well behaved jug you will need help to allow the jugs to communicate with each other.

Now Use the Jug in the Game