HW User-Created Types in Java Lab6: User-Created Types in Java A new class in Java can extend a superclass (or implement an interface) and be used wherever an instance of the superclass is expected....

Java homework


HW User-Created Types in Java Lab6: User-Created Types in Java A new class in Java can extend a superclass (or implement an interface) and be used wherever an instance of the superclass is expected. Classes typically provide an implementation of toString() so their printed representation can be generated for string concatenation or the println() method, both of which call toString(). If elements in the new class can be ordered, the class can implement the Comparable interface, allowing instances to be passed to existing methods, such as Collections.sort(). User-Created Types in Java First, some background. We can express a fraction as a numerator and denominator. Some examples are 1/2 (with a numerator of 1 and a denominator of 2) and 3/2 (with a numerator of 3 and a denominator of 2). A mixed number consists of three integer parts: a whole number, a numerator, and a denominator. Some examples of mixed numbers are 1 1/2 and 0 3/8, which are equal to the fractions 3/2 and 3/8, respectively. The purpose of this lab is to see the behavior of newly created numeric types (extending java.lang.Number). Start with these two warm-up questions: Q1. What are some subclasses of Number? Q2. What is the difference between int and Integer? Are there cases that one can be used where the other cannot? Your job will be to implement the classes Num, Fraction, and MixedNumber in a package edu.usfca.cs345 as shown in the following diagram: https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#toString() https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#toString() https://docs.oracle.com/javase/7/docs/api/java/io/PrintStream.html#println(java.lang.Object) https://docs.oracle.com/javase/7/docs/api/java/io/PrintStream.html#println(java.lang.Object) https://docs.oracle.com/javase/tutorial/collections/interfaces/order.html https://docs.oracle.com/javase/tutorial/collections/interfaces/order.html https://docs.oracle.com/javase/8/docs/api/java/lang/Comparable.html https://docs.oracle.com/javase/7/docs/api/java/util/Collections.html#sort(java.util.List) https://docs.oracle.com/javase/8/docs/api/java/lang/Number.html https://docs.oracle.com/javase/8/docs/api/java/lang/Number.html Here are some tips for reading the above UML static class diagram: ● An “I” in a purple circle indicates an interface. ● A “C” in a green circle indicates a class. A superimposed “A” indicates that the class is abstract. ● The names of abstract classes and methods are in italics. ● Package names appear below interface/class names. ● Solid arrows indicate a subclass-superclass relationship; a dotted arrow indicates an implementation-interface relationship. ● A solid green circle indicates that a field is public; a red square means that it is private. ● Constructors are marked with a “C”. ● Only the types, not the names, of formal parameters are shown. The return type is shown after a colon. ● The UML does not indicate that all of the methods in Num, Fraction, and MixedNumber override superclass methods (such as toString() and floatValue()) or implement extract methods (such as compareTo()). Q3. Where are the methods toString() and equals() first declared? The answer is not any of the classes in the above figure, although those methods are overridden by some of them. Q4. How can Num have two different direct supertypes if Java doesn’t support multiple inheritance? First, implement Num. You will need to carefully read the documentation for its concrete methods in the API where they are first declared. When implementing Number’s abstract methods that return whole numbers, round toward zero when converting from double. Do not add any constructors or methods not shown in the class diagram. Include code in the lab report. Next, implement Fraction and MixedNumber. The printed representation of the fraction with the numerator 1 and the denominator 2 should be “1/2” (with no space). The printed representation of the mixed number with the whole number 1, the numerator 2, and the denominator 3 should be “1 2/3” (with a single space between the numbers 1 and 2). Finally, implement tests that show: ● Instances of each class are printed correctly. ● compareTo() and equals() (which you should define in Num) work (return the right value) when comparing instances of: ○ Fraction and Fraction ○ Fraction and MixedNumber (in either order) ○ MixedNumber and MixedNumber ● Calling Collections.sort() on a List does the right thing. Turn in your Num, Fraction, MixedNumber classes and the output of your tests. These .java files should be submitted to the Canvas assignment along with the .pdf of this document. Q5. Can you make equals() comparisons work with other classes that extend Number? Either do so, or explain why it is not possible.
Apr 08, 2022
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here