有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java实现OOP中的等式优先度

我想翻译一段java代码。我只在一个类中实现了OOP,但我想使用两个类实现OOP 原始代码仅在一个类中工作 然而,当我创建两个类equationMain和equationData时,我在metod getter中得到了一个错误 当我尝试返回m(斜率)的值时,public double calculateSlope() “将方法类型更改为字符串” 我不明白,因为我想重新计算一个双精度值

enter image description here 我将感谢任何帮助与此代码

只有一个类中的原始代码:

package equation;

import javax.swing.JOptionPane;

public class Equation {

    public static void main(String[] args) {
        // TODO Auto-generated method stub


        //variables for coordenates, slope, etc.
        double x1, x2, y1, y2, m, b;

        String firstcoordinate = JOptionPane.showInputDialog("Introduce coordinate 1 x1,y1 split by ,: ");

        String[] values = firstcoordinate.split(",");

        x1 = Double.parseDouble(values[0]);
        y1 = Double.parseDouble(values[1]);

        String secondcoordinate = JOptionPane.showInputDialog("Introduce coordinate 2 x2,y2 split by ,: ");


        values = secondcoordinate.split(",");

        x2 = Double.parseDouble(values[0]);
        y2 = Double.parseDouble(values[1]);

         if ((x1 == x2) || ((x2 - x1) == 0))
         {
             System.out.println("There is not equation!!");

         }
         else
         {
             m = (y2 - y1) / (x2 - x1);
             //y1-mx1=b

             b = (y1 - (m * x1));

             System.out.println("Coordinates entered: ");
             System.out.println(" *-Coordinate 1: (" + x1 + ", " + y1 + ")");
             System.out.println(" *-Coordinate 2: (" + x2 + ", " + y2 + ")");
             System.out.println("The slope m is = " + m );
             System.out.println("The equation is y = " + m + "x + " + b);
         }

    }

}

然而,我的新项目是:

//main class equationMain
package equationPOO;

import javax.swing.JOptionPane;

public class equationMain {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        //variables for coordenates, slope, etc.
        double x1, x2, y1, y2, m, b;

        String firstcoordinate = JOptionPane.showInputDialog("Introduce coordinate 1 x1,y1 split by ,: ");
        String[] values = firstcoordinate.split(",");

        x1 = Double.parseDouble(values[0]);
        y1 = Double.parseDouble(values[1]);

        String secondcoordinate = JOptionPane.showInputDialog("Introduce coordinate 2 x2,y2 split by ,: ");


        values = secondcoordinate.split(",");

        x2 = Double.parseDouble(values[0]);
        y2 = Double.parseDouble(values[1]);

        equationData eq = new equationData(x1,x2,y1,y2);



        if((x1 == x2) || ((x2 - x1) == 0))
        {
            eq.message();
        }
        else
        {
            eq.calculateSlope();

        }

    }

}

我想遵循程序的第二个类是 包装方程

public class equationData {


    //constructor
    public equationData(double x1, double x2, double y1, double y2)
    {
        this.x1= x1;
        this.x2= x2;
        this.y1= y1;
        this.y2= y2;


    }

    //getter for calculating slope m
    public double calculateSlope()
    {
        double m,b;

        m = (y2 - y1) / (x2 - x1);

        //y1-mx1=b
        b = (y1 - (m * x1));
        System.out.println("Coordinates entered: ");
        System.out.println(" *-Coordinate 1: (" + x1 + ", " + y1 + ")");
        System.out.println(" *-Coordinate 2: (" + x2 + ", " + y2 + ")");
        System.out.println("The slope m is = " + m );
        System.out.println("The equation is y = " + m + "x + " + b);


        return "The slope m is = " + m; 

    }

    //getter send a message is ((x1 == x2) || ((x2 - x1) == 0))
    public String message()
    {
        return "There is not equation!!";
    }




    private double x1,x2,y1,y2;


}

共 (0) 个答案