简体   繁体   中英

Method from other class and use of toString()

I am trying to make this code work, receive 2 points (x, y) & (x1,y1) from user on MAIN and use a method from another class and get the calculation.

Also it would be very nice to receive an explanation about 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;
    }

}

If you would like to receive the distance-calculation via method toString() of your class Triangle , you just need to implement it same like method calculate :

Possible solution using toString

Your class would look like this:

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

What probably went wrong?

When you tried to implement toString() like this:

// 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

Refactoring = optimizing your code

use static on utility-method : Your calculation method does not depend on any variables ( x,y,x1,y1 ) declared inside the class, but on parameters only. So it is independent and can be made static . Thus would make it a utility-method that can be called from outside like Pytaguras.calculate(...) . See Java Tutorial on static .

use constructor for initialization of variables: Your 2 points can be directly initialized by using a constructor. Thus you do not need to call both new Pytaguras() and setDouble(x1, y1, x2, y2) . You could simply call new Pytaguras(x1, y1, x2, y2) . See Java Tutorial on constructor .

You can simply assign the values through the class constructor.

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

}

And then on your main:

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

}

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