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

Create java class named fraction. This class is used to represent a ratio of two

ID: 3690706 • Letter: C

Question

Create java class named fraction. This class is used to represent a ratio of two integers. Include methods that allow the user to set the numerator and the denominator. The output should be displayed as a ration. i.e. (5/9) style? The fraction class should contain the following: Private instance variables in store the numerator, denominator, and the ratio_value. Construction(s) that see all of the instance variables. Public methods named reduce () that returns lowest terms of a fraction. A public method named to string() that returns a String containing the fraction as a ratio (x/y). Create a test program that allows the user to create array of 7 fractions. Then the program will sort the fraction in ascending order. The highest and the lowest fractions are thrown away and the remaining fractions are added terms and displayed on the screen. For example if the sum is 20/60, the program should display 1/3. Write a sort method in the test program to sort the array of fractions and calculate the sum. You need to turn in the source coded and screen shot of the output. You may be asked to give a demonstration of your running application. No credit, will be given if the program doesn't compile successfully. The due date has to be met; otherwise the program will not be accepted.

Explanation / Answer

Fraction.java

package org.students;

import java.util.Scanner;

public class Fraction implements Comparable {
public int numerator = 1;
public int denominator = 1;
public int gcd;
public Fraction() {
super();
}
public Fraction(int numerator, int denominator)   
{   
//One can not have a zero denominator   
if (denominator == 0)   
{   
throw new IllegalArgumentException("Cannot set denominator to zero");
}   
  
//Make the numerator "store" the sign   
if (denominator < 0)   
{   
numerator = numerator * (-1);   
denominator *= -1;   
}   

this.numerator = numerator;   
this.denominator = denominator;   

reduce();   
}
//-------------------------------------------------------
//This reduces the fraction by dividing both the numerator and the
//denominator by their greatest common divisor.
//-----------------------------------------------------------
public void reduce()
{
if (numerator != 0)
{
int common = gcd(Math.abs(numerator),denominator);

numerator = numerator / common;
denominator = denominator / common;
}
else
denominator = 1;
}
  
  
//---------------------------------------------------------------
//This computes and returns the greatest common divisor of the two
//positive parameters. It uses Eculid's algorithm
//----------------------------------------------------------------

public int gcd(int m, int n)
{
int r;

//Make sure that m is the smaller of the two numbers
if (m > n)
{
r = m;
m = n;
n = r;
}

r = n % m;

while(r != 0)
{
n = m;
m = r;
r = n % m;
}
  
return m;   
}
  
public int getNumerator() {
return numerator;
}
public void setNumerator(int numerator) {
this.numerator = numerator;
}
public int getDenominator() {
return denominator;
}
public void setDenominator(int denominator) {
this.denominator = denominator;
}

public double decimal(double numerator, double denominator) {
return numerator / denominator;
}

@Override
public String toString() {
return numerator + "/" + denominator;
}
@Override
public int compareTo(Object o)   
{   
   Fraction frac=(Fraction)o;
// use long to prevent the overflow
long a = this.getNumerator();   
long b = this.getDenominator();   
long c = frac.getNumerator();   
long d = frac.getDenominator();   

long l = a*d - b*c;   
// can't use subtraction; use a nested ternary operator
return (l < 0 ? -1 : (l > 0 ? 1 : 0));
}

   }

__________________________________________________________________________________________

Test.java

package org.students;

import java.util.Arrays;
import java.util.Scanner;

public abstract class Test {
   static Fraction farray[];

   /*
   * private void selectionSort() { int minIndex=0, i=0, smallestIndex=0;
   * Fraction f = new Fraction();
   *
   * for(i = 0; i <7; i++) { smallestIndex = i; for(minIndex = i+1;minIndex <
   * 7; minIndex++) { if(f.compare(farray[minIndex],farray[smallestIndex])<0)
   * { // swap here Fraction temp = A[minIndex]; A[minIndex] = A[i]; A[i] =
   * temp; } } } }
   */

   public static void main(String[] args) {
       int numerator, denominator;

       Scanner sc = new Scanner(System.in);
       farray = new Fraction[7];

       for (int i = 0; i < 7; i++) {
           while (true) {
               System.out.println("Enter Fraction " + (i + 1));
               System.out.print("Enter the Numerator :");
               numerator = sc.nextInt();
               System.out.print("Enter the Denominator :");
               denominator = sc.nextInt();
               if (denominator == 0) {
                   System.out
                           .println(":: Denominator should not be Zero(0) ::");
                   continue;
               } else {
                   farray[i] = new Fraction(numerator, denominator);

               }
               break;
           }// end of while loop

       }
       System.out.println(" ");
       System.out.println("The elements in the Fraction Array are::");
       // To display the Elements in the Fraction array.
       for (int j = 0; j < 7; j++) {
           System.out.print(farray[j] + " , ");
       }
       Arrays.sort(farray);
       System.out.println(" ");
       System.out
               .println("The elements in the Fraction Array after sorting are::");
       // To display the Elements in the Fraction array.
       for (int j = 0; j < 7; j++) {
           System.out.print(farray[j] + " ");
       }

       // Fractions sum
       Fraction f = new Fraction();
       int tempGcd;
       int tempFactorOne;
       int tempFactorTwo;
       Fraction sum = farray[1];

       int num = 0, den = 0;
       for (int i = 2; i < 6; i++) {
           num = sum.getNumerator() * farray[i].getDenominator()
                   + farray[i].getNumerator() * sum.getDenominator();
           den = sum.getDenominator() * farray[i].getDenominator();
           sum.setNumerator(num);
           sum.setDenominator(den);
       }
       Fraction f1 = new Fraction(sum.getNumerator(), sum.getDenominator());
       System.out.println(" ");
       System.out.println("sum of the fractions excluding the smallest and largest is:" + f1.getNumerator() + "/"
               + f1.getDenominator());

   }
}

______________________________________________________________________________________

output:

Enter Fraction 1
Enter the Numerator :1
Enter the Denominator :3
Enter Fraction 2
Enter the Numerator :3
Enter the Denominator :5
Enter Fraction 3
Enter the Numerator :5
Enter the Denominator :7
Enter Fraction 4
Enter the Numerator :7
Enter the Denominator :9
Enter Fraction 5
Enter the Numerator :3
Enter the Denominator :7
Enter Fraction 6
Enter the Numerator :7
Enter the Denominator :9
Enter Fraction 7
Enter the Numerator :1
Enter the Denominator :6

The elements in the Fraction Array are::
1/3 , 3/5 , 5/7 , 7/9 , 3/7 , 7/9 , 1/6 ,

The elements in the Fraction Array after sorting are::
1/6 1/3 3/7 3/5 5/7 7/9 7/9   
sum of the fractions is :899/315

______________________________________________________________________________________________

______________________________________________________________________________________________

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