Write in Java The MyMath Interface You will create two classes, MyFraction and M
ID: 3594062 • Letter: W
Question
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<MyFraction> for the MyFraction class
implements MyMath<MySet> for the MySet class
public interface MyMath<T> {
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.
Explanation / Answer
// MyFraction.java
package Classes;
import Interface.MyMath;
public class MyFraction implements MyMath<MyFraction> {
private int numerator;
private int denominator;
private char sign;
// Default Constructor.
public MyFraction() {
}
// Constructor to initialize the MyFraction.
public MyFraction(int numerator, int denominator, char sign) {
this.numerator = numerator;
this.denominator = denominator;
this.sign = sign;
}
public int getNumerator() {
return numerator;
}
public void setNumerator(int numerator) {
this.numerator = numerator;
}
public int getDenominator() {
return denominator;
}
public void setDenominator(int denominator) {
this.denominator = denominator;
}
public char getSign() {
return sign;
}
public void setSign(char sign) {
this.sign = sign;
}
// Code for Addition.
@Override
public MyFraction add(MyFraction o) {
MyFraction fraction = new MyFraction();
if (this.sign == o.sign) {
// If signs are equal.
if (o.denominator == this.denominator) {
// If denominator is same
fraction.numerator = this.numerator + o.numerator;
fraction.denominator = this.denominator;
fraction.sign = this.sign;
} else {
// If the denominators are different.
fraction.numerator = (this.numerator * o.denominator) + (o.numerator * this.denominator);
fraction.denominator = this.denominator * o.denominator;
fraction.sign = this.sign;
}
} else {
// If signs are different.
if (o.denominator == this.denominator) {
// If denominator is same.
fraction.numerator = this.numerator - o.numerator;
fraction.denominator = this.denominator;
} else {
// If the denominators are different.
fraction.numerator = Math.abs((this.numerator * o.denominator) - (o.numerator * this.denominator));
fraction.denominator = this.denominator * o.denominator;
}
if (((this.numerator * o.denominator) - (o.numerator * this.denominator)) > 0)
fraction.sign = this.sign;
else
fraction.sign = o.sign;
}
return fraction;
}
// Code for Subtraction.
@Override
public MyFraction subtract(MyFraction o) {
MyFraction fraction = new MyFraction();
if (this.sign == o.sign) {
// If signs are equal.
if (o.denominator == this.denominator) {
// If denominator is same.
fraction.numerator = this.numerator - o.numerator;
fraction.denominator = this.denominator;
fraction.sign = this.sign;
} else {
// If the denominators are different.
fraction.numerator = (this.numerator * o.denominator) - (o.numerator * this.denominator);
fraction.denominator = this.denominator * o.denominator;
fraction.sign = this.sign;
}
} else {
// If signs are different.
if (o.denominator == this.denominator) {
// If denominator is same.
fraction.numerator = this.numerator + o.numerator;
fraction.denominator = this.denominator;
} else {
// If the denominators are different.
fraction.numerator = Math.abs((this.numerator * o.denominator) + (o.numerator * this.denominator));
fraction.denominator = this.denominator * o.denominator;
}
if (((this.numerator * o.denominator) + (o.numerator * this.denominator)) > 0)
fraction.sign = this.sign;
else
fraction.sign = o.sign;
}
return fraction;
}
// Code for Division.
@Override
public MyFraction divide(MyFraction o) {
MyFraction fraction = new MyFraction();
fraction.numerator = this.numerator * o.denominator;
fraction.denominator = this.denominator * o.numerator;
// Code to determine the sign.
if (this.sign == o.sign ) {
fraction.sign = '+';
} else {
fraction.sign = '-';
}
return fraction;
}
// Code for Multiplication.
@Override
public MyFraction multiply(MyFraction o) {
MyFraction fraction = new MyFraction();
fraction.numerator = this.numerator * o.numerator;
fraction.denominator = this.denominator * o.denominator;
// Code to determine the sign.
if (this.sign == o.sign ) {
fraction.sign = '+';
} else {
fraction.sign = '-';
}
return fraction;
}
// Code to print the string.
public String toString() {
if (sign == '-')
return sign + numerator + "/" + denominator;
else
return numerator + "/" + denominator;
}
}
==================================================================================
//MySet.java
package Classes;
import Interface.MyMath;
public class MySet implements MyMath<MySet>{
private int[] values;
public MySet(int[] Values){
this.values = Values;
this.removeDuplicates();
//System.out.println(values.toString());
}
public int[] getValues() {
return values;
}
public void setValues(int[] values) {
this.values = values;
}
@Override
public MySet add(MySet o) {
int[] new_array = new int[this.values.length+o.getValues().length];
int length = this.values.length;
for(int i=0; i <this.values.length;i++){
new_array[i] = this.values[i];
}
for(int i=0; i<o.getValues().length;i++){
new_array[i+length] = o.getValues()[i];
}
MySet result = new MySet(new_array);
result.removeDuplicates();
return result;
}
@Override
public MySet subtract(MySet o) {
boolean[] count_array = new boolean[o.getValues().length];
int counter =0;
for(int i=0; i<o.getValues().length; i++){
for(int j=0;j<this.values.length;j++){
if(this.values[j] == o.values[i] && count_array[i]==false){
count_array[i]=true;
counter++;
}
}
}
int[] new_array = new int[o.getValues().length - counter];
int current=0;
for(int i=0; i< o.getValues().length; i++){
if(count_array[i] == false){
new_array[current] = o.getValues()[i];
current++;
}
}
MySet result = new MySet(new_array);
return result;
}
@Override
public MySet divide(MySet o) {
boolean[] count_array = new boolean[o.getValues().length];
int counter =0;
for(int i=0; i<o.getValues().length; i++){
for(int j=0;j<this.values.length;j++){
if(this.values[j] == o.values[i] && count_array[i]==false){
count_array[i]=true;
counter++;
}
}
}
int[] new_array = new int[counter];
int current=0;
for(int i=0; i< o.getValues().length; i++){
if(count_array[i] == true){
new_array[current] = o.getValues()[i];
current++;
}
}
MySet result = new MySet(new_array);
result.removeDuplicates();
return result;
}
@Override
public MySet multiply(MySet o) {
boolean[] count_array = new boolean[this.values.length+o.getValues().length];
int counter =0;
for(int i=0; i<this.values.length; i++){
for(int j=0;j<o.getValues().length ;j++){
if(this.values[i] == o.values[j] && count_array[i]==false){
count_array[i]=true;
count_array[this.values.length+j]= true;
counter = counter+2;
}
}
}
int[] new_array = new int[(this.values.length + o.getValues().length) - counter];
int current=0;
for( int i=0; i< this.values.length; i++){
if(count_array[i] == false){
new_array[current] = this.values[i];
current++;
}
}
int length = this.values.length;
for(int i=length; i< o.getValues().length+length; i++){
if(count_array[i] == false){
new_array[current] = o.getValues()[i-length];
current++;
}
}
MySet result = new MySet(new_array);
return result;
}
//Removes the duplicates from this array.
private void removeDuplicates(){
boolean[] count_array = new boolean[this.values.length];
int count = 0;
for(int i=0; i< this.values.length; i++){
for(int j=i+1; j< this.values.length; j++){
if(this.values[i]== this.values[j] && count_array[j]==false){
count_array[j] = true;
count++;
}
}
}
int[] new_array = new int[this.values.length-count];
int counter= 0;
for(int i =0; i< this.values.length; i++){
if(count_array[i]==false){
new_array[counter] = this.values[i];
counter++;
}
}
this.values = new_array;
}
}
=====================================================================================
//MyMath.java
package Interface;
public interface MyMath<T> {
public T add(T o);
public T subtract(T o);
public T divide(T o);
public T multiply(T o);
}
===================================================================================
//MyFractionTests.java
package JUnit;
import static org.junit.Assert.*;
import org.junit.Test;
import Classes.MyFraction;
public class MyFractionTests {
MyFraction fraction = new MyFraction(2, 5, '-');
@Test
public void testAddition() {
MyFraction result = fraction.add(new MyFraction(3, 4, '+'));
assertEquals(new MyFraction(7,20,'+').toString(), result.toString());
}
@Test
public void testAdditionTwo() {
MyFraction result = fraction.add(new MyFraction(3, 4, '-'));
assertEquals(new MyFraction(23,20,'-').toString(), result.toString());
}
@Test
public void testSubstraction(){
MyFraction result = fraction.subtract(new MyFraction(1, 19, '-'));
assertEquals(new MyFraction(33,95,'-').toString(), result.toString());
}
@Test
public void testSubstractionTwo(){
MyFraction result = fraction.subtract(new MyFraction(1, 19, '+'));
assertEquals(new MyFraction(43,95,'-').toString(), result.toString());
}
@Test
public void testMultiplication(){
MyFraction result = fraction.multiply(new MyFraction(3, 4, '+'));
assertEquals(new MyFraction(6,20,'-').toString(), result.toString());
}
@Test
public void testMultiplicationTwo(){
MyFraction result = fraction.multiply(new MyFraction(3, 4, '-'));
assertEquals(new MyFraction(6,20,'+').toString(), result.toString());
}
@Test
public void testDivide(){
MyFraction result = fraction.divide(new MyFraction(1, 19, '-'));
assertEquals(new MyFraction(38,5,'+').toString(), result.toString());
}
@Test
public void testDivideTwo(){
MyFraction result = fraction.divide(new MyFraction(1, 19, '+'));
assertEquals(new MyFraction(38,5,'-').toString(), result.toString());
}
}
==================================================================================
//MySetTest.java
package JUnit;
import java.util.Arrays;
import org.junit.Test;
import Classes.MySet;
import junit.framework.TestCase;
public class MySetTest extends TestCase {
MySet set = new MySet(new int[]{1, 2, 3, 4, 5, 6, 12, 1, 2, 3, 4, 5 });
//If duplicate values are deleted.
@Test
public void testDuplicate(){
assertTrue(Arrays.equals(new int[]{1, 2, 3, 4, 5, 6, 12}, set.getValues()));
}
//Adding two sets.
@Test
public void testAdd(){
MySet result = set.add(new MySet(new int[]{2, 3, 4, 7, 8, 9}));
//System.out.println(set.getValues());
assertTrue(Arrays.equals(new int[]{1, 2, 3, 4, 5, 6, 12, 7, 8, 9}, result.getValues()));
}
//Substracting two sets.
@Test
public void testSubstract(){
MySet result = set.subtract(new MySet(new int[]{2, 3, 4, 7, 8, 9, 13, 24, 35}));
//System.out.println(set.getValues());
assertTrue(Arrays.equals(new int[]{7, 8, 9, 13, 24, 35}, result.getValues()));
}
//Multiplying two sets.
@Test
public void testMultiply(){
MySet result = set.multiply(new MySet(new int[]{1, 2, 3, 4, 5, 13, 24, 35}));
//System.out.println(set.getValues());
assertTrue(Arrays.equals(new int[]{6, 12, 13, 24, 35}, result.getValues()));
}
//Divide two sets.
@Test
public void testDivide(){
MySet result = set.divide(new MySet(new int[]{1, 2, 3, 4, 5, 13, 24, 35}));
//System.out.println(set.getValues());
assertTrue(Arrays.equals(new int[]{1, 2, 3, 4, 5}, result.getValues()));
}
}
===================================================================================
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.