只需提供一些有关覆盖和比较的指导

时间:2018-10-31 04:20:36

标签: java

所以我不确定我的输出应该是什么,因为我遇到了障碍。我知道我应该在某个地方使用Override。我在cirlce1类中为findPerimeter进行了尝试。不会在那里工作。共3个部分

首先是这个GeometricObject1抽象类。

cc = ifelse(m1 < m2[,m2_to_compare], 1, 0)
# depending on your seed:
sapply(1:10, function(colm) rowSums(cc[,m2_to_compare == colm]))
#      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
# [1,]    3    1   19    8    9   11   17    2   12    16
# [2,]    2   19   14   10   10   11    9    1    0    14
# [3,]   16   16   17    7    5   20    1   16    2    17
# [4,]   13    2    0   11   20   11    6    5   12     2
# [5,]    0   10    2    1   10   17    3   14    5     7
# [6,]   11    7   17    9   20   18   18   16    7     4
# [7,]   15    3    5    5    8    5    3    3    9     1
# [8,]    0   18    5    8    9   15    9   17    0    20
# [9,]   15   14    5    1    5    0    6   17   19     6
#[10,]    6    1    4   10   11   12    0    9    7     5

下一个是Circle1类。我知道我需要填写更多的代码体,但是我对如何格式化compareto块有点迷茫(我正在上网,正在使用我的书)。

abstract class GeometricObject1 {

protected String color;
protected double weight;

    // Default construct  
protected GeometricObject1() {     
        color = "white";
         weight = 1.0; 
    }


    // Construct a geometric object
protected GeometricObject1(String color, double weight) {     
    this.color = color;     
    this.weight = weight;   
} 



// Getter method for color   
public String getColor() {     
    return color;   

} 

// Setter method for color 
public void setColor(String color) {    
    this.color = color;   
} 

// Getter method for weight
public double getWeight() {     
    return weight;   
} //getWeight

// Setter method for weight   
 public void setWeight(double weight) {     
    this.weight = weight;   
} //setWeight


//Write code here for Abstract method for area
public abstract double getArea(); 


//Write code here Abstract method for perimeter

public abstract double getPerimeter();

}

最后一部分是我的testcircle类,并且2/3编译良好。是我的Circle1课给我带来了麻烦。

Circle1.java:1:错误:Circle1不是抽象的,并且不覆盖GeometricObject1中的抽象方法getPerimeter() 是我编译circle1类时遇到的错误

class Circle1 extends GeometricObject1 implements Comparable {
protected double radius;
final double pi = Math.PI;

    // Default constructor   
public Circle1() {     
    this(1.0, "white", 1.0);
}   

    // Construct circle with specified radius 
public Circle1(double radius) {     
    super("white", 1.0);
    this.radius = radius;
}

    // Construct a circle with specified radius, weight, and color
public Circle1(double radius, String color, double weight) {     
super(color, weight);
     this.radius = radius;   
} 

    // Getter method for radius
public double getRadius() {     
    return radius;
}    
    // Setter method for radius
public void setRadius(double radius) {
    this.radius = radius;
}   
    // Implement the findArea method defined in GeometricObject   
public double findArea() { 
return pi * Math.pow(radius, 2);   
// Write your code here   
} 

    // Implement the findPerimeter method defined in GeometricObject

public double findPerimeter() { 
return 2 * pi * radius;   

} 
    // Override the equals() method defined in the Object class
public boolean equals(Circle1 circle) {    
// Write your code here 
} 
    // Override the toString() method defined in the Object class
public String toString() {    
// Write your code here   
} 
    // Implement the compareTo method defined in Comparable   
public int compareTo(Object o) { 
    // Write your code here   
} 
public static Circle1 max(Comparable o1, Comparable o2) {    
// Write your code here   
    }
}

2 个答案:

答案 0 :(得分:0)

您必须在Circle1类中覆盖getPerimeter()getArea()

您是否将getPerimeter()输入为findPerimeter()?因为getPerimeter()函数必须被覆盖。

答案 1 :(得分:0)

您必须在继承类中定义未定义的抽象类方法。在您的情况下,public double getArea()public double getPerimeter()

    class Circle1 extends GeometricObject1 implements Comparable {
        protected double radius;
        final double pi = Math.PI;

        // Default constructor   
        public Circle1() {     
            this(1.0, "white", 1.0);
        }   

        // Construct circle with specified radius 
        public Circle1(double radius) {     
            super("white", 1.0);
            this.radius = radius;
        }

        // Construct a circle with specified radius, weight, and color
        public Circle1(double radius, String color, double weight) {     
            super(color, weight);
            this.radius = radius;   
        } 

        // Getter method for radius
        public double getRadius() {     
            return radius;
        }    
        // Setter method for radius
        public void setRadius(double radius) {
            this.radius = radius;
        }   

        // Override the equals() method defined in the Object class
        public boolean equals(Circle1 circle) {    
            // Write your code here 
            return false;
        } 
        // Override the toString() method defined in the Object class
        public String toString() {    
            // Write your code here   
            return "";
        } 
        // Implement the compareTo method defined in Comparable   
        public int compareTo(Object o) { 
            // Write your code here   
            return 0;

        } 
        public static Circle1 max(Comparable o1, Comparable o2) {    
            // Write your code here   
            return o1.compareTo(o2) < 0 ? (Circle1) o2 : (Circle1) o1;
        }

        @Override
        public double getArea() {
            return pi * Math.pow(radius, 2); 
        }

        @Override
        public double getPerimeter() {
            return 2 * pi * radius;  
        }
    }