
Design a class named rectangle to represent a rectangle. The Class contain:

Java Implement Rectangle class
class rectangle{
double width=1;
double height=1;
String color="white";
rectangle(){
}
rectangle(double w,double h){
this.width=w;
this.height=h;
}
public void setHeight(double height){
this.height=height;
}
public double getHeight(){
return height;
}
public void setWidth(double width){
this.width=width;
}
public double getWidth(){
return width;
}
public void setColor(String color){
this.color=color;
}
public String getColor(){
return color;
}
public double getArea(){
double area=width*height;
return area;
}
public double getPerimeter(){
double p=width+height;
double perimeter=2*p;
return perimeter;
}
public static void main(String[] args){
rectangle rect1=new rectangle();
System.out.println("Width: "+rect1.getWidth());
System.out.println("Height: "+rect1.getHeight());
System.out.println("Color: "+rect1.getColor());
System.out.println("Area: "+rect1.getArea());
System.out.println("Perimeter: "+rect1.getPerimeter());
System.out.println();
rectangle rect2=new rectangle(5,10);
System.out.println("Width: "+rect2.getWidth());
System.out.println("Height: "+rect2.getHeight());
rect2.setColor("Red");
System.out.println("Color: "+rect2.getColor());
System.out.println("Area: "+rect2.getArea());
System.out.println("Perimeter: "+rect2.getPerimeter());
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.