Using Java to create the following classes below. Create a parent class called C
ID: 3854240 • 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, called getValue(). 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).
Please follow the instructions above for a good rate
Explanation / Answer
Hi,
Please see below the classes.Please comment for any queries/feedbacks.
Thanks,
ChessPiece.java
public class ChessPiece {
//instance variable
private boolean color;
private int value;
//Constructor
public ChessPiece(boolean color){
if(this instanceof Pawn ){
this.value = 1;
}
else if(this instanceof Knight ){
this.value = 2;
}
else if(this instanceof Bishop ){
this.value = 3;
}
else if(this instanceof Rook ){
this.value = 5;
}
else if(this instanceof Queen ){
this.value = 9;
}
else if(this instanceof King ){
this.value = 1000;
}
this.color =color;
}
/**
* move() which actually does nothing but System.out.println(“moving…”).
*/
public void move(){
System.out.println("moving...");
}
public boolean isColor() {
return color;
}
public void setColor(boolean color) {
this.color = color;
}
/**
* accessor method for it, called getValue()
* @return
*/
public int getValue() {
if(this instanceof Pawn ){
this.value = 1;
}
else if(this instanceof Knight ){
this.value = 2;
}
else if(this instanceof Bishop ){
this.value = 3;
}
else if(this instanceof Rook ){
this.value = 5;
}
else if(this instanceof Queen ){
this.value = 9;
}
else if(this instanceof King ){
this.value = 1000;
}
return value;
}
public void setValue(int value) {
this.value = value;
}
/**
* ChessPiece class overrides the toString() method and returns the name of its class
*/
public String toString(){
String retString = "";
if(isColor()){
retString = retString + "White ";
}
else{
retString = retString +"Black ";
}
if(this instanceof Pawn ){
retString = retString +"Pawn ";
}
else if(this instanceof Knight ){
retString = retString +"Knight ";
}
else if(this instanceof Bishop ){
retString = retString +"Bishop";
}
else if(this instanceof Rook ){
retString = retString +"Rook ";
}
else if(this instanceof Queen ){
retString = retString +"Queen ";
}
else if(this instanceof King ){
retString = retString +"King ";
}
return retString;
}
}
Knight.java
public class Knight extends ChessPiece {
//Constructor
public Knight(boolean color) {
super(color);
}
@Override
public void move(){
System.out.println("like an L");
}
@Override
/**
* subclasses further overrides the toString() method and returns the name of its class in addition to its value
*/
public String toString(){
String retString = super.toString() + "("+this.getValue()+")";
return retString;
}
}
Bishop.java
public class Bishop extends ChessPiece {
//Constructor
public Bishop(boolean color) {
super(color);
}
@Override
public void move(){
System.out.println("diagonally");
}
@Override
/**
* subclasses further overrides the toString() method and returns the name of its class in addition to its value
*/
public String toString(){
String retString = super.toString() + "("+this.getValue()+")";
return retString;
}
}
Pawn.java
public class Pawn extends ChessPiece {
//Constructor
public Pawn(boolean color) {
super(color);
}
@Override
public void move(){
System.out.println("forward 1.");
}
@Override
/**
* subclasses further overrides the toString() method and returns the name of its class in addition to its value
*/
public String toString(){
String retString = super.toString() + "("+this.getValue()+")";
return retString;
}
}
Rook.java
public class Rook extends ChessPiece {
////Constructor
public Rook(boolean color) {
super(color);
}
@Override
public void move(){
System.out.println("horizontally or vertically");
}
@Override
/**
* subclasses further overrides the toString() method and returns the name of its class in addition to its value
*/
public String toString(){
String retString = super.toString() + "("+this.getValue()+")";
return retString;
}
}
Queen.java
public class Queen extends ChessPiece {
////Constructor
public Queen(boolean color) {
super(color);
}
@Override
public void move(){
System.out.println("like a bishop or a rook");
}
@Override
/**
* subclasses further overrides the toString() method and returns the name of its class in addition to its value
*/
public String toString(){
String retString = super.toString() + "("+this.getValue()+")";
return retString;
}
}
King.java
public class King extends ChessPiece {
//Constructor
public King(boolean color) {
super(color);
}
@Override
public void move(){
System.out.println("one square");
}
@Override
/**
* subclasses further overrides the toString() method and returns the name of its class in addition to its value
*/
public String toString(){
String retString = super.toString() + "("+this.getValue()+")";
return retString;
}
}
Main.java
/**
* Create a class called Main with a main() method that sets up a chessboard on a two-dimensional array of Pieces:
* @author
*
*/
public class Main {
public static int row = 0;
public static int column = 0;
public static void main(String[] args) {
//Initialising the chessboard
ChessPiece[][] ChessBoard = {
{ new Rook(true), new Knight(true), new Bishop(true),
new Queen(true), new King(true), new Bishop(true),
new Knight(true), new Rook(true) },
{ new Pawn(true), new Pawn(true), new Pawn(true),
new Pawn(true), new Pawn(true), new Pawn(true),
new Pawn(true), new Pawn(true) },
{ null, null, null, null, null, null, null, null },
{ null, null, null, null, null, null, null, null },
{ null, null, null, null, null, null, null, null },
{ null, null, null, null, null, null, null, null },
{ new Pawn(false), new Pawn(false), new Pawn(false),
new Pawn(false), new Pawn(false), new Pawn(false),
new Pawn(false), new Pawn(false) },
{ new Rook(false), new Knight(false), new Bishop(false),
new Queen(false), new King(false), new Bishop(false),
new Knight(false), new Rook(false) } };
//The main() method loops through the board and prints each piece (thus triggering its toString() method).
for (row = 0; row < ChessBoard.length; row++)
{
System.out.println("");
System.out.println("---------------");
for(column = 0; column < ChessBoard[row].length; column++)
{
if(null!= ChessBoard[row][column]){
System.out.print(ChessBoard[row][column].toString()+"| ");
}
else{
System.out.print("EMPTY"+"| ");
}
}
}
System.out.println("");
System.out.println("---------------");
//Moving the piece
ChessBoard[1][2].move();
ChessBoard[6][1].move();
}
}
Sample output :
---------------
White Rook (5)| White Knight (2)| White Bishop(3)| White Queen (9)| White King (1000)| White Bishop(3)| White Knight (2)| White Rook (5)|
---------------
White Pawn (1)| White Pawn (1)| White Pawn (1)| White Pawn (1)| White Pawn (1)| White Pawn (1)| White Pawn (1)| White Pawn (1)|
---------------
EMPTY| EMPTY| EMPTY| EMPTY| EMPTY| EMPTY| EMPTY| EMPTY|
---------------
EMPTY| EMPTY| EMPTY| EMPTY| EMPTY| EMPTY| EMPTY| EMPTY|
---------------
EMPTY| EMPTY| EMPTY| EMPTY| EMPTY| EMPTY| EMPTY| EMPTY|
---------------
EMPTY| EMPTY| EMPTY| EMPTY| EMPTY| EMPTY| EMPTY| EMPTY|
---------------
Black Pawn (1)| Black Pawn (1)| Black Pawn (1)| Black Pawn (1)| Black Pawn (1)| Black Pawn (1)| Black Pawn (1)| Black Pawn (1)|
---------------
Black Rook (5)| Black Knight (2)| Black Bishop(3)| Black Queen (9)| Black King (1000)| Black Bishop(3)| Black Knight (2)| Black Rook (5)|
---------------
forward 1.
forward 1.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.