Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Design a class called rectangle. The class contains: Two double data fields name

ID: 3633283 • Letter: D

Question

Design a class called 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 for both width and height.

A no-arg constructor that creates a default traingle.

A method named getArea() that returns the area of this rectangle.

A method named getPerimeter() that returns the perimeter

Implement the class. Write a test program that creates two rectangles objects- one with width 4 and a height 40 and the other with a width of 3.5 and height 35.9. Display the width, height,area and perimeter of each rectangle in this order

Explanation / Answer

class rectangle{ double width=1; double height=1; String color="white"; rectangle(){ } rectangle(double w,double h){ this.width=w; this.height=h; } public void setHeight(double height){ this.height=height; } public double getHeight(){ return height; } public void setWidth(double width){ this.width=width; } public double getWidth(){ return width; } public void setColor(String color){ this.color=color; } public String getColor(){ return color; } public double getArea(){ double area=width*height; return area; } public double getPerimeter(){ double p=width+height; double perimeter=2*p; return perimeter; } public static void main(String[] args){ rectangle rect1=new rectangle(); System.out.println("Width: "+rect1.getWidth()); System.out.println("Height: "+rect1.getHeight()); System.out.println("Color: "+rect1.getColor()); System.out.println("Area: "+rect1.getArea()); System.out.println("Perimeter: "+rect1.getPerimeter()); System.out.println(); rectangle rect2=new rectangle(5,10); System.out.println("Width: "+rect2.getWidth()); System.out.println("Height: "+rect2.getHeight()); rect2.setColor("Red"); System.out.println("Color: "+rect2.getColor()); System.out.println("Area: "+rect2.getArea()); System.out.println("Perimeter: "+rect2.getPerimeter()); } }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote