Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Complete Java code and its explanation, gets thumbs up and much appreciation. Youll write a few simple classes and an enum that you may find

Complete Java code and its explanation, gets thumbs up and much appreciation.

Youll write a few simple classes and an enum that you may find useful when you write a PGN reader later.

Solution Description

Write the following classes and enums: Color an enum with two values, WHITE and BLACK which represent the colors of the chess pieces. Square a class to represent squares on a chess board. Square should be instantiable and have the following constructors and methods:

a public constructor Square(char file, char rank) which uses a file name such as 'a' and rank name such as '1' to initialize instance variables that store the file and rank (as char s), and optionally the String name of the square that would be returned by toString() (see below). Ideally, this constructor should delegate to the other constructor, described below.

a public constructor Square(String name) which uses a square name such as "a1" to initialize the instance variables described in the other constructor above.

a public instance method toString() which returns a String representation of the square name, e.g., "a1" . a properly written equals method that overrides the equals method from java.lang.Object and returns true for Square objects that have the same file

and rank values, false otherwise. Piece a class to represent chess pieces (Some people distinguish between pawns and pieces, well call pawns pieces as well.) Piece should be abstract and

have the following constructors and methods:

a public constructor that takes a Color parameter and stores its value in an instance variable

a public getColor() instance method that returns the Color of the piece

a public abstract instance method algebraicName() which returns a String containing the algebraic name of the piece, e.g., "" for pawns, or one of "K", "Q", "B", "N", "R" .

a public abstract instance method fenName() which returns a String containing the name for the piece according to FEN notation from http://www.saremba.de/chessgml/standards/pgn/pgn- complete.htm#c16.1 .

a public abstract instance method movesFrom(Square square) which returns a Square[] containg all the squares the piece could move to from square on a chess board containing only the piece.

A subclass of Piece named King which overrides Piece s abstract methods appropriately A subclass of Piece named Queen which overrides Piece s abstract methods appropriately A subclass of Piece named Bishop which overrides Piece s abstract methods appropriately A subclass of Piece named Knight which overrides Piece s abstract methods appropriately A subclass of Piece named Rook which overrides Piece s abstract methods appropriately A subclass of Piece named Pawn which overrides Piece s abstract methods appropriately

Add descriptions of how the the classes function, and they will be tested by for example: (Stub methods so that all classes compile and return values, as most of Piece and its subclasses will be checked in isolation)

Piece knight = new Knight(Color.BLACK); assert knight.algebraicName().equals("N"); assert knight.fenName().equals("n"); Square[] attackedSquares = knight.movesFrom(new Square("f6")); // test that attackedSquares contains e8, g8, etc.

Square a1 = new Square("a1"); Square otherA1 = new Square('a', '1'); Square h8 = new Square("h8"); assert a1.equals(otherA1); assert !a1.equals(h8);

There are some bonus points.

5 points Color enum 5 points constructor Square(char file, char rank) 5 points constructor Square(String name) 5 points Square s toString() method 5 points Square s equals method 5 points Piece s proper declaration and constructor 5 points Piece s getColor() method 5 points Piece s algebraicName() method 5 points Piece s fenName() method 5 points Piece s movesFrom(Square square) method 15 points for each subclass of Piece being instantiable and having correct implementations of the abstract methods from Piece`

image text in transcribedimage text in transcribed

Write the following classes and enums: Color - an enum with two values, WHITE and BLACK which represent the colors of the chess pieces. Square-a class to represent squares on a chess board. Square should be instantiable and have the following constructors and methods: a public constructor Square(char file, char rank) which uses a file name such as 'a' and rank name such as 1' to initialize instance variables that store the file and rank (as char s), and optionally the String name of the square that would be returned by toString() (see below). Ideally, this constructor should delegate to the other constructor, described below. o a public constructor Square(String name) which uses a square name such as "al" to initialize the instance variables described in the other constructor above. a public instance method toString( ) which returns a String representation of the square name, e.g., "al". a properly written equals method that overrides the equals method from java. lang. Object and returns true for Square objects that have the same file and rank values, false otherwise. Piece - a class to represent chess pieces (Some people distinguish between pawns and pieces, we'll call pawns pieces as well.) Piece should be abstract and have the following constructors and methods: a public constructor that takes a Color parameter and stores its value in an instance variable a public getColor() a public abstract instance method algebra icName( ) which returns a string containing the algebraic name of the piece, eg., "" for pawns, or one of o instance method that returns the color of the piece o a public abstract instance method fenName) which returns a String containing the FEN name for the piece. a public abstract instance method moves From (Square square) which returns a Squarell containg all the squares the piece could move to from square on a chess board containing only the piece. .A subclass of Piece named King which overrides Piece 's abstract methods appropriately A subclass of Piece named Queen which overrides Piece 's abstract methods appropriately . A subclass of Piece named Bishop which overrides Piece 's abstract methods appropriately .A subclass of Piece named Knight which overrides Piece 's abstract methods appropriately A subclass of Piece named Rook which overrides Piece 's abstract methods appropriately A subclass of Piece named Pawn which overrides Piece 's abstract methods appropriately Write the following classes and enums: Color - an enum with two values, WHITE and BLACK which represent the colors of the chess pieces. Square-a class to represent squares on a chess board. Square should be instantiable and have the following constructors and methods: a public constructor Square(char file, char rank) which uses a file name such as 'a' and rank name such as 1' to initialize instance variables that store the file and rank (as char s), and optionally the String name of the square that would be returned by toString() (see below). Ideally, this constructor should delegate to the other constructor, described below. o a public constructor Square(String name) which uses a square name such as "al" to initialize the instance variables described in the other constructor above. a public instance method toString( ) which returns a String representation of the square name, e.g., "al". a properly written equals method that overrides the equals method from java. lang. Object and returns true for Square objects that have the same file and rank values, false otherwise. Piece - a class to represent chess pieces (Some people distinguish between pawns and pieces, we'll call pawns pieces as well.) Piece should be abstract and have the following constructors and methods: a public constructor that takes a Color parameter and stores its value in an instance variable a public getColor() a public abstract instance method algebra icName( ) which returns a string containing the algebraic name of the piece, eg., "" for pawns, or one of o instance method that returns the color of the piece o a public abstract instance method fenName) which returns a String containing the FEN name for the piece. a public abstract instance method moves From (Square square) which returns a Squarell containg all the squares the piece could move to from square on a chess board containing only the piece. .A subclass of Piece named King which overrides Piece 's abstract methods appropriately A subclass of Piece named Queen which overrides Piece 's abstract methods appropriately . A subclass of Piece named Bishop which overrides Piece 's abstract methods appropriately .A subclass of Piece named Knight which overrides Piece 's abstract methods appropriately A subclass of Piece named Rook which overrides Piece 's abstract methods appropriately A subclass of Piece named Pawn which overrides Piece 's abstract methods appropriately

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Essentials of Database Management

Authors: Jeffrey A. Hoffer, Heikki Topi, Ramesh Venkataraman

1st edition

133405680, 9780133547702 , 978-0133405682

More Books

Students also viewed these Databases questions