# Java Program



## purified3 (Nov 11, 2004)

I am having a little trouble with this program, I understand it, but I have errors that I don't know how to fix, while I did the same thing somewhere else, and I never got these errors. The program takes in 4 numbers as you can see, two numerators and two denominators. The program uses GUI input to show the two fractions mulitplied, divided, added, and subtracted. The code is as follows....

import javax.swing.JOptionPane; 

public class Practice8b
{
public static void main (String args[])
{
String strNum1 = JOptionPane.showInputDialog("Enter Numerator 1");
String strDen1 = JOptionPane.showInputDialog("Enter Denominator 1");

String strNum2 = JOptionPane.showInputDialog("Enter Numerator 2");
String strDen2 = JOptionPane.showInputDialog("Enter Denominator 2");

int num1 = Integer.parseInt(strNum1);
int den1 = Integer.parseInt(strDen1);

int num2 = Integer.parseInt(strNum2);
int den2 = Integer.parseInt(strDen2);

Rational r1 = new Rational(num1,den1);
Rational r2 = new Rational(num2,den2);
Rational r3 = new Rational();
Rational r4 = new Rational();
Rational r5 = new Rational();
Rational r6 = new Rational();

r3.multiply(r1,r2);
r4.divide(r1,r2);
r5.add(r1,r2);
r6.subtract(r1,r2);

JOptionPane.showMessageDialog(null,r1.getOriginal() + " * " + r2.getOriginal() + " = " + r3.getRational()
+ "\n" + r1.getOriginal() + " / " + r2.getOriginal() + " = " + r4.getRational()
+ "\n" + r1.getOriginal() + " + " + r2.getOriginal() + " = " + r5.getRational()
+ "\n" + r1.getOriginal() + " - " + r2.getOriginal() + " = " + r6.getRational());


System.exit(0);

}


class Rational
{
private int num,den,firstNum,firstDen;


public Rational()
{
num = firstNum = 0;
den = firstDen = 1;
}


public Rational(int n, int d)
{
num = firstNum = n;
den = firstDen = d;
}


public int getGCF(int n1,int n2)
{
int rem = n1 % n2;

while(rem !=0)
{
n1 = n2;
n2 = rem;
rem = n1 % n2;
}

return rem;

}


private void reduce()
{
int gcf = getGCF(num,den);
num/=gcf;
den/=gcf;

}


public double getDecimal()
{
double decimal = (double) num / den;
return decimal;
}

public String getRational()
{
int gcf = getGCF(num,den);
num = num/gcf;
den = den/gcf;

return num + "/" + den;
}


public String getOriginal()
{
return num + "/" + den;
}


public int getNum()
{
return num;
}


public int getDen()
{
return den;
}


public void multiply(Rational f1,Rational f2)
{
num = firstNum = f1.getNum() * f2.getNum();
den = firstDen = f1.getDen() * f2.getDen();
}


public void divide(Rational f1,Rational f2)
{
num = firstNum = f1.getNum() * f2.getDen();
den = firstDen = f1.getNum() * f2.getNum();
}


public void add(Rational f1,Rational f2)
{


}


public void subtract(Rational f1,Rational f2)
{

}
}
}


The errors I get are.....

--------------------Configuration: <Default>--------------------
C:\Documents and Settings\JCreator\Practice8b.java:21: non-static variable this cannot be referenced from a static context
Rational r1 = new Rational(num1,den1);
^
C:\Documents and Settings\JCreator\Practice8b.java:22: non-static variable this cannot be referenced from a static context
Rational r2 = new Rational(num2,den2);
^
C:\Documents and Settings\JCreator\Practice8b.java:23: non-static variable this cannot be referenced from a static context
Rational r3 = new Rational();
^
C:\Documents and Settings\JCreator\Practice8b.java:24: non-static variable this cannot be referenced from a static context
Rational r4 = new Rational();
^
C:\Documents and Settings\JCreator\Practice8b.java:25: non-static variable this cannot be referenced from a static context
Rational r5 = new Rational();
^
C:\Documents and Settings\JCreator\Practice8b.java:26: non-static variable this cannot be referenced from a static context
Rational r6 = new Rational();
^
6 errors

Process completed.


----------



## impact (Nov 7, 2005)

Purely for debugging purposes, why not put all of your static implementation in an instance of Practice8b? So just cut and paste everthing in a new Practice8b() constructor, for instance. Then call new Practice8b() from your static main. It shouldn't be necessary. It's the sort of thing I do just to get around the specific error. Because there might be other errors further in that aren't being reported.

Did you intend to create and use an inner class, by the way? Just checking. This computer at school doesn't have Swing, so I can't run and debug your code.


----------



## purified3 (Nov 11, 2004)

Yes, I pretty much noticed that I put the so called Rational class into the main method and forgot to close the main method. I fixed a few things in the program , and it runs, multiplying and dividing the fractions are simple, but I cannot figure how to add them in a way where if I add 3/4 and 1/2 then it will come as 5/4, at least I think that is it, I just want to know how to add and subtract the fractions. I pretty much have it down, I just want to figure out how to add and subtract them because its different on paper than by computer.

//Practice8b.java

import javax.swing.JOptionPane; 

public class Practice8b
{
public static void main (String args[])
{
String strNum1 = JOptionPane.showInputDialog("Enter Numerator 1");
String strDen1 = JOptionPane.showInputDialog("Enter Denominator 1");

String strNum2 = JOptionPane.showInputDialog("Enter Numerator 2");
String strDen2 = JOptionPane.showInputDialog("Enter Denominator 2");

int num1 = Integer.parseInt(strNum1);
int den1 = Integer.parseInt(strDen1);

int num2 = Integer.parseInt(strNum2);
int den2 = Integer.parseInt(strDen2);

Rational r1 = new Rational(num1,den1);
Rational r2 = new Rational(num2,den2);
Rational r3 = new Rational();
Rational r4 = new Rational();
Rational r5 = new Rational();
Rational r6 = new Rational();

r3.multiply(r1,r2);
r4.divide(r1,r2);
r5.add(r1,r2);
r6.subtract(r1,r2);

JOptionPane.showMessageDialog(null,r1.getOriginal() + " * " + r2.getOriginal() + " = " + r3.getRational()
+ "\n" + r1.getOriginal() + " / " + r2.getOriginal() + " = " + r4.getRational()
+ "\n" + r1.getOriginal() + " + " + r2.getOriginal() + " = " + r5.getRational()
+ "\n" + r1.getOriginal() + " - " + r2.getOriginal() + " = " + r6.getRational());


System.exit(0);

}

}


class Rational
{
private int num,den,firstNum,firstDen;


public Rational()
{
num = firstNum = 0;
den = firstDen = 1;
}


public Rational(int n, int d)
{
num = firstNum = n;
den = firstDen = d;
}


public int getGCF(int n1,int n2)
{
int rem = n1 % n2;

while(rem !=0)
{
n1 = n2;
n2 = rem;
rem = n1 % n2;
}

return n2;

}


private void reduce()
{
int gcf = getGCF(num,den);
num/=gcf;
den/=gcf;

}


public double getDecimal()
{
double decimal = (double) num / den;
return decimal;
}

public String getRational()
{
return num + "/" + den;
}


public String getOriginal()
{
return num + "/" + den;
}


public int getNum()
{
return num;
}


public int getDen()
{
return den;
}


public void multiply(Rational f1,Rational f2)
{
num = firstNum = f1.getNum() * f2.getNum();
den = firstDen = f1.getDen() * f2.getDen();
reduce();
}


public void divide(Rational f1,Rational f2)
{
num = firstNum = f1.getNum() * f2.getDen();
den = firstDen = f1.getDen() * f2.getNum();
reduce();


}


public void add(Rational f1,Rational f2)
{
num = firstNum = f1.getNum() + getGCF(num,den);
den = firstDen = f1.getDen() + getGCF(num,den);
//Pretty much close, but doesn't work.

}


public void subtract(Rational f1,Rational f2)
{
num = firstNum = f1.getNum() - f2.getNum();
den = firstDen = f1.getDen() - f2.getDen();	
//I don't really know what to do, probly close to add 
method.
}

}


----------



## impact (Nov 7, 2005)

I'm not too swift in the basic math area myself. I think what you want to know is how to go about reducing the fraction effectively without resorting to decimals. So let's say you've added two fractions using the highest possible denominator.

This is not a pretty approach . . .

double myDenominator = 144;
double myNumerator = 84;

boolean canBeReduced = true;

while(canBeReduced) {
canBeReduced = false;

for(int n = 2; !canBeReduced && n < 500; n++) {
double possibleNewNumerator = myNumerator / (double)n;
double possibleNewDenominator = myDenominator / (double)n;
double result = possibleNewNumerator / possibleNewDenominator;

if((int)result == result) {
myNumerator = possibleNewNumerator;
myDenominator = possibleNewDenominator;
canBeReduced = true;
}
}
}

System.out.println("My numerator is " + myNumerator);
System.out.println("My denominator is " + myDenominator);

I haven't actually tried out the code. It's just off the top of my head. But you get the idea, I guess. The problem with my approach is that there is clearly a lot of waste. At the same time I can brutishly ignore all the effort involved in sampling primary numbers.


----------

