6 Replies - 2064 Views - Last Post: 20 September 2012 - 12:25 PM Rate Topic: -----

#1 ElectricTuna.exe  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 02-September 12

Data values...Lost in translation?

Posted 19 September 2012 - 02:39 PM

So I'm making a program that takes two fractions and performs various operations on them {+, -, *, }. Users enter both fractions via two JOptionPane.showInputDialog boxes and picks an operator from a drop down list. Another JOption dialog box shows them their answer and a do-while loop is executed asking them if they want to try again. All of that is fine and well, and I've gotten it down...for the most part.

My problem is that the values are lost somewhere between the point that the user enters the numbers and the end result is displayed to the user. In other words, you might enter "2/15" and "3/8" and choose addition, and the output will look like this:

2/15 + 3/8 = 0/0

and I can't figure out why. Can someone please help me out? Here's my code:

FractionReducer.java:
 public class FractionReducer{
	
        private String fracStr;
	private int num;
	private int denom;
	private int redux1;
	private int redux2;
	
	public void setFraction(int newNumerator, int newDenominator){//Euclid's algorithm
		if(newNumerator < 0){
			newNumerator = 0 - newNumerator;
		}
		if(newDenominator < 0){
			newDenominator = 0 - newDenominator;
		}
		while(newNumerator != newDenominator){
			if(newNumerator > newDenominator){
			num = newNumerator - newDenominator;
			}
			else{
			denom = newDenominator - newNumerator;
			}
		}
		redux1 = (newNumerator / num);
		redux2 = (newDenominator / denom);
	}

	public void setFraction(){
	
	}
	
	public String getResultsFrac(){
		fracStr = redux1 + "/" + redux2;
		return fracStr;
	}
	
	public int getResultsNum(){
		return redux1;
	}
	
	public int getResultsDenom(){
		return redux2;
	}
}



FractionCalc.java:
import java.util.StringTokenizer;

public class FractionCalc{
	public void setInputData(String frac1, String frac2, String operator){
		//Only use the numbers, not the "/" between them
		StringTokenizer tokenizer = new StringTokenizer(frac1, "/");
		StringTokenizer tokenizer2 = new StringTokenizer(frac2, "/");
		
		//Get the numbers as strings
		String num1 = tokenizer.nextToken();
		String denom1 = tokenizer.nextToken();
		String num2 = tokenizer2.nextToken();
		String denom2 = tokenizer2.nextToken();
		
		//Convert the number strings into integers
		int Numerator1 = Integer.parseInt(num1);
		int Denominator1 = Integer.parseInt(denom1);
		int Numerator2 = Integer.parseInt(num2);
		int Denominator2 = Integer.parseInt(denom2);
		
		//Do the math
		String[] operatorList = {"+", "-", "*", ""};
		if(operator.equals(operatorList[0])){//Addition
			int newDenominator = (Denominator1 * Denominator2);
			int Quotient1 = ((newDenominator / Denominator1) * Numerator1);
			int Quotient2 = ((newDenominator / Denominator2) * Numerator2);
			int newNumerator = (Quotient1 + Quotient2);
		}
		else if(operator.equals(operatorList[1])){//Subtraction
			int newDenominator = (Denominator1 * Denominator2);
			int Quotient1 = ((newDenominator / Denominator1) * Numerator1);
			int Quotient2 = ((newDenominator / Denominator2) * Numerator2);
			int newNumerator = (Quotient1 - Quotient2);
		}
		else if(operator.equals(operatorList[2])){//Multiplication
			int newNumerator = (Numerator1 * Numerator2);
			int newDenominator = (Denominator1 * Denominator2);
		}
		else if(operator.equals(operatorList[3])){//Division
			int newNumerator = (Numerator1 * Denominator2);
			int newDenominator = (Denominator1 * Numerator2);
		}
	}
	
	public String getResults(){
		FractionReducer Obj1 = new FractionReducer();
		return Obj1.getResultsFrac();
	}
	
	public void getDecimalResults(){

	}
}



FractionUI.java:
import javax.swing.JOptionPane;

public class FractionUI{
	public void FractionUI(){
		String intro = "Author: BLANK\nClass: BLANK";
		JOptionPane.showMessageDialog(null, intro);		
	}
	
	public void run(){
		//Three strings
		String frac1, frac2, operator;
		int tryAgain;
		
		do{
			//get the fractions as strings...
			frac1 = JOptionPane.showInputDialog(null, "Please enter a fraction e.g. \"2/15\" ");
			frac2 = JOptionPane.showInputDialog(null, "Please enter another fraction e.g. \"3/8\" ");
		
			//choose an operator
			String[] operatorList = {"+", "-", "*", ""};
			Object retVal = JOptionPane.showInputDialog(null, 
			"Select operator: ", "Combobox on JOptionPane",
			JOptionPane.INFORMATION_MESSAGE, null, operatorList, operatorList[0]);
			operator = (String)retVal;
			
			FractionCalc Obj = new FractionCalc();
			FractionReducer Obj2 = new FractionReducer();
			
			String message = frac1 + " " + operator + " " + frac2 + " "
			+ " = " + Obj2.getResultsNum() + "/" + Obj2.getResultsDenom()
			+ " /// " + Obj.getResults();
			JOptionPane.showMessageDialog(null, message);
			
			int option = JOptionPane.showConfirmDialog(null, "Would you like to go again?");
			tryAgain = option;
		}
		while (tryAgain == 0);
			
			String outro = "Good-bye!";
			JOptionPane.showMessageDialog(null, outro);
		}
	}



and the main just creates an object and calls the run() method from Fraction UI.

Is This A Good Question/Topic? 0
  • +

Replies To: Data values...Lost in translation?

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,913
  • Joined: 06-March 08

Re: Data values...Lost in translation?

Posted 19 September 2012 - 08:27 PM

I read your FractionReducer and couldn't really understand it.
So I cutr & paste it in a single program. You can try it with different values, it simply does not work

 public class FractionReducer{
	
    private String fracStr;
	private int num;
	private int denom;
	private int redux1;
	private int redux2;
	
	public FractionReducer(int newNumerator, int newDenominator){//Euclid's algorithm
		System.out.println("Called with " + newNumerator + " / " + newDenominator);
		if(newNumerator < 0){
			newNumerator = 0 - newNumerator;
		}
		if(newDenominator < 0){
			newDenominator = 0 - newDenominator;
		}
		
		while(newNumerator != newDenominator){
			if(newNumerator > newDenominator){
			num = newNumerator - newDenominator;
			System.out.println("num set to:   "  + num);
			}
			else{
			denom = newDenominator - newNumerator;
			System.out.println("denom set to: "  + denom);			
			}
		}
		System.out.println("Ready for reduction with: " + num + " and " + denom);
		redux1 = (newNumerator / num);
		redux2 = (newDenominator / denom);
		System.out.println("Redux1: " + redux1 + " redux2: " + redux2);
	}
	public static void main(String[] args) {
		new FractionReducer(3,5);
	}

}


Was This Post Helpful? 0
  • +
  • -

#3 ElectricTuna.exe  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 02-September 12

Re: Data values...Lost in translation?

Posted 19 September 2012 - 08:52 PM

I'm guessing there's some sort of error in the algorithm that I'm using to find the GCF. Do you have any advice on how I might be able to rewrite it so that it works?
Was This Post Helpful? 0
  • +
  • -

#4 ElectricTuna.exe  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 02-September 12

Re: Data values...Lost in translation?

Posted 19 September 2012 - 11:13 PM

...There's nothing wrong with the algorithm:

 import javax.swing.JOptionPane;
 
 public class test{
	
    private String fracStr;
	private int num;
	private int denom;
	private int redux1;
	private int redux2;
	String message;
	
	public void test1(int newNumerator, int newDenominator){//Euclid's algorithm
		num = newNumerator; //num retains the original value
		denom = newDenominator;//denom retains the original value
		message = "Called with " + newNumerator + " / " + newDenominator;
		JOptionPane.showMessageDialog(null, message);
		if(newNumerator < 0){ //if num is negative...
			newNumerator = 0 - newNumerator; //make it positive
		}
		if(newDenominator < 0){//if denom is negative...
			newDenominator = 0 - newDenominator; //make it positive
		}
		
		do{ //do-while or while will work, either way...
			if(newNumerator > newDenominator){ //if the numerator is larger than the denominator...
				newNumerator = newNumerator - newDenominator; //subtract the numerator from the denominator...
				message = "num set to: " + num;
				JOptionPane.showMessageDialog(null, message);
			}
			else if (newNumerator < newDenominator){//if denom is larger...
				newDenominator = newDenominator - newNumerator; //do the same thing...
				message = "denom set to: "  + denom;		
				JOptionPane.showMessageDialog(null, message);
			}
			else{//if not, then end the loop
				break;
			}
		}while(newNumerator != newDenominator);
		message = "Ready for reduction with: " + num + " and " + denom;
		JOptionPane.showMessageDialog(null, message);
		redux1 = (num / newNumerator); //original value divided by the new value...
		redux2 = (denom / newDenominator); //original value divided by the new value...
		message = "Redux1: " + redux1 + " redux2: " + redux2; // and you get your reduced fraction
		JOptionPane.showMessageDialog(null, message);
	}
	public static void main(String[] args) {
		test Obj = new test();
		Obj.test1(20,40);
	}

}


Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,913
  • Joined: 06-March 08

Re: Data values...Lost in translation?

Posted 20 September 2012 - 03:51 AM

Because denom is an exact multiple of num
Try with 3/5
Was This Post Helpful? 0
  • +
  • -

#6 ElectricTuna.exe  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 02-September 12

Re: Data values...Lost in translation?

Posted 20 September 2012 - 04:49 AM

You got the infinite loop because there wasn't a break in the loop, or some condition, such as an output that says "your numbers don't have a GCF" present.

Anyway, I'm not going to suggest that it's the only way or the "best way" to write that algorithm. That's besides the point. I need help understanding how to reference the data contained in the variables that are contained within the FractionCalc.java and apply them in the algorthim.

It has something to do with "pass by value" or something, because Java doesn't use pointers.
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,913
  • Joined: 06-March 08

Re: Data values...Lost in translation?

Posted 20 September 2012 - 12:25 PM

View PostElectricTuna.exe, on 20 September 2012 - 07:49 AM, said:

It has something to do with "pass by value" or something, because Java doesn't use pointers.

Java passes all basic datatype by value and, as all objects in Java are effectively pointers to an object passes these pointers by value.

So you will need to create a FractionReducer object
FractionReducer fr = new FractionReducer();
call its setFraction() method
fr.setFraction(num, denom);

then you can call its getter

num = fr.getRedux1();
denom = fr.getRedux2();
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1