Results 1 to 7 of 7
Thread: Fraction Problem
 09162009, 04:47 AM #1Member
 Join Date
 Sep 2009
 Posts
 5
 Rep Power
 0
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
}
}
 09162009, 08:39 AM #2Senior Member
 Join Date
 Aug 2009
 Posts
 2,388
 Rep Power
 9
Just from
Java Code:public static void divide(Fraction f2);
 09162009, 03:14 PM #3Member
 Join Date
 Sep 2009
 Posts
 5
 Rep Power
 0
Well what should it look like? My teacher said she wants a static method for those two.
 09162009, 03:35 PM #4Senior Member
 Join Date
 Aug 2009
 Posts
 2,388
 Rep Power
 9
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.
 09162009, 03:40 PM #5Moderator
 Join Date
 Apr 2009
 Posts
 12,609
 Rep Power
 21
 09162009, 03:43 PM #6Senior Member
 Join Date
 Aug 2009
 Posts
 2,388
 Rep Power
 9
 09162009, 03:51 PM #7Moderator
 Join Date
 Apr 2009
 Posts
 12,609
 Rep Power
 21
Similar Threads

Need help on Fraction
By mitch25 in forum New To JavaReplies: 2Last Post: 07242009, 09:46 PM 
Decimal to Fraction method
By Chasingxsuns in forum New To JavaReplies: 2Last Post: 07162009, 02:42 AM 
Rediculous problem?! Assigning a fraction to a double variable
By Tzoshirzup in forum New To JavaReplies: 3Last Post: 11242008, 08:01 PM 
(Help) Fraction Summation and Exponents
By SapphireSpark in forum New To JavaReplies: 19Last Post: 10092008, 04:01 AM 
Big Fraction 1.00
By JavaBean in forum Java SoftwareReplies: 1Last Post: 03262008, 05:24 AM
Bookmarks