Need help understanding method calls and such
Hi,
First and foremost, this is not homework (believe it or not some forums freak out about this, so wanted to get this out of the way). I took a Java course in college and want to get back into it. I can do some simple stuff, but am more interested in the basis of object oriented programming (in other words not doing everything in the main method).
Here is the code I'm working with:
Code :
import javax.swing.*;
public class Experiment
{
public static void main(String [] args)
{
String number1 = JOptionPane.showInputDialog(null, "Please enter your first number");
String number2 = JOptionPane.showInputDialog(null, "Please enter your second number");
Double getnumber1 = Double.parseDouble(number1);
Double getnumber2 = Double.parseDouble(number2);
JOptionPane.showMessageDialog(null, "The number's are " + getnumber1 + "and" + getnumber2);
//JOptionPane.showMessageDialog(null, "The numbers added together are" + addNumbers() );
}
/*
public double addNumbers()
{
double total;
total = getnumber1 + getnumber2;
return total;
}
*/
}
What I'm doing
I'm using the JOptionPane simple dialog boxes to try and add together some numbers generated by users. I like JOptionPane, so please don't suggest using Scanner. What I'm trying to do is have the addNumbers part return up to the last JOptionPane message which will show the user the total. I commented out the parts that don't work, the last part of the program and the last JOptionPane message dialog.
What I want to do (ideally) is do all the calculations out of the main method and then be able to call them when needed. So the last part of the code may be totally incorrect (just the commented out part) and the last JOptionPane (also commented out).
So can someone explain to me in simple terms the syntax for calling other methods and such? If I'm totally off base and am doing everything completely wrong, please let me know as well.
Re: Need help understanding method calls and such
Quote:
the parts that don't work
What didn't work? If there are errors, don't ignore them, copy and paste them here.
The error is probably about using non-static methods. Your short and simple program doesn't use classes for your tests so you should make all your methods static. Later when you start using classes, you'll want to remove all the static modifiers, except for main()s.
The other problem has to do with the scope of some of your variables. They are ONLY know inside of the main() method. For other methods to see them, they must be defined outside of any method. Again you'll need the static modifier.
Re: Need help understanding method calls and such
Sorry, I didn't think the error would be useful because I figured I was doing it totally wrong.
You are right about the error, "non-static method addNumbers() cannot be referenced from a static context".
So if I move the getnumber variables out of the main method, I get:
Code :
public class Experiment
{
double getnumber1, getnumber2;
public static void main(String [] args)
{
String number1 = JOptionPane.showInputDialog(null, "Please enter your first number");
String number2 = JOptionPane.showInputDialog(null, "Please enter your second number");
Double getnumber1 = Double.parseDouble(number1);
Double getnumber2 = Double.parseDouble(number2);
JOptionPane.showMessageDialog(null, "The number's are " + getnumber1 + "and" + getnumber2);
JOptionPane.showMessageDialog(null, "The numbers added togethere are" + addNumbers() );
}
public double addNumbers()
{
double total;
total = getnumber1 + getnumber2;
return total;
}
}
So is there an easy way to get around that problem without rewriting the entire simple program? To be honest, I figured the very last chunk of code (starting with public double) was incorrect.
Re: Need help understanding method calls and such
You now have two definitions for getnumber1 and getnumber2. The ones in the main() method will get values, the ones outside will not.
Re: Need help understanding method calls and such
Norm is not entirely correct, but hes partly there. It is true that you have two definitions, but lets actually look at the error.
You are both correct and incorrect.
1st issue is that addNumbers needs to be declared static. This is because you are calling it in the main.
2nd is that the getnumber1 and getnumber2 variables ALSO need to be declared static, because these are being references both in the static main and the static addNumbers method. There is an alternative to setting the variables static. You can also send them with the method call. Alternatively yet again, you can create the addNumbers method inside the main.
3rd issue is that when you declared the variables getnumber1 and getnumber2 in the main, you declared them Double and not double. The difference? Double is the Double Object. double is the double simple-type. The Double.parseDouble(String) method returns a double (simple-type).
Here is the code for all 3 examples.
Declaring both method and variables static.
Code :
public class Experiment
{
static double getnumber1, getnumber2;
public static void main(String [] args)
{
String number1 = JOptionPane.showInputDialog(null, "Please enter your first number");
String number2 = JOptionPane.showInputDialog(null, "Please enter your second number");
getnumber1 = Double.parseDouble(number1);
getnumber2 = Double.parseDouble(number2);
JOptionPane.showMessageDialog(null, "The number's are " + getnumber1 + "and" + getnumber2);
JOptionPane.showMessageDialog(null, "The numbers added togethere are" + addNumbers() );
}
public static double addNumbers()
{
double total;
total = getnumber1 + getnumber2;
return total;
}
}
Sending variables with method
Code :
public class Experiment
{
public static void main(String [] args)
{
String number1 = JOptionPane.showInputDialog(null, "Please enter your first number");
String number2 = JOptionPane.showInputDialog(null, "Please enter your second number");
double getnumber1 = Double.parseDouble(number1);
double getnumber2 = Double.parseDouble(number2);
JOptionPane.showMessageDialog(null, "The number's are " + getnumber1 + "and" + getnumber2);
JOptionPane.showMessageDialog(null, "The numbers added togethere are" + addNumbers(getNumber1,getNumber2) );
}
public static double addNumbers(double v1, double v2)
{
double total;
total = v1+ v2;
return total;
}
}
Creating method in main
Code :
public class Experiment
{
public static void main(String [] args)
{
String number1 = JOptionPane.showInputDialog(null, "Please enter your first number");
String number2 = JOptionPane.showInputDialog(null, "Please enter your second number");
double getnumber1 = Double.parseDouble(number1);
double getnumber2 = Double.parseDouble(number2);
JOptionPane.showMessageDialog(null, "The number's are " + getnumber1 + "and" + getnumber2);
JOptionPane.showMessageDialog(null, "The numbers added togethere are" + addNumbers() );
public double addNumbers()
{
double total;
total = getnumber1 + getnumber2;
return total;
}
}
}
Also, at the risk of getting smeared, JOptionPane kicks the crap out of Scanner.
Even so, I always prefer making my own GUIs.
Re: Need help understanding method calls and such
Thanks for the explanations.
Re: Need help understanding method calls and such
The third one is incorrect. I cant remember how to properly create methods in the main. The other 2 are correct though.
Re: Need help understanding method calls and such
You aren't allowed to create methods inside of other methods (as far as I know).
You can create anonymous classes, though and these can have methods inside of them.
Code Java:
public class Test
{
public static void main(String[] args)
{
Runnable run = new Runnable()
{
@Override
public void run()
{
System.out.println("Hello world!");
}
};
new Thread(run).start();
}
}
Re: Need help understanding method calls and such
Thats probably what I was thinking of.