This should be a simple one for you java prodigy\'s. Essentially I\'m working on
ID: 3531849 • Letter: T
Question
This should be a simple one for you java prodigy's. Essentially I'm working on a first year assignment that is to create a game where there is a player with a jar and also 3 jars on the ground, each containing one stone. The player must collect the stones from the jars and place them in his own.
I'm stuck on the last task for the assignment which is to;
Define a pickUp method for class Player that takes a jar as a parameter. When invoked, the stone contained in the given jar should be removed from that jar and stored into the player
Explanation / Answer
/*Explaination: Hey since stone is a private datamember of jar it can't be accessed by player class through jar's object. You need to define amethod in jar class called removeStone and then call that method through jars object. this is the only solution that come to my mind. Is this solution acceptable ? if yes I will code it for you...
Private data members can assesed via public methods only so you will need to add a method to jar class or else make stone public ?
You can also accomplish this by adding getter and setter method in your jar class to change its property. Currently I don't see any such method in the class thus it is impossible to modify the jar object once it is created
I have solved your plroblem using getter and setter methods below
save it as Player.java and run after compiling
*/
//Global class
import java.util.*;
class Global
{
public static final Scanner keyboard = new Scanner(System.in);
}
//Ground class
class Ground
{
private Jar jar1, jar2, jar3;
private Player player;
Ground()
{
player = new Player();
jar1 = new Jar(1, new Stone());
jar2 = new Jar(2, new Stone());
jar3 = new Jar(3, new Stone());
}
}
//Player class
public class Player
{
private String name;
private int position;
private Jar jar;
Player()
{
position = 0;
jar = new Jar();
System.out.print("Enter player's name: ");
name = Global.keyboard.nextLine();
}
public void move(int distance)
{
position = position+distance;
jar.move(distance);
}
public void pickUp(Jar jar)
{
Stone temp=jar.getStone();//get the stone of jar passed
this.jar.setStone(temp);//add it to jar of player
jar.setStone(null);//remove stone from passed jar
}
}
//Jar class
class Jar
{
private int position;
private Stone stone;
Jar()
{
position = 0;
stone = null;
}
Jar(int position, Stone stone)
{
this.position = position;
this.stone = stone;
}
public void move(int distance)
{
position = position+distance;
}
public Stone getStone(){//getter
return stone;
}
public void setStone(Stone s){//setter
stone=s;
}
}
//Stone class
class Stone
{
private String name;
private int weight;
Stone()
{
System.out.print("Enter stone name: ");
name = Global.keyboard.nextLine();
System.out.print("Enter stone weight: ");
weight = Global.keyboard.nextInt();
Global.keyboard.nextLine();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.