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

Creating Additional Classes for a Monopoly game Create a new class of objects fo

ID: 3772271 • Letter: C

Question

Creating Additional Classes for a Monopoly game

Create a new class of objects for a player in a monopoly game, and revise the public method in the Monopoly NetBeans project so that a player can move around the board. We are not implementing a complete monopoly game, but just creating a player class and testing how it works with the BoardSquare class

1. Design and create the class declaration for the player class according to the annotated UML diagram below, and add the class it to the NetBeans Monopoly project.

2. Modify the main() method in the Monopoly class so that the player moves around the board, as follows:

a. the player’s location will be the square that the player’s token is currently on.

b. the player’s location should start on Square zero, the Go square.

c. the computer should pick a random number beween 1 and 12 to simulate the roll of a pair of dice. It should pick two random numbers between 1 and 6 and add them together to ensure the same probability as rolling real dice.

d. The main method should then move the player to the new square by adding the random number to the number of the square the player is on, and set the player’s location to the new square. The squares are numbered 0 to 39, so whenever the new square number would go above 39 it should be reset (If newsquare > 39, then newsquare = newsquare – 39).

e. When a player arrives on a square:

i. the rent for the square should be subtracted from the player’s bank balance

ii. a message should be displayed (console or JoptionPane, it’s up to you) with

1. the name of the player

2. the name of the player’s token

3. the roll of the dice

4. the name of the square the player is now on

5. the rent for the square, if it is not zero

6. the player’s new bank balance The exact format of the message is up to you.

f. after a player has arrived on a square, the program should say “press enter to continue”, then use an input statement of some kind (keyboard scanner or inputDialog Window) to continue to the next turn.

g. The program should continue (loop) until the user stops the program, or the player’s bank balance is zero.

racecar,wheelbarrow,battleship, tophat, etc.

the number of the square the player is on initialized to zero

(name,token,location,bank balance)

package monopoly;

import java.util.*;


public class Monopoly {

/**
* @param args the command line arguments
*/
public static void main(String[] args) throws Exception
{

BoardSquare[] square = new BoardSquare[40]; // array of 40 monopoly squares
  
int i; // a loop counter
  
// call the method to load the array
loadArray(square);
  
// test the code by printing the data for each square
  
System.out.println("Data from the array of Monopoly board squares. Each line has: ");
System.out.println("name of the square, type, rent, price, color ");
for( i=0; i<40; i++)
System.out.println( square[i].toString() );
  
} // main()
//***********************************************************************
  
// method to load the BoardSquare array from a data file
public static void loadArray(BoardSquare[] square) throws Exception
{
int i; // a loop counter
  
// declare temporary variables to hold BoardSquare properties read from a file
String inName;
String inType;
int inPrice;
int inRent;
String inColor;
  
// Create a File class object linked to the name of the file to be read
java.io.File squareFile = new java.io.File("squares.txt");

// Create a Scanner named infile to read the input stream from the file
Scanner infile = new Scanner(squareFile);

  
/* This loop reads data into the square array.
* Each item of data is a separate line in the file.
* There are 40 sets of data for the 40 squares.
*/
for( i=0; i<40; i++)
{
// read data from the file into temporary variables
// read Strings directly; parse integers
inName = infile.nextLine();
inType = infile.nextLine();
inPrice = Integer.parseInt( infile.nextLine() );
inRent = Integer.parseInt( infile.nextLine() );;
inColor = infile.nextLine();

// intialze each square with the BoardSquare constructor
square[i] = new BoardSquare(inName, inType, inPrice, inRent, inColor);
} // end for
infile.close();

} // endLoadArray
//***********************************************************************

} // end class Monopoly
//***************************************************************************



class BoardSquare
{

private String name; // the name of the square
private String type; // property, railroad, utility, plain, tax, or toJail
private int price; // cost to buy the square; zero means not for sale
private int rent; // rent paid by a player who lands on the square
private String color; // many are null; this is not the Java Color class

// constructors
public BoardSquare()
{
name = "";
type = "";
price = 0;
rent = 0;
color = "";
} // end Square()

public BoardSquare(String name, String type, int price, int rent, String color)
{
this.name = name;
this.type = type;
this.price = price;
this.rent = rent;
this.color = color;
} // end Square((String name, String type, int price, int rent, String color)

// accesors for each property
public String getName()
{
return name;
} //end getName()

public String getType()
{
return type;
} //end getType()

public int getPrice()
{
return price;
} //end getPrice()

public int getRent()
{
return rent;
} //end getRent()

public String getColor()
{
return color;
} //end getColor()
  
// a method to return the BoardSquare's data as a String
public String toString()
{
String info;
info = (name +", "+type+", "+price + ", "+ rent+ ", "+color);
return info;
} //end toString()
  
}

Player (player for a Monopoly game) -name: String name of the player -token: String

racecar,wheelbarrow,battleship, tophat, etc.

-location: int

the number of the square the player is on initialized to zero

-balance: int the player's current bank balance initialized to 1500 +Player():void +Player(String,String,int,int): void

(name,token,location,bank balance)

+getName(): String +getToken(): String +getLocation() int +getBalane() int +setName(String): void +setToken(String): void returns data about the player as a String +setLocation(int):void +setBalance(int):voide +toString:() String

Explanation / Answer

import java.util.*;

class Monopoly {
    public static void main(String[] args) throws Exception{
        BoardSquare[] square = new BoardSquare[40]; // array of 40 monopoly squares
        int i; // a loop counter
        loadArray(square);
        // test the code by printing the data for each square
        System.out.println("Data from the array of Monopoly board squares. Each line has: ");
        System.out.println("name of the square, type, rent, price, color ");
        for(i=0; i<40; i++)
            System.out.println(square[i].toString());
        play(square);
    } // main()
    //***********************************************************************

    // method to load the BoardSquare array from a data file
    public static void loadArray(BoardSquare[] square) throws Exception{
        int i; // a loop counter
        // declare temporary variables to hold BoardSquare properties read from a file
        String inName;  
        String inType;
        int inPrice;
        int inRent;
        String inColor;
      
        // Create a File class object linked to the name of the file to be read
        java.io.File squareFile = new java.io.File("squares.txt");
      
        // Create a Scanner named infile to read the input stream from the file
        Scanner infile = new Scanner(squareFile);

        /* This loop reads data into the square array.
         * Each item of data is a separate line in the file.
         * There are 40 sets of data for the 40 squares.
         */
        for(i=0; i<40; i++){
            // read data from the file into temporary variables
            // read Strings directly; parse integers
            inName = infile.nextLine();
            inType = infile.nextLine();
            inPrice = Integer.parseInt( infile.nextLine() );
            inRent = Integer.parseInt( infile.nextLine() );;
            inColor = infile.nextLine();

            // intialze each square with the BoardSquare constructor
            square[i] = new BoardSquare(inName, inType, inPrice, inRent, inColor);
        } // end for
        infile.close();

    } // endLoadArray
    //***********************************************************************

    public static void play(BoardSquare[] square){
        Player p = new Player("WATSON","racecar",0,1500)
        Random rand = new Random();
        while (true){
            int pos_1 = rand().nextInt() % 6 + 1;
            int pos_2 = rand().nextInt() % 6 + 1;
            int curr_pos = p.getLocation();
          
            if (curr_pos + pos_1 + pos_2 > 39)
                p.setLocation(curr_pos + pos_1 + pos_2 - 39);
            else
                p.setLocation(curr_pos + pos_1 + pos_2);
            p.setBalance(p.getBalance() - square[p.getLocation()].getRent());
          
            if (p.getBalance() == 0)    break;
          
            System.out.println("press enter to continue : ");
            System.out.println("Press 'y' or 'Y' to continue : ");
            String ch = infile.nextLine();
          
            p.toString();
            System.out.println("Roll of the dice are :   "+(pos_1)+" "+(pos_2));
            square[p.getLocation()].toString();
          
            if (ch.charAt(0) == 'y' || ch.charAt(0) == 'Y')
                continue;
            else break;
        }
    }

} // end class Monopoly

class Player{
    private String name;
    private String token;
    private int location;
    private int bank_balance;
    public player(){
        name = "";
        token = "";
        location = 0;
        bank_balance = 1500;
    }
    public player(String name,String token,int location,int bank_balance){
        this.name = name;
        this.token = token;
        this.location = location;
        this.bank_balance = bank_balance;
    }
    public String getName(){
        return name;
    }
    public String getToken(){
        return token;
    }
    public int getLocation(){
        return location;
    }
    public int getBalance(){
        return bank_balance;
    }
    public void setName(String name){
        this.name = name;
    }
    public void setToken(String token){
        this.token = token;
    }
    public void setLocation(int location){
        this.location = location;
    }
    public void setBalance(int bank_balance){
        this.bank_balance = bank_balance;
    }
    public String toString(){
        String info;
        info = (name +", " + token +", "+ location + ", "+ bank_balance);
        return info;  
    }
}

class BoardSquare {
    private String name;    // the name of the square
    private String type;    // property, railroad, utility, plain, tax, or toJail
    private int price;      // cost to buy the square; zero means not for sale
    private int rent;       // rent paid by a player who lands on the square
    private String color;   // many are null; this is not the Java Color class

    // constructors
    public BoardSquare() {
        name = "";
        type = "";
        price = 0;
        rent = 0;
        color = "";
    } // end Square()

    public BoardSquare(String name, String type, int price, int rent, String color) {
        this.name = name;
        this.type = type;
        this.price = price;
        this.rent = rent;
        this.color = color;
    } // end Square((String name, String type, int price, int rent, String color)

    // accesors for each property
    public String getName() {
        return name;
    } //end getName()

    public String getType() {
        return type;
    } //end getType()

    public int getPrice() {
        return price;
    } //end getPrice()

    public int getRent() {
        return rent;
    } //end getRent()
  
    public String getColor(){
        return color;
    } //end getColor()
      
    // a method to return the BoardSquare's data as a String
    public String toString() {
        String info;
        info = (name +", "+type+", "+price + ", "+ rent+ ", "+color);
        return info;  
    } //end toString()
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote