Question
Use string methods and for/while loops please nothing too complicated. Here's my old source code: public class Fraction { private int numerator; private int denominator;
Use string methods and for/while loops please nothing too complicated.
Here's my old source code:
public class Fraction {
private int numerator;
private int denominator;
public Fraction() {
numerator = 0;
denominator = 0;
}
// returns the denominator when called.
public int getDenominator() {
return this.denominator;
}
// returns the numerator when called.
public int getNumerator() {
return this.numerator;
}
public Fraction(int numerator, int denominator) {
this.numerator = numerator;
this.denominator = denominator;
}
// Returns the string representation of the object with conditional statements.
public String toString() {
simplify(); // used simplify() method before printing
int d = denominator;
int n = numerator;
if (d == 0) {
return "DNE";
} else if (d == 1 && n != 0) {
return "" + n;
} else if (n == 0) {
return "0";
}
return "(" + this.numerator + "/" + this.denominator + ")";
}
// overloaded method that uses two conditional statements if the denominator of the two fractions being subtracted is different and does the regular subtraction operation if it's the same. It also creates a new intance of Fraction and stores its values in the numerator and denominator.
public Fraction sum(Fraction other) {
if (denominator != other.getDenominator()) {
int SumN = numerator * other.getDenominator() + this.denominator * other.getNumerator();
int SumD = denominator * other.getDenominator();
return new Fraction(SumN, SumD);
} else {
int SumNum = numerator + other.getNumerator();
int SumDen = denominator;
return new Fraction(SumNum, SumDen);
}
}
// overloaded method that uses two conditional statements if the denominator of the two fractions being subtracted is different and does the regular subtraction operation if it's the same. It also creates a new intance of Fraction and stores its values in the numerator and denominator.
public Fraction diff(Fraction other) {
if (denominator != other.getDenominator()) {
int DiffN = numerator * other.getDenominator() - this.denominator * other.getNumerator();
int DiffD = denominator * other.getDenominator();
return new Fraction(DiffN, DiffD);
} else {
int DiffNum = numerator - other.getNumerator();
int DiffDen = denominator;
return new Fraction(DiffNum, DiffDen);
}
}
// overloaded method that multiplies numerator to other.getNumerator() and denominator to getDenominator(). It also creates a new intance of Fraction and stores its values in the numerator and denominator.
public Fraction mult(Fraction other) {
int MultNum = numerator * other.getNumerator();
int MultDen = denominator * other.getDenominator();
return new Fraction(MultNum, MultDen);
}
// overloaded method that multiplies the recipricol numerator to other.getDenominator() and denominator to getNumerator(). It also creates a new intance of Fraction and stores its values in the numerator and denominator.
public Fraction div(Fraction other) {
int DivNum = numerator * other.getDenominator();
int DivDen = denominator * other.getNumerator();
return new Fraction(DivNum, DivDen);
}
// simplifies the numerator and denominator to the lowest terms.
private void simplify() {
int gcd = 1;
int num;
int den;
if(numerator
num = -numerator;
else{
num = numerator;
}
if (denominator
den = -denominator;
den = denominator;
for(int f = 2; f
if(num%f==0 && den%f==0) gcd = f;
}
numerator/=gcd;
denominator/=gcd;
}
}
There should be a filein.txt for my old code and fileout.txt for new code outputted.
Assignment: The program will remove the blanks at the beginning of each line and replace them with an integer count of the number of blanks present. Two spaces should follow after the integer value, then the rest of the line should be transferred unchanged. Instructions: 1. Run your program on an old source code text file that has a great amount of indentation. Include at least three levels of indenting. 2. Again, make sure the file name you are "saving as" does not wipe out an existing text file. 3. Turn in your source code and then a "before" and "after" look. If you want, run Squeeze.java on itself, but make sure you have a backup disk copy somewhere else. 4. An example is provided on the next page. Before Squeezing: // A short example file float singleTax (float income) 1 if (incomeStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started