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

***Please comment what each line does if possible*** You are given two JAVA clas

ID: 3872347 • Letter: #

Question

***Please comment what each line does if possible***

You are given two JAVA classes, first is class Fraction which have methods to do various calculations related with fractional numbers. Second is class FractionTest which uses the methods defined in Fraction for various inputs and print out the results. The FractionTest class is already given so you don’t need to do anything. Class Fraction already have some predefined methods, you just need to add the following methods.

1.Add two more constructors. One of the constructors will have no parameters; it initializes the fraction to 0/1. The other constructor will have one parameter, representing the numerator of the fraction; the denominator of the fraction will be 1.

2.Add divide method to take another Fraction as the parameter and return a new Fraction that is the division of current Fraction and the Fraction in the parameter. (public Fraction divide(Fraction f)).

3.Add scaleup and scaledown methods to the Fraction class. The scaleup method will take a factor as the parameter and multiply the numerator by the factor. The scaledown method will take a factor as the parameter and multiply the denominator by the factor(scaledown). You must check that the scaledown factor is not 0. If it is 0, a warning message is printed out and no scaling is operated.

4.Add a scale method which will have two parameters: factor and flag. The flag is boolean. If flag is true, then scale up the fraction(scaleup); otherwise scale down the fraction. (Use methods from no. 3)

Only create the required methods and do not create methods other than asked. You do not need to simplify the fractions to lower values (If the result is 10/20, no need to simplify to 1/2). For the given inputs, your final output should be (Do not modify FractionTest class):

1. The addition result is:29/8

2. The division result is:60/80

3. Scaling Up firstFraction: 25/8

4. Scaling Down firstFraction: 5/40

5. Scaling firstFraction: 50/8

6. Scaling secondFraction: 3/10

Warning: Cannot scale down with 0.

Note:

1.Make sure the FractionTest.java can be compiled together with your Fraction class.

2.Submit only the Fraction.java file

3.The FractionTest class is only for testing purpose. We will change the fraction numbers in the FractionTest program when testing your submission.

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

public class Fraction {
// Instance variables
private int numerator; // Numerator of fraction
private int denominator; // Denominator of fraction

// Constructors
public Fraction(int num, int denom) {
numerator = num;
denominator = denom;
}

// Instance methods
public int getNumerator() {
return numerator;
}

public int getDenominator() {
return denominator;
}

public Fraction add(Fraction f) {
int num = numerator * f.denominator +
f.numerator * denominator;
int denom = denominator * f.denominator;
return new Fraction(num, denom);
}
}

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

public class FractionTest {

public static void main(String [] args){

Fraction firstFraction = new Fraction(5,8);

Fraction secondFraction = new Fraction(3);

Fraction thirdFraction = new Fraction (10,12);

Fraction fourthFraction = new Fraction();

Fraction addition = firstFraction.add(secondFraction);

System.out.println("1. The addition result is:"+addition.getNumerator()+"/"+addition.getDenominator() );

Fraction division = firstFraction.divide(thirdFraction);

System.out.println("2. The division result is:"+division.getNumerator()+"/"+division.getDenominator() );

int scaleUpFactor = 5;

Fraction scaledUp = firstFraction.scaleUp(scaleUpFactor);

System.out.println("3. Scaling Up firstFraction: "+scaledUp.getNumerator()+"/"+scaledUp.getDenominator() );

int scaleDownFactor = 5;

Fraction scaledDown = firstFraction.scaleDown(scaleDownFactor);

System.out.println("4. Scaling Down firstFraction: "+scaledDown.getNumerator()+"/"+scaledDown.getDenominator() );

int scaleFactor = 10;

Fraction scaled = firstFraction.scale(scaleFactor,true);

System.out.println("5. Scaling firstFraction: "+scaled.getNumerator()+"/"+scaled.getDenominator() );

scaled = secondFraction.scale(scaleFactor,false);

System.out.println("6. Scaling secondFraction: "+scaled.getNumerator()+"/"+scaled.getDenominator() );

scaled = secondFraction.scale(0,false);

System.out.println("7. Scaling secondFraction: "+scaled.getNumerator()+"/"+scaled.getDenominator() );

}

}

Explanation / Answer

Fraction.java