Solved

calling a method in Java

Posted on 2014-02-20
6
338 Views
Last Modified: 2014-02-20
I'm using Eclipse IDE for Java Developer.

So in my java main method I set a variable like this:


sum = number1 + number2;

Then  I use this which displays my sum in a window.

// display the results
JOptionPane.showMessageDialog( null, "The sum is " + sum, "Results", JOptionPane.PLAIN_MESSAGE );

So this works correctly.



Now I'm trying to do something similar but I want to have the sum calculation in a method called sumNumbers.  So if my method looks like this how would I call this method using JOptionPane

public void sumNumbers(double number1, double number2) {
      double sum;
            
      sum = number1 + number2;
            
}


I tried something like this, but i don't think it's correct:

// display the results
JOptionPane.showMessageDialog( null, "The sum is " + sumNumbers(number1, number2), "Results", JOptionPane.PLAIN_MESSAGE );


Any know how I call a method with parameters using JOptionPane.showMessageDialog?
0
Comment
Question by:maqskywalker
  • 3
  • 2
6 Comments
 
LVL 37

Assisted Solution

by:zzynx
zzynx earned 300 total points
ID: 39872789
>> "The sum is " + sumNumbers(number1, number2)
If you want to use it like this, your method should return the result:

public double sumNumbers(double number1, double number2) {
       return number1 + number2;
}
0
 
LVL 23

Accepted Solution

by:
Michael74 earned 200 total points
ID: 39872808
Further to zzynx correct answer, the void keyword in your method signifies that the method does not return any value. As you can see in zzynx's response the method specifies that it returns a double value.
 
Have a look at this tutorial for more information
http://docs.oracle.com/javase/tutorial/java/javaOO/returnvalue.html
0
 
LVL 1

Author Comment

by:maqskywalker
ID: 39872851
My question is on calling a method using JOptionPane.showMessageDialog

So then assuming my method looks like this:

public double sumNumbers(double number1, double number2) {
       return number1 + number2;
}




Is this how I would call it ?

JOptionPane.showMessageDialog( null, "The sum is " + sumNumbers(number1, number2), "Results", JOptionPane.PLAIN_MESSAGE );
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 37

Expert Comment

by:zzynx
ID: 39872854
Yes!
Just try it.
0
 
LVL 1

Author Comment

by:maqskywalker
ID: 39872860
thanks guys. your help worked.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 39872928
You're welcome.
Thanx 4 axxepting
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

948 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now