Question
Using Java to create the following classes below. Create a parent class called ChessPiece . Then create child classes called Pawn , Knight , Bishop
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 parents 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
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started