I am getting errors whenever I try to compile. Can someoneplease provide a error free code?/fix the error in this code.
public class Rectangle { } } } { } } } } } } } double x; double y; double width; double height; String color; public Rectangle(double x, double y, double height, double width, String color) { if(x < 0 || x > 1) { x = 0.0; if (y < 0 || y > 1) { if (width <0 || width > 1) (width = 1; } if (height <0 || height > 1) {height = 1;}| if (!color.equals("black") && !color.equals("red") !color.equals("green") && !color.equals("blue")) { color="red"; } y = 0.0; this.x = x; this.y = y; this.width = width; this.height = height; this.color= color; } public double getX() { return this.x; } public double getY() { return this.y; } public double getWidth() { return this.width; } public double getHeight() { return this.height; } public String getColor() { return this.color; } public void setX(double x) { if (x + this.width <= 1) { this.x = x; public void setY(double y) { if (y + this.height <= 1) { this.y = y; public void setWidth (double w) { if (w > 0 && x + w <= 1) { this.width = w; public void setHeight (double h) { if (h> 0 && y + h <= 1) { public void setColor(String c) { if (!c.equals("black") && !c.equals("red") !c.equals("green") && !c.equals("blue")) { return;} this.height = h; this.color= c; public static boolean isValid(double x, double y, double w, double h) { if (x < 0 || x > 1) { if (y < 0 || y > 1) { return false; if (w < 0 || w > 1) { return true; return false; public String toString() { if (h <0 || h > 1) { return false; if (!color.equals("black")&&!color.equals("red") !color.equals("green") && !color.equals("blue")) { return false; return false; String str = ""; str + "X= " + this.x + " "Y = " + this.y + ", str + str + "W = " + this.width + ", str == "H= " + this.height str + "C = " + this.color; return str;
} //yes } { } { } }} } public double computePerimeter() { return 2 * x + 2 *y; } public double computeArea() { return x*y; } public boolean equals (Rectangle rect) { if (rect == this) return true; if (rect == null) return false; if (rect.getClass() != this.getClass()) return false; Rectangle that = (Rectangle) rect; if (this.x != that.x) return false; if (this.y != that.y) return false; if (this.width != that.width) return false; if (this.height != that.height) return false; return true; public boolean containsPoint (Rectangle rect) { return (rect.x >= x) && (rect.width <= width) && (rect.y >= y) && (rect.height <= height); public boolean containsRectangle(Rectangle rect) if (this.x > rect.width || rect.x > this.width) return false; if (this.y < rect.height || rect.y < this.height) return false; return true; public boolean intersects (Rectangle that) return this.width >= that.x && this.height >= that.y && that.width >= this.x && that.height >= this.y;
!color.equals("green") && !color.equals("blue")) { 31: error: non-static variable color cannot be referenced from !color.equals("green") && !color.equals("blue")) { A A
public class Driver { public static void main(String[] args) { Rectangle rect = new Rectangle(0, 0, 0.5, 0.5, "Blue"); System.out.println("X: "+rectangle.getX()); System.out.println("Y: "+rectangle.getY()); System.out.println("Height: "+rectangle.getHeight()); System.out.println("Width: "+rectangle.getWidth()); System.out.println("Color: "+rectangle.getColor()); System.out.println("Perimeter :"+rectangle.computePerimeter()); System.out.println("Area :"+rectangle.computeArea()); }}
I am getting errors whenever I try to compile. Can someone please provide a error free code?/fix the error in this code.
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am