Method from other class and use of toString()

Popkiness :

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

}
hc_dev :

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.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=358469&siteId=1