For lab #3, you will implement ten classes. This may seem like an daunting task
ID: 3791471 • Letter: F
Question
For lab #3, you will implement ten classes. This may seem like an daunting task but none of the classes are large and a couple are quite trivial. The ten classes created for this assignment will provide a framework for building the tile set used in our final project (Mah Jong Solitaire). Beginning with chapter 7, you will add the code necessary to make these classes visible, graphical classes.
Assignment
Implement the classes illustrated in the following UML class diagram. Accessability is denoted by the +, -, and # symbol at the left of each feature. Classes whose names are written in italics are abstract. The classes are abstract because no instances are required in the final version of Mah Jong. However, you may find it easier to develop, debug, and validate the classes if they are made concrete. When you submit lab 3, these three classes may be abstract or concrete at your discretion. (In older versions of Java, abstract classes were required to have at least one abstract method, but this requirement was dropped in pervious versions of the language.) Create an executable program by adding the provided Lab3.java file, which will provide some tests for your classes, and which will print the output of a few representative toString methods.
The matches Method
The matches method is very similar to but more simple than the equals method. In fact, you may follow Horstmann and Cornell's equals "recipe" to create various matches methods throughout the lab #3 (p. 232, steps 5 and 6). The result of the matches method is determined as follows:
1. Tile class matches method is true if both objects are instances of the same class (strict test, so instance of will not work); it is false otherwise
2. RankTile class matches method is true if both object are instances of the same class (again, strict test) and if the ranks are equal; it is false otherwise (call the Tile matches method to determine if the tiles are instances of the same class)
3. CharacterTile class matches method is true if both object are instances of the same class (again, strict test) and if the symbols are equal; it is false otherwise (call the Tile matches method to determine if the tiles are instances of the same class)
The RankTile and the CharacterTile can call the Tile matches method to avoid duplicating code. Classes that do not implement a matches method will inherit the method from their superclass.
Classes and the toString MethodValid CircleTile ranks range from 1 to 9, while valid BambooTile ranks range from 2 to 9. The
toString methods should return string such as "Circle 5" or "Bamboo 7".
Valid CharacterTile symbols are '1' through '9' and 'N', 'E', 'W', 'S', 'C', and 'F'. The toString method should return "Character 1" through "Character 9" for the first nine tiles. The return for the remaining tiles is as follows:
N: "North Wind"
E: "East Wind"
W: "West Wind"
S: "South Wind"
C: "Red Dragon"
F: "Green Dragon"
The PictureTile toString method will return the name, which will be one of the following: "Chrysanthemum", "Orchid", "Plum", "Bamboo", "Spring", "Summer", "Fall", or "Winter".
Tile +matches in other Tile) boolean Rank Tile Character Tile #symbol char #rank int +RankTile (in rank int) +CharacterTile(in symbol char) +matches(in other Tile) boolean +matches in other Tile) boolean +toString String BambooTile Circle Tile Season Tile +Circle Tile(in rank int) +BambooTile(in rank int) +toString0 String +toString(): String WhiteDragonTile Picture Tile name: String +toString String +Picture Tile(in name: String) +toString() String Flower Tile Bamboo1 Tile ttoString0 StringExplanation / Answer
public class Bamboo1Tile extends PictureTile{
private String name;
public Bamboo1Tile(String name) {
super(name);
// TODO Auto-generated constructor stub
}
public int hashCode() {
final int prime = 31;
int result = super.hashCode();
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Bamboo1Tile other = (Bamboo1Tile) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
public String toString() {
return "Bamboo1Tile [name=" + name + "]";
}
}
public class BambooTile extends RankTile {
private int rank;
public BambooTile(int rank) {
super(rank);
}
public int hashCode() {
final int prime = 31;
int result = super.hashCode();
result = prime * result + rank;
return result;
}
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
BambooTile other = (BambooTile) obj;
if (rank != other.rank)
return false;
return true;
}
public String toString() {
return "BambooTile [rank=" + rank + "]";
}
}
public class CharacterTile extends Tile {
protected char symbol;
public CharacterTile(char symbol) {
this.symbol = symbol;
}
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + symbol;
return result;
}
public boolean matches(Tile obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
CharacterTile other = (CharacterTile) obj;
if (symbol != other.symbol)
return false;
return true;
}
public String toString() {
return "CharacterTile [symbol=" + symbol + "]";
}
}
public class CircleTile extends RankTile{
private int rank;
public CircleTile(int rank) {
super(rank);
}
public int hashCode() {
final int prime = 31;
int result = super.hashCode();
result = prime * result + rank;
return result;
}
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
CircleTile other = (CircleTile) obj;
if (rank != other.rank)
return false;
return true;
}
public String toString() {
return "CircleTile [rank=" + rank + "]";
}
}
public class FlowerTile extends PictureTile{
protected String name;
public FlowerTile(String name) {
super(name);
// TODO Auto-generated constructor stub
}
public int hashCode() {
final int prime = 31;
int result = super.hashCode();
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
FlowerTile other = (FlowerTile) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
public String toString() {
return "FlowerTile [name=" + name + "]";
}
}
public class PictureTile extends Tile{
protected String name;
public PictureTile(String name) {
this.name = name;
}
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
public boolean matches(Tile obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
PictureTile other = (PictureTile) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
public String toString() {
return "PictureTile [name=" + name + "]";
}
}
public class RankTile extends Tile{
protected int rank;
public RankTile(int rank) {
this.rank = rank;
}
public int getRank() {
return rank;
}
public void setRank(int rank) {
this.rank = rank;
}
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + rank;
return result;
}
public boolean matches(Tile obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
RankTile other = (RankTile) obj;
if (rank != other.rank)
return false;
return true;
}
public String toString() {
return "RankTile [rank=" + rank + "]";
}
}
public class SeasonTile extends PictureTile{
private String name;
public SeasonTile(String name) {
super(name);
// TODO Auto-generated constructor stub
}
public int hashCode() {
final int prime = 31;
int result = super.hashCode();
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
public boolean equals(Tile obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
SeasonTile other = (SeasonTile) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
public String toString() {
return "SeasonTile [name=" + name + "]";
}
}
public abstract class Tile {
abstract boolean matches(Tile tile);
}
public class WhiteDragonTile extends Tile {
protected char symbol;
public WhiteDragonTile(char symbol) {
this.symbol = symbol;
}
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + symbol;
return result;
}
public boolean matches(Tile obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
WhiteDragonTile other = (WhiteDragonTile) obj;
if (symbol != other.symbol)
return false;
return true;
}
public String toString() {
return "WhiteDragonTile [symbol=" + symbol + "]";
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.