Write a class named Rectangle to represent rectangles The da

Write a class named Rectangle to represent rectangles. The data fields are width, height, and color. Use double for width and height, and String for color. Suppose that all the rectangles are the same color. You need to provide the accessor methods for the properties and a find Area () method for computing the area of the rectangle. The outline of the class is given as follows: public class Rectangle {private double width = 1; private double height = 1; private String color = \"white\"; public Rectangle () {} public Rectangle (double width, double height, String color){} public double get Width () {} public void set Width (double width) {} public double get Height (){} public String getColor (){} public void setColor (String color) {} public double find Area () {} public String toString () {}} Write a client program(test class) to test the class Rectangle, in the client program, create two Rectangle objects. Assign any width and height to the two objects. Assign the first object the color red, and the second, yellow. Display the properties of both objects and find their areas. Write a class named Fan to model fans. The properties are speed, on, radius, and color. You need to provide the accessor methods for the properties, and the toString method for retuning a string consisting of all the string values of all the properties in this class. Suppose the fan has three fixed speeds. Use constants 1, 2, and 3 to denote slow, medium, and fast speeds. The outline of the class is given as follows: public class Fan {final int SLOW = 1; public final int MEDIUM = 2; public final int FAST = 3; private int speed = SLOW; private boolean on = false; private double radius = 5; private String color = \"white\"; public Fan () {} public Fan (int speed, boolean on, double radius, String color){} public int getSpeed () {} public void setSpeed (int speed) {} public boolean isOn () {} public void setOn () {} public void setOn () {} public double getRadius () {} public void setRadius (double radius) {} public String getColor (){} public void setColor (String color) {} public String toString (){}} Write a client program(test application) to test the Fan class. In the client program, create a Fan object. Assign maximum speed, radius 10, color yellow, and turn it on. Display the object by invoking its toString method.

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

 Write a class named Rectangle to represent rectangles. The data fields are width, height, and color. Use double for width and height, and String for color. Sup
 Write a class named Rectangle to represent rectangles. The data fields are width, height, and color. Use double for width and height, and String for color. Sup
 Write a class named Rectangle to represent rectangles. The data fields are width, height, and color. Use double for width and height, and String for color. Sup

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site