Question

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

How do I assign the integer values to my setters above? I accepted coordinate input from a user as a String ex. 3,4. Then, I split it with comma and store it into an array. After that, I am parsing it to change its value to integer rather than string, because I need to do math with the numbers. I am not sure how to assign its integer value to the setters or if that is that way I should be doing it.

Était-ce utile?

La solution

One problem with the code you gave is in the split method. At the end, you make assignments to all your variables, but you use double x1 = ..., so you create a new variable called x1 which is not the same as this.x1. You then never use this variable. That makes me think that you meant to do this:

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

This time, there is no double at the beginning of the assingments.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top