Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Below is the code for Task 1; can someone please give me the code for task 2 and 3 Territory.java public class Territory { private

Below is the code for Task 1; can someone please give me the code for task 2 and 3

image text in transcribedimage text in transcribed

Territory.java

public class Territory {

private int row;

private int column;

private String owner;

private int armies;

public Territory(int row, int col) {

this.row = row;

this.column = col;

}

public int getRow() {

return row;

}

public void setRow(int row) {

this.row = row;

}

public int getColumn() {

return column;

}

public void setColumn(int column) {

this.column = column;

}

public String getOwner() {

return owner;

}

public void setOwner(String owner) {

this.owner = owner;

}

public int getArmies() {

return armies;

}

public void setArmies(int armies) {

this.armies = armies;

}

public void placeArmies(Player player, int numOfArmies) {

this.setOwner(player.getName());

this.setArmies(this.getArmies() + numOfArmies);

}

public String toString() {

return "["+row+","+column+"]"+owner+"("+armies+")";

}

}

Player.java

public class Player {

private String name;

private int unplacedArmies;

Player() {

count++;

System.out.print("Enter player " + count + " 's name: ");

name = World.keyword.nextLine();

}

private static int count = 0;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getUnplacedArmies() {

return unplacedArmies;

}

public void setUnplacedArmies(int unplacedArmies) {

this.unplacedArmies = unplacedArmies;

}

public static int getCount() {

return count;

}

public void placeArmies(int numOfArmies) {

this.setUnplacedArmies(this.getUnplacedArmies() - numOfArmies);

}

}

World.java

public class World {

private Territory territory1;

private Territory territory2;

private Territory territory3;

private Territory territory4;

private Player player1;

private Player player2;

public static final Scanner keyword = new Scanner(System.in);

public World() {

player1 = new Player();

player2 = new Player();

territory1 = new Territory(0, 0);

territory2 = new Territory(1, 0);

territory3 = new Territory(0, 1);

territory4 = new Territory(1, 1);

}

public String toString() {

return territory1+" "+territory2+" "+territory3+" "+territory4;

}

public void placeArmies(Player player, Territory territory) {

System.out.print("How many armies would you like to place on "+territory+"? ");

int numOfArmies = keyword.nextInt();

player.placeArmies(numOfArmies);

territory.placeArmies(player, numOfArmies);

}

public Player getPlayer1() {

return player1;

}

public Player getPlayer2() {

return player2;

}

public Territory getTerritory1() {

return territory1;

}

public Territory getTerritory2() {

return territory2;

}

public Territory getTerritory3() {

return territory3;

}

public Territory getTerritory4() {

return territory4;

}

}

Main.java

public class Main {

public static void main(String[] args) {

World world = new World();

System.out.println(world.getPlayer1().getName() + ", please place your armies");

System.out.println(world);

world.placeArmies(world.getPlayer1(), world.getTerritory1());

System.out.println(world);

world.placeArmies(world.getPlayer1(), world.getTerritory2());

System.out.println(world);

System.out.println(world.getPlayer2().getName() + ", please place your armies");

System.out.println(world);

world.placeArmies(world.getPlayer2(), world.getTerritory3());

System.out.println(world);

world.placeArmies(world.getPlayer2(), world.getTerritory4());

System.out.println(world);

}

}

202 Code these elasses and fields in a Blue project and submit them to PLATE 203 Class names must begin with an upperease letter while field names must begin witha 204 lowercase letter. For compound words, the first letter of the second word should be capitalised. 205 All classes must be public and all fields must be private. 177 Explanation of the sample output 178 The world is created with 4 territories laidut in a 2x2 grid The first territory is at position [0,01 in the The second territory is at position 1,01 in the top-left corner top-right comer The third territory is at position 0, in eThe fourth territory is at positionis in 207 208 In this part, you will create the various classes that comprise the game. After completing part A, your project in BlueJ will look something like this bottom-left corner the bottom-right corner 179 The program begins by asking the user to input the name of each player. It then prints on a 180 ingle line the details of the 4 terories separated by a space. Each teritory is displayed in the 18 form poaition) owner (number ofarmiea).Forexample, [0,0 Joe (2) describes a 182 erritory at position [0,0] with 2 armies placed on it owned by Joe, while [1,0]null (0 183 describes a territory at position,01 with zero amies owned by nobody 184 The program then asks the first player to place armies anywhere on the top two territories, and 185 asks the second player to place armies anywhere on the bottom two territories. The number of 186 armies and the owner ofeach territory is updated and displayed to the user 187 Thus the final line in the box on the previous page describes a seenario like this Joe has two armies here Joe has one army here Sam has one army here Sam has two armies here 188 Tasks 189 These tasks must be completed in the order listed below 209 10 The actual position of your class icons will probably vary from what is shown above 2 NOTE: Unlike the lab tests, there is no "skeleton" file for the assignment. To start the 190 Task 1: Classes and fields 191 The following classes (written in italics) and "fields" "private data members" (enclosed in 192 double quotes) are more precisely defined in the attached javadoc: 212 assignment, select "New project" off the "Project" menu, as shown below 193.The World has 4 territories erritoryl" "erritory2". "leritory3". "Territory4") and 194 195 196 two players C"player", "player2") Tools View Help You should also define an additional field keyboard thus: public atatic final Scanner keyboard new Scanner (Syatem.in) 197 198Each Teritory has a "ow", a "column", an "owner" and a number of "armies" 199 200 Open Project Cl Open Recent Open Non Bel Open ZPIAR Each Player has a "name" and a number of"unplacedArmies". 213 You should also define an additional field "count" whichis shared by all players This will be used to keep track of the total number of players in the game. 201 214

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Transactions On Large Scale Data And Knowledge Centered Systems X Special Issue On Database And Expert Systems Applications Lncs 8220

Authors: Abdelkader Hameurlain ,Josef Kung ,Roland Wagner ,Stephen W. Liddle ,Klaus-Dieter Schewe ,Xiaofang Zhou

2013th Edition

ISBN: 3642412203, 978-3642412202

Students also viewed these Databases questions