# Fraction Problem

• 09-16-2009, 04:47 AM
Jakora33
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.

Quote:

// 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
}

{
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
}

}

• 09-16-2009, 08:39 AM
r035198x
Just from

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.
• 09-16-2009, 03:14 PM
Jakora33
Well what should it look like? My teacher said she wants a static method for those two.
• 09-16-2009, 03:35 PM
r035198x
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.
• 09-16-2009, 03:40 PM
Tolls
Quote:

Originally Posted by Jakora33
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
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...
• 09-16-2009, 03:43 PM
r035198x
Quote:

Originally Posted by Tolls
...

I have it on my memory stick...

No. I'm better. I have it on my phone so there.
• 09-16-2009, 03:51 PM
Tolls
Quote:

Originally Posted by r035198x
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.