Question

Write a program two classes that will complete this secret square game. First download the driver...

Write a program two classes that will complete this secret square game. First download the driver and include it in your project.

Next create a class Space with the following

  • Instance variables
    • hasBeenUncovered: a true or false value indicating whether or not the space had been checked
    • isSecretSquare: a true or false value indicating if this space is the square
  • Constructors
    • Default: sets both instance variables to false
    • Parameterized: Takes in two parameters that set the instance variables to those values
  • Accessors and Mutators for every instance variable
  • Other Methods
    • toString: This returns a “#” if the space has been uncovered and a “_” if it has not

Next create a class Board with the following

  • Instance variable
    • board: This is a 2D array of type Space (which is defined above)
  • Class Constant:
    • BOARD_SIZE: This should be public, static, final, and set to 5
  • Constructors
    • Default: Initializes the board to the board size
  • Accessor ONLY for the instance variable board
  • Other Methods
    • initialize: This method which takes in nothing, and returns nothing initializes each board to a new instance of Space. Once it has done that it randomly picks one Space in board and sets it to the secret square.
    • isSecretSquare: This method returns whether or not a specific space on the board is the secret square. It should take in two variables which correspond to a specific space on the board (IE a specific space’s indices).
  • printBoard: This method which has no parameters and returns nothing simply prints out every space in board. As seen in the example dialog below.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

Note: Since the driver program is not provided, I’m not attaching any output for this program, just the two required classes only.

// Space.java

public class Space {

      // attributes

      private boolean hasBeenUncovered;

      private boolean isSecretSquare;

      // default constructor

      public Space() {

            hasBeenUncovered = false;

            isSecretSquare = false;

      }

      // paramterized constructor

      public Space(boolean hasBeenUncovered, boolean isSecretSquare) {

            this.hasBeenUncovered = hasBeenUncovered;

            this.isSecretSquare = isSecretSquare;

      }

      // getters and setters

      public boolean isUncovered() {

            return hasBeenUncovered;

      }

      public void setUncovered(boolean hasBeenUncovered) {

            this.hasBeenUncovered = hasBeenUncovered;

      }

      public boolean isSecretSquare() {

            return isSecretSquare;

      }

      public void setSecretSquare(boolean isSecretSquare) {

            this.isSecretSquare = isSecretSquare;

      }

      @Override

      public String toString() {

            if (hasBeenUncovered) {

                  //uncovered

                  return "#";

            } else {

                  //covered

                  return "_";

            }

      }

}

// Board.java

public class Board {

      // 2d array of spaces

      private Space[][] board;

      // board size

      private final int BOARD_SIZE = 5;

      // default constructor

      public Board() {

            // initializing 2d array

            board = new Space[BOARD_SIZE][BOARD_SIZE];

            // initializing board and choosing a secret space

            initialize();

      }

      // accessor for board

      public Space[][] getBoard() {

            return board;

      }

      // method to initialize the board

      public void initialize() {

            // looping and initializing all spaces to default

            for (int i = 0; i < BOARD_SIZE; i++) {

                  for (int j = 0; j < BOARD_SIZE; j++) {

                        board[i][j] = new Space();

                  }

            }

            // generating two values between 0 and BOARD_SIZE-1

            int randRow = (int) (Math.random() * BOARD_SIZE);

            int randCol = (int) (Math.random() * BOARD_SIZE);

            // setting space at these coordinates as secret square

            board[randRow][randCol].setSecretSquare(true);

      }

      // returns true if a space is secret square

      public boolean isSecretSquare(int row, int col) {

            // validating indices

            if (row < 0 || row >= BOARD_SIZE || col < 0 || col >= BOARD_SIZE) {

                  return false;// invalid index

            }

            // using isSecretSquare() method of space and returning result

            return board[row][col].isSecretSquare();

      }

      // method to print the board. here since no specifications are given and no

      // output is shown, i'm just printing this as a 2D matrix

      public void printBoard() {

            for (int i = 0; i < BOARD_SIZE; i++) {

                  for (int j = 0; j < BOARD_SIZE; j++) {

                        System.out.print(board[i][j]);

                  }

                  System.out.println();

            }

      }

}

Add a comment
Know the answer?
Add Answer to:
Write a program two classes that will complete this secret square game. First download the driver...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In Java,enhace the program base on the first one. 1. You are going to design (and...

    In Java,enhace the program base on the first one. 1. You are going to design (and code) a class called Name. The class Name will contain three properties: first, the first name which is a String initial, the middle initial, which is a single character. last, the last name, which is a String The class has three constructors: A default constructor, which accepts no parameters, calls constructors for the first and last name and sets the initial equal to a...

  • You will write a two-class Java program that implements the Game of 21. This is a...

    You will write a two-class Java program that implements the Game of 21. This is a fairly simple game where a player plays against a “dealer”. The player will receive two and optionally three numbers. Each number is randomly generated in the range 1 to 11 inclusive (in notation: [1,11]). The player’s score is the sum of these numbers. The dealer will receive two random numbers, also in [1,11]. The player wins if its score is greater than the dealer’s...

  • JAVA QUESTION 16 Write and submit the java source for the following class specification: The name...

    JAVA QUESTION 16 Write and submit the java source for the following class specification: The name of the class is Question_16 Class Question_16 has 3 instance variables: name of type String, age of type int and height of type double. Class Question_16 has the following methods: print - outputs the data values stored in the instance variables with the appropriate label setName - method to set the name setAge - method to set the age setHeight - method to set...

  • Create an abstract class “Appointment” that represents an event on a calendar. The “Appointment” class will...

    Create an abstract class “Appointment” that represents an event on a calendar. The “Appointment” class will have four instance variables: • An instance variable called “year” which will be of type int • An instance variable called “month” which will be of type int • An instance variable called “day” which will be of type int • An instance variable called “description” which will be of type String The “Appointment” class must also implement the following methods: • A getter...

  • Code in JAVA You are asked to implement “Connect 4” which is a two player game....

    Code in JAVA You are asked to implement “Connect 4” which is a two player game. The game will be demonstrated in class. The game is played on a 6x7 grid, and is similar to tic-tac-toe, except that instead of getting three in a row, you must get four in a row. To take your turn, you choose a column, and slide a checker of your color into the column that you choose. The checker drops into the slot, falling...

  • Programming Assignment 1 Write a class called Clock. Your class should have 3 instance variables, one...

    Programming Assignment 1 Write a class called Clock. Your class should have 3 instance variables, one for the hour, one for the minute and one for the second. Your class should have the following methods: A default constructor that takes no parameters (make sure this constructor assigns values to the instance variables) A constructor that takes 3 parameters, one for each instance variable A mutator method called setHour which takes a single integer parameter. This method sets the value of...

  • Need help to create general class Classes Data Element - Ticket Create an abstract class called...

    Need help to create general class Classes Data Element - Ticket Create an abstract class called Ticket with: two abstract methods called calculateTicketPrice which returns a double and getld0 which returns an int instance variables (one of them is an object of the enumerated type Format) which are common to all the subclasses of Ticket toString method, getters and setters and at least 2 constructors, one of the constructors must be the default (no-arg) constructor. . Data Element - subclasses...

  • Complete the program that solves the Eight Queens problem. The program’s output should look similar to:...

    Complete the program that solves the Eight Queens problem. The program’s output should look similar to: |1|0|0|0|0|0|0|0| |0|0|0|0|0|0|1|0| |0|0|0|0|1|0|0|0| |0|0|0|0|0|0|0|1| |0|1|0|0|0|0|0|0| |0|0|0|1|0|0|0|0| |0|0|0|0|0|1|0|0| |0|0|1|0|0|0|0|0| Use the Queens class given. In your implementation of the Queens class, complete the body of all methods marked as “To be implemented in Programming Problem 1.” Do not change any of the global variable declarations, constructor or placeQueens methods. Here is what I have so far with notes of what is needed. public class Queens...

  • House Class Specification The House class represents a house. A house has an address (address instance...

    House Class Specification The House class represents a house. A house has an address (address instance variable), a year it was built (built instance variable), and the names of residents of the house (residents instance variable). The declaration of each variable follows. private String address; private int built; private StringBuffer residents; The class methods are: 1. Constructor - Takes a string (representing the address) and an integer (representing the year the house was built) as parameters, and initializes the corresponding...

  • Create a UML diagram to help design the class described in exercise 3 below. Do this...

    Create a UML diagram to help design the class described in exercise 3 below. Do this exercise before you attempt to code the solution. Think about what instance variables will be required to describe a Baby class object; should they be private or public? Determine what class methods are required; should they be private or public? Write Java code for a Baby class. A Baby has a name of type String and an age of type integer. Supply two constructors:...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT