Alter your Fraction class so that the Fractions are reduced

Alter your Fraction class so that the Fractions are reduced whenever necessary - i.e. in the constructor and mutators.

NOTE: You need only consider POSITIVE values for the numerator and denominator

import java.util.Scanner;

/**
*
* @author zzz
*/
public class Lab7 {

public static class Fraction
{
private int numerator;
private int denominator;

public Fraction()
{
numerator = 0;
denominator=1;
}
public Fraction(int n, int d)
{ int g=gcd(n,d);
//complete this
}

public int getNumerator() {
return numerator;
}


public void setNumerator(int n) {
int d=denominator;
int g=gcd(n,d);
//complete this
  
}


public int getDenominator() {
return denominator;
}

  
public void setDenominator(int d) {
int n=numerator;
int g=gcd(n,d);
//complete this
}
public Fraction add(Fraction g)
{
int a=this.numerator;
int b=this.denominator;
int c=g.numerator;
int d=g.denominator;
Fraction v=new Fraction(a*d+b*c,b*d);
return v;
}
public Fraction subtract(Fraction g)
{
int a=this.numerator;
int b=this.denominator;
int c=g.numerator;
int d=g.denominator;
Fraction v=new Fraction(a*d-b*c,b*d);
return v;
}
public Fraction multiply(Fraction g)
{
int a=this.numerator;
int b=this.denominator;
int c=g.numerator;
int d=g.denominator;
Fraction v=new Fraction(a*c,b*d);
return v;
}
public Fraction divide(Fraction g)
{
int a=this.numerator;
int b=this.denominator;
int c=g.numerator;
int d=g.denominator;
Fraction v=new Fraction(a*d,b*c);
return v;
}
public String toString()
{
return numerator + \"/\" + denominator;
}
  
private int gcd(int int1, int int2)
{

}


}
public static void main(String[] args) {

//test constructor
Fraction g= new Fraction(4,8);
System.out.println(g);
Fraction f= new Fraction();
f.setNumerator(60);
f.setDenominator(90);
System.out.print(\"60/90 is \");
System.out.println(f);
Scanner keyboard = new Scanner (System.in);
//get the data for the next fraction and construct it
int n1=keyboard.nextInt();
int d1=keyboard.nextInt();
Fraction one=new Fraction (n1,d1);
//get the data for the next fraction and construct it
n1=keyboard.nextInt();
d1=keyboard.nextInt();
Fraction two=new Fraction (n1,d1);
//test methods for add, subtract, multiply, divide
System.out.println(one + \" + \" + two + \" = \" + one.add(two));
System.out.println(one + \" - \" + two + \" = \" + one.subtract(two));
System.out.println(one + \" * \" + two + \" = \" + one.multiply(two));
System.out.println(one + \" divided by \" + two + \" = \" + one.divide(two));
  
}
}

Solution

Hi

I have updated the code and highlighted the code changes below.

Lab7.java


import java.util.Scanner;

public class Lab7 {
public static class Fraction
{
   private int numerator;
   private int denominator;
   public Fraction()
   {
   numerator = 0;
   denominator=1;
   }
   public Fraction(int n, int d)
   { int g=gcd(n,d);
   //complete this
   numerator = n;
   denominator=d;

   }
     
   public int getNumerator() {
   return numerator;
   }
     
   public void setNumerator(int n) {
   int d=denominator;
   int g=gcd(n,d);
   //complete this
   this.numerator = n;
   }
     
   public int getDenominator() {
   return denominator;
   }
  
   public void setDenominator(int d) {
   int n=numerator;
   int g=gcd(n,d);
   //complete this
   denominator = d;
   }
   public Fraction add(Fraction g)
   {
   int a=this.numerator;
   int b=this.denominator;
   int c=g.numerator;
   int d=g.denominator;
   Fraction v=new Fraction(a*d+b*c,b*d);
   return v;
   }
   public Fraction subtract(Fraction g)
   {
   int a=this.numerator;
   int b=this.denominator;
   int c=g.numerator;
   int d=g.denominator;
   Fraction v=new Fraction(a*d-b*c,b*d);
   return v;
   }
   public Fraction multiply(Fraction g)
   {
   int a=this.numerator;
   int b=this.denominator;
   int c=g.numerator;
   int d=g.denominator;
   Fraction v=new Fraction(a*c,b*d);
   return v;
   }
   public Fraction divide(Fraction g)
   {
   int a=this.numerator;
   int b=this.denominator;
   int c=g.numerator;
   int d=g.denominator;
   Fraction v=new Fraction(a*d,b*c);
   return v;
   }
   public String toString()
   {
   return numerator + \"/\" + denominator;
   }
  
   private int gcd(int int1, int int2)
   {
   //complete this
       return int2 == 0 ? int1 : gcd(int2, int1 % int2);
   }


}
public static void main(String[] args) {

//test constructor
Fraction g= new Fraction(4,8);
System.out.println(g);
Fraction f= new Fraction();
f.setNumerator(60);
f.setDenominator(90);
System.out.print(\"60/90 is \");
System.out.println(f);
Scanner keyboard = new Scanner (System.in);
//get the data for the next fraction and construct it
int n1=keyboard.nextInt();
int d1=keyboard.nextInt();
Fraction one=new Fraction (n1,d1);
//get the data for the next fraction and construct it
n1=keyboard.nextInt();
d1=keyboard.nextInt();
Fraction two=new Fraction (n1,d1);
//test methods for add, subtract, multiply, divide
System.out.println(one + \" + \" + two + \" = \" + one.add(two));
System.out.println(one + \" - \" + two + \" = \" + one.subtract(two));
System.out.println(one + \" * \" + two + \" = \" + one.multiply(two));
System.out.println(one + \" divided by \" + two + \" = \" + one.divide(two));
  
}
}

Output:

4/8
60/90 is 60/90
5
6
7
8
5/6 + 7/8 = 82/48
5/6 - 7/8 = -2/48
5/6 * 7/8 = 35/48
5/6 divided by 7/8 = 40/42

Alter your Fraction class so that the Fractions are reduced whenever necessary - i.e. in the constructor and mutators. NOTE: You need only consider POSITIVE val
Alter your Fraction class so that the Fractions are reduced whenever necessary - i.e. in the constructor and mutators. NOTE: You need only consider POSITIVE val
Alter your Fraction class so that the Fractions are reduced whenever necessary - i.e. in the constructor and mutators. NOTE: You need only consider POSITIVE val
Alter your Fraction class so that the Fractions are reduced whenever necessary - i.e. in the constructor and mutators. NOTE: You need only consider POSITIVE val
Alter your Fraction class so that the Fractions are reduced whenever necessary - i.e. in the constructor and mutators. NOTE: You need only consider POSITIVE val

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site