calling method to a main in java

660 views Asked by At

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);
    }
3

There are 3 answers

0
duffymo On BEST ANSWER

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);
    }
}
0
Madhan On

Code analysis

 public class A2 {

    //Missing method body no return values ..Is this an abstact function?/
    public static double Hypo(double a, double b, double c);
    double a, b, c;

    //Whats this part doing hanging in the middle??
    {
    //where is the variable declaration of hyp
        hyp = ((a * a) + (b * b));
        c = Math.sqrt(hyp);
    }

    int x, y; 
    //variable c is already  declared
    double c;
    String text1 = JOptionPane.showInputDialog("How long is side A? ");
     //variable x is already  declared
    int x = Integer.parseInt(text1);
    //JOptionPane not JOptionPanes
    String text2 = JOptionPanes.howInputDialog("How long is side B? ");
    //variable y is already  declared
    int y = Integer.parseInt(text2);
     //variable c is already  declared and Hypo function has three arguements in the declaration
    double c = A2.Hypo(x, y);
//wont work because the whole code is buggy
    JOptionPane.showMessageDialog (null, "The hypotenuse of the triangle is " +c);
    }
}
2
hs2345 On

To elaborate more:

import javax.swing.JOptionPane;

public class A2 {

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


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

}

You need to choose a correct return type, whether it be void, int, double, etc, and each method with a return type should return a value with the set type.

You also always need at least one main method in a program. There can be multiple in different classes.

You will need to use a more specific variable names, and follow oracle convention for brackets {}.

DO not declare a variable twice as in:

int x, y;
int x = 1; // WRONG
x = 1; // Correct