I'm a student working on a project in Java where I need to practice using inheritance by extending the default Rectangle class; the child class MyRectangle
needs to have constructors that accept parameters that describe the rectangle's border and fill colors as well as its dimensions. I also need to include getter/setter methods for the border/color attributes, a method that will rotate a rectangle by 180 degrees, and methods to calculate and return the area and perimeter of a rectangle. All of the required methods/fields are described in greater detail in the documentation file:///C:/Users/sweis/Downloads/MyRectangle.html
here, and the output should end up looking like this: https://i.stack.imgur.com/KP7LB.png
I've written some code, but I've gotten a lot of error messages referencing the portion of the program where I actually define the MyRectangle
class and set up constructors, including "Non-static variable cannot be referenced from a static context"
and "error: cannot find symbol"
. I'm pretty new at Java so I'm really not sure, but I'm afraid I'm fundamentally misunderstanding something about inheritance, and I'm struggling to resolve these errors on my own. I would really appreciate it if someone could look at what I've written so far and help me understand what I'm doing wrong so I can better understand this concept. Here are the portions that are causing problems:
import java.awt.Rectangle;
class Main {
public static void main(String[] args) {
MyRectangle mr1 = new MyRectangle(0, 0, 4, 2, "blue", "red", true);
MyRectangle mr2 = new MyRectangle(5, 5, 10, 3, "green");
MyRectangle mr3 = new MyRectangle();
System.out.println(mr1 + " " + "area = " + mr1.area() + " perimeter = " + mr1.perimeter());
System.out.println(mr2 + " " + "area = " + mr2.area() + " perimeter = " + mr2.perimeter());
System.out.println(mr3 + " " + "area = " + mr3.area() + " perimeter = " + mr3.perimeter());
System.out.println(mr4 + " " + "area = " + mr4.area() + " perimeter = " + mr4.perimeter());
}
public static class MyRectangle extends Rectangle {
public static int width, height;
public String color, borderColor;
public boolean border;
public MyRectangle() {
super();
}
public MyRectangle(int x, int y, int w, int h, String c) {
width = w;
height = h;
color = c;
}
public MyRectangle(int x, int y, int w, int h, String c, String bc, boolean b) {
borderColor = bc;
border = b;
}
public int area() {
return (width * height);
}
public int perimeter() {
return ((width * 2) + (height * 2));
}
public void setBorder(boolean newBorder) {
border = newBorder;
}
}
}
}
If you have any questions or need me to clarify anything I'll definitely do so. Thank you!