I have been stuck on this problem for weeks. I am working on a game where a play
ID: 3534616 • Letter: I
Question
I have been stuck on this problem for weeks. I am working on a game where a player has to collect 3 stones and place them in a certain order to unlock a treasure chest and win the treasure.
There are 3 jars with stones on the ground. Each stone with its own weight and name. There are also 3 jars on the chest which are initially empty (stones set to null).
The description for this task is:
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 ethod should return true if and only if all 3 stones have been placed on the treasure chest in the correct order as described above.
When I have finished each task, I upload the code to a checking program which tells me if my code is correct or not. With my code I am getting an incorrect result.
What the output should be: What my output is:
Explanation / Answer
This is the modified Ground.java
-----------------------------------------------------
package BenjaminKing;
public class Ground
{
private Jar jar1, jar2, jar3;
private Player player;
private Chest chest;
Ground()
{
player = new Player();
jar1 = new Jar(1, new Stone());
jar2 = new Jar(2, new Stone());
jar3 = new Jar(3, new Stone());
chest = new Chest();
}
public Chest getChest() {
return chest;
}
public void setChest(Chest chest) {
this.chest = chest;
}
public boolean isChestUnlocked()
{
this.jar1.moveTo(chest.getJar1());
this.jar2.moveTo(chest.getJar2());
this.jar3.moveTo(chest.getJar3());
System.out.println(chest.getJar1().getStone().getWeight());
if(chest.getJar1().getStone()!=null && chest.getJar2().getStone()!=null &&chest.getJar3().getStone()!=null)
{
int a=chest.getJar1().getStone().getWeight();
int b=chest.getJar2().getStone().getWeight();
int c=chest.getJar3().getStone().getWeight();
int d=chest.getCombination();
if((a+(b*c)==d))
{
return true;
}
else
{
return false;
}
}
else
return false;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.