# Thread: Calling Variables in Multiple Methods

1. Member
Join Date
Jan 2011
Posts
27
Rep Power
0

## 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.");

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. Senior Member
Join Date
Aug 2011
Posts
251
Rep Power
6

## 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. Member
Join Date
Jan 2011
Posts
27
Rep Power
0

## Re: Calling Variables in Multiple Methods

I dont understand?

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

## 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. Member
Join Date
Jan 2010
Posts
19
Rep Power
0

## 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. ## Re: Calling Variables in Multiple Methods

Originally Posted by isuru
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. Member
Join Date
Jan 2010
Posts
19
Rep Power
0

## Re: Calling Variables in Multiple Methods

Originally Posted by Fubarable
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. ## Re: Calling Variables in Multiple Methods

Originally Posted by isuru
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. Member
Join Date
Jan 2010
Posts
19
Rep Power
0

## Re: Calling Variables in Multiple Methods

Originally Posted by Fubarable
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. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,712
Rep Power
14

## Re: Calling Variables in Multiple Methods

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

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

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