Results 1 to 11 of 11
Thread: Printing Object Information
 11042010, 01:49 AM #1Member
 Join Date
 Nov 2010
 Posts
 7
 Rep Power
 0
Printing Object Information
So I created two fraction objects using:
Java Code:Fraction f1 = new Fraction(num1, denom1); Fraction f2 = new Fraction(num2, denom2);
Java Code:Fraction result = f1.plus(f2);
Java Code:System.out.println(f1.print() + " " + operation + " " + f2.print() + " = " + result.print());
And when I compile it runs into a problem at 'result.print', but if I comment that part out, f1 and f2 print just fine. Why?

 11042010, 02:27 AM #3Member
 Join Date
 Nov 2010
 Posts
 7
 Rep Power
 0
The compiler says 'Cannot find symbol' and points to result.print()

Last edited by Fubarable; 11042010 at 02:36 AM.
 11042010, 02:43 AM #5Member
 Join Date
 Nov 2010
 Posts
 7
 Rep Power
 0
Ok I was assuming this was an obvious mistake I made, my apologies. Here's the plus function in the Fraction class:
Java Code:public Fraction plus(Fraction f) { int newDenom = denominator * f.denominator; int newNum = (numerator * f.denominator) + (f.numerator * denominator); Fraction result = new Fraction(newNum, newDenom); return result; }
Java Code:Fraction f1 = new Fraction(num1, denom1); Fraction f2 = new Fraction(num2, denom2);
Java Code:Fraction result = f1.plus(f2);
Last edited by dom12; 11042010 at 02:49 AM.

We still need to see more. Let's see the code where you create your result and where you try to print it. Are they in the same method? If not, then again you may be having a scope issue where the result variable is only visible in the method that it was declared in.

You may need to show us either the whole program or a large subportion of the program, but especially the main method.
 11042010, 03:57 AM #8Member
 Join Date
 Nov 2010
 Posts
 7
 Rep Power
 0
Fraction class:
Java Code:public class Fraction { private int numerator; private int denominator; public Fraction(int n, int d) { numerator = n; denominator = d; } public Fraction plus(Fraction f) { int newDenom = denominator * f.denominator; int newNum = (numerator * f.denominator) + (f.numerator * denominator); Fraction result2 = new Fraction(newNum, newDenom); System.out.println(newNum + "/" + newDenom); return result2; } /* public Fraction minus(Fraction f) { return 0; } public Fraction times(Fraction f) { return 0; } public Fraction divide(Fraction f) { return 0; } */ public String print() { String result = numerator + "/" + denominator; return result; } }
Java Code:import java.util.*; public class Proj6 { public static void main(String[] args) { Scanner s = new Scanner(System.in); System.out.print("Enter the expression (like 2/3 + 3/4): "); String in = s.nextLine(); StringTokenizer st = new StringTokenizer(in, " "); String first = st.nextToken(); char operation = st.nextToken().charAt(0); String second = st.nextToken(); StringTokenizer t = new StringTokenizer(first, "/"); int num1 = Integer.parseInt(t.nextToken()); int denom1 = Integer.parseInt(t.nextToken()); StringTokenizer t2 = new StringTokenizer(second, "/"); int num2 = Integer.parseInt(t2.nextToken()); int denom2 = Integer.parseInt(t2.nextToken()); int com1 = simplify(num1, denom1); int com2 = simplify(num2, denom2); num1 /= com1; denom1 /= com1; num2 /= com2; denom2 /= com2; Fraction f1 = new Fraction(num1, denom1); Fraction f2 = new Fraction(num2, denom2); if(operation == '+') { Fraction result = f1.plus(f2); } else { Fraction result = new Fraction(1,2); } /* if(operation == '') { Fraction result = f1.minus(f2); } if(operation == '*') { Fraction result = f1.times(f2); } if(operation == '/') { Fraction result = f1.divide(f2); } */ System.out.println(); System.out.println(f1.print() + " " + operation + " " + f2.print() + " = " /*+ result.print()*/); } //**SIMPLIFY**// public static int simplify(int x, int y) { if(x<y) { int a = x; x = y; y = a; } int z = x%y; while(z!=0) { x = y; y = z; z = x%z; } return y; //returns common divisor } }
 11042010, 05:28 AM #9Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,565
 Rep Power
 12
Java has a notion of scope that Fubarable refered to above somewhere. The thing is that when you declare a variable like
Java Code:Fraction result = f1.plus(f2);
The variable result is only valid until the closing } of the block you are in at the time. For some variables  like numerator  that means they are visible through out the whole class. For others, like result, they don't last very long because the block ends on the very next line.
Once a variable is "out of scope" it's gone! Hence the compiler complains about result.print() because there is no such thing as result any more.
The general rule is: declare variables in the same scope that you use them. (to repeat: scope means from { to a matching }).
In your case move the declaration of result to just before those if statements.

The Java compiler is also quite picky about every variable that you use having a value. So you might want to make result null to begin with. This only applies to variables whose value you use the way you use result in result.print(). So don't go thinking you always have to initialise variables.
 11042010, 03:59 PM #10Member
 Join Date
 Nov 2010
 Posts
 7
 Rep Power
 0
Thank you so much pbrockway! I declared the fraction result at the beginning of the program and it all works now. I now see the error of my ways.
 11042010, 06:39 PM #11Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,565
 Rep Power
 12
Similar Threads

Create object of unknown class, based on existing object
By Zack in forum New To JavaReplies: 2Last Post: 06222010, 04:29 AM 
Printing values from object in Array?
By thesinter in forum New To JavaReplies: 3Last Post: 01202010, 05:19 AM 
Rotated Shape Object Line weight is not retaining properly in printing
By dorairaj in forum AWT / SwingReplies: 7Last Post: 10062009, 05:58 AM 
Printing Information in a node
By Tenn in forum New To JavaReplies: 4Last Post: 04302009, 05:43 AM 
[SOLVED] printing address of current instance of object?
By emceenugget in forum New To JavaReplies: 1Last Post: 02092009, 09:36 PM
Bookmarks