Hey there Java brains, I am pretty inexperienced with coding and have gotten a b
ID: 3534812 • Letter: H
Question
Hey there Java brains,
I am pretty inexperienced with coding and have gotten a bit stuck here. I have deleted the last bit of code I had written to make your responses easier (in the chest class) so the first bit is easy I know.
I now need to define an int 'combination' and int 'position' and 3 jars in the chest class. Then have it print 'Enter chest combination (5-10).
the chest should be created at position 4 with 3 empty jars.
It is necessary for your program to detect when the player has placed all 3 stones on the chest in the
correct order in order to recognise that the game is over. The chest is locked with a combination
chosen by the user. A set of 3 stones A,B,C placed on the chest will unlock the chest only if the
weight of A plus the weight of B multiplied by the weight of C equals the combination. i.e.
A+B*C=combination.
Define a method in class Ground called isChestUnlocked() which is public, takes no parameters and
returns a boolean. This method should return true if and only if all 3 stones have been placed on the
treasure chest in the correct order as described above.
Define a public toString() method on class Ground that returns a String in the following format:
Jack[](0) [Quartz#4](1) [Pearl#3](2) [Opal#2](3) /6(4)
This string describes the player “Jack†with an empty jar at position 0, three jars at positions 1, 2
and 3 containing rocks named “Quartzâ€, “Pearl†and “Opal†with weights of 4, 3 and 2 respectively,
and the chest with combination 6 at position 4. The numbers and names may of course differ
depending on what data is entered by the user when the constructors are executed. Thus, your string
should always incorporate data directly from the fields. If the chest is unlocked, it should be
rendered as /(4) instead of /6(4).
This task must be implemented compositionally with a toString() method in each class
Define a method in class Ground called turn() which is public, takes no parameters and returns
nothing. This method allows the player to have his/her turn by asking the user to type in some
command, either 'l' to move the player left, 'r' to move the player right, 'p' to pick up a rock at the
current position or 'd' to drop a rock at the current position.
Your method should follow exactly the following output format:
Move (l/r/p/d): r
Your program must print the prompt â€Move (l/r/p/d): †and then read the user's input.
 If the user types ‘l’ or ‘r’, your program should send a message to the player object to move
one position to the left or right respectively.
 If the user types ‘p’, your program should send a message to the player to pick up a rock
from a jar on the ground at the current position. This command does nothing if the player’s
jar already contains a rock. Note that you should apply good design principles to simplify
your code, since you will also receive marks for the quality of your design.
 If the user types ‘d’, your program should send a message to the player to drop a rock into a
jar on the ground at the current position. This command does nothing if there is already a
rock in a jar at that position. (You should define any additional methods as necessary)
Modify the behaviour of the ‘p’ (pickup) and ‘d’ (drop) commands so that if the player’s current
position is the same as that of the chest, a rock will be picked up from, or dropped onto the chest.
Because the chest contains 3 jars at identical positions, picking up and dropping rocks over the
chest follows priority rules:
ï‚· When dropping a rock, jar1 is tried first, and if it already contains a rock, then jar2 is tried,
and then jar3.
ï‚· When picking up a rock, the player looks in jar3 first, and if no rock is in there, then jar2 is
tried, and then jar1.
public class Ground
{
private Player player;
private Jar jar1;
private Jar jar2;
private Jar jar3;
public Ground()
{
player = new Player();
jar1 = new Jar(1, new Stone());
jar2 = new Jar(2, new Stone());
jar3 = new Jar(3, new Stone());
}
}
public class Player
{
private String name;
private int position;
private Jar jar;
public Player()
{
System.out.print("Enter player's name: ");
name = Global.keyboard.nextLine();
position = 0;
jar = new Jar();
}
public void move(int distance)
{
position += distance;
jar.move(distance);
}
public void pickUp(Jar jar)
{
jar.moveTo(this.jar);
}
}
public class Jar
{
private int position;
private Stone stone;
public Jar()
{
this.position = 0;
this.stone = null;
}
public Jar(int position, Stone stone)
{
this.position = position;
this.stone = stone;
}
public void move(int distance)
{
position += distance;
}
public void moveTo(Jar jar)
{
jar.stone = stone;
stone = null;
}
}
public class Stone
{
private String name;
private int weight;
public Stone()
{
System.out.print("Enter stone name: ");
name = Global.keyboard.nextLine();
System.out.print("Enter stone weight: ");
weight = Global.keyboard.nextInt();
Global.keyboard.nextLine();
}
}
import java.util.Scanner;
public class Global
{
public static final Scanner keyboard = new Scanner(System.in);
}
public class Chest
{
private Jar jar1;
private Jar jar2;
private Jar jar3;
}
Explanation / Answer
contact me on timacs12@gmail.com