java - 调用静态方法时的类实例

标签 java class static

我想做的是创建一个可以找到两点之间的中点的方法。我还试图显示这些中点的象限。问题在于,在将坐标更改为中点之后和显示该点之前,它无法再次正确设置象限。这是我的代码:

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. 在中点计算中除以 2 而不是 2.0 是有问题的。

  2. 无论是在setX()、setY()还是setQ()中,正确处理a或b等于0的情况if a==0 or b==0 then q = ""

  3. 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));

关于java - 调用静态方法时的类实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19988314/

相关文章:

jquery - 使用 jQuery 将类添加到 li

c++ - 静态类数据成员的范围

java - 创建对象时可以初始化final变量吗?

java - 像这样使用继承是个坏主意吗

java - 如何从 JTextField 获取文本?

c++ - 在多个源文件中访问相同的类实例

python - 如何定义一个 super 强大的class风格的dict对象?

c# - 在 C# 中,属性可以应用于静态类、方法或属性吗?

c - 如果函数是静态的,为什么我的本地函数声明不起作用?

java - XML自动编辑器(基于XSD方案)