Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Help me finish this Java program code! Please!! This is what my teacher wants an

ID: 3641075 • Letter: H

Question

Help me finish this Java program code! Please!! This is what my teacher wants and I am stumped...please help.

thanks,
john

"Apples in the Orchard"

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();
}
}

class BushelBasket
{
// Write your code here.
}
Other Requirements

Use good programming style, as described briefly in A Short Summary Of Style and in more detail in Programming Style. You do not have to supply a comment for methods -- just the comment at the start of each Java class (the BushelBasket class for this program).

Explanation / Answer

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();
}
}
--------------------------------------------------------------------------------------
public class BushelBasket
{
//Atributes
private int numberOfApples;

//Constructors
public BushelBasket(int pNumbreOfApples)
{
if(pNumbreOfApples <= 125 && pNumbreOfApples >= 0)
{
numberOfApples = pNumbreOfApples;
}
else if(pNumbreOfApples > 125)
{
numberOfApples = 125;
}
else if(pNumbreOfApples < 0)
{
numberOfApples = 0;
}
}

public BushelBasket()
{
numberOfApples = 0;
}

//Methods
public boolean isEmpty()
{
if (numberOfApples == 0)
{
return true;
}
else
{
return false;
}
}

public boolean isFull()
{
if (numberOfApples == 125)
{
return true;
}
else
{
return false;
}
}

public int getApples()
{
return numberOfApples;
}

public int roomLeftInBasket()
{
return 125-numberOfApples;

}

public void spill()
{
numberOfApples = 0;
}

public void eat(int i)
{
if(numberOfApples > i)
{
numberOfApples -= i;
}
else
{
numberOfApples = 0;
}
}

public void pick(int i)
{
int pNumberOfApples = numberOfApples;

if(pNumberOfApples + i <= 125)
{
numberOfApples += i;
}
else
{
spill();
}
}

public void print()
{
System.out.print("This bushel basket has "+ numberOfApples+" apples in it.");
}
}