Answered You can buy a ready-made answer or pick a professional tutor to order an original one.
1. Create a new file called Fraction.java 2. In this file, create a class Fraction. a. This class should have 2 attributes, declare it, don't forget those fields/attributes need to be private b. Crea
1. Create a new file called Fraction.java
2. In this file, create a class Fraction.
a. This class should have 2 attributes, declare it, don't forget those fields/attributes need to be private
b. Create 2 constructors,
i. One constructor without any parameter. This constructor will initialize the first attribute to 0 and the second to 1.
ii. The second constructor will have 2 parameters that will initialize the 2 attributes of the Fraction class.
c. Create a getter and a setter for each private fields.
d. Create 4 methods, addition, subtract, multiply, divide. Don't forget to declare those methods public so every objects can use them. Since those methods needs to be called by an instance of the class Fraction, what do those methods need to achieve the addition/subtraction/multiplication/division of TWO fractions. The 4 methods need to return a brand NEW fraction result of the addition/subtraction/multiplication/division of the 2 fractions. a/b + c/d = a/b - c/d = a/b * c/d = a/b / c/d
e. Create a method properFraction, that has no parameter. This method will return a whole number and modify the original Fraction to a proper Fraction. For instance:
Fraction f1 = new Fraction(7,2);// f1-> 7/2 System.out.print(f1.properFraction());// output: 3, f1->1/2 Fraction f2 = new Fraction(2,5); // f2-> 2/5 System.out.print(f2.properFraction());// output: 0, f1->2/5
f. Create a method toString that will return a String made with the 2 attributes with the following format "4/3".
3. In the Main.java file, create 3 Fractions with different values. Test the addition/subtraction/multiplication/division and the properFractions methods.
- @
- ANSWER
java code
public class Fraction {
//2 attributes
private int numerator;
private int denominator;
// default constructor
public Fraction() {
numerator = 0;
denominator = 1;
}
public Fraction(int num , int denom) {
numerator = num;
denominator = denom;
}
//setter methods
public void setNumerator(int N){
numerator = N;
}
public void setDenominator(int D){
denominator = D;
}
//accessor methods
public int getNumerator(){
return numerator;
}
public int getDenominator(){
return denominator;
}
public Fraction addition(Fraction other){
int n = getNumerator() * other.getDenominator() + other.getNumerator() * getDenominator();
int d = getDenominator() * other.getDenominator();
return new Fraction (n, d);
}
public Fraction subtraction(Fraction other){
int n = getNumerator() * other.getDenominator() - other.getNumerator() * getDenominator();
int d = getDenominator() * other.getDenominator();
return new Fraction (n, d);
}
public Fraction multiplication(Fraction other){
int n = getNumerator() * other.getNumerator();
int d = getDenominator() * other.getDenominator();
return new Fraction (n, d);
}
public Fraction division (Fraction other){
int n = getNumerator() * other.getDenominator();
int d = getDenominator() * other.getNumerator();
return new Fraction (n, d);
}
public String toString(){
return getNumerator()+"/"+getDenominator() ;
}
public String properFraction(){
double value = this.getNumerator() / (double)this.getDenominator() ;
double converted_d = 0;
int divisor = 0 ;
double d = ( value - (int)value) ;
String text = Double.toString(Math.abs(d));
int integerPlaces = text.indexOf('.');
int decimalPlaces = text.length() - integerPlaces - 1;
//whole number
if (text.length() == 3 && text.charAt(text.length() - 1) == '0') {
decimalPlaces = 0;
} else {
converted_d = d*Math.pow(10, decimalPlaces);
divisor = GCD((int)converted_d, (int)Math.pow(10, decimalPlaces));
}
return (int)value+", f1->"+(int)(converted_d/divisor) + "/" + (int)((Math.pow(10, decimalPlaces))/divisor) ;
}
public static int GCD(int num, int denom) {
if (denom == 0) {
return num;
}
return GCD(denom, num % denom);
}
public static void main(String[] args){
Fraction frac1 = new Fraction (7,2);
Fraction frac2 = new Fraction (5,3);
Fraction frac3 =new Fraction ();
System.out.println(frac1.properFraction());
frac3 = frac1.addition (frac2);
System.out.println (frac3);
frac3 = frac1.subtraction (frac2);
System.out.println (frac3);
frac3 = frac1.multiplication (frac2);
System.out.println (frac3);
frac3 = frac1.division (frac2);
System.out.println (frac3);
}// main
} // class