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

Write the following classes in Java with detailed comments: A circle class that

ID: 3732561 • Letter: W

Question

Write the following classes in Java with detailed comments:

A circle class that has a field to represent the radius (as a double). The class should have a method to set the radius and get the radius. The class should have an empty constructor to initialize the radius to 1, and a non-empty constructor to initialize the radius to a given value. The class should be encapsulated.

A rectangle class that has fields to represent the width and height (as doubles). The class should have methods to: set the height, get the height, set the width, get the width, and a method to set both the width and height. The class should have a non-empty constructor to initialize the fields to given values. The class should be encapsulated.

Write a class called ShapeCalculator. The class should have two methods named clacArea. Both should return doubles. One should take a circle object as a parameter and return the area of that circle. The other should take a rectangle object and return the area of that rectangle.

Write a main class. In the main method, create 3 circle objects with different radii and 2 rectangles with different widths and heights. Create a ShapeCalculator object. Call the ShapeCalculator’s area method on each circle and rectangle. Print the areas of each.

In the problem above, how does Java know which method to run? What is this called?

Explanation / Answer

public class Circle { private double radius; public Circle(double radius) { this.radius = radius; } public Circle() { this.radius = 1; } public double getRadius() { return radius; } public void setRadius(double radius) { this.radius = radius; } }