Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2014
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.70 KB | None | 0 0
  1.  
  2.  
  3. public class AppleOrchard
  4. {
  5.     public static void main(String [] args)
  6.     {
  7.         System.out.println("Rick...");
  8.         BushelBasket rick = new BushelBasket(0);
  9.         rick.print();
  10.         rick.pick(11);
  11.         rick.pick(22);
  12.         rick.print();
  13.         rick.eat(4);
  14.         rick.print();
  15.         rick.spill();
  16.         rick.print();
  17.        
  18.         System.out.println("Newt...");
  19.         BushelBasket newt = new BushelBasket(100);
  20.         newt.print();
  21.        
  22.         System.out.println( newt.isEmpty() );
  23.         System.out.println( newt.isFull() );
  24.         System.out.println( newt.getApples() );
  25.         System.out.println( newt.roomLeftInBasket() );
  26.        
  27.         System.out.println("Michele...");
  28.         BushelBasket michele = new BushelBasket(0);
  29.         System.out.println( michele.isEmpty() );
  30.         System.out.println( michele.isFull() );
  31.         michele.pick(25);
  32.         System.out.println( michele.isEmpty() );
  33.         System.out.println( michele.isFull() );
  34.         michele.pick(100);
  35.         System.out.println( michele.isEmpty() );
  36.         System.out.println( michele.isFull() );
  37.        
  38.         System.out.println("Herman...");
  39.         BushelBasket herman = new BushelBasket(-5);  // should default to 0
  40.         herman.print();
  41.        
  42.         System.out.println("Jon...");
  43.         BushelBasket jon = new BushelBasket(300);  // should default to 125
  44.         jon.print();
  45.        
  46.         System.out.println("Ron...");
  47.         BushelBasket ron = new BushelBasket(20);  // starts with 20
  48.         ron.print();
  49.         ron.eat(50);  // can only eat down to zero apples
  50.         ron.print();  // should see zero apples
  51.         ron.eat(10);  // back to 10
  52.         ron.pick(1000);  // basket can only hold 125 apples
  53.         ron.print();  // should print 125
  54.        
  55.         System.out.println("Gary...");
  56.         BushelBasket gary = new BushelBasket();  // should default to 0
  57.         gary.print();
  58.     }
  59. }
  60.  
  61. class BushelBasket
  62. {
  63.    
  64.    private int apples;
  65.    
  66.    
  67.    
  68.    
  69.    
  70.    public boolean isEmpty(int apples)
  71.    
  72.    {
  73.        
  74.        
  75.        if (apples == 0)
  76.            
  77.            return true;
  78.            return false;
  79.            
  80.            
  81.    }
  82.    
  83.    
  84.    public boolean isFull(int apples)
  85.    {
  86.        if (apples == 125)
  87.            
  88.            return true;
  89.        
  90.        else
  91.        
  92.            return false;
  93.    }
  94.    
  95.    public String print(int apples)
  96.    
  97.    {
  98.          
  99.          
  100.          System.out.println("This bushel basket has" + this.apples + "apples in it.");
  101.          
  102.          return print;
  103.          
  104.    }
  105.    //public int apples; /*Global Scope*/
  106.    public int pick(int apples/*Local Scope*/)
  107.    
  108.    {
  109.        /*Global Scope*/ // Local Scope
  110.        this.apples += apples;
  111.        if (this.apples > 125)
  112.        {
  113.         //Oops! We spilled.
  114.            this.apples = 125;
  115.            //Assuming set of real apples is [1,125]
  116.        }
  117.        //We return an integer, presumably the apple count
  118.        return this.apples;
  119.    }
  120.    
  121.    public int eat(int apples)
  122.    
  123.    {
  124.        
  125.       this.apples -= apples;
  126.       if (this.apples < 0)
  127.       {
  128.           this.apples =0;
  129.       }
  130.       return eat;
  131.    }
  132.      
  133.    public int spill(int apples)
  134.    
  135.    {
  136.    
  137.    
  138.        this.apples = 0;
  139.        
  140.        return spill;
  141.    }      
  142.    
  143.    public String roomLeftInBasket(int apples)
  144.    {
  145.        return 125 - this.apples;
  146.        
  147.    }
  148.        
  149.    public void setApples(int a)
  150.    
  151.    {
  152.    
  153.        apples = a;
  154.    }
  155.    
  156.    public int getApples()
  157.    
  158.    {
  159.        
  160.        return apples;
  161.    
  162.    }
  163.    
  164.        
  165.    
  166.    
  167.    
  168.    
  169.    
  170.    
  171.    
  172.    
  173.    
  174.    
  175.    
  176.    
  177.    
  178.    
  179.    
  180.        
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement