Write a class named Rectangle, which represents different rectangles. The private data fields are width, length, area and color. Use double for width and length and String for color. The methods are getWidth(), getLenght(), getColor() and findArea(). Use a class variable for color.
Include the above class in a program that uses it.
public Rectangle(){ height = 1; width = 1; color = "White"; }
public void setHeight(double high){ height = high; }
public void setWidth(double wid){ width = wid; }
public void setColor(String col){ color = col; }
public double getArea(){ return height*width; }
public double getPerimeter(){ return 2*(height + width); }
public void getColor(){ System.out.println("Color is: " + color +" "); return; }
} public class RectangleDemo { public static void main(String[] args){ Rectangle box1 = new Rectangle(); Rectangle box2 = new Rectangle(4, 40); Rectangle box3 = new Rectangle(3.5, 35.9);
System.out.println("The perimeter of the first box is: " + box1.getPerimeter() + " "); System.out.println("The perimeter of the second box is: " + box2.getPerimeter() + " "); System.out.println("The perimeter of the third box is: " + box3.getPerimeter() + " ");
System.out.println("The area of the first box is: " + box1.getArea() + " "); System.out.println("The area of the second box is: " + box2.getArea() + " "); System.out.println("The area of the third box is: " + box3.getArea() + " "); } }
Comments
Leave a comment