Thread: Home work help

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    3
    Rep Power
    0

    Home work help


    The Bucket Class
    This program keeps track of a bunch of people picking blackberries in the woods. The main method is already written in the PickingBerries class -- you need to create the class Bucket. A Bucket object keeps tracks of the number of blackberries that a person has picked and placed in their bucket. When a Bucket is created it has no berries in it. Then the number of berries in the bucket increases as the person picks berries. If the person picks ten berries, then the number of berries in the bucket goes up by ten.

    There are two methods that let you know how many berries are currently in a bucket. The method report prints the status of the bucket to the console with System.out.println or System.out.printf (whichever print method you prefer). The message has the general format

    This buck has 15 berries.

    You can also ask a bucket how many berries it currently contains with a call to getBerries. The getBerries method returns the number of berries currently in the bucket. Note that most of the methods in the Bucket class have a void return type, whereas the getBerries method is going to have an int return type, and the isEmpty method returns a boolean. The isEmpty method simply returns true if the number of berries in a bucket is currently zero, and returns false if there are some berries in the bucket.

    Copy the code shown below into a file named PickingBerries.java, then complete the Bucket class by writing the following methods:

    •pick
    •spillAll
    •getBerries
    •report
    •isEmpty
    Your program should give exactly the Expected Output shown below when run with the main method given in the PickingBerries class. Do not change the code in the main method in any way.

    Expected Output
    Out in the forest, some friends were picking blackberries...Alan...This bucket has 0 berriesThis bucket has 12 berriesThis bucket has 32 berriesThis bucket has 0 berriesLisa...This bucket has 150 berriesRight now: 150After picking one more: 151Ian...This bucket has 5 berriesAt the moment, together they have 156 berriesSallie has not picked any berries yet.Sallie has picked some berries.
    Run Your Program with this PickingBerries Code
    public class PickingBerries
    {
    public static void main(String [] args)
    {
    System.out.println("Out in the forest, some friends were picking blackberries...");

    Bucket alan = new Bucket(); // Alan has an empty bucket
    Bucket lisa = new Bucket(); // Lisa has an empty bucket
    Bucket ian = new Bucket();

    System.out.println("Alan...");

    alan.report(); // Prints to console: "This bucket has 0 berries."
    alan.pick(10); // 10 in bucket now
    alan.pick(2); // now up to 12
    alan.report(); // Prints to console: "This bucket has 12 berries."

    alan.pick(20); // now up to 32
    alan.report();

    alan.spillAll(); // all the berries spilled out of the bucket
    alan.report(); // should report zero berries in the bucket here

    System.out.println("Lisa...");
    lisa.pick(100);
    lisa.pick(50); // up to 150
    lisa.report();

    int amount = lisa.getBerries(); // the value 150 is returned and placed in amount
    System.out.printf("Right now: %d\n", amount);
    lisa.pick(1);
    int now = lisa.getBerries();
    System.out.printf("After picking one more: %d\n", lisa.getBerries());

    System.out.println("Ian...");
    ian.pick(5);
    ian.report();

    System.out.printf("At the moment, together they have %d berries\n",
    alan.getBerries() + lisa.getBerries() + ian.getBerries() );

    Bucket sallie = new Bucket();

    if (sallie.isEmpty())
    System.out.println("Sallie has not picked any berries yet.");
    else
    System.out.println("Sallie has picked some berries.");

    sallie.pick(10);
    sallie.pick(33);

    if (sallie.isEmpty())
    System.out.println("Sallie has not picked any berries yet.");
    else
    System.out.println("Sallie has picked some berries.");
    }
    }

    class Bucket
    {
    // Write your code here


    }
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    20
    Rep Power
    0
    public class Bucket(){
    private int bucket;//will keep track of how many berries in bucket

    public Bucket(){
    bucket = 0;//initializes bucket to 0 because 0 berries in bucket at start
    }

    public void pick(int berryNum){//accepts # of berries put in in main and adds to bucket
    bucket = bucket + berryNum;
    }

    public void spillAll(){//sets bucket to 0
    bucket = 0;
    }

    public int getBerries(){//returns bucket so can be stored in amount in main
    return(bucket);
    }

    public void report(){//reports # of berries in bucket
    System.out.print("This bucket has " + bucket + "berries");
    }

    public boolean isEmpty(){//reports true or false if bucket is empty
    boolean isEmpty;
    if (bucket == 0){
    isEmpty = true;
    }
    esle{
    isEmpty = false;
    }
    }
    }

    Comments on this post

    • mythos_ disagrees : doing other peoples homework is not helping anybody
    • NormR disagrees : spoonfeeding code does not help op learn to program
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    3
    Rep Power
    0
    thank you very much..

    Originally Posted by Person33
    public class Bucket(){
    private int bucket;//will keep track of how many berries in bucket

    public Bucket(){
    bucket = 0;//initializes bucket to 0 because 0 berries in bucket at start
    }

    public void pick(int berryNum){//accepts # of berries put in in main and adds to bucket
    bucket = bucket + berryNum;
    }

    public void spillAll(){//sets bucket to 0
    bucket = 0;
    }

    public int getBerries(){//returns bucket so can be stored in amount in main
    return(bucket);
    }

    public void report(){//reports # of berries in bucket
    System.out.print("This bucket has " + bucket + "berries");
    }

    public boolean isEmpty(){//reports true or false if bucket is empty
    boolean isEmpty;
    if (bucket == 0){
    isEmpty = true;
    }
    esle{
    isEmpty = false;
    }
    }
    }
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    20
    Rep Power
    0
    Sorry, I am new to this site. I will take your advice and not give code answers in the future.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2005
    Posts
    74
    Rep Power
    33
    Originally Posted by Person33
    Sorry, I am new to this site. I will take your advice and not give code answers in the future.
    I don't think they meant don't give code answers at all... after all, we're here to help. BUT, only help those who at least attempt it. No-one minds helping someone but doing it for them is a different matter. I usually insist on being payed to code (like my day job)

IMN logo majestic logo threadwatch logo seochat tools logo