File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Calling on another method... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Calling on another method..." Watch "Calling on another method..." New topic
Author

Calling on another method...

Tim Schmidt
Greenhorn

Joined: Mar 05, 2009
Posts: 6
I'm attempting to create a program that has a method calculating the hypotenuse of a right triangle accepting two doubles for the sides and returns a double. I am then trying to integrate that method into a program that accepts the two sides from the keyboard then prints the value of the hypotenuse on the command line.
my question is how do i call upon the method,

public double hyp(double side1,double side2)
{
return (Math.sqrt(side1*side1 + side2*side2));
}

to then accept the input from the keyboard?
Monu Tripathi
Rancher

Joined: Oct 12, 2008
Posts: 1369
    
    1

Tim Schmidt wrote:..
my question is how do i call upon the method


Try this.

Please make a note of following...

This is a very basic question and you should have been able to find an answer in any book.
We'd appreciate if you, when asking a question tell others what have you tried first

Thanks.


[List of FAQs] | [Android FAQ] | [Samuh Varta]
Tim Schmidt
Greenhorn

Joined: Mar 05, 2009
Posts: 6
Heres what I am trying right now:

import java.util.Scanner;

public class Hypot
{

public void determineHyp()
{
Scanner keyboard = new Scanner(System.in);

System.out.println("Enter Side 1");
double side1 = keyboard.nextDouble();

System.out.println("Enter Side 2 ");
double side2 = keyboard.nextDouble();

double result = hyp( side1, side2 );

System.out.println( "Hypotenuse is: " + result );
}

public double hyp(double s1, double s2)
{
return (Math.sqrt(s1*s1 + s2*s2));
}

}


It compiles but when i run it it gets an error:

----jGRASP exec: java Hypot

java.lang.NoSuchMethodError: main
Exception in thread "main"
----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
Tim Schmidt
Greenhorn

Joined: Mar 05, 2009
Posts: 6


Try this.



Yes, I have been reading how to do that for the past hour, my teacher confused me i think.
Monu Tripathi
Rancher

Joined: Oct 12, 2008
Posts: 1369
    
    1

To run or execute a java program JVM needs an entry point to your program, which is a method called "main". Also, main method should have following signature:

public static void main(String a[])

Try running a simple application first, like a HelloWorld program. A lot of things will become clearer.

Use Code Tags when pasting code. The code becomes easier to read.

amitabh mehra
Ranch Hand

Joined: Dec 05, 2006
Posts: 98
You are missing the entry point - main() method... if you have no idea about it, perhaps you would like to have a look here
Also use code tags to post your code. It makes reading easier.
Tim Schmidt
Greenhorn

Joined: Mar 05, 2009
Posts: 6
Oh okay, I've used that before i just didn't realize it needed to be in everything... I've tried implementing it but it's not working. Do i just tack that in the beginning then call on the determine hyp?

Heres what I'm trying now:



but it's still not working. do i need to create a test program to run the program? or is there a way to keep it all together?
Monu Tripathi
Rancher

Joined: Oct 12, 2008
Posts: 1369
    
    1

Oh okay, I've used that before i just didn't realize it needed to be in everything...


A class can exist without main method(most of the API classes that you import do not contain a main method). When a class does not have a main method, it cannot be executed by typing java <Classname>. Without a main method a class is just a piece of code that can be used by other classes.

When you want to execute a set of instructions directly, you require an entry point. A program execution starts at main. Code inside main method controls the flow of program by instantiating other classes(creating objects of these classes) and invoking methods on these instances.

You need to read up on this. Grab a copy of "Head First Java" or "Thinking in Java" from Library!

but it's still not working. do i need to create a test program to run the program? or is there a way to keep it all together?

As regards your program, if you want to execute it, add the keyword "static" to your hyp and determineHyp functions.



You cannot execute an instance method without creating an instance of the class. You can call static methods from one static method though.



 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Calling on another method...