Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Trying to call a method in class Fraction

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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.

    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();
    }

    }
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default 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?

  3. The Following User Says Thank You to GregBrannon For This Useful Post:

    jc100 (September 18th, 2013)

  4. #3
    Junior Member
    Join Date
    Sep 2013
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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.

  5. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default 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.

  6. #5
    Junior Member
    Join Date
    Sep 2013
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Trying to call a method in class Fraction

    Got it, thank you very much for your help!

Similar Threads

  1. How do you call a method from the same non-application class?
    By polop050505 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 19th, 2013, 12:12 PM
  2. Can't call method from external class
    By zlloyd1 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: November 24th, 2012, 08:30 PM
  3. Can't call paint() method from another class
    By Huw in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 18th, 2012, 02:15 AM
  4. update static field and call method within the class?
    By GeneralPihota in forum Object Oriented Programming
    Replies: 7
    Last Post: February 6th, 2012, 09:20 PM
  5. Call method(s) within the same class
    By mwr76 in forum Object Oriented Programming
    Replies: 8
    Last Post: September 26th, 2011, 12:58 AM