You are developing a new game in which different kinds of disks are played in pa
ID: 3789097 • Letter: Y
Question
You are developing a new game in which different kinds of disks are played in pairs on a game board. Two disks can be played whey they meet the following criteria:
1. Both disks are the same kind (i.e., instances of the same class)
2. Spotted disks have the same number of spots
3. Sided disks have the same number of sides
4. Colored disks are the same color
Write the four “canPlay” methods (do not write complete classes) indicated in the accompanying UML class diagram without duplicating code (in place of duplicating code, call the canPlay method in a super class). Note that the UML diagram is not complete - it just presents the information relevant to this problem. The typical Java equals method is similar to the canPlay method, but note the argument type in the UML class diagram below. See Horstmann and Cornell’s “recipe for writing the perfect equals method” for suggestions (especially for step 1). Disregard the private attribute named "memberName."
Disk +can Play(Disk d) boolean Spotted Disk Fancy Disk -memberName Spots int +can Play(Disk d) boolean Colored Disk Sided Disk -color String sides int +can Play(Disk d) boolean +can Play(Disk d) booleanExplanation / Answer
Complete explanantion is provided in comments. However reading Horstmann and Cornell’s “recipe for writing the perfect equals method” is advised. These CanPlay method are implemented in same fashion as we override the equals method.
Disk Class:
public class Disk {
public boolean canPlay(Disk d) {
// The common null check is extracted to parent implementation
if (d == null) //Check if incoming object is null
return false;
else
return true;
}
}
SpottedDisk Class:
public class SpottedDisk extends Disk{
private int spots;
@Override // Mentioning that the function is overridden
public boolean canPlay(Disk d) {
if (this == d) //Check if objects are exactly same, i.e same initializations
return true;
if (!super.canPlay(d)) //null check
return false;
if (!(d instanceof SpottedDisk)) //Check if objects are of same instance
return false;
SpottedDisk other = (SpottedDisk) d;
if (spots != other.spots) //Check if object's variable spot have same value
return false;
return true;
}
}
ColoredDisk class:
public class ColoredDisk extends FancyDisk{
private String color;
@Override // Mentioning that the function is overridden
public boolean canPlay(Disk d) {
if (this == d) //Check if objects are exactly same, i.e same initializations
return true;
if (!super.canPlay(d)) //null check
return false;
if (!(d instanceof ColoredDisk)) //Check if objects are of same instance
return false;
ColoredDisk other = (ColoredDisk) d;
if (color == null) { //Check if object's variable color have same value
if (other.color != null)
return false;
} else if (!color.equals(other.color))
return false;
return true;
}
}
SidedDisk class:
public class SidedDisk extends FancyDisk{
private int sides;
@Override // Mentioning that the function is overridden
public boolean canPlay(Disk d) {
if (this == d) //Check if objects are exactly same, i.e same initializations
return true;
if (!super.canPlay(d)) //null check
return false;
if (!(d instanceof SidedDisk)) //Check if objects are of same instance
return false;
SidedDisk other = (SidedDisk) d;
if (sides != other.sides) //Check if object's variable sides have same value
return false;
return true;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.