不兼容类型错误在命令提示符中。

huangapple 未分类评论46阅读模式
英文:

Incompatible types error in Command Prompt

问题

class Point
{
    private int x;
    private int y;

    // Constructor
    public Point()
    {
        // nothing
    }

    // Second constructor
    public Point(int x, int y)
    {
        this.x = x;
        this.y = y;
    }

    // Copy constructor
    public Point(Point p)
    {
        this(p.x, p.y);
    }

    private double distance(Point p)
    {
        int dX = this.x - p.x;
        int dY = this.y - p.y;
        double result = Math.sqrt(dX * dX + dY * dY);
        return result;
    }

    public double getDistance(Point p)
    {
        return distance(p); // This line has been corrected.
    }

    // Getter
    public int getX()
    {
        return x;
    }

    public int getY()
    {
        return y;
    }

    // Setter
    public void set(int x, int y)
    {
        this.x = x;
        this.y = y;
    }

    public String toString()
    {
        return String.format("Given Point (%d, %d)", x, y);
    }
}
英文:

I have a work about calculating the distances between two points. It consist of Point Class, Line Class as well as Main Class. The following is my Point class. After working on the private double distance(Point p) method, I am unable to return p at the public double getDistance(Point p) method. I run code on Command Prompt and it shows error: incompatible types: Point cannot be converted to double. Please advice.

class Point
{
    private int x;
    private int y;

//Constructor
public Point()
{
	//nothing
}

//Second constructor
public Point (int x, int y)
{
	this.x = x;
	this.y = y;
}

//Copy constructor
public Point (Point p)
{
	this (p.x, p.y);
}

private double distance(Point p)
{
	int dX = this.x - p.x;
	int dY = this.y - p.y;
	double result = Math.sqrt(dX * dX + dY * dY);
	return result;
}

public double getDistance(Point p)
{	     
	return p;
}

//getter
public int getX()
{
	return x;
}

public int getY()
{
	return y;
}

//setter
public void set(int x, int y)
{
	this.x = x;
	this.y = y;
}

public String toString ()
{
	return String.format ("Given Point (%d, %d)", x, y);
}

}

答案1

得分: 0

你的参数是对象 Point p,并将其作为 double 类型返回。

在你的代码块中,你声明了返回对象 Point p 而不是 double 数据类型。

public double getDistance(Point p) {
   return p; 
}

如果你只想计算对象之间的距离,请使用你的 distance() 方法。该方法已经将计算出的距离作为 double 类型返回。

private double distance(Point p) {
    int dX = this.x - p.x;
    int dY = this.y - p.y;
    double result = Math.sqrt(dX * dX + dY * dY);
    return result;
}
英文:

You have the object Point p as your parameter and return it as a double.

In your block of code you're stating a return of the Object Point p and not a double data type.

public double getDistance(Point p) {
   return p; 
}

If you're just trying to calculate the distance of the object, use your distance() method. This method already returns the distance calculated as a double.

private double distance(Point p) {
    int dX = this.x - p.x;
    int dY = this.y - p.y;
    double result = Math.sqrt(dX * dX + dY * dY);
    return result;
}

huangapple
  • 本文由 发表于 2020年4月8日 21:24:31
  • 转载请务必保留本文链接:https://java.coder-hub.com/61101793.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定