can some help me fix FractionArray I have provided both codes on top the Fraction.java and FractionArray.java that needs corrections
Fraction.java
//Definition of Fraction class public class Fraction { // Declaration of private data members private int num; private int denom;
// Definition of parameterized constructor public Fraction(int n, int d) { num = n; this.setDenom(d); this.reduceFraction(); this.adjustSign(); }
// Definition of setNum method that is used to set the value of num variable public void setNum(int n) { num = n; }
// Definition of setNum method that is used to set the value of denom variable public void setDenom(int d) { // If condition to check if the value of denominator is zero if (d != 0) { denom = d; } // if value is zero reseting the value to 1 and printing message to screen else { System.out.println("For Fraction: " + this.toString() + ": "); System.out.println("Denominator cannot be zero; denominator value is being reset to one"); denom = 1; System.out.println("The Fraction is now : " + this.toString()); } }
// Definition of getter method for num public int getNum() { return num; }
// Definition of getter method for denom public int getDenom() { return denom; }
// Definition of multiply method that takes a fraction object as input and // performs the fraction multiplication with calling object public Fraction multiply(Fraction f2) { int newNum, newDenom;
newNum = (this.getNum() * f2.getNum()); newDenom = (this.getDenom() * f2.getDenom());
Fraction res = new Fraction(newNum, newDenom); // reducing the resulting fraction into simplest form res.reduceFraction(); return res; }
// Definition of toString() method that prints the fraction with format public String toString() { String strFraction;
if (denom == 1) { strFraction = "( " + num + " )"; } else { strFraction = "( " + num + " / " + denom + " )"; } return strFraction; }
public void reduceFraction() { int d = __gcd(this.num, this.denom); this.num = this.num / d; this.denom = this.denom / d; }
public int __gcd(int a, int b) { if (b == 0) return a; return __gcd(b, a % b);
}
public void adjustSign() { if (this.denom
public Fraction divide(Fraction f2) { int newNum, newDenom;
newNum = this.num * f2.denom; newDenom = this.denom * f2.num; Fraction res = new Fraction(newNum, newDenom); res.reduceFraction(); return res; }
public Fraction add(Fraction f2) { int newNum, newDenom; int gcd = __gcd(this.denom, f2.denom);
// Denominator of resulting fraction would be the LCM of both denominators // finding LCM of both denominator using GCD // LCM * GCD = a * b newDenom = (this.denom * f2.denom) / gcd;
// Changing the fractions to have same denominator // Numerator of the resulting fraction can now be calculated newNum = (this.num) * (newDenom / this.denom) + (f2.num) * (newDenom / f2.denom); Fraction res = new Fraction(newNum, newDenom); // reducing the resulting fraction into simplest form res.reduceFraction(); return res; }
public Fraction substract(Fraction f2) { int newNum, newDenom; // Denominator of resulting fraction would be the LCM of both denominators // finding LCM of both denominator using GCD // LCM * GCD = a * b newDenom = (this.getDenom() * f2.getDenom()); // Changing the fractions to have same denominator // Numerator of the resulting fraction can now be calculated newNum = (this.getNum() * f2.getDenom()) - (f2.getNum() * this.getDenom());
Fraction res = new Fraction(newNum, newDenom); return res; }
// definition of getDouble method that returns the floating point value of the // fraction public double getDouble() { double res = (double) this.num / (double) this.denom; return res; } }
FractionArray the one that needs correction
//import statements for Random class and ArrayList class import java.util.Random; import java.util.ArrayList;
public class FractionArray { public static void main(String[] args) { //constant for number of Fractions to instantiate final int NUM_FRACTIONS = 5; //constant for max random number to generate //this defines range for random int: [0, 20] final int MAX_RANDOM = 21; //call getArrayList( ) to create the ArrayList of Fractions objects ArrayList fractionList = getArrayList(NUM_FRACTIONS, MAX_RANDOM); //display the entire ArrayList System.out.println("The fraction list contains:"); System.out.println(fractionList); } //method to create an ArrayList of Fraction object //return-type: ArrayList //parameters: size of the ArrayList and the max random value public static ArrayList getArrayList(int numFractions, int maxRandom) { //create Random object to get random nums Random randomObject = new Random(); //create array of random ints for numerator int[] numVals = getRandomInts(randomObject, numFractions, maxRandom); //create array of random ints for denominator int [] numVals = getRandomInts(randomObject, numFractions, maxRandom); //create an ArrayList object for the list of Fractions ArrayList fractList = new ArrayList(); //populate the fractList ArrayList for (int i = 0; i port java,uti1. Scanner; blic class FibArray 10 //Definition of Fraction class public class Fraction 13 I/ Declaration of private data members private int num; private int denom; II Definition of parameterized constructor public Fraction(int n, int d) \{ num =n; this. SetDenom(d); this. reducefraction(); this.adjustsign(); \} // Definition of setllum method that is used to set the value of num variable public void setNum(int n ) \{ num =n; I/ Definition of setNum method that is used to set the value of denom variable public void setDenom(int d) \{ I/ If condition to check if the value of denominator is zero if (d=0) \{ \} denom = d; I/ if value is zero reseting the value to 1 and printing pessage to screen else {S System.out.println("For Fraction: " + this.tostring() + ": "); System. out.println("Denominator cannot be zero; denominator value is being reset to one"); denom = 1; System.out.println("The Fraction is now : " + this.toString()); 1/ Definition of getter method for num public int getNum() \{ return num; \} 1) Definition of getter method for denom public int getDenom() \{ return denom; \} // Definition of multiply method that takes a fraction object as input and II performs the fraction multiplication with calling object public Fraction multiply(Fraction f2 ) \{ int newNum, newDenom; return strfraction; I/ Denominator of resulting fraction would be the LCM of both denominators 11. finding LCM of both denominator using GCD 11LCMGCD=ab newDenom = (this. denom * f2. denom) /gcd II Changing the fractions to have same denominator I/ Numerator of the resulting fraction can now be calculated newNum =( this. num )( newDenom / this. denom )+(f2. num ) (newDenom /f2. denom); Fraction res = new Fraction(newNum, newDenom); 1/ reducing the resulting fraction into simplest form res.reduceFraction(); return res; 3 public Fraction substract(Fraction f2) \{ int newNum, newDenom; I/ Denominator of resulting fraction would be the LCM of both denominators 1/ finding LCM of both denominator using GCD 1/LCMGCD=ab newDenom =( this getDenom()+2getDenom()); I/ Changing the fractions to have same denominator 1/ Numerator of the resulting fraction can now be calculated newNum =( this getNum()+2getDenom())(f2getNum() this getDenom()); Fraction res = new Fraction(newNum, newDenom); return res; \} II definition of getDouble method that returns the floating point value of the 1/ fraction public double getDouble() \{ double res = (double) this.num / (double) this.denom; return res; \} \} - Create an ArrayList object that contains Fraction objects - The integer values of the Fraction objects are obtained from two parallel arrays that are randomly populated with integer values in the range [0,20] - Write a method that returns an array of randomly generated integer values - Write a method that returns an ArrayList of Fraction objects - Most of the code and pseudocode are provided in subsequent slides Lab15 Program output To solve Lab15 do the following - Create a new folder; name it Lab15 - Copy the Fraction.java file from your completed Project5 - Paste this in your Lab15 folder - This file should not need to be modified - Create a new Java file; name this FractionArray.java - Save this in your Lab15 folder - This will be the driver class in which we instantiate Fraction objects - Write the method to return an array of random int values - Write the method to return an ArrayList of Fraction objects - Write the main method Method to return an array of random integer values - Name of the method: getRandomInts - Return-type: int[ ] - Reminder: If you forget the square brackets, you will have a bug! - Parameters: - An object of class Random - An int that specifies how many numbers to generate - An int that specifies the maxPlusOne random value to generate - Pseudocode: - Declare an array of ints of correct size - Use for loop to access each element of the array - Assign a random int to each element - After exiting the loop, return the array of int values Method to return an ArrayList object of Fraction objects - Name of the method: getArrayList - Return-type: ArrayList - Reminder: If you forget the angle brackets enclosing Fraction, you will have a bug! - Parameters: - An int that specifies how many Fraction objects to generate - An int that specifies the maxPlusOne random value to generate - Pseudocode: - Instantiate an object of class Random - Call the getRandomints() method to create an array of numerator values - Call the getRandomInts( ) method to create an array of denominator values - REMINDER: These two arrays MUST be the same size, or you will have a bug Method to return an ArrayList object of Fraction objects - Pseudocode (continued): - Instantiate an ArrayList object to contain the Fraction objects - Use a for loop to instantiate the required number of Fraction objects - Pseudocode for body of loop: - Instantiate a Fraction object using the current numerator value and the current denominator value - Add this Fraction object to the ArrayList> object - Implement println statements so that the user can see the progress of the code - Output should be similar to sample output that you have been given - After exiting the loop, return the ArrayList object - If the name given to the ArrayList object is fractiist, the code is as follows: Sample program output: Additional sample program output