其他类的方法和toString()的使用

时间:2019-12-17 19:10:13

标签: java methods double tostring calculation

我正在尝试使此代码正常工作,在MAIN上从用户那里收到2分(x,y)和(x1,y1),并使用其他类中的方法来获取计算结果。

也很高兴收到有关toString的说明。

import java.util.Scanner;
public class Test{
    public static void main(String[]args)
    {
        Scanner scan = new Scanner(System.in);
        double x , y , x1 , y1;
        Pytaguras Triangle = new Pytaguras();

        System.out.println("Hello , this program is using Pytagoras formula" +
        " on the 4 point that you enter");
        System.out.println("Please enter 4 points to calculate the distance");
        x = scan.nextDouble();
        y = scan.nextDouble();
        x1 = scan.nextDouble();
        y1 = scan.nextDouble();
        Triangle.setDouble( x, y,  x1, y1);
        System.out.println("the distance between 2 points is :" + Triangle.calculate() );

    }
}

public class Pytaguras
{
    private double x, y, x1 ,y1 ;
    public void setDouble(double _x, double _y, double _x1, double _y1)
    {   _x=x;
        _y=y;
        _x1=x1;
        _y1=y1;}

    public double calculate(double _x , double _y , double _x1 , double _y1 )
    {   double s;
         s = Math.sqrt((_x-_y)*(_x-_y)+(_x1-_y1)*(_x1-_y1));
        return s;
    }

}

2 个答案:

答案 0 :(得分:2)

如果您想通过类toString()的方法Triangle接收距离计算,则只需像方法calculate一样实现:

使用toString的可行解决方案

您的课程如下所示:

public class Pytaguras {
    private double x, y, x1 ,y1 ;
    public void setDouble(double _x, double _y, double _x1, double _y1) {
        _x=x;
        _y=y;
        _x1=x1;
        _y1=y1;
    }

    public static double calculate(double _x , double _y , double _x1 , double _y1 ) {
        return Math.sqrt((_x-_y)*(_x-_y)+(_x1-_y1)*(_x1-_y1));
    }

    public String toString() {
        return "Distance is " + Math.sqrt((x - y)*(x -y)+(x1 - y1)*(x1 - y1));
    }
}

可能出了什么问题?

当您尝试像这样实现toString()时:

// code omitted for clarity

public double calculate(double _x , double _y , double _x1 , double _y1 ) {   
  double s; // variable declared locally, thus can be used only inside this method
  s = Math.sqrt((_x-_y)*(_x-_y)+(_x1-_y1)*(_x1-_y1));
  return s;
}

// code omitted for clarity

public String toString() {
  return s; // will give compiler-error, since symbol/variable "s" is not visible here, but only inside method calculate
}

// code omitted for clarity

重构=优化代码

在实用程序方法上使用static您的calculation方法不依赖于类内部声明的任何变量(x,y,x1,y1),而仅依赖于参数。因此它是独立的,可以设为static。因此,它可以成为一种实用程序方法,可以像Pytaguras.calculate(...)这样从外部调用。参见Java Tutorial on static

使用构造函数初始化变量:您可以使用构造函数直接初始化2点。因此,您无需同时调用new Pytaguras()setDouble(x1, y1, x2, y2)。您可以简单地致电new Pytaguras(x1, y1, x2, y2)。参见Java Tutorial on constructor

答案 1 :(得分:0)

您可以简单地通过类构造函数分配值。

public class Triangle {
    private double x, y, x1 , y1;

    public Triangle(double new_x, double new_y, double new_x1, double new_y1) {
        x = new_x;
        y = new_y;
        x1 = new_x1;
        y1 = new_y1;
    }

    public double calculate() {   
        return Math.sqrt((x-y)*(x-y)+(x1-y1)*(x1-y1));
    }

}

然后在您的主菜单上

public static void main(String[]args) {
        Scanner scan = new Scanner(System.in);
        double x , y , x1 , y1;
        Pytaguras Triangle = new Pytaguras();

        System.out.println("Hello , this program is using Pytagoras formula" +
        " on the 4 point that you enter");
        System.out.println("Please enter 4 points to calculate the distance");
        x = scan.nextDouble();
        y = scan.nextDouble();
        x1 = scan.nextDouble();
        y1 = scan.nextDouble();
        Triangle triangle = new Triangle(x, y, x1, y1);
        System.out.println("the distance between 2 points is :" + triangle.calculate());

}
相关问题