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

Part A (5%) Lines of Code Required: About 80 lines, excluding blank lines Topics

ID: 3596760 • Letter: P

Question

Part A (5%) Lines of Code Required: About 80 lines, excluding blank lines Topics: Data flow, OO programming basics, Control flow. Objectives: This assignment supports objectives 1-4 and graduate attributes A1, B2, B5, C3. In Part A and also Part B, you will build a simple game where the player’s goal is to collect 3 stones and place them in a certain order to unlock a treasure chest and win the treasure. All the arrows inside the above Figure of a BlueJ window will only appear in your BlueJ implementation when you have completed Part A. Tasks These tasks must be completed in the order listed below. Task 1: Classes and Fields / Private Data Members You should create the following classes (written underlined and BOLD) and “fields” / “private data members” (enclosed in double quotes): Each Stone has a “name” (which is a String) and a “weight” (which is an integer). This takes about 5 lines of code, ignoring blank lines, and with opening and closing braces on one line each. Each Jar has a “position” (which is an integer) and a “stone” (i.e. instance of class Stone, declared thus … Stone stone). This takes about 5 lines of code, ignoring blank lines, and with opening and closing braces on one line each. (Here, “Jar” is a class. It is NOT short for Java Archive File.) Each Player has a “name” (which is a String), a “position” (which is an integer) and a “jar” (i.e. instance of class Jar, declared thus … Jar jar). This takes about 6 lines of code, ignoring blank lines, and with opening and closing braces on one line each. The Ground has a “player” (i.e. instance of class Player), and three instances of class Jar, named “jar1”, “jar2” and “jar3”. This takes about 5-7 lines of code, ignoring blank lines, and with opening and closing braces on one line each. You must also define an additional class Global, thus: import java.util.Scanner; public class Global { public static final Scanner keyboard = new Scanner(System.in); } Because “keyboard” has been defined as “public static”, it can be accessed from any other class in your program; for example: String name = Global.keyboard.nextLine(); In the tasks below, instead of creating a new Scanner in several places in your code, use this global “keyboard” in all your classes. All class names must begin with an uppercase letter (e.g. Stone) while field names must begin with a lowercase letter (e.g. weight). For compound words, the first letter of the second and subsequent words should be capitalized (e.g. ladyGaGa). All classes must be public and all fields must be private. Code all these classes and fields in a BlueJ project and submit them to PLATE. Task 2: Simple constructors In this task, you will add constructors to some of classes to initialise the program. More than one of your constructors will need to read input from the user. Define constructors for each class as follows, and in the following order. 2.1 Stone Define a constructor for class Stone that reads input from the user and initialises the name and weight fields according to the following I/O (bold text indicates sample user input): Enter stone name: Diamond Enter stone weight: 12 Warning: NEVER save your Java Archive (“JAR”) file INSIDE your BlueJ project folder. When you create a JAR file, it zips up the entire contents of your project folder. If the project directory already contains a JAR file, that old JAR file is included in the new JAR file. If you do that several times, you end up with a HUGE JAR file that PLATE will not accept. Programming Fundamentals (48023) Spring 2017 Assignment (Released September 19) Page 7 Technical note: In the above example, the nextLine() method will read 8 characters (7 letters plus the newline character), while nextInt() will read only 2 characters (leaving the newline character unread). To avoid any problems after using nextInt(), you should immediately consume the remaining newline character by adding this line: Global.keyboard.nextLine(); This constructor takes about 8 lines of code, ignoring blank lines, and with opening and closing braces on one line each. Three of those 8 lines should be (not consecutively): System.out.print("Enter stone name: "); System.out.print("Enter stone weight: "); Global.keyboard.nextLine(); Submit your code to PLATE to receive marks for this part, and do the same for each subsequent part below. You are required to submit your progress to plate while you are making progress. 2.2 Jar Define a constructor for class Jar that initialises the position to zero and the stone to null. This takes about 5 lines of code, ignoring blank lines, and with opening and closing braces on one line each. 2.3 Player Define a constructor for class Player that asks the user to input their name as illustrated in the box below, where the bold text indicates user’s input. This constructor also initialises the position to zero, and creates the player’s jar using new Jar(). Enter player’s name: Joe This takes about 7 lines of code, ignoring blank lines, and with opening and closing braces on one line each. One of those lines should be: System.out.print("Enter player's name: "); Another of the lines should include Global.keyboard.nextLine(); Task 3: Advanced constructors 3.1 Jar Define another constructor for class Jar that takes two parameters, the initial position (integer) and an instance of class Stone, and uses these parameters to initialise the corresponding fields of the jar (i.e. “position” and “stone”). Define this constructor below your existing no-parameters constructor. This takes about 5 lines of code, ignoring blank lines, and with opening and closing braces on one line each. 3.2 Ground Define a constructor (with no parameters) for class Ground that creates the player and creates three jars, where each jar contains one stone. Creating the player will require one line of code, which includes the Programming Fundamentals (48023) Spring 2017 Assignment (Released September 19) Page 8 reserved word “new”. Creating the three jars will require at least three lines of code, with each line including one or two occurrences of the reserved word “new”. The stone for “jar1” should be in position 1, the stone for “jar2” should be in position 2, and the stone for “jar3” should be in position 3. Hint: when creating each of the three jars, the second parameter to the constructor is new Stone() NOTE: TASKS 4 AND 5 REQUIRE A SOLID GRASP OF CLASSES AND OBJECTS. IT IS RECOMMENDED THAT STUDENTS DO NOT SPEND EXCESSIVE TIME ON TASKS 4 AND 5. Task 4: move() methods in classes Player and Jar Define a “move” method for class Player that takes an integer distance as a parameter, which can be positive or negative. When invoked, this distance should be added to the player’s current position. Define this method compositionally so that when the player is moved, the player’s jar also moves with the player in the same direction and by the same distance. To implement this compositionally, you will also need to define a “move” method inside the Jar class so that when the player is asked to move, the player can then tell the jar to move in the same direction. This “move” method in class “Player” takes about 5 lines of code, ignoring blank lines, and with opening and closing braces on one line each in other words, the body of the method between the opening and closing braces is two lines of code, where one of those lines is a call to the “move” method inside the jar. The “move” method in class “Jar” takes about 4 lines of code, ignoring blank lines, and with opening and closing braces on one line each in other words, the body of the method between the opening and closing braces is a single line of code. Task 5: pickUp() method in class Player Add the following method to the class “Jar”: public void moveTo(Jar jar) { jar.stone = stone; stone = null; } Define a “pickup” method for class Player that takes an instance of class Jar as a parameter. This “pickUp” method takes 4 lines of code, ignoring blank lines, and with opening and closing braces on one line each in other words, the body of the method between the opening and closing braces is a single line of code. When this “pickUp” method is invoked, the stone contained in the given jar passed as a parameter should be removed from that jar and stored into the player’s jar. This is done in a single line of code by making a call to the method ”moveTo” inside the instance of class Jar which was passed as a parameter. The parameter for “moveTo” is the field / private data member of class Jar in this class player. Part B (10%) Lines of Code Required: About 200 lines, excluding blank lines Topics: Data flow, OO programming basics, Control flow. Objectives: This assignment supports objectives 1-5 and graduate attributes A1, B2, B5, C3. In Part B of the assignment, you will finish the game that you started building in Part A, using your solution to Part A as a starting point. Tasks These tasks must be completed in the order listed below. Task 1: Chest Define a public class Chest according to the following description. A Chest has an integer “combination”, an integer “position” and 3 jars (“jar1”, “jar2” and “jar3”). The constructor for class Chest should read the combination from the user according to the following I/O: Enter chest combination (5-10): 7 The chest should be created at position 4, and 3 empty jars should be created at the same position as the chest itself. Update the definition of class Ground to reflect that the ground has 1 player, 3 jars and a chest. Before continuing, make any necessary changes to your existing code to ensure that all constructors, methods and classes are public and all fields are private. Note!! Your Part B solution should be submitted on PLATE under the link “Treasure - Part B 2017 Spring”. Be careful not to submit under the link for Part A. Task 2: Unlocking the chest 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. Task 3: toString() method 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) Programming Fundamentals (48023) Spring 2017 Assignment (Released September 19) Page 11 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. The String returned by this method can embed variables this way: return "string1” + “string2” + variable + “another string” + … You can also use the StringBuffer class described in the text book. You must not, however, use the sprintf method (since it uses the untaught varargs feature). Task 4: turn() method 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. 6. 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. 7. 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. 8. 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) Hi, I need help with part B task 3 & 4 Below I am posting my codes: Ground: public class Ground { private Player player; private Jar jar1; private Jar jar2; private Jar jar3; private Chest chest; public 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 boolean isChestUnlocked() { return chest.getJar1Weight() + chest.getJar2Weight() * chest.getJar3Weight() == chest.getCombination(); } } Player 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 = position + distance; jar.move(distance); } public void pickUp ( Jar dJar) { dJar.moveTo(jar); } public String getName() { return name; } public Jar getJar() { return this.jar; } public int getPosition() { return this.position; } public String toString() { return getName() + "[](" + position + ")"; } } Jar public class Jar { private int position; private Stone stone; public Jar() { position = 0; stone = null; } public Jar( int firstPosition, Stone firstStone) { position = firstPosition; stone = firstStone; } public void move (int distance) { position = position + distance; } public void moveTo(Jar jar) { jar.stone = stone; stone = null; } public int getStoneWeight() { if (stone!= null) return stone.getWeight(); else return 0; } } Global import java.util.*; public class Global { public static final Scanner keyboard = new Scanner(System.in); } Stone ublic 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(); } public int getWeight() { return weight; } Chest public class Chest { private static int combination; private static int position; private Jar jar1; private Jar jar2; private Jar jar3; public Chest() { System.out.print("Enter chest combination (5-10): "); combination = Global.keyboard.nextInt(); Global.keyboard.nextLine(); position = 4; jar1 = new Jar(4, null); jar2 = new Jar(4, null); jar3 = new Jar(4, null); } public int getCombination() { return combination; } public int getJar1Weight() { return jar1.getStoneWeight(); } public int getJar2Weight() { return jar2.getStoneWeight(); } public int getJar3Weight() { return jar3.getStoneWeight(); } } Please help task 3 & 4 part B

Explanation / Answer

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;

}