JAVA. I keep getting errors in my code. I don't understand why.Can someone please print out my code and show the code withouterrors.
public class Rectangle{
double x; double y; double width; double height; String color;
public Rectangle(double x, double y, doubleheight, double width, String color) { if(x < 0 || x > 1) { x = 0.0;} if (y < 0 || y > 1) { y = 0.0;} 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";
}
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) { this.height = h;}}
public void setColor(String c) { if (!c.equals("black")&& !c.equals("red") !c.equals("green")&& !c.equals("blue")) {{ return;} this.color = c;} public static boolean isValid(double x, doubley, double w, double h){ if (x < 0 || x > 1){ returnfalse;} if (y < 0 || y > 1){ returnfalse;} if (w < 0 || w > 1){ returnfalse;} if (h < 0 || h > 1){ returnfalse;} if(!color.equals("black")&&!color.equals("red") !color.equals("green")&& !color.equals("blue")) {
returnfalse;}
return true;
} public String toString() { String str = ""; str += "X = " + this.x + ","; str += "Y = " + this.y + ","; str += "W = " + this.width +", "; str += "H = " + this.height +", "; str += "C = " +this.color; return str;} public double computePerimeter() { return 2 * x + 2 * y; } public double computeArea() { return x * y; }
//yes public boolean equals(Rectangle rect){ if (rect == this) return true; if (rect == null) return false; if (rect.getClass() != this.getClass()) returnfalse; Rectangle that = (Rectangle) rect; if (this.x != that.x) return false; if (this.y != that.y) return false; if (this.width != that.width) returnfalse; if (this.height != that.height) returnfalse; return true;
} public boolean containsPoint(Rectangle rect){ return (rect.x >= x)&& (rect.width <= width) && (rect.y >=y) && (rect.height <= height); }
public boolean containsRectangle(Rectanglerect){ 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;}
/////////////
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());
}}
JAVA. I keep getting errors in my code. I don't understand why. Can someone please print out my code and show the code w
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am