Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write in Java The MyMath Interface You will create two classes, MyFraction and MySet. Each of these classes must implement the above interface. When you

Write in Java

The MyMath Interface

You will create two classes, MyFraction and MySet. Each of these classes must implement the above interface. When you implement the interface you must use the correct "generic" for the class which implements the interface: implements MyMath for the MyFraction class implements MyMath for the MySet class

public interface MyMath {

public T add(T o);

public T subtract(T o);

public T divide(T o);

public T multiply(T o);

}

The MyFraction Class

This class will be used to represent a fraction. This class should have three private instance variables, one for the numerator, one for the denominator, and one to hold the sign (+ or -) of the fraction. The sign must be stored as the character + or -. You should include a constructor which creates a MyFraction object from a given numerator, denominator and sign. The numerator and denominator should always be positive and the sign of the entire fraction should be determined by the value of the sign instance variable.

MyFraction should also implement the methods found in the MyMath interface.

add(MyFraction o) This should return a new MyFraction object which is the sum of two MyFraction objects.

subtract(MyFraction o) This should return a new MyFraction object which is the difference of two MyFraction objects.

multiply(MyFraction o) This should return a new MyFraction object which is the product of two MyFraction objects.

divide(MyFraction o) This should return a new MyFraction object which is the quotient of two MyFraction objects.

NOTE: Remember to deal with positive and negative fractions. Fractions where either the numerator or denominator are negative means the entire fraction is negative. Fractions where both the numerator and denominator are negative are positive. Keep this in mind when doing all math calculations.

MyFraction must have an equals() method that tests whether the MyFractions(the one for which this is an instance method and the one that comes in as a parameter) are equal in sign and magnitude

You should also implement toString() to print a MyFraction object. The output of a fraction should look like the following examples. Notice that when a fraction is positive no sign appears, and when the fraction is negative the sign appears on the left of the numerator.

-1/2

5/2

The MySet Class

NOTE: Java has a built in Set class. You may NOT use this class or any of its methods. Also, you may not use any of the built in Java Collection classes inside of your MySet class. This means no ArrayLists, Lists, Maps, etc.

A set is a collection of items in which each item is unique. You will implement a simplified version of the Java Set class. Your MySet class should have a private instance variable which is a reference to an array of ints. A MySet object can be constructed by passing a Java array to its constructor. The constructor of the MySet class should take the given array and create an array of integers in which each value is unique. In other words, if the user creates a MySet object from an array with duplicate values, then your constructor must strip out any duplicates and only keep unique values in the set. This array will be the one which is stored in the instance variable of your MySet class. NOTE: Remember that arrays are fixed in size, so you will have to account for the fact that new sets may have more or less values than an array has space for. You should create new arrays and copy values to them when necessary.

MySet must have an equals() method that tests whether two MySets (the one for which this is an instance method and the one that comes in as a parameter) contain the same integers.

MySet should also implement the methods found in the MyMath interface. Don't get hung up on the name of the methods even if they do not seem to intuitively relate to what the method actually does. Just follow the directions carefully.

add(MySet o) This should return a new MySet object which is the union of two MySet objects. Remember that a set may not contain any duplicate elements, so you must discard duplicates.

subtract(MySet o) This should return a new MySet object which is the relative complement of o in the current MySet. In other words, it should contain only those elements of the current MySet which are not contained in o.

multiply(MySet o) This should return a new MySet object which is the Symmetric Difference of two MySet objects.

divide(MySet o) This should return a new MySet object which is the intersection of two MySet objects.

You should also implement toString() to print your MySet objects.

The JUnit Test Cases:

Create

MySetTest.java(JUnit)

and

MyFractionTest.java(JUnit)

Each class should have an associated JUnit Test case which exhaustively tests every aspect of the classes.All instance variables should be tested. All constructors should be tested, and all methods should be tested. To test whether the result of an operation is correct, create a MySet, MyFraction that represents the correct answer and use the .equals() method of the class.

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

Database Driven Web Sites

Authors: Joline Morrison, Mike Morrison

2nd Edition

? 061906448X, 978-0619064488

More Books

Students also viewed these Databases questions