简体   繁体   English

调用静态方法时的类实例

[英]Class Instances While Invoking Static Methods

What I'm trying to do is create a method that can find the midpoint between two points. 我正在尝试做的是创建一个可以找到两点之间的中点的方法。 Also I'm trying to have the quadrant of these midpoints displayed. 另外,我试图显示这些中点的象限。 The problem is that it doesn't correctly set the quadrant again after changing the coordinates to the midpoint and before displaying the point. 问题在于,在将坐标更改为中点之后以及在显示该点之前,它无法再次正确设置象限。 Here's my code: 这是我的代码:

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;   
    }            
}
  1. Dividing by 2 instead of 2.0 in your midpoint calculation is problematic. 在中点计算中除以2而不是2.0是有问题的。

  2. Whether in setX(), setY() or setQ(), correctly handle the condition where a or b equals zero with if a==0 or b==0 then q = "" 无论在setX(),setY()还是setQ()中, if a==0 or b==0 then q = "" ,正确处理a或b等于零的if a==0 or b==0 then q = ""

  3. In distancestat() should be distancestat()应该是

double d = Math.sqrt(Math.pow(other1.b-other2.b,2)+Math.pow(other1.a-other2.a,2)); // change is at the end - other2.a //更改已结束-other2.a

vice

double d = Math.sqrt(Math.pow(other1.b-other2.b,2)+Math.pow(other1.a-other1.b,2));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM