How do I do the following in Java? I have a Box Class that I need to initialise
ID: 3750239 • Letter: H
Question
How do I do the following in Java?
I have a Box Class that I need to initialise the variables as per the below code, the row is 3 and the column is 3. The Player Class is Enum and the code is attached below the Box Class
public class Box {
Player content; // The move this box holds (Empty, X, or O)
int row, col; // Row and column of this box (Not currently used but possibly useful in future updates)
/**
* Constructor
*/
public Box(int row, int col) {
// TODO: Initialise the variables row, col, and content
}
/**
* Clear the box content to EMPTY
*/
public void clear() {
// TODO: Set the value of content to EMPTY (Remember this is an enum)
}
/**
* Display the content of the box
*/
public void display() {
// TODO: Print the content of this box (" X " if it Player.X, " O " for Player.O and " " for Player.Empty)
// Hint: Can use an if-else or switch statement
***************************************************************************************************
**
* Enumeration for the players move
*/
public enum Player {
EMPTY, X, O
}
}
Explanation / Answer
Hi,
Thank you for the opportunity to help you with your assignment.
The following is the code for your requirements as in the instructions.I have made sure to include all that is necessary for you to complete this assignment.
I have added comments in the code for your reference. I have included all the changes required in TODO.
To initialize the row and col, you need to create an object of Box class (see BoxTest.java) and pass the value 3 as shown in the code.
If you have any queries, need changes in the code or need help in following the code, leave me a comment.
I will reply within reasonable time.
If this answer helps you with your assignment, do upvote it.
Your vote will motivate me to work better.
The following is the code
//File: Box.java
public class Box {
Player content; // The move this box holds (Empty, X, or O)
int row, col; // Row and column of this box (Not currently used but possibly useful in future updates)
/**
* Constructor
*/
public Box(int row, int col) {
// TODO: Initialise the variables row, col, and content
this.row = row;
this.col = col;
this.content = Player.EMPTY;
}
/**
* Clear the box content to EMPTY
*/
public void clear() {
// TODO: Set the value of content to EMPTY (Remember this is an enum)
this.content = Player.EMPTY;
}
/**
* Display the content of the box
*/
public void display() {
// TODO: Print the content of this box (" X " if it Player.X, " O " for Player.O and " " for Player.Empty)
// Hint: Can use an if-else or switch statement
String output = "";
if(content == Player.EMPTY) {
output = " ";
} else if(content == Player.X) {
output = " X ";
} else if(content == Player.O) {
output = " 0 ";
}
System.out.print(output);
}
}
//File: Player.java
/***************************************************************************************************
**
* Enumeration for the players move
*/
enum Player {
EMPTY, X, O
};
//File: BoxTest.java
public class BoxTest {
public static void main(String[] args) {
//Create an object of Box class with row and column initialized to 3
Box boxObject = new Box(3, 3);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.