Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a Fraction class– that is, write your own class that will represent a frac

ID: 3778760 • Letter: W

Question

Write a Fraction class– that is, write your own class that will represent a fraction in your program. Each variable of type Fraction represents a single fraction. That means that the class should have at least two data fields that should be private: the numerator and the denominator of the fraction. The class also must have the following public class methods:

•Fraction(int n, int d); // constructor that defines a fraction n/d

•Fraction(); // constructor that defines a default fraction of 0/1

•Fraction(Scanner s); // constructor that defines a fraction via Scanner input

•double toDecimal(); // returns the decimal value of the fraction

•String toString(); // returns the string form of the fraction

"numerator" if denominator is 1

"numerator/denominator (decimal, with three decimal places)" otherwise

•int getNumerator(); // returns the numerator of the fraction

•int getDenominator(); // returns the denominator of the fraction

•Fraction plus(Fraction f); // returns fraction + parameter

•Fraction minus(Fraction f); // returns fraction - parameter

•Fraction times(Fraction f); // returns fraction * parameter

•Fraction divides(Fraction f); // returns fraction / parameter

You have been provided a private method:

•int[] simplifyFraction(int[] f); // returns simplified version of parameter

Using this method, all constructors should make sure that the fraction is in reduced form. You may not change the public API –that is, any methods not listed above must be made private

You will also have to write a program to test your Fraction class.

The following output is an example run:

== F1 ==

Enter numerator: 1

Enter denominator: 2

== F2 ==

Enter numerator: 3

Enter denominator: 5

F1: 1/2 (0.500)

F2: 3/5 (0.600)

F1+F1: 1

F2+F2: 6/5 (1.200)

F1+F2: 11/10 (1.100)

F2+F1: 11/10 (1.100)

F1-F1: 0

F2-F2: 0

F1-F2: - 1/10 (-0.100)

F2-F1: 1/10 (0.100)

F1*F1: 1/4 (0.250)

F2*F2: 9/25 (0.360)

F1*F2: 3/10 (0.300)

F2*F1: 3/10 (0.300)

F1/F1: 1

F2/F2: 1

F1/F2: 5/6 (0.833)

F2/F1: 6/5 (1.200)

F1*F1-F2: -7/20 (-0.350)

The program should use the Scanner constructor, the plus/minus/times/divides methods (as opposed to directly computing the outputs), and implicitly the toString method.

Here is the base code provided:

package edu.wit.cs.comp1000;

// TODO: document this class

public class PA9a {

      /**

      * Program execution point:

      * input two fractions via the keyboard

      * (using the Scanner constructor),

      * output the two fractions, all pairwise

      * {+ - * /} operations, and the first squared

      * minus the second

      *

      * @param args command-line arguments (ignored)

      */

      public static void main(String[] args) {

            // TODO: write your code here

      }

}

Explanation / Answer

Hi, I haven't used int[] simplifyFraction(int[] f); because it was not provided with the question, as mentioned. So, if you provide me that, I can incorporate it in the code.

Although, I have made my own version of it and used that!

The code is here:

import java.util.Scanner;

public class Fraction {

   private int numerator;
   private int denominator;

   public Fraction(int n, int d) {
       super();
       this.numerator = n;
       this.denominator = d;
       int fract[]=asFraction(numerator,denominator);
       numerator=fract[0];
       denominator=fract[1];
   }

   public Fraction() {
       this.numerator = 0;
       this.denominator = 1;
       int fract[]=asFraction(numerator,denominator);
       numerator=fract[0];
       denominator=fract[1];
   }

   public Fraction(Scanner s) {
       System.out.println("Enter numerator");
       this.numerator = Integer.parseInt(s.nextLine());
       System.out.println("Enter denominator");
       this.denominator = Integer.parseInt(s.nextLine());
       int fract[]=asFraction(numerator,denominator);
       numerator=fract[0];
       denominator=fract[1];
   }

   public double toDecimal() {
       return (double) this.numerator / this.denominator;
   }

   public int getNumerator() {
       return this.numerator;
   }

   public int getDenominator() {
       return this.denominator;
   }

   public Fraction plus(Fraction f) {
       Fraction answer;

       if (denominator == f.denominator) {
           answer = new Fraction(f.numerator + numerator, denominator);
       } else {
           int denominator = this.denominator * f.getDenominator();
           int numerator = this.numerator * f.getDenominator();
           numerator += f.getNumerator() * this.denominator;
           answer = new Fraction(numerator, denominator);

       }
       return answer;
   }

   // Fraction Subtract Method
   public Fraction minus(Fraction f) {
       Fraction answer;

       if (denominator == f.denominator) {
           answer = new Fraction(numerator - f.numerator, denominator);
       } else {
           int denominator = this.denominator * f.getDenominator();
           int numerator = this.numerator * f.getDenominator();
           numerator -= f.getNumerator() * this.denominator;
           answer = new Fraction(numerator, denominator);
       }
       return answer;
   }

   // Fraction Multiply Method
   Fraction times(Fraction f) {
       Fraction answer;
       int n = this.numerator * f.numerator;
       int d = this.denominator * f.denominator;
       answer = new Fraction(n, d);
       return answer;
   }

   // Fraction Divide Method
   Fraction divides(Fraction f) {
       Fraction answer;
       int n = this.numerator * f.denominator;
       int d = this.denominator * f.numerator;
       answer = new Fraction(n, d);
       return answer;
   }

  
   @Override
   public String toString() {
       if(this.denominator==1){
           return numerator + "";
       }
       else{
           return numerator +"/"+ denominator ;
       }
   }

   private static int gcm(int a, int b) {
   return b == 0 ? a : gcm(b, a % b);
   }

   private static int[] asFraction(int a, int b) {

       int gcm = gcm(a, b);
   int f[]=new int[2];
   f[0]=a/gcm;
   f[1]=b/gcm;
   return f;
   }
  
   public static void main(String[] args) {
       // TODO Auto-generated method stub

       Fraction f1=new Fraction(new Scanner(System.in));
       System.out.println("F1=" + f1 + "("+f1.toDecimal()+")");
      
       Fraction f2=new Fraction(new Scanner(System.in));
       System.out.println("F2=" + f2 + "("+f2.toDecimal()+")");
      
       System.out.println("F1/F2= "+ f1.plus(f2));
       System.out.println("F1/F2= "+ f1.minus(f2));
       System.out.println("F1/F2= "+ f1.times(f2));
       System.out.println("F1/F2= "+ f1.divides(f2));
      
      
      
   }

}

Sample run:

Enter numerator
4
Enter denominator
8
F1=1/2(0.5)
Enter numerator
1
Enter denominator
2
F2=1/2(0.5)
F1/F2= 1
F1/F2= 0
F1/F2= 1/4
F1/F2= 1

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote