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

Create a class with a name of Rectangle. The class should have two instance fiel

ID: 3742781 • Letter: C

Question

Create a class with a name of Rectangle. The class should have two instance fields that are of type double named width and height.   Make these fields publically accessible. Also include a method named getArea() that returns the area (width * height) of the rectangle (return type double). Also include a method called getPerimeter that returns the perimeter ((width + height) * 2) of the rectangle (return type double).

Create another class called Control that will have the public static void main(String[] args) method. Within that method create an instance of Rectangle. Set the values of width and height for the instance just constructed to 4 and 5 respectively. Print out the area and perimeter of the rectangle. Run the Control class.

Programming to use is Java

Explanation / Answer

class Rectangle { public double width, height; public double getArea() { return width * height; } public double getPerimeter() { return ((width+height) * 2); } } public class Control { public static void main(String[] args) { Rectangle rectangle = new Rectangle(); rectangle.width = 4; rectangle.height = 5; System.out.println("Area is " + rectangle.getArea()); System.out.println("Perimeter is " + rectangle.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