calling method to a main in java

Jonathan Cook

I'm writing a program that calculates the hypotenuse of a triangle, and I'm supposed to call up a method into the main.

Is it better to have them in 2 separate files, or to have the method I'm calling up in the program I'm running?

In the program, I keep getting error messages about the last line of code, with the JOptionPane output.

What am I getting wrong?

import javax.swing.JOptionPane;
import java.util.Scanner;
public class A2
{   

     public static double Hypo(double a,double b,double c);
        double a,b,c;
        {
            hyp=((a*a)+(b*b));
            c=Math.sqrt(hyp);
        }

    int x,y;
    double c;
    String text1=JOptionPane.showInputDialog("How long is side A? ");
    int x=Integer.parseInt(text1);
    String text2=JOptionPanes.howInputDialog("How long is side B? ");
    int y=Integer.parseInt(text2);
    double c=A2.Hypo(x,y);
    JOptionPane.showMessageDialog(null, "The hypotenuse of the triangle is " +c);
    }
duffymo

This code has so many problems it's hard to know where to begin.

Here's some advice:

  1. Good names matter. You can and must do better than A2 for a class.
  2. Learn and follow the Sun Java coding standards.
  3. Style and readability matter. Learn a good code layout and stick to it.

Start with this. It runs and gives correct results:

import javax.swing.*;

/**
 * A2
 * @author Michael
 * @link https://stackoverflow.com/questions/30965862/calling-method-to-a-main-in-java
 * @since 6/21/2015 11:00 AM
 */
public class SimpleMathDemo {

    public static double hypotenuse(double a,double b) {
        return Math.sqrt(a*a+b*b);
    }

    public static void main(String[] args) {
        String text1= JOptionPane.showInputDialog("How long is side A? ");
        int x=Integer.parseInt(text1);
        String text2=JOptionPane.showInputDialog("How long is side B? ");
        int y=Integer.parseInt(text2);
        double c= SimpleMathDemo.hypotenuse(x,y);
        JOptionPane.showMessageDialog(null, "The hypotenuse of the triangle is " +c);
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related