Results 1 to 7 of 7
  1. #1
    Jakora33 is offline Member
    Join Date
    Sep 2009
    Posts
    5
    Rep Power
    0

    Default Fraction Problem

    Hi, I'm having trouble programming this fraction program. Right now my homework says that I have to program an division class that has two parameters, and it returns the quotient of two fractions. The other one is an addition one that returns the sum of two fractions, using two parameters

    Everything in the program works except the parts i tried adding. I put the errors that I get in jcreator next to them, so hopefully some one can help.


    // James Novak
    // Fraction.java
    // September 8, 2009

    import java.util.Scanner;
    /** new fraction data type, and that the faction may only be positive or zero,
    *the fraction will always be in lowest terms
    *
    */
    public class Fraction
    {
    private int top; //numerator
    private int bottom; //denominator


    public Fraction()
    {

    top = 0;
    bottom = 1;

    }

    /** The fraction () method is a default constructor that creates
    * a default fraction that is equal to zero.
    *
    */

    public Fraction(int num, int denom)
    {
    if (denom == 0)
    throw new IllegalArgumentException("Second argument " + denom + " cannot be zero.");
    if (num < 0 || denom < 0)
    throw new IllegalArgumentException("Both arguments, " + num + " and " + denom + ", cannot be negative.");
    top = num;
    bottom = denom;
    reduce();

    }
    /** the method assign gives new values to the numerator and denominator
    * of an existing Fraction
    * @param num The value to assign to the numerator
    * @param denom The value to assign to the denominator
    * @exception IllegalArgumentException When the second argument is zero
    * @exception IllegalArgumentException When either argument is negative
    */
    public void assign(int num, int denom)
    {
    if (denom == 0)
    throw new IllegalArgumentException("Second argument " + denom + " cannot be zero.");
    if (num < 0 || denom < 0)
    throw new IllegalArgumentException("Both arguments, " + num + " and " + denom + ", cannot be negative.");

    top=num;
    bottom=denom;
    reduce();

    }
    /** The static multiply method returns the product
    *of the two parameters
    *@param f1 the first fraction to multiply
    *@param f2 the second fraction to multiply
    *@return The product of the f1 and f2
    */
    public static Fraction multiply(Fraction f1, Fraction f2)
    {
    Fraction temp = new Fraction();
    temp.top = f1.top * f2.top;
    temp.bottom = f2.bottom * f2.bottom;
    temp.reduce();
    return temp;

    }

    private void reduce()
    {
    if (top == 0)
    bottom = 1;
    else
    {
    int min = (top < bottom ? top : bottom);
    int gcd; //greatest common divisor
    for (gcd = min; gcd > 1; --gcd)
    if (top % gcd == 0 && bottom % gcd == 0)
    break;
    top = top / gcd;
    bottom = bottom / gcd;
    }
    }

    public void output()
    {
    if (top == 0)
    {
    System.out.print(0);
    return;
    }
    if (top >= bottom)
    {
    System.out.print(top / bottom);

    if (top % bottom != 0)
    System.out.print(" and ");
    }

    if (top % bottom != 0)
    System.out.print(top % bottom + "/" + bottom);
    }




    public void input()
    {
    Scanner keyboard = new Scanner(System.in);
    String line = keyboard.nextLine();
    int slash;
    int total = line.length();
    slash = -1;
    for (int i = 0; i < total; ++i)
    {
    if (line.charAt(i) == '/')
    {
    slash = i;
    break;
    }
    }
    if (slash == -1)
    {
    System.out.println("ERROR in input");
    }
    else
    {
    String first = line.substring(0, slash);
    String second = line.substring(slash + 1, total);
    first = first.trim();
    second = second.trim();
    top = Integer.parseInt(first);
    bottom = Integer.parseInt(second);
    reduce();
    }

    }



    public boolean equals(Fraction f2)
    {

    if (top == f2.top && bottom == f2.bottom)
    return true;
    else
    return false;
    }

    public boolean lessThan(Fraction f2)

    {

    double temp1 = (double)top / bottom;
    double temp2 = (double)f2.top / f2.bottom;

    if (temp1 < temp2)
    return true;
    else
    return false;

    }

    public boolean greaterThan(Fraction f2)

    {

    double temp1 = (double)top / bottom;
    double temp2 = (double)f2.top / f2.bottom;

    if (temp1 > temp2)
    return true;
    else
    return false;

    }

    public static void divide(Fraction f2); //Error: Missing method body, or declare abstract

    {
    double quotientA = (double)top / bottom; //ERROR: cannot find symbol variable f2
    double quotientB = (double)f2.top / f2.bottom; //ERROR: cannot find symbol variable f2
    }

    public static void addition(Fraction f2);

    {
    double newTop = (double)f2.bottom * top + f1.bottom * f2.top; //error cannot find symbol viarable f2
    double newBottom = (double)bottom * f2.bottom; //error cannot find symbol variable f2
    }


    }

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Just from

    Java Code:
    public static void divide(Fraction f2);
    It seems that you don't really know what a method is supposed to look like. Better get Sun's Java tutorial and read it from the start.

  3. #3
    Jakora33 is offline Member
    Join Date
    Sep 2009
    Posts
    5
    Rep Power
    0

    Default

    Well what should it look like? My teacher said she wants a static method for those two.

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Sun's tutorial explains what methods should look like and has lots of examples of methods including static ones. It's available for free and you can download the whole thing to your computer.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default

    Quote Originally Posted by Jakora33 View Post
    Well what should it look like? My teacher said she wants a static method for those two.
    And you very nearly have.
    Compare your static methods with the earlier non-static ones and see if you can spot a difference in how they've been declared. It's not the static keyword either...

    Quote Originally Posted by r035198x View Post
    Sun's tutorial explains what methods should look like and has lots of examples of methods including static ones. It's available for free and you can download the whole thing to your computer.
    I have it on my memory stick...

  6. #6
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Quote Originally Posted by Tolls View Post
    ...


    I have it on my memory stick...
    No. I'm better. I have it on my phone so there.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default

    Quote Originally Posted by r035198x View Post
    No. I'm better. I have it on my phone so there.
    My phone is most decidedly inadequate in that respect.
    It is, in fact, a phone...and nothing else.

    Gadgets?
    Pah!
    We don' need no steenkin' gadgets!

Similar Threads

  1. Need help on Fraction
    By mitch25 in forum New To Java
    Replies: 2
    Last Post: 07-24-2009, 09:46 PM
  2. Decimal to Fraction method
    By Chasingxsuns in forum New To Java
    Replies: 2
    Last Post: 07-16-2009, 02:42 AM
  3. Replies: 3
    Last Post: 11-24-2008, 07:01 PM
  4. (Help) Fraction Summation and Exponents
    By SapphireSpark in forum New To Java
    Replies: 19
    Last Post: 10-09-2008, 04:01 AM
  5. Big Fraction 1.00
    By JavaBean in forum Java Software
    Replies: 1
    Last Post: 03-26-2008, 04:24 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •