$30
Assignment 4 –
How to submit: compress and upload JAVA files to Blackboard
Network Log Utility.
In this assignment we implement a Java class hierarchy which represents various two‐dimensional and three‐dimensional shapes. The hierarchy extends classes/interfaces from Java’s API and defines new abstract and concrete classes. As a start, I am providing the class’ stubs without any implementation. You may choose to write your implementation without relying on the provided stubs; however, please adhere to the naming conventions shown in the UML diagram. A test class is provided for this assignment. You will be graded on the efficiency of your code; reuse parent implementation whenever possible. Please submit a ZIP file containing your 6 classes.
Note:
ü This is an individual assignment; please do your own work, sharing and/or copying code and/or solution ideas with/from others will result in a grade of 0 and disciplinary actions for all involved parties. If you run into problems and have done your best to solve them, please contact me before/after class or by e-mail.
ü A 20% grade deduction for every day the assignment is late.
Preamble
From Java’s API we will utilize a number of classes including:
I) 𝑇𝑟𝑒𝑒𝑆𝑒𝑡, a class for maintaining a collection of objects in a sorted fashion. Using 𝑇𝑟𝑒𝑒𝑆𝑒𝑡 is similar to using an 𝐴𝑟𝑟𝑎𝑦𝐿𝑖𝑠𝑡. The 𝑇𝑟𝑒𝑒𝑆𝑒𝑡 works best with objects that implement the 𝐶𝑜𝑚𝑝𝑎𝑟𝑎𝑏𝑙𝑒 interface.
II) 𝐶𝑜𝑚𝑝𝑎𝑟𝑎𝑏𝑙𝑒, a generic type interface (i.e. template); classes that implement this interface are forced to implement the method 𝑐𝑜𝑚𝑝𝑎𝑟𝑒𝑇𝑜. The 𝑐𝑜𝑚𝑝𝑎𝑟𝑒𝑇𝑜 defines how two objects of the same type are compared. The basic implementation of the 𝑐𝑜𝑚𝑝𝑎𝑟𝑒𝑇𝑜 is to, first, decide on how to compare two objects and, second, return either: ü A 0 if two objects are deemed equivalent.
ü A negative number if the left object is smaller than the right object
ü A positive number if the left object is greater than the right object
Here is an example which compares two class objects based on the 𝑓𝑖𝑟𝑠𝑡 and 𝑠𝑒𝑐𝑜𝑛𝑑 fields respectively. Two objects of type 𝑀𝑦𝐶𝑙𝑎𝑠𝑠 are deemed equal if their 𝑓𝑖𝑟𝑠𝑡 and 𝑠𝑒𝑐𝑜𝑛𝑑 values are the same. If the 𝑓𝑖𝑟𝑠𝑡 fields are not equal, the result of 𝑐𝑜𝑚𝑝𝑎𝑟𝑒𝑇𝑜 are based on comparing the 𝑓𝑖𝑟𝑠𝑡 fields only. However, if the 𝑓𝑖𝑟𝑠𝑡 fields are equal, the result of 𝑐𝑜𝑚𝑝𝑎𝑟𝑒𝑇𝑜 is based on the comparison between the 𝑠𝑒𝑐𝑜𝑛𝑑 fields.
// 𝑛𝑜𝑡𝑒 𝑡ℎ𝑒 𝑐𝑙𝑎𝑠𝑠 𝑛𝑎𝑚𝑒 𝑏𝑒𝑡𝑤𝑒𝑒𝑛 𝑎𝑛𝑔𝑙𝑒 𝑏𝑟𝑎𝑐𝑘𝑒𝑡𝑠
𝑝𝑢𝑏𝑙𝑖𝑐 𝑐𝑙𝑎𝑠𝑠 𝑀𝑦𝐶𝑙𝑎𝑠𝑠 𝑖𝑚𝑝𝑙𝑒𝑚𝑒𝑛𝑡𝑠 𝐶𝑜𝑚𝑝𝑎𝑟𝑎𝑏𝑙𝑒 𝑀𝑦𝐶𝑙𝑎𝑠𝑠
𝑖𝑛𝑡 𝑓𝑖𝑟𝑠𝑡;
𝑆𝑡𝑟𝑖𝑛𝑔 𝑠𝑒𝑐𝑜𝑛𝑑;
@𝑂𝑣𝑒𝑟𝑟𝑖𝑑𝑒
𝑝𝑢𝑏𝑙𝑖𝑐 𝑖𝑛𝑡 𝑐𝑜𝑚𝑝𝑎𝑟𝑒𝑇𝑜 𝑀𝑦𝐶𝑙𝑎𝑠𝑠 𝑜𝑡ℎ𝑒𝑟
𝑖𝑛𝑡 𝑟𝑒𝑠𝑢𝑙𝑡 𝐼𝑛𝑡𝑒𝑔𝑒𝑟. 𝑐𝑜𝑚𝑝𝑎𝑟𝑒 𝑓𝑖𝑟𝑠𝑡, 𝑜𝑡ℎ𝑒𝑟. 𝑓𝑖𝑟𝑠𝑡 ;
// 𝑖𝑓 𝑡ℎ𝑒 𝑓𝑖𝑟𝑠𝑡 𝑓𝑖𝑒𝑙𝑑𝑠 𝑚𝑎𝑡𝑐ℎ, 𝑟𝑒𝑠𝑢𝑙𝑡𝑠 𝑎𝑟𝑒 𝑏𝑎𝑠𝑒𝑑 𝑜𝑛 𝑐𝑜𝑚𝑝𝑎𝑟𝑖𝑛𝑔 𝑡ℎ𝑒 𝑠𝑒𝑐𝑜𝑛𝑑 𝑓𝑖𝑒𝑙𝑑𝑠
𝑖𝑓 𝑟𝑒𝑠𝑢𝑙𝑡 0
// 𝑟𝑒𝑠𝑢𝑙𝑡 𝑖𝑠 𝑏𝑎𝑠𝑒𝑑 𝑜𝑛 𝑐𝑜𝑚𝑝𝑎𝑟𝑖𝑛𝑔 𝑡ℎ𝑒 𝑠𝑒𝑐𝑜𝑛𝑑 𝑓𝑖𝑒𝑙𝑑𝑠
𝑟𝑒𝑡𝑢𝑟𝑛 𝑠𝑒𝑐𝑜𝑛𝑑. 𝑐𝑜𝑚𝑝𝑎𝑟𝑒𝑇𝑜 𝑜𝑡ℎ𝑒𝑟. 𝑠𝑒𝑐𝑜𝑛𝑑 ;
// ℎ𝑒𝑟𝑒 𝑤ℎ𝑒𝑛 𝑡ℎ𝑒 𝑓𝑖𝑟𝑠𝑡 𝑓𝑖𝑒𝑙𝑑𝑠 𝑑𝑜 𝑛𝑜𝑡 𝑚𝑎𝑡𝑐ℎ
𝑟𝑒𝑡𝑢𝑟𝑛 𝑟𝑒𝑠𝑢𝑙𝑡;
Class’ Description:
The assignment consists of 6 Java classes, 3 abstract and 3 non‐abstract classes and 1 test class. Class stubs are provided for an easier start.
I. 𝑺𝒉𝒂𝒑𝒆:
ü An abstract class which implements Java’s 𝐶𝑜𝑚𝑝𝑎𝑟𝑎𝑏𝑙𝑒 Interface
ü Contains two abstract methods, 𝑎𝑟𝑒𝑎 and 𝑝𝑒𝑟𝑖𝑚𝑒𝑡𝑒𝑟.
ü 𝑡𝑜𝑆𝑡𝑟𝑖𝑛𝑔: returns a space‐delimited string of its fields:
≪ 𝑣𝑎𝑙𝑢𝑒 𝑜𝑓 𝑖𝑑 ≫ ≪ 𝑣𝑎𝑙𝑢𝑒 𝑜𝑓 𝑛𝑎𝑚𝑒 ≫ ≪ 𝑣𝑎𝑙𝑢𝑒 𝑜𝑓 𝑑𝑒𝑠𝑐𝑟𝑖𝑝𝑡𝑖𝑜𝑛 ≫ ≪ 𝑣𝑎𝑙𝑢𝑒 𝑜𝑓 𝑐𝑜𝑙𝑜𝑟 ≫
ü 𝑔𝑒𝑡𝐶𝑜𝑙𝑜𝑟𝑁𝑎𝑚𝑒: returns the name of the 𝐶𝑜𝑙𝑜𝑟 as a 𝑆𝑡𝑟𝑖𝑛𝑔. This method is simply the reverse implementation of 𝑔𝑒𝑡𝐶𝑜𝑙𝑜𝑟 in the test class
ü 𝑐𝑜𝑚𝑝𝑎𝑟𝑒𝑇𝑜: numerically compares two objects of type 𝑆ℎ𝑎𝑝𝑒. Returns the value 0 if both objects have the same 𝑛𝑎𝑚𝑒 and 𝑐𝑜𝑙𝑜𝑟. Use 𝑔𝑒𝑡𝐶𝑜𝑙𝑜𝑟𝑁𝑎𝑚𝑒 when comparing colors. Note that the 𝑖𝑑 and 𝑑𝑒𝑠𝑐𝑟𝑖𝑝𝑡𝑖𝑜𝑛 fields are excluded here. Otherwise, return the results of the first mismatch comparison between 𝑛𝑎𝑚𝑒 then 𝑐𝑜𝑙𝑜𝑟. In other
words, if the two 𝑛𝑎𝑚𝑒 fields are the same return the results of comparing the two 𝑐𝑜𝑙𝑜𝑟 fields. II. 𝑺𝒉𝒂𝒑𝒆𝟐𝑫 and 𝑺𝒉𝒂𝒑𝒆𝟑𝑫:
ü Abstract classes which inherits from class 𝑆ℎ𝑎𝑝𝑒.
ü The non‐default constructor initializes the class’ private fields
ü 𝑡𝑜𝑆𝑡𝑟𝑖𝑛𝑔: returns the same value described in the parent class but includes ℎ𝑒𝑖𝑔ℎ𝑡, 𝑤𝑖𝑑𝑡ℎ, and 𝑙𝑒𝑛𝑔𝑡ℎ fields. The method must re‐use the parent class’ 𝑡𝑜𝑆𝑡𝑟𝑖𝑛𝑔 implementation.
ü 𝑐𝑜𝑚𝑝𝑎𝑟𝑒𝑇𝑜 numerically compares two objects of type 𝑆ℎ𝑎𝑝𝑒2𝐷 (or 𝑆ℎ𝑎𝑝𝑒3𝐷). Returns the value 0 if both objects have the same 𝑖𝑑, 𝑛𝑎𝑚𝑒, 𝑑𝑒𝑠𝑐𝑟𝑖𝑝𝑡𝑖𝑜𝑛, 𝑤𝑖𝑑𝑡ℎ, ℎ𝑒𝑖𝑔ℎ𝑡, and 𝑙𝑒𝑛𝑔𝑡ℎ (for 𝑆ℎ𝑎𝑝𝑒3𝐷 only). The 𝑐𝑜𝑚𝑝𝑎𝑟𝑒𝑇𝑜 in 𝑆ℎ𝑎𝑝𝑒2𝐷 must re‐use the 𝑐𝑜𝑚𝑝𝑎𝑟𝑒𝑇𝑜 from the 𝑆ℎ𝑎𝑝𝑒 class. The 𝑐𝑜𝑚𝑝𝑎𝑟𝑒𝑇𝑜 in 𝑆ℎ𝑎𝑝𝑒3𝐷 must reuse the 𝑐𝑜𝑚𝑝𝑎𝑟𝑒𝑇𝑜 from the 𝑆ℎ𝑎𝑢𝑢𝑝𝑒2𝐷 class.
III. 𝑸𝒖𝒂𝒅𝒓𝒊𝒍𝒂𝒕𝒆𝒓𝒂𝒍
ü Represents 90° angle quadrilateral 2D shapes
ü Inherits from class 𝑆ℎ𝑎𝑝𝑒
ü 𝑎𝑟𝑒𝑎 width height
ü 𝑝𝑒𝑟𝑖𝑚𝑒𝑡𝑒𝑟 2 width height
IV. 𝑸𝒖𝒂𝒅𝒓𝒊𝒍𝒂𝒕𝒆𝒓𝒂𝒍𝟑𝑫
ü Represents 90° angle quadrilateral 3D shapes
ü 𝑎𝑟𝑒𝑎 2 width height width length height length
ü 𝑝𝑒𝑟𝑖𝑚𝑒𝑡𝑒𝑟 4 width height length
V. 𝑺𝒉𝒂𝒑𝒆𝑳𝒊𝒔𝒕
ü Extends 𝑗𝑎𝑣𝑎. 𝑢𝑡𝑖𝑙. 𝑇𝑟𝑒𝑒𝑆𝑒𝑡
ü 𝑎𝑑𝑑: checks if a similar 𝑆ℎ𝑎𝑝𝑒 instance is already stored. If a similar object is found, the method returns false. If it is not, the object is added and the method returns 𝑡𝑟𝑢𝑒. YOU MUST USE the 𝑐𝑜𝑛𝑡𝑎𝑖𝑛𝑠 method from 𝑇𝑟𝑒𝑒𝑆𝑒𝑡 which requires that the method 𝑐𝑜𝑚𝑝𝑎𝑟𝑒𝑇𝑜 is be overloaded properly. Do not write your own search code.
ü 𝑔𝑒𝑡2𝐷𝑆ℎ𝑎𝑝𝑒𝑠: returns a new set containing instances of supertype 𝑆ℎ𝑎𝑝𝑒2𝐷 ONLY. Hint: the 𝑖𝑛𝑠𝑡𝑎𝑐𝑒𝑜𝑓 operator is useful here.
ü 𝑔𝑒𝑡3𝐷𝑆ℎ𝑎𝑝𝑒𝑠: returns a new set containing instances of supertype 𝑆ℎ𝑎𝑝𝑒3𝐷 ONLY.
ü 𝑝𝑟𝑖𝑛𝑡𝐹𝑜𝑟𝑚𝑎𝑡𝑡𝑒𝑑: prints a sorted and formatted table of all 𝑆ℎ𝑎𝑝𝑒 objects (Figure 2). The list is automatically sorted by 𝑁𝑎𝑚𝑒, then 𝐶𝑜𝑙𝑜𝑟, then 𝐷𝑖𝑚𝑒𝑛𝑠𝑖𝑜𝑛 based on the 𝑐𝑜𝑚𝑝𝑎𝑟𝑒𝑇𝑜 implementation described earlier.
VI. 𝑨𝟒𝑻𝒆𝒔𝒕:
ü This is the provided test class. Your code should work with this class AS IS. Please DO NOT modify or submit this class and adhere to the names provided in the class’ UML diagram (Figure 1). You may, however, comment lines of code until you are ready to test the methods they invoke.
ü Your code’s output should match the output shown in Figure 2
Grading:
Item
Points
Class Shape
10
Class Shape2D
10
Class Shape3D
10
Class Quadrilateral
10
Class Quadrilateral3D
10
Class ShapeList
0
add
10
get2DShapes and get3DShapes
10
printFormatted
10
Correct output
10
Efficiency of code
10
100
Figures:
Figure 1: Class UML Diagram
java.lang.UnsupportedOperationException: Unrecognized shape, skipping: 98241,BLACK,18.785:15.059,Sphere,A black sphere java.lang.UnsupportedOperationException: Unrecognized shape, skipping: 57057,BLACK,7.85:,Circle,A black sphere The list contains 29 Shape objects
There are 18 2-Dimensional shapes
There are 11 3-Dimensional shapes
+--------+-------------+---------+-------------------------+----------------------+ | ID | Name | Color | Dimensions | Description | +--------+-------------+---------+-------------------------+----------------------+ | 35779 | Cube | Blue | 39.20 X 46.91 X 27.15 | A blue cube | +--------+-------------+---------+-------------------------+----------------------+ | 90238 | Cube | Blue | 77.60 X 54.66 X 8.34 | A blue cube | +--------+-------------+---------+-------------------------+----------------------+ | 65701 | Cube | Red | 90.33 X 56.78 X 44.61 | A red cube | +--------+-------------+---------+-------------------------+----------------------+ | 40433 | Cube | Yellow | 94.89 X 43.88 X 21.47 | A yellow cube | +--------+-------------+---------+-------------------------+----------------------+ | 51060 | Cuboid | Black | 43.61 X 94.74 X 65.44 | A black cuboid | +--------+-------------+---------+-------------------------+----------------------+ | 90955 | Cuboid | Blue | 55.69 X 40.01 X 90.70 | A blue cuboid | +--------+-------------+---------+-------------------------+----------------------+ | 83912 | Cuboid | Blue | 8.09 X 45.01 X 96.79 | A blue cuboid | +--------+-------------+---------+-------------------------+----------------------+ | 64851 | Cuboid | Green | 98.18 X 5.51 X 64.22 | A green cuboid | +--------+-------------+---------+-------------------------+----------------------+ | 88174 | Cuboid | Green | 94.47 X 6.70 X 83.56 | A green cuboid | +--------+-------------+---------+-------------------------+----------------------+ | 37951 | Cuboid | Red | 70.19 X 32.90 X 94.36 | A red cuboid | +--------+-------------+---------+-------------------------+----------------------+ | 36830 | Cuboid | Yellow | 5.38 X 59.99 X 69.06 | A yellow cuboid | +--------+-------------+---------+-------------------------+----------------------+ | 48900 | Rectangle | Red | 57.56 X 4.03 | A red rectangle | +--------+-------------+---------+-------------------------+----------------------+ | 60665 | Rectangle | Red | 77.08 X 60.12 | A red rectangle | +--------+-------------+---------+-------------------------+----------------------+ | 90965 | Rectangle | Red | 94.77 X 61.20 | A red rectangle | +--------+-------------+---------+-------------------------+----------------------+ | 72916 | Rectangle | White | 24.37 X 86.85 | A white rectangle | +--------+-------------+---------+-------------------------+----------------------+ | 35886 | Rectangle | Yellow | 65.95 X 51.71 | A yellow rectangle | +--------+-------------+---------+-------------------------+----------------------+ | 60895 | Square | Black | 87.86 X 39.68 | A black square | +--------+-------------+---------+-------------------------+----------------------+ | 67132 | Square | Black | 33.89 X 83.52 | A black square | +--------+-------------+---------+-------------------------+----------------------+ | 44356 | Square | Blue | 57.32 X 54.03 | A blue square | +--------+-------------+---------+-------------------------+----------------------+ | 85368 | Square | Cyan | 70.95 X 41.82 | A cyan square | +--------+-------------+---------+-------------------------+----------------------+ | 99999 | Square | Cyan | 61.01 X 44.17 | A cyan square | +--------+-------------+---------+-------------------------+----------------------+ | 26449 | Square | Cyan | 84.56 X 77.16 | A cyan square | +--------+-------------+---------+-------------------------+----------------------+ | 71002 | Square | Green | 23.65 X 12.55 | A green square | +--------+-------------+---------+-------------------------+----------------------+ | 78853 | Square | Red | 7.28 X 60.85 | A red square | +--------+-------------+---------+-------------------------+----------------------+ | 37376 | Square | Red | 63.30 X 91.52 | A red square | +--------+-------------+---------+-------------------------+----------------------+ | 25280 | Square | White | 5.54 X 0.04 | A white square | +--------+-------------+---------+-------------------------+----------------------+ | 66544 | Square | White | 43.07 X 30.90 | A white square | +--------+-------------+---------+-------------------------+----------------------+ | 27982 | Square | White | 29.25 X 88.76 | A white square | +--------+-------------+---------+-------------------------+----------------------+ | 76667 | Square | Yellow | 38.50 X 62.44 | A yellow square |
+--------+-------------+---------+-------------------------+----------------------+
Figure 2: Test Class’ Output
UML Diagram Legend
Symbol
Description
Underlined
Indicates a static member
A private member (i.e. variable or method)
A private final member (i.e. variable)
A public field (i.e. variable or method)
A public abstract member (i.e. variable or method)
A public constructor
A static public member
An interface
A public class
A public abstract class
A hollowed arrow indicates inheritance
An open-ended arrow indicates composition
A dotted line and hollowed arrow indicate class implementation