Answer to Question #9858 in Java | JSP | JSF for rectangle

Question #9858
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.
1
Expert's answer
2012-05-22T10:54:00-0400
public class RectangleDemo {


private class Rectangle {


private double height;
private double width;
private String color;

public Rectangle(double wid, double high){
height = high;
width = wid;
}

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);

String Color = "Red";

box1.setColor(Color);
box2.setColor(Color);
box3.setColor(Color);

box1.getColor();
box2.getColor();
box3.getColor();

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() + "
");
}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS