Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Implementing Inheritance Hierarchies For lab #3, you will implement ten classes. This may seem like an daunting task but none of the classes are large

Implementing Inheritance Hierarchies

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.

image text in transcribed

The matches Method

The matches method is very similar to but more simple than the equals method. In fact, you may follow Horstmann's equals "recipe" to create various matches methods throughout the lab #3. 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 Method The CircleTile ranks range from 1 to 9, while valid BambooTile ranks range from 2 to 9. The

toString methods should return strings such as "Circle 5" or "Bamboo 7".

The 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 returns for the remaining tiles are 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".

Finally, there are two special cases (these and other special cases add most of the complexity of the overall project): WhiteDragonTile and Bamboo1Tile. The toString methods for these classes return "White Dragon" and "Bamboo 1" respectively (which may be hard coded in the methods). The bamboo 1 tile sometimes causes a bit of confusion. The name passed into the constructor of the PictureTile class names a file containing an image that will be painted on the tile; the bamboo 1 tile is typically rendered with an image of a sparrow but the toString method should still display Bamboo 1.

Note: you need Lab3.java which has the main method for testing your work.

public class Lab3 { public static void main(String[] args) { Tile c0 = new CircleTile(3); Tile c1 = new CircleTile(3); Tile c2 = new CircleTile(4); if (c0.matches(c1) && !c0.matches(c2)) System.out.println("Circle Tile matches: PASS"); else System.out.println("Circle Tile matches: FAIL"); Tile b0 = new BambooTile(3); Tile b1 = new BambooTile(3); Tile b2 = new BambooTile(4); if (b0.matches(b1) && !b0.matches(b2)) System.out.println("Bamboo Tile matches: PASS"); else System.out.println("Bamboo Tile matches: FAIL"); if (c0.matches(b0)) System.out.println("Circle/Bamboo Tile matches: Fail"); else System.out.println("Circle/Bamboo Tile matches: PASS"); Tile ch0 = new CharacterTile('3'); Tile ch1 = new CharacterTile('3'); Tile ch2 = new CharacterTile('W'); if (ch0.matches(ch1) && !ch0.matches(ch2)) System.out.println("Character Tile matches: PASS"); else System.out.println("Character Tile matches: FAIL"); Tile p0 = new SeasonTile("Spring"); Tile p1 = new SeasonTile("Summer"); Tile p2 = new FlowerTile("Plum"); Tile p3 = new FlowerTile("Orchid"); if (p0.matches(p1) && p2.matches(p3) && !p0.matches(p2)) System.out.println("Picture Tile matches: PASS"); else System.out.println("Picture Tile matches: FAIL"); Tile wd0 = new WhiteDragonTile(); Tile wd1 = new WhiteDragonTile(); Tile b1t0 = new Bamboo1Tile(); Tile b1t1 = new Bamboo1Tile(); if (wd0.matches(wd1) && b1t0.matches(b1t1) && !wd0.matches(b1t0)) System.out.println("Special Case Tiles matches: PASS"); else System.out.println("Special Case Tiles matches: FAIL"); System.out.println(); System.out.printf("Should print Circle 3%20s%n", c0); System.out.printf("Should print Bamboo 3%20s%n", b0); System.out.printf("Should print Character 3%20s%n", ch0); System.out.printf("Should print West Wind%20s%n", ch2); System.out.printf("Should print Spring%20s%n", p0); System.out.printf("Should print White Dragon%20s%n", wd0); System.out.printf("Should print Bamboo 1%20s%n", b1t1); } } 
Tile +matches(in other : Tile): boolean CharacterTileWhite DragonTile Rank Tile Picture Tile #symbol : char +Character Tile(in symbol: char) #rank : int name String toString) : String+Picture Tile(in name: String) Rank Tile(in rank int) +matches(in other Tile): boolean+matches(in other Tile): boolean +to String) String +toString() String CircleTile BambooTile Season Tile FlowerTile Bamboo1Tile +CircleTile(in rank : int) | toString:String |+BambooTle(in rank : int) +to String) String +Bamboo1Tile0 Season Tile(in name String)+ +FlowerTile(in name String) to String: String

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

Pro SQL Server Wait Statistics

Authors: Enrico Van De Laar

1st Edition

1484211391, 9781484211397

More Books

Students also viewed these Databases questions

Question

What is the difference between Needs and GAP Analyses?

Answered: 1 week ago

Question

What are ERP suites? Are HCMSs part of ERPs?

Answered: 1 week ago