Results 1 to 11 of 11
  1. #1
    Join Date
    Jan 2011
    Posts
    27
    Rep Power
    0

    Default Calling Variables in Multiple Methods

    Hello All,

    I am just going through some past exam papers answering some questions as and where I can.
    I am stuck on one question which requires user input to be squared and cubed.

    One method needs to be called squared, where it does its calcs, and another method called cubed. However, the cubed method needs to call the squared method.
    This is all in the question by the way.

    Can anyone point me in the right direction please?

    Java Code:
    import java.util.Scanner;
    
    public class SquareAndCubed {
    	
    	
    	public static void main (String [] args){
    		
    		Scanner in = new Scanner(System.in);
    		System.out.println("This is a program that will show your number squared and cubed.");
    		System.out.println("Please enter a number: ");
    
    		int userInput = in.nextInt();
    		
    		
    	}
    
    	// New PRIVATE METHOD for squared number
    			private void squaredNumber(int userInput){
    				System.out.println("The number you chose was: " + userInput);
    
    
    
    			} 
    			
    		// New PRIVATE METHOD for cubed number
    			private void cubedNumber(){
    		
    				
    			}
    			
    
    }

  2. #2
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    4

    Default Re: Calling Variables in Multiple Methods

    Your methods should be static since you don't create an instance of the class to work on.
    And you don't even call them from the main method.

  3. #3
    Join Date
    Jan 2011
    Posts
    27
    Rep Power
    0

    Default Re: Calling Variables in Multiple Methods

    I dont understand?

  4. #4
    Join Date
    Jan 2011
    Posts
    27
    Rep Power
    0

    Default Re: Calling Variables in Multiple Methods

    It does not state this in the exam question. Are you saying that the exam papers are incorrect? Or perhaps we are being lectured incorrectly?

  5. #5
    isuru is offline Member
    Join Date
    Jan 2010
    Posts
    19
    Rep Power
    0

    Default Re: Calling Variables in Multiple Methods

    ********Requested to delete**********
    So you can learn by trying, actually that is the only way one can learn, not by copying.
    Last edited by isuru; 02-07-2012 at 05:28 AM.

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

    Default Re: Calling Variables in Multiple Methods

    Quote Originally Posted by isuru View Post
    Is this you are looking for?
    .....

    Nice going isuru. We're all very impressed with how smart you are, but wouldn't the original poster learn more if you gave him suggestions that helped him write the code himself rather than having you write it for him? Sure it's a lot easier to simply plop the solution on the page, and it takes more effort to try to teach the OP, but isn't it worth the effort in the long run?

  7. #7
    isuru is offline Member
    Join Date
    Jan 2010
    Posts
    19
    Rep Power
    0

    Default Re: Calling Variables in Multiple Methods

    Quote Originally Posted by Fubarable View Post
    Nice going isuru. We're all very impressed with how smart you are, but wouldn't the original poster learn more if you gave him suggestions that helped him write the code himself rather than having you write it for him? Sure it's a lot easier to simply plop the solution on the page, and it takes more effort to try to teach the OP, but isn't it worth the effort in the long run?
    Oh! I am so sorry. I didn't know that. Next time I won't do that. If you want I will delete it.

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

    Default Re: Calling Variables in Multiple Methods

    Quote Originally Posted by isuru View Post
    Oh! I am so sorry. I didn't know that. Next time I won't do that. If you want I will delete it.
    Yes, I think it would be a good idea for you to delete your previous post.

  9. #9
    isuru is offline Member
    Join Date
    Jan 2010
    Posts
    19
    Rep Power
    0

    Default Re: Calling Variables in Multiple Methods

    Quote Originally Posted by Fubarable View Post
    Yes, I think it would be a good idea for you to delete your previous post.
    Here I deleted it, but sometimes I search through google and go to stackoverflow and sites like this and there i find out the solution. But if someone do the same isn't he be disappoint. I feel like I should post the answer in somewhere like pastebin or my blog and put the link here, but i think it is spamming right?

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

    Default Re: Calling Variables in Multiple Methods

    [deleted] move along, nothing to see here...

  11. #11
    Join Date
    Jan 2011
    Posts
    27
    Rep Power
    0

    Default Re: Calling Variables in Multiple Methods

    Thanks for attempting to help me all. Yes I do need to learn, I just need pointing in the right direction thats all.
    The thread is placed in New To Java.
    I am just a little fed up at the moment because our lectures could be better. The lecturer is brilliant at what he does, dont get me wrong. He has written really cool papers on programming and that.
    But he is from the Ukrane, and has a strong Ukranian accent. He is quite difficult to understand at times.
    I am just asking for some help thats all please.
    Last edited by PrimalScientist; 02-07-2012 at 11:48 AM.

Similar Threads

  1. Calling Variables
    By Soulpole in forum New To Java
    Replies: 7
    Last Post: 01-28-2012, 09:26 PM
  2. Using variables in different methods
    By squirmytoad in forum New To Java
    Replies: 2
    Last Post: 01-10-2012, 09:04 PM
  3. using variables in multiple methods
    By cagipple in forum New To Java
    Replies: 2
    Last Post: 09-14-2011, 04:44 AM
  4. Static variables and methods
    By sudharani in forum New To Java
    Replies: 5
    Last Post: 04-25-2011, 03:07 PM
  5. variables-methods
    By Warren in forum New To Java
    Replies: 1
    Last Post: 11-28-2007, 05:14 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
  •