Design a class named Rectangle to represent a rectangle. The class contains:
-
Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 (one) for both width and height.
-
A string data field named color that specifies the color of a rectangle. Hypothetically, assume that all rectangles have the same color. The default color is white.
-
A no-arg constructor that creates a default rectangle.
-
A constructor that creates a rectangle with the specified width and height.
-
A method named getArea() that returns the area of this rectangle.
-
A method named getPerimeter() that returns the perimeter.
2 comments:
public class class_reg {
public static void main(String[] args) {
// TODO, add your application code
System.out.println("Hello World!");
rectangle myrectangle = new rectangle();
System.out.println(myrectangle.width + " " + myrectangle.length + " " + myrectangle.color);
System.out.println(myrectangle.getArea());
System.out.println(myrectangle.getPerimeter());
rectangle yourrectangle = new rectangle();
System.out.println("The area of the rectangle width is " + yourrectangle.width + ",the length is " + yourrectangle.length + "and the color is " + yourrectangle.color);
System.out.println("The area of the rectangle is " + yourrectangle.getArea());
System.out.println("The perimeter of your rectangle is " + yourrectangle.getPerimeter());
}
}
class rectangle {
double width, length;
String color;
rectangle() { // constructor for default rectangle
width =1;
length =1;
color="white";
}
rectangle(double newwidth, double newlength , String newcolor) { // constructor for specified width and height.
width =newwidth;
length = newlength;
color = newcolor;
}
double getArea() // getArea()method
{
return width*length;
}
double getPerimeter()
{
return (width*2)+(length*2);
}
} //end rectangle class
TQ Azri
Post a Comment