简体   繁体   中英

calling method to a main in java

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM