user2510694
user2510694

Reputation: 15

Need a lot of help making a square class

I'm trying to make a square class using Dr. Java. I have taken most of the code from a rectangle class but it has left me with a mess. I'm currently a beginner when it comes to Java so I'm really lost right now. If you have any correction or tips on how to correct my square class please let me know. Thanks

    package graphics2;

/**
 * A class that represents a square with given origin, width, and height.
 */
public class Square extends Graphics {
  // The private width and height of this square.
  private double width = 0;
  private double height = 0;  

  /**
   * Constructs a square with given origin, width, and height.
   */
  public Square(Point origin, double side) {
    super(origin, side, side);
    setOrigin(new Point(0, 0));
    width = height = side;
}

  /**
   * Constructs a square with given width and height at origin (0, 0).
   */
  public Square(double side) {
    setOrigin(new Point(0, 0));
    width = height = side;
  }
  /**
   * Returns the square's side of this square.
   */
  public double  getSide() {return width;}

  /**
   * Returns the width coordinate of this square.
   */
  public double getWidth() {return width; }

  /**
   * Returns the height coordinate of this square.
   */
  public double getHeight() {return height; }

  /**
   * Returns the area of this square.
   */
  public double area() {
    return width * height;
  }
}

Also here is the error I'm receiving:

    1 error found:
File: C:\Users\GreatOne\Desktop\06Labs-\graphics2\Square.java  [line: 15]
Error: The constructor graphics2.Graphics(graphics2.Point, double, double) is undefined

Upvotes: 0

Views: 1104

Answers (2)

aksh1t
aksh1t

Reputation: 5448

Your code explained according to the errors :

Errors

  • Error 1 [line: 15] :Constructor undefined. What you want is super(origin, side);, instead of super(origin, side, side); I assume. Either that, or you should define the constructor with super(origin, side, side);

  • Error 2,3,4,5 [line: 17, 18, 26, 27]: You are using variables w and h inside the constructors, without having them as method parameters. Since this is a square you want, change width = w and height = h to width = side and height = side in both the constructors. As you are passing the variable side in the method parameter, this will not create a problem.

  • Last error [line: 32] : You are returning the variable side in the getter method public double getSide(). Since side is not a variable of the class, it shows an error. Change that to return width or return height. Since this is a square, both will be equal.

Upvotes: 0

William Morrison
William Morrison

Reputation: 11006

Chris, don't extend Graphics. That's very wrong. Don't extend anything.

Your constructors need correction, they're not matching up with how you're trying to create Squares.

Also you are missing multiple variables.

I would suggest rather than have someone on stackoverflow sort out this mess, you open up your textbook or read some tutorials online. I can fix this in minutes for you, but it won't help you as I'm skeptical you'll understand how to use it.

Please study up. You'll be better for it.

Upvotes: 5

Related Questions