Using Java to create the following classes below. Create a parent class called C
ID: 3859370 • Letter: U
Question
Using Java to create the following classes below.
Create a parent class called ChessPiece. Then create child classes called Pawn, Knight, Bishop, Rook, Queen, and King.
The ChessPiece class has an instance variable for color (a boolean: white?) and value (an int), indicating how ‘important’ it is, which is set by the constructor parameter. It also has an accessor method for it, calledgetValue(). The value of each class is:
Pawn 1
Knight 2
Bishop 3
Rook 5
Queen 9
King 1000
This class also has a method called move() which actually does nothing but System.out.println(“moving…”).
The ChessPiece class overrides the toString() method and returns the name of its class (e.g. “White Pawn” or “Black Knight”, etc…).
Each of the six ChessPiece subclasses further overrides the toString() method and returns the name of its class in addition to its value (e.g. “White Pawn(1)” or “Black Knight(2)”, etc…). Note: use a super call to the parent’s toString() method! Each of the six ChessPiece subclasses also overrides the move() method, to System.out.println() how this particular piece moves:
Pawn “forward 1”
Knight “like an L”
Bishop “diagonally”
Rook “horizontally or vertically”
Queen “like a bishop or a rook”
King “one square”
Note: Use the @Override annotation every time you override a method.
Main
Create a class called Main with a main() method that sets up a chessboard on a two-dimensional array of Pieces:
https://en.wikipedia.org/wiki/Chess#Setup
The main() method loops through the board and prints each piece (thus triggering its toString() method).
Continue from above, the ChessPiece class and its six subclasses.
The ChessPiece class must override the equals() method (and therefore also the hashCode method) as follows:
If two ChessPiece objects have a value within 1 of each other, they are considered equal.
The Pawn class must contain two new instance variables:
boolean hasBeenPromoted;
ChessPiece newPiece;
In the game of chess, when a Pawn reaches the far side of the board, it is exchanged for another ChessPiece; for example, a Pawn can “become” a Rook, or a Queen, etc…. It cannot become a King or Pawn though. Enforce these rules in a new method called public void promote(ChessPiece newPiece).
Override the equals() method of the Pawn class so that Pawns are NOT equal if one has been promoted and another has not. Pawns are also NOT equal if they have been promoted to different ChessPiece types.
Note: Use the @Override annotation every time you override a method.
Please follow the instructions above for a good rate
Explanation / Answer
Given below is the code for the question. Please rate the answer if it helped. Thank you.
ChessPiece.java
public class ChessPiece {
protected boolean isWhite;
protected int value;
public ChessPiece(boolean white, int val)
{
isWhite = white;
value = val;
}
public int getValue()
{
return value;
}
public boolean isWhite()
{
return isWhite;
}
public void move()
{
System.out.println("moving...");
}
@Override
public String toString()
{
String str = "";
if(isWhite)
str = "White ";
else
str = "Black ";
switch(value)
{
case 1:
str+="Pawn";
break;
case 2:
str+="Knight";
break;
case 3:
str+="Bishop";
break;
case 5:
str+="Rook";
break;
case 9:
str+="Queen";
break;
case 1000:
str+="King";
break;
}
return str;
}
@Override
public boolean equals(Object obj)
{
if(obj instanceof ChessPiece)
{
ChessPiece other = (ChessPiece) obj;
if(getValue() - other.getValue() == 0)
return true;
else
return false;
}
return false;
}
}
Pawn.java
public class Pawn extends ChessPiece {
protected boolean hasBeenPromoted;
protected ChessPiece newPiece;
public Pawn(boolean white) {
super(white, 1);
hasBeenPromoted = false;
newPiece = null;
}
@Override
public String toString()
{
return super.toString() + "(" + getValue() + ")";
}
@Override
public void move()
{
super.move();
System.out.print("forward 1");
}
public void promote(ChessPiece piece)
{
//promote only if newpiece is not pawn or king
if(newPiece != null && newPiece.getValue() != 1 && newPiece.getValue() != 1000)
{
hasBeenPromoted = true;
this.newPiece = piece;
}
}
@Override
public boolean equals(Object obj)
{
if(obj instanceof Pawn)
{
Pawn other = (Pawn) obj;
if(hasBeenPromoted && other.hasBeenPromoted) //both are promoted
{
return newPiece.getValue() == other.newPiece.getValue();
}
else if(!hasBeenPromoted && !other.hasBeenPromoted) //both are not promoted
return true;
else
return false;
}
else
return false;
}
}
Knight.java
public class Knight extends ChessPiece {
public Knight(boolean white) {
super(white, 2);
}
@Override
public String toString()
{
return super.toString() + "(" + getValue() + ")";
}
@Override
public void move()
{
super.move();
System.out.print("like an L");
}
}
Bishop.java
public class Bishop extends ChessPiece{
public Bishop(boolean white) {
super(white, 3);
}
@Override
public String toString()
{
return super.toString() + "(" + getValue() + ")";
}
@Override
public void move()
{
super.move();
System.out.print("like an L");
}
}
Rook.java
public class Rook extends ChessPiece{
public Rook(boolean white) {
super(white, 5);
}
@Override
public String toString()
{
return super.toString() + "(" + getValue() + ")";
}
@Override
public void move()
{
super.move();
System.out.print("horizontally or vertically");
}
}
Queen.java
public class Queen extends ChessPiece{
public Queen(boolean white) {
super(white, 9);
}
@Override
public String toString()
{
return super.toString() + "(" + getValue() + ")";
}
@Override
public void move()
{
super.move();
System.out.print("like a bishop or a rook");
}
}
King.java
public class King extends ChessPiece{
public King(boolean white) {
super(white, 1000);
}
@Override
public String toString()
{
return super.toString() + "(" + getValue() + ")";
}
@Override
public void move()
{
super.move();
System.out.print("one square");
}
}
TestChessPiece.java
public class TestChessPiece {
private static void initialize(ChessPiece chessBoard[][])
{
//initialize
for(int col = 0 ; col < 8; col++)
{
//both row 1 and row 6 should have pawns
chessBoard[1][col] = new Pawn(false); //black pawns
chessBoard[6][col] = new Pawn(true); //white pawns
}
//2 black and 2 white rooks
chessBoard[0][0] = new Rook(false);
chessBoard[0][7] = new Rook(false);
chessBoard[7][0] = new Rook(true);
chessBoard[7][7] = new Rook(true);
//2 black and 2 white Knights
chessBoard[0][1] = new Knight(false);
chessBoard[0][6] = new Knight(false);
chessBoard[7][1] = new Knight(true);
chessBoard[7][6] = new Knight(true);
//2 black and 2 white bishops
chessBoard[0][2] = new Bishop(false);
chessBoard[0][5] = new Bishop(false);
chessBoard[7][2] = new Bishop(true);
chessBoard[7][5] = new Bishop(true);
//1 black and 1 white king
chessBoard[0][3] = new King(false);
chessBoard[7][3] = new King(true);
//1 black and 1 white queen
chessBoard[0][4] = new Queen(false);
chessBoard[7][4] = new Queen(true);
}
private static void display(ChessPiece board[][])
{
System.out.print(" ");
for(int i = 1; i <= 8; i++)
System.out.printf(" %10d%10s", i," ");
System.out.println();
for(int i = 0; i < 8; i++)
{
System.out.print((i+1) + "| ");
for(int j = 0; j < 8; j++)
{
if(board[i][j] == null)
System.out.printf("%20s|"," ");
else
System.out.printf("%20s|",board[i][j]);
}
System.out.print(" ");
for(int k = 0; k < 8*20 + 9; k++)
System.out.print("-");
System.out.println();
}
System.out.println();
}
public static void main(String[] args) {
ChessPiece chessBoard[][] = new ChessPiece[8][8];
initialize(chessBoard);
display(chessBoard);
}
}
output
1 2 3 4 5 6 7 8
1| Black Rook(5)| Black Knight(2)| Black Bishop(3)| Black King(1000)| Black Queen(9)| Black Bishop(3)| Black Knight(2)| Black Rook(5)|
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2| Black Pawn(1)| Black Pawn(1)| Black Pawn(1)| Black Pawn(1)| Black Pawn(1)| Black Pawn(1)| Black Pawn(1)| Black Pawn(1)|
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
3| | | | | | | | |
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
4| | | | | | | | |
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
5| | | | | | | | |
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
6| | | | | | | | |
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
7| White Pawn(1)| White Pawn(1)| White Pawn(1)| White Pawn(1)| White Pawn(1)| White Pawn(1)| White Pawn(1)| White Pawn(1)|
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
8| White Rook(5)| White Knight(2)| White Bishop(3)| White King(1000)| White Queen(9)| White Bishop(3)| White Knight(2)| White Rook(5)|
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.