Solved

calling a method in Java

Posted on 2014-02-20
6
339 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This video teaches viewers about errors in exception handling.

776 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