Write a class named Rectangle to represent rectangles The da
Solution
public class rectangle {
double width;
double height;
String color=\"\";
rectangle(double width,double height,String color){
setwidth(width);
setheight(height);
setcolor(color);
}
public void setcolor(String color) {
this.color=color;
}
public void setheight(double height) {
this.height=height;
}
public void setwidth(double width) {
this.width=width;
}
public String getcolor(){
return color;
}
public double getwidth(){
return width;
}
public double getheight(){
return height;
}
public String tostring(){
return \"width is:\"+width+\"\\theight is: \"+height+\"\\tcolor is:\"+color;
}
public double findArea(){
double area=height*width;
return area;
}
}
main class:
public class maind1 {
public static void main(String[] args) {
rectangle r1=new rectangle(12.1,3.3,\"red\");
double area= r1.findArea();
System.out.println(r1.tostring());
System.out.println(area);
rectangle r2=new rectangle(3.5,7.4,\"yellow\");
area= r2.findArea();
System.out.println(r2.tostring());
System.out.println(\"area is\"+area);
}
}
output:
width is:12.1 height is: 3.3 color is:red
39.93
width is:3.5 height is: 7.4 color is:yellow
area is25.900000000000002
2nd program:
public class fan {
final int slow=1;
final int medium=2;
final int fast=3;
private int speed=slow;
private boolean on=false;
private double radius=5;
private String color=\"red\";
fan(){
}
fan(int speed,boolean on,double radius,String color){
seton(on);
setradius(radius);
setcolor(color);
setspeed(speed);
}
public void setspeed(int speed) {
this.speed=speed;
}
public int getspeed(){
return speed;
}
public double getradius() {
return radius;
}
public String getcolor() {
return color;
}
public boolean geton() {
return on;
}
public void setcolor(String color) {
this.color=color;
}
public void setradius(double radius) {
this.radius=radius;
}
public void seton(boolean on) {
this.on=on;
}
public String tostring(){
return \"speed is:\"+speed+\"\\tcondition:\"+on+\"\\tradius :\"+radius+\"\\tcolor:\"+color;
}
}
public class maind1 {
public static void main(String[] args) {
fan f1=new fan(3,true,10,\"yellow\");
System.out.println(f1.tostring());
}
}
output:
speed is:3 condition:true radius :10.0 color:yellow


