Starting from:

$25

Assignment № 4- Implementing an Interface



Implementing an Interface
In Java, the interface construct is used to describe the similarities that classes share. Like
abstract classes, interfaces cannot be instantiated. They are implemented and do not have any
instance variable. Methods of an Interface do not contain implementation in versions of the Java
Development Kit prior to SE Platform 8. In Java SE Platform 8, default and static methods may
have implementation in the interface definition. In this course, we will not implement methods
in an interface definition; we will only use interfaces in the ‘traditional’ way. Java does not allow
multiple inheritance; that is, where one class extends the behavior of two or more classes. We
can also use the interface construct to mimic multiple inheritance.
In this lab session, you will define a class Fraction that implements a user-defined interface
called Arithmetic and write a main class, FractionDemo, to test your Fraction class implementation.
The Arithmetic Interface
Defince a public interface with the following methods:
1. public Object add(Ojbect obj) : computes and returns the sum of two objects of a class.
2. public Object subtract(Ojbect obj) : computes and returns the difference of two objects of
a class.
3. public Object multiply(Ojbect obj) : computes and returns the product of two objects of a
class.
4. public Object divide(Ojbect obj) : computes and returns the quotient of two objects of a
class.
The Fraction Class
Definition 1. A fraction is a rational number expressed in the form a/b (in-line notation) or a
b
(traditional "display" notation), where a is called the numerator and b is called the denominator.
When written in-line, the slash "/" between numerator and denominator is called a solidus.
Define a class Fraction, that implements the Arithmetic interface. The Fraction class will
consist of two instance variables, top and bottom, both integers, representing the numerator and
denominator, respectively, of a fraction. In addition to methods from the Arithmetic interface that
it implements, it will also have the following methods:
Assignment № 4 Page 1
1. public Fraction() : a default constructor that creates the fraction 0
1
.
2. public Fraction(int n, int d) : a parameterized constructor that creates a fraction n
d
or throws
an IllegalArgumentException when d is 0.
3. private static int gCD(int a, int b) : an auxiliary method that computes the GCD of a and b
using the Euclidean GCD algorithm described in Laboratory Excercise 2 handout. Tweak
the algorithm so that this method throws an ArithmeticException when both a and b are 0.
4. Override the public String toString() method so that it returns a string representation of the
triangle in the form n/d, where n is the numerator and d is the denominator of the fraction
5. Override the public boolean equals (Object obj) method so that it returns true when two
fractions are equal and, otherwise, false. The fractions a
b
and c
d
are equal when ad = bc.
Also, if the explicit parameter is not an object of the Fraction class, the method returns
false.
For each method of the Arithmetic interface that this class implements, you must verify that the
explicit parameter is an object of the Fraction class. Throw an IllegalArgumentException if it is
not. Also, for each arithmetic operations on fractions, normalize the answer. To normalize a
fraction, do the following:
1. Divide both its numerator and denominator by their GCD. The numerator and denominator
are now relative primes. The fraction is then said to be simplified.
2. If the denominator is negative, multiply both the numerator and denominator by -1. This
ensures that you get an equivalent fraction with a positive denominator.
Adding and Subtracting Fractions:
a
b
+
c
d
=
ad + bc
bd (1)
a
b

c
d
=
ad − bc
bd (2)
Multiplying and Dividing Fractions:
a
b
×
c
d
=
ac
bd (3)
a
b
÷
c
d
=
ad
bc (4)
The divide method should throw an ArithmeticException when c is 0.
The FractionDemo Class
Define a class, FractionDemo, consisting of only one method, the main method, that does the
following:
1. Creates three fractions: f1 =
7
8
, f2 =
3
4
and f3 =
10
−4
.
Assignment № 4 Page 2
2. Displays each fraction in in-line notation, as shown in the sample run, by invoking the
toString method.
3. Invoking the relevant methods, computes and displays the value of the expression f1(f2−f3)
f2+f3
.
4. Invoking the equals method, determine whether or not f1 (f2 − f3) and f1f2 − f1f3 are
equal.
5. Finally, computes ?
1
f2
?5
. (Hint: Create the fraction 1
1
and perform repeated multiplication
to obtain the power.)
Additional Requirements
Do not define any other class or interface or implement any additional methods other than those
described in this handout. Also, do not change the signature of any of the methods described
above. Compile and run your program to make sure that it works. Write header comments for
each class and interface using the following Javadoc documentation template:
/**
* Explain the purpose of this class or interface; what it does <br
* CSC 1351 Lab # 4
* @author YOUR NAME
* @since DATE THE CLASS/ITERFACE WAS WRITTEN
*/
For the Fraction class, after the @since line, add the following Javadoc:
* @see Arithmetic
For the FractionDemo class, after the @since line, add the following Javadoc:
* @see Fraction
Remove all Netbeans-generated Javadoc documentation. Add Javadoc documentation for each
instance variable and method in both the Arithmetic interface and Fraction class. Run the
Javadoc utility to make sure that it generates documentation for the Arithmetic interface and the
Fraction class. Locate your source files, Arithmetic.java, Fraction.java and FractionDemo.java
and enclose them in a zip file, YOURPAWSID_lab04.zip, and submit your lab assignment for
grading using the digital dropbox set up for this purpose. Here is a sample program interaction:
Listing 1: Sample Run.
1 f1 = 7/8
2 f2 = 3/4
3 f3 = 10/-4
4 f1(f2 - f3)/(f2 + f3) = -13/8
5 f1(f2 - f3) and f1f2 - f1f3 are equal.
6 (1/f2)^5 = 1024/243
Assignment № 4 Page 3

More products