需要帮助将“已解析”值分配给setter

时间:2013-10-16 23:56:09

标签: java setter

/****Setters and Getter****/

public double getx1() {
return x1;//x1 getter
}

public void setx1(double x1) {
this.x1 = x1;//x1 setter
}

public double getx2() {
return x2;//x2 getter
}

public void setx2(double x2) {
this.x2 = x2;//x2 setter
}

public double getx3() {
return x3;//x3 getter
}

public void setx3(double x3) {
this.x3 = x3;//x3 setter
}

public double gety1() {
return y1;//y1 getter
}

public void sety1(double y1) {
this.y1 = y1;//y1 setter
}

public double gety2() {
return y2;//y2 getter
}

public void sety2(double y2) {
this.y2 = y2;//y2 setter
}

public double gety3() {
return y3;//y3 getter
}

public void sety3(double y3) {
this.y3 = y3;//y3 setter
}

/****Splitting existing Array made up by coordinates by comma******/

public void split() {
    String[] Coord1 = point1.split(",");
    String[] Coord2 = point2.split(",");
    String[] Coord3 = point3.split(",");


/****Changing String inputs of the coordinates to integers******/

    double x1 = Integer.parseInt(Coord1[0]);
    double x2 = Integer.parseInt(Coord2[0]);
    double x3 = Integer.parseInt(Coord3[0]);
    double y1 = Integer.parseInt(Coord1[1]);
    double y2 = Integer.parseInt(Coord2[1]);
    double y3 = Integer.parseInt(Coord3[1]);
}


/***MY perimeter calculations***/
public double perimeter(){
    side1 = Math.sqrt(((y1-x1)*(y1-x1))+((y2-x2)*(y2-x2)));
    side2 = Math.sqrt(((y2-x2)*(y2-x2))+((y3-x3)*(y3-x3)));
    side3 = Math.sqrt(((y3-x3)*(y3-x3))+((y1-x1)*(y1-x1)));

    double perimeter = side1+side2+side3;//perimeter formula

    return perimeter;

如何将整数值分配给上面的setter?我接受来自用户的坐标输入作为String ex。 3,4。然后,我用逗号分割它并将其存储到数组中。之后,我正在解析它以将其值更改为整数而不是字符串,因为我需要对数字进行数学运算。我不知道如何将其整数值分配给setter,或者如果那样我应该这样做。

1 个答案:

答案 0 :(得分:0)

您提供的代码存在的一个问题是split方法。最后,您为所有变量进行了分配,但是使用了double x1 = ...,因此您创建了一个名为x1的新变量,该变量与this.x1不同。然后你永远不会使用这个变量。这让我觉得你打算这样做:

x1 = Integer.parseInt(Coord1[0]);
x2 = Integer.parseInt(Coord2[0]);
x3 = Integer.parseInt(Coord3[0]);
y1 = Integer.parseInt(Coord1[1]);
y2 = Integer.parseInt(Coord2[1]);
y3 = Integer.parseInt(Coord3[1]);

这一次,在分配开始时没有double