Java无法找到方法

时间:2015-11-08 03:52:44

标签: java

当我尝试在另一个类中使用我成功编译的代码时,它会显示错误:在我的max和compareTo方法中找不到符号。

我真的可以使用这个帮助我主要是新编码这是我的第一篇文章所以请原谅我,如果我公然违反规则。

public class assignment13_5 {
  public static void main(String[] args){
    TriangleFromSimpleGeometricObject x = new TriangleFromSimpleGeometricObject(4, 4, 4, "blue", true);
    TriangleFromSimpleGeometricObject y = new TriangleFromSimpleGeometricObject(4, 4, 4, "white", false);
    int i = x.compareTo(y);
    //this code is from my text book
    System.out.print(max(i));
  }
}

/**I got the base code from http://www.cs.armstrong.edu/liang/intro9e/html/GeometricObject.html?
then modified it for the comparable method*/

public abstract class GeometricObject {
  private String color = "white";
  private boolean filled;
  private java.util.Date dateCreated;

  /** Construct a default geometric object */
  protected GeometricObject() {
    dateCreated = new java.util.Date();
  }

  /** Construct a geometric object with color and filled value */
  protected GeometricObject(String color, boolean filled) {
    dateCreated = new java.util.Date();
    this.color = color;
    this.filled = filled;
  }

  /** Return color */
  public String getColor() {
    return color;
  }

  /** Set a new color */
  public void setColor(String color) {
    this.color = color;
  }

  /** Return filled. Since filled is boolean,
   *  the get method is named isFilled */
  public boolean isFilled() {
    return filled;
  }

  /** Set a new filled */
  public void setFilled(boolean filled) {
    this.filled = filled;
  }

  /** Get dateCreated */
  public java.util.Date getDateCreated() {
    return dateCreated;
  }

  @Override
  public String toString() {
    return "created on " + dateCreated + "\ncolor: " + color +
      " and filled: " + filled;
  }

  /** Abstract method getArea */
  public abstract double getArea();

  /** Abstract method getPerimeter */
  public abstract double getPerimeter();


public static String max(int x){
    if (x == 1)
      return "The first is bigger.";
    else if (x == -1)
      return "The second is bigger.";
    else
      return "They are equal.";
  }
public abstract class ComparableGeometricObject extends GeometricObject
  implements Comparable<ComparableGeometricObject> {

  //@Override // Implement the compareTo method defined in Comparable
  public abstract int compareTo(GeometricObject o) {
    if (getArea() > o.getArea())
      return 1;
    else if (getArea() < o.getArea())
      return -1;
    else
      return 0;
  }

  @Override // Implement the toString method in GeometricObject
  public String toString() {
    return super.toString() + " Area: " + getArea();
  }
}
  }

public class TriangleFromSimpleGeometricObject extends SimpleGeometricObject {

    private double s1 = 1;
    private double s2 = 1;
    private double s3 = 1;
    private double side;

    public TriangleFromSimpleGeometricObject(){
    }





    public TriangleFromSimpleGeometricObject(double s1, double s2, double s3, String color, boolean filled) throws IllegalTriangleException{

            this.s1 = s1;
            this.s2 = s2;
            this.s3 = s3;
            setColor(color);
            setFilled(filled);

          if (s1 + s2 <= s3){
            if (s1 < s2){
              if (s1 < s3)
                side = s1;
            }
            else if (s2 < s1){
              if (s2 < s3)
                side = s2;
            }
            else
              side = s3;
            throw new IllegalTriangleException("N/A");
          }
        }

        public double getArea(){
          double s = (s1 + s2 + s3) / 2;

          double area = Math.sqrt(s * (s - s1) * ( s - s2) * ( s - s3) );

          return area;
        }

        public double getPerimeter(){
          double perimeter = s1 + s2 + s3;;

          return perimeter;
        }

        public void setSide(int side, int newSide) {
          if (side == 1)
            s1 = newSide;

          if (side == 2)
            s2 = newSide;

          if (side == 3)
            s3 = newSide;
        }

        public void getSide(int side){
          if (side == 1)
            System.out.println("The first side is: " + s1);

          if (side == 2)
            System.out.println("The first side is: " + s2);

          if (side == 3)
            System.out.println("The first side is: " + s3);
        }

        public String toString(){
          return "Triangle: side1 = " + s1 + " side2 = " + s2 + " side3 = " + s3 + " color = " + getColor() + " is filled = " + isFilled();
        }
    }

public class RectangleFromSimpleGeometricObject
  extends SimpleGeometricObject {

  private double width;
  private double height;

  public RectangleFromSimpleGeometricObject() {
  }

  public RectangleFromSimpleGeometricObject(
    double width, double height) {
    this.width = width;
    this.height = height;
  }

  public RectangleFromSimpleGeometricObject(
    double width, double height, String color, boolean filled) {
    this.width = width;
    this.height = height;
    setColor(color);
    setFilled(filled);
  }

  /** Return width */
  public double getWidth() {
    return width;
  }

  /** Set a new width */
  public void setWidth(double width) {
    this.width = width;
  }

  /** Return height */
  public double getHeight() {
    return height;
  }

  /** Set a new height */
  public void setHeight(double height) {
    this.height = height;
  }

  /** Return area */
  public double getArea() {
    return width * height;
  }

  /** Return perimeter */
  public double getPerimeter() {
    return 2 * (width + height);
  }
}

1 个答案:

答案 0 :(得分:1)

compareTo()的引用位于TriangleFromSimpleGeometricObject类型的对象上;在那个课程中,需要在其括号内声明一个声明,如

public int compareTo(TriangleFromSimpleGeometricObject y) {
  // with code in here
}

方法max()显然应该在你发布的课程中,所以在其中应该是声明

private static ? max(int i) {  // could also be public, or even protected
   // with code in here
}

(更正为静态)它需要是静态的,因为它是从静态方法调用而不引用实例。我们不知道它的返回类型。

这些事情显然不是这样,因为编译器说它无法找到它们。这有多种原因;要知道它们是什么,我们必须看到这个类和TriangleFromSimpleGeometricObject的所有代码。

---已编辑,现在我们有更多信息

方法max(int)是GeometricObject上的静态方法;为了调用它,您必须在方法调用前加上类名,或者在该类中。所以改变你的行

System.out.println(max(i));

System.out.println(GeometricObject.max(i));

那应该修复那里的编译错误。这会解决所有问题吗?

让我顺便说一下,max对于那种方法来说是一个特别糟糕的名字(我说不知道你或别人是否写过它)。它不计算最大值,与最大值没有任何关系,因此它的名称不反映它的作用。但修复它不会修复编译错误,即使你能够改变它,也不是你所要求的。