Results 1 to 3 of 3
  1. #1
    semi_noob is offline Member
    Join Date
    Apr 2012
    Posts
    1
    Rep Power
    0

    Default I need help creating classes to make a program run!!!

    Assume that a group of people have gone to a local apple orchard to pick some apples. Each person is given a bushel basket that they can fill up with apples. Each bushel basket holds 125 apples -- if you try to put more apples in the basket, they just roll off the top and fall to the ground. For this program you are going to write the BushelBasket class. The BushelBasket class is going to have objects that are very well encapsulated -- i.e., the data at the heart of each object, which is the number of apples currently in the basket, is always going to be accurate and cannot be corrupted.

    The main method below shows how the BushelBasket object gets used. You can create a basket that is empty, or it might already have some apples in it. The value number of apples that can be in a basket is between zero and 125, inclusive. If there is an attempt to create a basket with a negative number of apples, then set the basket count to zero. If they try to create a basket with more than 125 apples, set the count to 125.

    As people pick apples, the number of apples in their basket increases. If they eat apples from their basket, then the number of apples in their basket decreases. If they spill their basket, then count goes to zero.

    There are several boolean and int value returning methods in a BushelBasket object. The isEmpty and isFull methods let you know if the basket is empty or full (each of these methods returns a boolean value). You can also call getApples to get a current count of the number of apples currently in the basket, and roomLeftInBasket lets you know how many apples you still have to pick to fill the basket.

    Take the code below, copy it into jGRASP, and complete the BushelBasket class. When you run the main method below with your BushelBasket class, then you should get the output shown here:Output

    Rick...
    This bushel basket has 0 apples in it.
    This bushel basket has 33 apples in it.
    This bushel basket has 29 apples in it.
    This bushel basket has 0 apples in it.
    Newt...
    This bushel basket has 100 apples in it.
    false
    false
    100
    25
    Michele...
    true
    false
    false
    false
    false
    true
    Herman...
    This bushel basket has 0 apples in it.
    Jon...
    This bushel basket has 125 apples in it.
    Ron...
    This bushel basket has 20 apples in it.
    This bushel basket has 0 apples in it.
    This bushel basket has 125 apples in it.
    Gary...
    This bushel basket has 0 apples in it.
    Starting Code

    public class AppleOrchard
    {
    public static void main(String [] args)
    {
    System.out.println("Rick...");
    BushelBasket rick = new BushelBasket(0);
    rick.print();
    rick.pick(11);
    rick.pick(22);
    rick.print();
    rick.eat(4);
    rick.print();
    rick.spill();
    rick.print();

    System.out.println("Newt...");
    BushelBasket newt = new BushelBasket(100);
    newt.print();

    System.out.println( newt.isEmpty() );
    System.out.println( newt.isFull() );
    System.out.println( newt.getApples() );
    System.out.println( newt.roomLeftInBasket() );

    System.out.println("Michele...");
    BushelBasket michele = new BushelBasket(0);
    System.out.println( michele.isEmpty() );
    System.out.println( michele.isFull() );
    michele.pick(25);
    System.out.println( michele.isEmpty() );
    System.out.println( michele.isFull() );
    michele.pick(100);
    System.out.println( michele.isEmpty() );
    System.out.println( michele.isFull() );

    System.out.println("Herman...");
    BushelBasket herman = new BushelBasket(-5); // should default to 0
    herman.print();

    System.out.println("Jon...");
    BushelBasket jon = new BushelBasket(300); // should default to 125
    jon.print();

    System.out.println("Ron...");
    BushelBasket ron = new BushelBasket(20); // starts with 20
    ron.print();
    ron.eat(50); // can only eat down to zero apples
    ron.print(); // should see zero apples
    ron.eat(10); // back to 10
    ron.pick(1000); // basket can only hold 125 apples
    ron.print(); // should print 125

    System.out.println("Gary...");
    BushelBasket gary = new BushelBasket(); // should default to 0
    gary.print();
    }
    }

    and here is what I have so far and can't get the code to run right can anyone help me with this?

    class BushelBasket
    {
    private int DEFAULT_APPLES=0;
    private int DEFAULT_APPLES2=125;

    private int apples;

    public void spill()
    {
    apples = 0;
    }

    public void pick(int x)
    {
    apples = apples + x;
    }

    public void eat(int x)
    {
    apples = apples - x;
    }

    public int getApples()
    {
    return apples;
    }

    public void print()
    {
    int x = getApples();
    System.out.println("This bushel basket has " + x + " apples in it.");
    }

    public boolean isEmpty()
    {
    int emtpy = 0;

    if (apples <= emtpy)
    {
    return true;
    }
    else
    {
    return false;
    }
    }

    public boolean isFull()
    {
    int full = 125;

    if (apples >= full)
    {
    return true;
    }

    else
    {
    return false;
    }
    }

    public boolean roomLeftInBasket()
    {
    int full = 125;

    if (apples < full)
    {
    return true;
    }

    else
    {
    return false;
    }
    }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: I need help creating classes to make a program run!!!

    Thank you for posting your homework here. I'm sure that you know that no one is going to do this for you. Please ask an answerable question and we'll probably help you, but please do it soon. If you do this, I'll delete my comments here, but if not, I'll have no choice but to delete this thread as a homework dump.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

Similar Threads

  1. Replies: 4
    Last Post: 07-23-2011, 08:08 PM
  2. Replies: 23
    Last Post: 04-18-2011, 06:54 PM
  3. how to make to classes in the bank account program
    By buzzing in forum New To Java
    Replies: 3
    Last Post: 11-02-2010, 04:02 AM
  4. classes and creating objects
    By Latanyar in forum New To Java
    Replies: 4
    Last Post: 11-01-2010, 07:14 PM
  5. Creating inner classes
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-16-2008, 11:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •