# Calling Variables in Multiple Methods

• 02-07-2012, 12:11 AM
PrimalScientist
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?

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(){

}

}

• 02-07-2012, 12:33 AM
tnrh1
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.
• 02-07-2012, 12:35 AM
PrimalScientist
Re: Calling Variables in Multiple Methods
I dont understand?
• 02-07-2012, 12:37 AM
PrimalScientist
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?
• 02-07-2012, 05:12 AM
isuru
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.
• 02-07-2012, 05:18 AM
Fubarable
Re: Calling Variables in Multiple Methods
Quote:

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?
• 02-07-2012, 05:20 AM
isuru
Re: Calling Variables in Multiple Methods
Quote:

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.
• 02-07-2012, 05:26 AM
Fubarable
Re: Calling Variables in Multiple Methods
Quote:

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.
• 02-07-2012, 05:32 AM
isuru
Re: Calling Variables in Multiple Methods
Quote:

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?
• 02-07-2012, 05:42 AM
pbrockway2
Re: Calling Variables in Multiple Methods
[deleted] move along, nothing to see here...
• 02-07-2012, 11:26 AM
PrimalScientist
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.