Welcome to Tech Support Forum home to more then 136,000 problems solved. Issues have included: Spyware, Malware, Virus Issues, Windows, Microsoft, Linux, Networking, Security, Hardware, and Gaming Getting your problem solved is as easy as:
1. Registering for a free account
2. Asking your question
3. Receiving an answer

Registered members:
* Get free support
* Communicate privately with other members (PM).
* Removal of this message
* See fewer ads.
* And much more..

 



Want to know how to post a question? click here Having problems with spyware and pop-ups? First Steps
Go Back   Tech Support Forum > The IT Pro > Programming
User Name
Password
Site Map Register Donate Rules Blogs Mark Forums Read


Programming A discussion forum for programs and programming used in tech-related businesses.

Reply
 
LinkBack Thread Tools
Old 11-06-2005, 07:00 PM   #1 (permalink)
Registered User
 
Join Date: Nov 2004
Posts: 200
OS: Win7 Ultx64, Ubuntu 9.10, Gentoo, Backtrack 4


Java Program

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.
purified3 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Important Information
Join the #1 Tech Support Forum Today - It's Totally Free!

TechSupportForum.com is a leading support website for your computer needs. We offer free, friendly and personalized computer support. Why pay to have your computer fixed when you can do it for free.

Join TechSupportforum.com Today - Click Here

Old 11-07-2005, 12:26 PM   #2 (permalink)
Registered User
 
Join Date: Nov 2005
Posts: 3
OS: XP


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.
impact is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 11-07-2005, 04:09 PM   #3 (permalink)
Registered User
 
Join Date: Nov 2004
Posts: 200
OS: Win7 Ultx64, Ubuntu 9.10, Gentoo, Backtrack 4


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

}
purified3 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 11-09-2005, 10:36 AM   #4 (permalink)
Registered User
 
Join Date: Nov 2005
Posts: 3
OS: XP


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.
impact is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Reply


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT -7. The time now is 02:20 PM.



Copyright 2001 - 2009, Tech Support Forum
Home Tips Plus | Outdoor Basecamp | Automotive Support Forum

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85