Exercises_class_3.pdf

Exercises, class 3

1. Here is Java code to create a chess piece:

public class ChessPiece { public String pieceName; public String position; public ChessPiece(String pieceName, String position){ this.pieceName = pieceName; this.position = position; } }

a) Make this class abstract and add an abstract method to it, called move.

b) Take each of the classes from last exercise and have it implement the move method.

Note: the body of the concrete classes’ methods will only contain code to print “Movement!” to the screen.

2.

a) Create an abstract class called Room.

This class has 3 attributes:  xCoordinate  yCoordinate  zCoordinate

You will have to decide the type of these attributes.

The Room class also has an abstract method called open, and another abstract method called close.

b) Create two concrete classes that are children of the Room class, called Kitchen and Bedroom. A concrete class (representing concrete rooms) has 2 additional attributes in addition to the attributes of its parent class.

Also, when a concrete class implements the open method, it contains code to print the message “Room [insert number here] opened!” and when it implements the close method, it contains code to print the message “Room [insert number here] closed!”