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.");
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(){
}
}
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.
Re: Calling Variables in Multiple Methods
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?
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.
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?
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.
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.
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?
Re: Calling Variables in Multiple Methods
[deleted] move along, nothing to see here...
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.