Language is Java - Please to not answer if the answer is not complete. Complete
ID: 3873326 • Letter: L
Question
Language is Java - Please to not answer if the answer is not complete.
Complete the implementation of the Fraction class. Your class should provide at least the following public methods
a default constructor - Did this
a constructor supplied with a numerator and a denominator - Did this
a constructor supplied with a whole number - Did this
a constructor supplied with another fraction, so you can make a copy of a fraction
a method named equals to determine whether a fraction supplied as a parameter is equal to this fraction
RETURNS BOOLEAN
a method named isLessThan to determine whether this fraction is less than the parameter fraction
RETURNS BOOLEAN
methods named add, subtract, multiply, and divide each of which accepts a parameter and returns a fraction that results from the indicated operation - Did this
toString and toDouble methods - Did toString
accessor methods getNumerator, getDenominator, and getSign (which should return a character, '-' or '+') to allow the client to individually see the numerator, denominator, and sign
Your class should ensure that at the end of every operation, the stored fraction value is always in simplest form with non-negative denominator.
I did most of it, but am having a hard time with the equal, and less than.
Explanation / Answer
//Fraction.java
import java.util.*;
import java.lang.*;
public class Fraction{
private int a;
private int b;
//Two parameter constructor that initializes the numerator and demoninator
public Fraction(int a,int b)
{
this.a = a;
this.b = b;
if(this.a<0 && this.b<0)
{
this.a = Math.abs(this.a);
this.b = Math.abs(this.b);
}
if(this.b<0)
{
this.a = -(this.a);
this.b = Math.abs(this.b);
}
if(this.b == 0)
{
throw new IllegalArgumentException("Data is not valid!");
}
}
//One parameter constructor that initializes the object equal in value to the integer parameter
public Fraction(int a)
{
this(a, 1);
}
//Zero parameter constructor that initializes the object to 0, meaning the numerator is 0 and the denominator is 1
public Fraction()
{
this(0,1);
}
//exposes the value of the numerator field to the user
public int getNumerator()
{
return this.a;
}
//exposes the value of the denominator field to the user
public int getDenominator()
{
return this.b;
}
//expose the sign of the fraction
public char sign()
{
if(this.a<0 || this.b <0)
return '-';
else
return '+';
}
//"numerator/denominator", a String representation of the Fraction
public String toString()
{
return (getNumerator()+"/"+getDenominator());
}
//the result of numerator / denominator
public double toDouble()
{
return a/b;
}
//returns a new Fraction that is the sum of other and this fractions
public Fraction add(Fraction c)
{
Fraction sum = new Fraction();
sum.a = (this.a * c.b) + (this.b * c.a);
sum.b = this.b * c.b;
sum.toLowestTerm();
return sum;
}
//returns a new Fraction that is the difference between the other and this fraction
public Fraction subtract(Fraction c)
{
Fraction diff = new Fraction();
diff.a = (this.a * c.b) - (this.b * c.a);
diff.b = this.b * c.b;
diff.toLowestTerm();
return diff;
}
//returns a new Fraction that is the product of the other and this fraction
public Fraction multiple(Fraction c)
{
Fraction multi = new Fraction();
multi.a = this.a * c.a;
multi.b = this.b * c.b;
multi.toLowestTerm();
return multi;
}
//returns a new Fraction that is the division of the other and this fraction, throw an IllegalArgumentException() if the user askes you to divide by 0
public Fraction divide(Fraction c)
{
Fraction div = new Fraction();
div.a = this.a * c.b;
div.b = this.b * c.a;
div.toLowestTerm();
return div;
}
//must take in an "Object" to properly override the Object class's equals method, but should ultimately check if two fractions are equal
public boolean equate(Fraction c)
{
this.toLowestTerm();
c.toLowestTerm();
if(this.a == c.a && this.b == c.b)
{
return true;
}
else
{
return false;
}
}
//converts the current fraction to the lowest terms
public void toLowestTerm()
{
int c = gcd(Math.abs(this.a),Math.abs(this.b));
this.a = this.a / c;
this.b = this.b / c;
}
//takes in two ints and determines the greatest common divisor of the two ints, should be a static method
public static int gcd(int num,int den)
{
int temp;
if(num<den)
{
temp = num;
num = den;
den = temp;
}
temp = num % den;
if(temp == 0)
{
return den;
}
num = den;
den = temp;
return gcd(num,den);
}
public boolean isLessThan(Fraction c)
{
if((this.a/this.b) < (c.a/c.b))
return true;
else
return false;
}
}
//Fracchegg.java
import java.util.*;
public class Fracchegg{
public static void main(String[] args)
{
Fraction n1 = new Fraction(4,2);
Fraction n2 = new Fraction(3);
System.out.println(n1.equals(n2));
System.out.println(n1.isLessThan(n2));
System.out.println(n1.add(n2));
System.out.println(n1.subtract(n2));
System.out.println(n1.multiple(n2));
System.out.println(n1.divide(n2));
System.out.println(n1.toString()+n2.toString());
System.out.println(n1.sign()+n1.getNumerator()+ "/"+n1.getDenominator());
System.out.println(n2.sign()+n2.getNumerator()+ "/"+n2.getDenominator());
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.