Results 1 to 11 of 11
  1. #1
    dom12 is offline Member
    Join Date
    Nov 2010
    Posts
    7
    Rep Power
    0

    Question Printing Object Information

    So I created two fraction objects using:

    Java Code:
    Fraction f1 = new Fraction(num1, denom1);
    Fraction f2 = new Fraction(num2, denom2);
    And I initialized the Fraction result using:

    Java Code:
    Fraction result = f1.plus(f2);
    My print statement is:

    Java Code:
    System.out.println(f1.print() + " " + operation + " " + f2.print() + " = " + result.print());
    operation is just a char variable and not important.
    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?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by dom12 View Post
    So I created two fraction objects using:
    ....

    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?
    Don't know. What do you mean specifically by "it runs int a problem..."? Since this statement tells us nothing that we can use to be able to help you.

  3. #3
    dom12 is offline Member
    Join Date
    Nov 2010
    Posts
    7
    Rep Power
    0

    Default

    The compiler says 'Cannot find symbol' and points to result.print()

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by dom12 View Post
    The compiler says 'Cannot find symbol' and points to result.print()
    The problem is not evident in what you're showing us and this is likely because it is in code that you're not showing us. My guess is it's a scope issue, that the result variable is not in scope when you try to use it. But again, we don't have enough information.
    Last edited by Fubarable; 11-04-2010 at 03:36 AM.

  5. #5
    dom12 is offline Member
    Join Date
    Nov 2010
    Posts
    7
    Rep Power
    0

    Default

    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;
    	}
    Both :
    Java Code:
    Fraction f1 = new Fraction(num1, denom1);
    Fraction f2 = new Fraction(num2, denom2);
    and:
    Java Code:
    Fraction result = f1.plus(f2);
    are in the main method, and so is the line that calls the print method for f1, f2 and result.
    Last edited by dom12; 11-04-2010 at 03:49 AM.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    You may need to show us either the whole program or a large sub-portion of the program, but especially the main method.

  8. #8
    dom12 is offline Member
    Join Date
    Nov 2010
    Posts
    7
    Rep Power
    0

    Default

    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;
    	}
     }
    Main Method:
    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		
    		}
    }
    The divide, times, and minus functions are all commented out because they are not complete. Thanks for taking the time to look through this if you do.

  9. #9
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    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.

  10. #10
    dom12 is offline Member
    Join Date
    Nov 2010
    Posts
    7
    Rep Power
    0

    Default

    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.

  11. #11
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    You're welcome. Good luck with the rest of the assignment.

Similar Threads

  1. Replies: 2
    Last Post: 06-22-2010, 05:29 AM
  2. Printing values from object in Array?
    By thesinter in forum New To Java
    Replies: 3
    Last Post: 01-20-2010, 06:19 AM
  3. Replies: 7
    Last Post: 10-06-2009, 06:58 AM
  4. Printing Information in a node
    By Tenn in forum New To Java
    Replies: 4
    Last Post: 04-30-2009, 06:43 AM
  5. [SOLVED] printing address of current instance of object?
    By emceenugget in forum New To Java
    Replies: 1
    Last Post: 02-09-2009, 10:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •