Question: 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
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)).
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.
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; otherwise scale down the fraction.
Both scaledown and scale methods must check if the factor is 0. If it is 0, a warning message is printed out and no scaling is operated.
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.
Write a program named FractionScale that prompts the user of a fraction and a scale factor. Here is what the user will see on the screen:
This program performs the scaling operations on a fraction.
Enter a fraction: 3/7
Scale up or down (1: up, 0: down): 1
Enter a scale factor: 2
Scaled fraction is: 6/7
You can assume that the user always enters two integers separated by a slash (/) for the faction. However, the user may enter any number of spaces before and after each integer.
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);
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
