# Trying to call a method in class Fraction

• September 18th, 2013, 04:27 PM
jc100
Trying to call a method in class Fraction
Hello all, I have to create a Fraction class and a test class for an assignment and I have run into a problem getting my reduce() method to work. Could you please take a look, any feedback would be much appreciated. :confused:

Here is the Fraction class:

package fraction;

public class Fraction
{
private int num;
private int den;

public Fraction(int a, int b)
{
num = a;
den = b;
}

public Fraction()
{
num = 1;
den = 1;
}

public String toString()
{
return num + "/" + den;
}

public double getDecimal()
{
double decimal = ((double)num / den);
return decimal;
}

public void reduce()
{
int n = num;
int d = den;

while(n != d)
{
if(n > d)
n = n - d;

if(d > n)
d = d - n;
}
}

public String toMixed()
{
int a = num;
int b = den;
int whole = num / den;

while(a != b)
{
if(a > b)
a = a - b;
if(b > a)
b = b - a;
}

Fraction g = new Fraction(num % den / b, den / b);

if(whole > 0)
return whole + " " + g.toString();
else
return g.toString();
}
}

And here is the FractionTest class:

package fraction;

import java.util.Scanner;

public class FractionClassTest
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);

int choice = 0;
Fraction[] fraction;
fraction = new Fraction[5];
fraction[0] = new Fraction(8, 24);
fraction[1] = new Fraction(30, 75);
fraction[2] = new Fraction(27, 48);
fraction[3] = new Fraction(75, 45);
fraction[4] = new Fraction(5, 4);

System.out.println("Enter 1 to test the toString() method, 2 to test the reduce() method,"
+ " 3 to test the toMixed method, or 4 to quit: ");
choice = input.nextInt();
while(choice != 4)
{
if(choice == 1)
for(int i = 0; i < fraction.length; i++)
{
System.out.println(fraction[i].toString());
}

if(choice == 2)
for(int i = 0; i < fraction.length; i++)
{
fraction[i].reduce();
}

if(choice == 3)
for(int i = 0; i < fraction.length; i++)
{
System.out.println(fraction[i].toMixed());
}
System.out.println("Enter 1 to test the toString() method, 2 to test the reduce() method,"
+ " 3 to test the toMixed() method, or 4 to quit: ");
choice = input.nextInt();
}

}
}
• September 18th, 2013, 05:40 PM
GregBrannon
Re: Trying to call a method in class Fraction
Please post your code in code tags. You can learn how in the Announcements post at the top of the WWWMC forum. Please edit your post and fix it.

The reduce() method doesn't output any results. What are you expecting to happen?
• September 18th, 2013, 05:54 PM
jc100
Re: Trying to call a method in class Fraction
There seems to be a problem with the edit post function right now, but I will continue trying to get it to work in order to fix my post using code tags.

I understand that the reduce() method doesn't output any results, being that it is of the void type. I am trying to call the reduce() method in the test class to reduce the array of fraction objects with choice #2.
• September 18th, 2013, 06:03 PM
GregBrannon
Re: Trying to call a method in class Fraction
The method is being called, 5 times. You could add a print statement to the method to prove it yourself.
• September 18th, 2013, 06:09 PM
jc100
Re: Trying to call a method in class Fraction
Got it, thank you very much for your help!