Object oriented programming
Exercises, class 5
1. Here is a partially coded Chronometer class:
public class Chronometer {
private int hour;
private int minute;
private double second;
...
Code the rest of the class so it has:
• A constructor to initialize the hours, minutes and seconds to 0. • A constructor to initialize the hours, minutes and seconds to the values passed to the constructor
upon creation of a new Chronometer object.
• 3 getters, one for each attribute • 3 setters, one for each attribute
2.
a) Code a definition for a class named ChessPiece. Its (public) attributes are:
The name of the piece
Its position on the chess board
b) Code a definition for a class named ChessBoard that represents a chess board. Its attributes are:
the height of the board in number of tiles (m tiles) – private attribute
the width of the board in number of tiles (n tiles) – private attribute
a 2D array of type ChessPiece[][] of m rows by n columns – private attribute
public attributes of type ChessPiece, one for each type of piece on the chess board: the pawn, the king,
the queen, the bishop, the knight, the rook. The value of each piece is the name of the piece.
c) For the ChessBoard class, code a constructor that takes values to initialize these attributes.
d) For the ChessBoard class, code 3 getters, one for each attribute that is not a chess piece.
e) For the ChessBoard class, code 3 setters, one for each attribute that is not a chess piece.
f) How could encapsulation be improved in this program? No code needed here; just answer the
question.
g) How could coupling be reduced in this program? No code needed here; just answer the question.