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

*MAKE SURE IT PROMPTS THE USER* Create a Shape class 1. Using the abstraction co

ID: 3679436 • Letter: #

Question

*MAKE SURE IT PROMPTS THE USER*

Create a Shape class

1. Using the abstraction concept of OOP, define variables and methods in the Shape class that are common to the Circle and Rectangle classes (see UML Class Diagram section)

A. Encapsulate the variables and methods appropriately

1. Public – All objects in the program have access

2. Protected – Only objects that inherit the class have access

3. Private – Only the object has access

2. Update the Circle and Rectangle classes to inherit the Shape class Note: In Java, a class inherits another class using the “extends” keyword.

3. Remove the common variables and methods from the Circle and Rectangle classes

A. Override methods as needed Note: Overriding a method is necessary when an object behaves differently from the inherited object’s behavior.

1. Use “@Override” to indicate the method is overriding an inherited method

4. Create a UML Class Diagram of the Circle, Rectangle and Shape classes

A. Show that the Circle and Rectangle classes inherit the Shape class Create a Java program that performs the following tasks: 1. Implement the classes defined in the UML Class Diagram created in step 4 above

2. Prompt the user for the following inputs (doubles)

A. The radius (length) of a circle

B. The length and width of a rectangle 3

. Create the object instances of the Circle and Rectangle classes

A. Pass the radius (length) as the parameter for the Circle class constructor

1. “Circle circle = new Circle(radius);”

B. Pass the length and width as the parameters for the Rectangle class constructor

1. “Rectangle rectangle = new Rectangle(length, width);”

4. Perform the calculations

A. Calculate Area 1. “circle.calcArea();”

B. Calculate Perimeter 1. “rectangle.calcPerimeter();”

5. Display the results of the Circle and Rectangle calculations (see Example Output section) A. Display decimal values to three decimal places ?

Example Output

Homework 6

Ima Java Programmer

Enter the radius of a cicle [1 to 10]: 4

Enter the length of a rectangle [1 to 10]: 5

Enter the width of a rectangle [1 to 10]: 8

Circle:

Area: 50.265

Perimeter: 25.133

Rectangle:

Area: 40.000

Perimeter: 26.000

UML Class Diagram Rectangle Circle area double area: double length double length double perimeter double perimeter double width double width double Circle (length double) void Rectangle(length double, width double) void calcArea void calcArea( void calcPerimeter(): void calcPerimeter():void getArea double getArea0 double getPerimeter(): double getPerimeter(): double setLength length double) void setLength (length double) void setWidth width double) void

Explanation / Answer

TestShapeDemo.java


import java.util.Scanner;

abstract class Shape {

public abstract double area();//abstract class contains public and abstract methods

public abstract double perimeter();
}

class Rectangle extends Shape {

final double breadth, length; //sides

Rectangle(double breadth, double length) {
this.breadth = breadth;
this.length = length;
}

public double area() {
// Area = breadth * length
return breadth * length;
}

public double perimeter() {
// Perimeter = 2(length + breadth)
return 2 * (breadth + length);
}
}

class Circle extends Shape {

double radius;
double pi = Math.PI;

public Circle(double radius) {
this.radius = radius;
}

public double area() {
// Area of a circle = r^2
return pi * Math.pow(radius, 2);
}

public double perimeter() {
// Perimeter of a circle = 2r
return 2 * pi * radius;
}
}

public class TestShapeDemo {//driver program

public static void main(String[] args) {

// Rectangle test
Scanner sc = new Scanner(System.in);//keyboard inputting
// Circle test
System.out.println("enter radius of a circle");
double radius = sc.nextDouble();
System.out.println("enter length of the rectangle");
double length = sc.nextDouble();
System.out.println("enter breadthof the rectangle");
double breadth = sc.nextDouble();
Shape circle = new Circle(radius);
System.out.println("Circle radius: " + radius
+ " Area of a circle: " + circle.area()
+ " Perimeter of a circle: " + circle.perimeter() + " ");

Shape rectangle = new Rectangle(breadth, length);
System.out.println("Rectangle breadth: " + breadth + " and length: " + length
+ " area of a rectangle: " + rectangle.area()
+ " perimeter of a rectangle: " + rectangle.perimeter() + " ");

}
}

output

enter radius of a circle
4
enter length of the rectangle
5
enter breadthof the rectangle
8
Circle radius: 4.0
Area of a circle: 50.26548245743669
Perimeter of a circle: 25.132741228718345

Rectangle breadth: 8.0 and length: 5.0
area of a rectangle: 40.0
perimeter of a rectangle: 26.0

BUILD SUCCESSFUL (total time: 13 seconds)