Solved

calling a method in Java

Posted on 2014-02-20
6
335 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
What is the compatible hibernate version for spring 4.3.1 10 60
Updating Java 9 87
allStar challenge 1 41
Problem to start Neon 20 51
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

708 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

13 Experts available now in Live!

Get 1:1 Help Now