Hi I've been working on this
java program in class and I have to have two static methods that find the quotient of two fractions and the sum of two fractions. Everything in this program works up until the end. I have a feeling that I might be using the wrong kind of static method and i was wondering if some one could tell me how to make my code work. Both methods can only use two parameters each.
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 f1 or f2
double quotientB = (double)f2.top / f2.bottom; //ERROR: cannot find symbol variable f1 or f2
}
public static void addition(Fraction f2);
{
double newTop = (double)f2.bottom * top + f1.bottom * f2.top; //error cannot find symbol viarable f1 or f2
double newBottom = (double)bottom * f2.bottom; //error cannot find symbol variable f1 or f2
}
}