I have a class runner that is supposed to do the keep it change it flip it method when doing fractions.
public class Fraction2
{
private int numerator = 0;
private int denominator = 1;
public Fraction2()
{
numerator = 0;
denominator = 1;
}
public Fraction2(int num, int denom)
{
numerator = num;
denominator = denom;
}
public void show(int num, int denom)
{
System.out.println(numerator + "/" + denominator);
}
public String toString()
{
return (numerator + "/" + denominator);
}
public Fraction2 mult(Fraction2 temp)
{
return new Fraction2(this.numerator * temp.numerator,
this.denominator * temp.denominator);
}
public Fraction2 add(Fraction2 sent)
{
int cmmndenom = sent.denominator * this.denominator;
int answer = this.numerator * sent.denominator + sent.numerator * this.denominator;
return new Fraction2(answer, cmmndenom);
}
public Fraction2 divide(Fraction2 sent)
{
//keep it change it flip it method
Fraction2 answer = new Fraction2(this.numerator * sent.denominator,
this.denominator * sent.numerator);
return answer;
}
public Fraction2 reduce()
{
int ansNumer = numerator;
int ansDenom = denominator;
for (int i = 1; ((i <= (numerator)) && i <= (denominator)); i++)
{
if (((numerator % i) == 0) && ((denominator % i) == 0))
{
ansNumer = numerator / i;
ansDenom = denominator / i;
}
}
return new Fraction2(ansNumer, ansDenom);
}
}
When I put in 1/4 and 2/6 and I should be getting 6/8 but I'm getting 8/6. The rest of the methods work except for the dividing part. I'm just stuck on how to get 6/8 instead of 8/6.
import java.util.Scanner;
public class FractionTesterv2
{
public static void main(String args[])
{
System.out.println("\f");
Scanner input = new Scanner(System.in);
int numerator;
int denominator;
int addCount = 0;
int multCount = 0;
int divCount = 0;
//***************************************
//Creating future objects
Fraction2 fraction3 = new Fraction2();
Fraction2 addAnswer = new Fraction2();
Fraction2 multAnswer = new Fraction2();
Fraction2 divAnswer = new Fraction2();
//***************************************
System.out.println("Enter a numerator");
numerator = input.nextInt();
System.out.println("Enter a denominator");
denominator = input.nextInt();
//creates first fraction
Fraction2 fraction2 = new Fraction2(numerator, denominator);
System.out.println("would you like to add another fraction?");
String choice = input.nextLine();
choice = input.nextLine();
// Will only display first fraction entered
if (choice.equals("no"))
System.out.println(fraction2);
else
{
while (!(choice.equals("no")))
{
System.out.println("Enter a numerator");
numerator = input.nextInt();
System.out.println("Enter a denominator");
denominator = input.nextInt();
fraction3 = new Fraction2(numerator, denominator);
//***************************************
//Adding Fractions
if (addCount == 0)
addAnswer = fraction2;
addCount = 1;
addAnswer = fraction3.add(addAnswer);
System.out.println("add: " + addAnswer);
//***************************************
//Multiplying Fractions
if (multCount == 0)
multAnswer = fraction2;
multCount = 1;
multAnswer = fraction3.mult(multAnswer);
System.out.println("Multiply: " + multAnswer);
//***************************************
//Dividing Fractions
if (divCount == 0)
divAnswer = fraction2;
divCount = 1;
divAnswer = fraction3.divide(divAnswer);
System.out.println("Divide: " + divAnswer);
//***************************************
System.out.println("would you like to add another Fraction?");
choice = input.nextLine();
choice = input.nextLine();
}
Fraction2 addReduced = addAnswer.reduce();
Fraction2 multReduced = multAnswer.reduce();
Fraction2 divReduced = divAnswer.reduce();
System.out.println("Adding Reduced: " + addReduced);
System.out.println("Multiply Reduced: " + multReduced);
System.out.println("Dividing Reduced: " + divReduced);
}
}
}
My code is fairly large to start and I'm sure I can figure out how to simplify it. Just looking for help on the one part.
The first two numbers that you enter ("1", "4") are used to build fraction2
.
The next two numbers ("2", "6") are used to build fraction3
.
And then you calculate:
divAnswer = fraction3.divide(fraction2);
"2/6" divided by "1/4" is "8/6" - if you want "6/8" then you would have to calculate "1/4" divded by "2/6", which would be
divAnswer = fraction2.divide(fraction3);
For chained division operations you could write
// this shows an extract of a larger fraction of your code!
divAnswer = fraction2;
while (!(choice.equals("no")))
//...
// no need for some strange divCount flag here
divAnwser = divAnswer.divide(fraction3);
//...
}
or, similar to what your doing now:
while (!(choice.equals("no")))
//...
if (divCount == 0)
divAnswer = fraction2;
divCount = 1;
divAnswer = divAnswer.divide(fraction3);
//...
}
The basic problem is that your current code has the division operations swapped and that is what I tried to show.