我尝试做的是创建一种可以找到两点之间中点的方法。此外,我还试图显示这些中点的象限。问题是在将坐标更改为中点并且在显示点之前,它没有再次正确设置象限。这是我的代码:
public class OrderedPairTest {
public static void main(String[] args) {
OrderedPair op1 = new OrderedPair(0,0);
OrderedPair op2 = new OrderedPair(0,0);
OrderedPair op3 = new OrderedPair(0,0);
System.out.println(op1.a);
System.out.println(op1.b);
System.out.println(op1.setX(2));
System.out.println(op1.setY(3));
System.out.println(op2.moveX(-1));
System.out.println(op2.moveY(-3));
System.out.println(op3.moveXY(-4,4));
System.out.println(op1.printOP());
System.out.println(op2.printOP());
System.out.println(op3.printOP());
System.out.println(op1.distance(op2));
System.out.println(op1.distance(op3));
System.out.println("The distance between the two points is: "+op1.distancestat(op2,op3));
System.out.println("The midpoint is at: "+op1.midpoint(op2)+op2.q);
System.out.println("The midpoint is at: "+op2.midpointstat(op1,op3)+op2.q);
}
}
public class OrderedPair {
int a,b;
public OrderedPair(int x,int y){
a = x;
b = y;
}
String q = "";
String msg = "";
String error = "";
String result = "";
public int getX() { //Gets the value of X
result = "X is "+a+".";
return a;
}
public int getY() { //Gets the value of Y
result = "Y is "+b+".";
return b;
}
public String setX(int x) { //Sets the value of X
a = x;
result = "X has been set to "+a;
if (b != 0) setQ();
else;
return result;
}
public String setY(int y) { //Sets the value of Y
b = y;
result = "Y has been set to "+b;
if (a != 0) setQ();
else;
return result;
}
public String toString() { //Turns the variables into a string
msg = ("("+a+","+b+") Q"+q);
result = "Values have been converted into string.";
return msg;
}
public String moveX(int amt) { //Moves X a predefined amount of units
a+=amt;
result = "X has been moved "+amt+" units.";
if ((b != 0) && (a != 0)) setQ();
else;
return result;
}
public String moveY(int amt) { //Moves Y a predefined amount of units
b+=amt;
result = "Y has been moved "+amt+" units.";
if ((b != 0) && (a != 0)) setQ();
else;
return result;
}
public String moveXY(int amt1, int amt2){ //Moves X and Y a predefined amount of units
a+=amt1;
b+=amt2;
result = "X has been moved "+amt1+" units.Y has been moved "+amt2+" units.";
if ((b != 0) && (a != 0)) setQ();
else;
return result;
}
public String distance(OrderedPair other) { //Gets the distance between two pairs of choordinates
double d = Math.sqrt(Math.pow(other.b-this.b,2)+Math.pow(other.a-this.a,2));
result = "The distance between the two points is: "+d;
return result;
}
public static double distancestat(OrderedPair other1, OrderedPair other2) { //Gets the distance between two pairs of static choordinates
double d = Math.sqrt(Math.pow(other1.b-other2.b,2)+Math.pow(other1.a-other1.b,2));
return d;
}
public OrderedPair midpoint(OrderedPair other) { //Gets the midpoint between two pairs of choordinates
return new OrderedPair((this.getX()+other.getX())/2,(this.getY()+other.getY())/2);
}
public static OrderedPair midpointstat(OrderedPair other1, OrderedPair other2) { //Gets the midpoint between two pairs of static choordinates
return new OrderedPair((other1.getX()+other2.getX())/2,(other1.getY()+other2.getY())/2);
}
public String printOP() { //Calls toString() then prints the string set by toString()
toString();
if (q!= "") return msg;
else error = "Q has not been set.";
return error;
}
private String setQ() { //Sets the quadrant based on the signs of the choordinates
if ((a!=0)&&(b!=0)){
if (a > 0){
if (b > 0) q = "I";
else q = "IV";
}
else {
if (b > 0) q = "II";
else q = "III";
}
result = "The quadrant has been set.";
return result;
}
else error = "X and/or Y are zero, please assign the variables a nonzero value.";
return error;
}
}
答案 0 :(得分:1)
在中点计算中除以2而不是2.0是有问题的。
无论是在setX(),setY()还是setQ()中,使用if a==0 or b==0 then q = ""
正确处理a或b等于零的条件
distancestat()
应
double d = Math.sqrt(Math.pow(other1.b-other2.b,2)+Math.pow(other1.a-other2.a,2));
//更改结束 - other2.a
副
double d = Math.sqrt(Math.pow(other1.b-other2.b,2)+Math.pow(other1.a-other1.b,2));