• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Calling on another method...

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Rancher
Posts: 1369
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Tim Schmidt
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Try this.



Yes, I have been reading how to do that for the past hour, my teacher confused me i think.
 
Monu Tripathi
Rancher
Posts: 1369
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1369
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.



 
reply
    Bookmark Topic Watch Topic
  • New Topic