STEP 1 Modify your fraction class as shown below. Include only the following cod
ID: 3859082 • Letter: S
Question
STEP 1 Modify your fraction class as shown below.
Include only the following code as an adjustment to your fraction class that exists. Be careful to include definitions where comments lie below for various fraction methods such as for the difference(), subtract() and multiply() methods which you defined for your last lab.
Don’t worry about the code you had for your main() method from last lab as that will be included in step 2 automatically in another file and code will be given for that as well.
Further note that the display method is also gone from the code below and has been replaced with a toString() method, which will be explained later in detail.
Adjusted source for class Fraction:
public class Fraction
{
int num, den;
//default constructor
public Fraction()
{
num = 1;
den = 1;
}
//overloaded constructor
public Fraction(int n, int d)
{
num = n;
if (d==0)
{
System.out.println( "error- division by zero");
System.exit(0); // termimate program if division by 0
}
else
den = d;
}
public Fraction Sum(Fraction someFraction)
{
int n = someFraction.den+someFraction.num*den;
int d = den*someFraction.den;
return new Fraction(n/gcd(n,d),d/gcd(n,d)); //return refreshed member values
}
public Fraction Difference(Fraction someFraction)
{
//define function logic below
}
public Fraction Multiply(Fraction someFraction)
{
//define function logic below
}
public Fraction Division(Fraction someFraction)
{
//define function logic below
}
//find and return greatest common denominator
public int gcd(int n, int d)
{
int remainder;
while (d != 0)
{
remainder = n % d;
n = d;
d = remainder;
}
return n;
}
public String toString() // Display method
{
return num + "/" + den;
}
}//end class
STEP 2 Test your partially modified class.
You may want to run your Fraction class file for now, just to check for syntax errors, but note running the file will throw an error, as shown below, indicating there is no main method included with your class.
Exception in thread "main" java.lang.NoSuchMethodError: main
STEP 3 Create a derived class called FractionOps, which will inherit from the base class Fraction.
Create an additional file in your source folder and call the class file FractionOps. The class will inherit features of the base class Fraction, so include the keyword extends to accomplish this. Use the starter code that follows to initiate method calls in main that will provide results of sums, differences, etc. to the user.
Starter source for class FractionOps:
public class FractionOps extends Fraction{
public FractionOps(int n, int d)
{
super(n,d); //call base constructor
}
public static void main(String [] args)
{
Fraction frac1 = new FractionOps(1,2);
Fraction frac2 = new FractionOps(1,4);
Fraction fracResult = new Fraction();
fracResult = frac1.Sum(frac2); // Result sb 3/4
//finish coding to call the Difference, Multiply and division functions
//finish print statement to include displays for the fraction operations for
//differences, multiplication and division
System.out.println("Sum result = " + fracResult + " ");
}//end main
}
You’ll notice something cool here. For one you are passing an object (namely fracResult) thru to the output stream, via the println() method which actually does the print out for you from the overloaded toString() method defined in the base class. Check the toString() method from the base class to see what it is doing every time a class object is passed into a print statement automatically.
Further note the call to super in the constructor of the derived class as it feeds data (in our case-the fraction’s numerator and denominator) from the derived constructor
to the base class, to get the data flowing into the base class needed for the calculations of the program.
(Please show the full code after motified following the introduction)
Explanation / Answer
public class Fraction
{
int num, den;
//default constructor
public Fraction()
{
num = 1;
den = 1;
}
//overloaded constructor
public Fraction(int n, int d)
{
num = n;
if (d==0)
{
System.out.println( "error- division by zero");
System.exit(0); // termimate program if division by 0
}
else
den = d;
}
public Fraction Sum(Fraction someFraction)
{
int n = someFraction.den+someFraction.num*den;
int d = den*someFraction.den;
return new Fraction(n/gcd(n,d),d/gcd(n,d)); //return refreshed member values
}
public Fraction Difference(Fraction someFraction)
{
int n = someFraction.den-someFraction.num*den;
int d = den*someFraction.den;
return new Fraction(n/gcd(n,d),d/gcd(n,d)); //return refreshed member values
}
public Fraction Multiply(Fraction someFraction)
{
int n = num*someFraction.num;
int d = den*someFraction.den;
return new Fraction(n/gcd(n,d),d/gcd(n,d)); //return refreshed member values
}
public Fraction Division(Fraction someFraction)
{
int n = num *someFraction.den;
int d = den*someFraction.num;
return new Fraction(n/gcd(n,d),d/gcd(n,d)); //return refreshed member values
}
//find and return greatest common denominator
public int gcd(int n, int d)
{
int remainder;
while (d != 0)
{
remainder = n % d;
n = d;
d = remainder;
}
return n;
}
public String toString() // Display method
{
return num + "/" + den;
}
}//end class
public class FractionOps extends Fraction{
public FractionOps(int n, int d)
{
super(n,d); //call base constructor
}
public static void main(String [] args)
{
Fraction frac1 = new FractionOps(1,2);
Fraction frac2 = new FractionOps(1,4);
Fraction fracResult = new Fraction();
fracResult = frac1.Sum(frac2); // Result sb 3/4
System.out.println("Sum result = " + fracResult + " ");
//finish coding to call the Difference, Multiply and division functions
fracResult = frac1.Difference(frac2);
System.out.println("Difference result = " + fracResult + " ");
fracResult = frac1.Multiply(frac2);
System.out.println("Multiplication result = " + fracResult + " ");
fracResult = frac1.Division(frac2);
//finish print statement to include displays for the fraction operations for
//differences, multiplication and division
System.out.println("Division result = " + fracResult + " ");
}//end main
}
output:
Sum result = 3/4
Difference result = 1/4
Multiplication result = 1/8
Division result = 2/1
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.