Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted as XML by bob ( 15 years ago )
import java.util.*;
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 Ch35Ex1
{
  public static void main ( String[] args )
  {
   Scanner scan = new Scanner(System.in);
   int x,y,z = 0;

   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 );
   while(z == 0){


    System.out.println("The Jams are:");
     hubbard.print();
     System.out.println("Make your Selection: (Enter a 1, 2, or 3)");
     x = scan.nextInt();

     if(x == -1 ){
      z = 1;
     }
     else{
      hubbard.select(x);

      System.out.println("How much would you like to spread?");
      y = scan.nextInt();
      hubbard.spread(y);
     }



   }
   System.out.println("Good bye");
  }
}

 

Revise this Paste

Your Name: Code Language: