Peter Basolo
Peter Basolo

Reputation: 49

Java cant find methods

When I try to use my succesfully compiled code in another class it says error:Symbol not found on my max and compareTo methods.

i could really use this help i am mostly new to coding an this is my first post here so please pardon me if i blatantly broke the rules.

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);
  }
}

Upvotes: 0

Views: 202

Answers (1)

arcy
arcy

Reputation: 13123

The reference to compareTo() is on an object of type TriangleFromSimpleGeometricObject; in that class there needs to be a declaration, within its braces, like

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

The method max() is evidently supposed to be in the class you posted, so within it should be the declaration

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

(corrected to be static) It needs to be static since it is being called from a static method without reference to an instance. We don't know its return type.

These things evidently aren't the case, since the compiler is saying it cannot find them. There are various reasons for that; to know what they are, we would have to see all the code for both this class and for TriangleFromSimpleGeometricObject.

--- edited, now that we have more information

The method max(int) is a static method on GeometricObject; in order to invoke it, you either have to preface the method call with the class name, or be within that class. So change your line

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

to

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

and that should fix the compilation error there. Does that fix all the problems?

Let me just say in passing that max is a particularly bad name for that method (and I say that without knowing whether you or someone else wrote it). It does not calculate a maximum, doesn't have anything to do with maximums, so its name does not reflect what it does do. But fixing that won't fix the compilation error, even assuming you are able to change it, and it isn't what you asked about.

Upvotes: 1

Related Questions