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

This problem can use loops, arrays, strings, and functions. It needs to be able

ID: 668868 • Letter: T

Question

This problem can use loops, arrays, strings, and functions. It needs to be able to accommodate a whole number portion of a fraction (as in the '3' in 3 1/2):

You are developing a Fraction structure for Teacher’s Pet Software. The structure contains
three public data fields for whole number, numerator, and denominator. Using the
same structure, write a main()function that declares an array of five Fraction objects.
Prompt the user for values for each field of each Fraction. Do not allow the user to enter
a value of 0 for the denominator of any Fraction; for each Fraction, continue to prompt
the user for a denominator value until a non-zero value is entered. After all the objects
have been entered:
» Display the whole number, numerator, and denominator for each of the five Fraction
objects as entered.
» Next, display the five Fraction objects all converted to proper form—that is, if the user
entered a value such as 2 6/4, now display it as 3 1/2.
» Next, display the sum of all the Fractions and the arithmetic average of all the
Fractions.

Explanation / Answer

==========================================

Output:

==========================================


Enter Fraction1:
Enter Whole Number: 2
Enter Numerator: 6
Enter Denominator: 4

Enter Fraction2:
Enter Whole Number: 3
Enter Numerator: 2
Enter Denominator: 4

Enter Fraction3:
Enter Whole Number: 4
Enter Numerator: 5
Enter Denominator: 6

Enter Fraction4:
Enter Whole Number: 2
Enter Numerator: 4
Enter Denominator: 7

Enter Fraction5:
Enter Whole Number: 2
Enter Numerator: 3
Enter Denominator: 4
You enterered:2 6/4 After refined:3 1/2
You enterered:3 2/4 After refined:3 1/2
You enterered:4 5/6 After refined:4 5/6
You enterered:2 4/7 After refined:2 4/7
You enterered:2 3/4 After refined:2 3/4

============================================

Fraction.java

============================================

public class Fraction {

   private int wholeNumber;
   private int numerator;
   private int denominator;

   // Default constructor
   public Fraction() {

   }

   // Parameterized constructor
   public Fraction(int wholeNumber, int numerator, int denominator) {

       this.wholeNumber = wholeNumber;
       this.numerator = numerator;
       this.denominator = denominator;
   }

   public int getWholeNumber() {
       return wholeNumber;
   }

   public void setWholeNumber(int wholeNumber) {
       this.wholeNumber = wholeNumber;
   }

   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 int hcf(int m, int n) {

       int h = 1;
       int p = m * n;
       for (int i = 2; i < p; i++) {
           if ((m % i == 0) && (n % i == 0)) {
               h = i;
           }
       }
       return h;
   }

   public void refineFraction() {
       int sum = (wholeNumber * denominator) + numerator;
       int refNumerator = sum;
       int refDenomenator = denominator;

       int hcf = hcf(refNumerator, refDenomenator);
       numerator = (refNumerator / hcf)%(refDenomenator / hcf);
       denominator = (refDenomenator / hcf);
       wholeNumber = (refNumerator / hcf)/(refDenomenator / hcf);
      
   }

   @Override
   public String toString() {

       return wholeNumber + " " + numerator + "/" + denominator;
   }
}

============================================

FractionTest.java

============================================

import java.util.Scanner;


public class FractionTest {

   public static void main(String[] args) {

       Fraction fractions[] = new Fraction[5];
       int wholeNumber;
       int numerator;
       int denominator;
       Scanner sc = new Scanner(System.in);
      
       for(int i=0;i<fractions.length;i++) {
           System.out.println(" Enter Fraction"+(i+1)+":");
           System.out.print("Enter Whole Number: ");
           wholeNumber = sc.nextInt();
           System.out.print("Enter Numerator: ");
           numerator = sc.nextInt();
           System.out.print("Enter Denominator: ");
           while(true) {
               denominator = sc.nextInt();
               if(denominator!=0) {

                   break;
               } else {
                   System.out.print("Zero not allowed, Re-enter denominator:");
               }
           }

          
           Fraction fraction = new Fraction(wholeNumber, numerator, denominator);
           fractions[i]=fraction;
       }
      
       for(int i=0;i<fractions.length;i++) {
           System.out.print("You enterered:"+fractions[i]);
           fractions[i].refineFraction();
           System.out.println(" After refined:"+fractions[i]);
          
       }
   }

}

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