Instructions Create a Shape class 1. Using the abstraction concept of OOP, defin
ID: 3679247 • Letter: I
Question
Instructions 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 Circle Rectangle 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 getArea double getPerimeter(): double getPerimeter(): double setLength (length double) void setLength (length double) void setWidth (width double) voidExplanation / Answer
CircleShape.java
public class CircleShape extends NewShape{
private double r=0;
private double a=0;
private double p=0;
public CircleShape(String n, double x){
super (n);
r = x;
a = this.calcArea(x);
p = this.calcPerimeter(x);
}
private double calcArea(double x){
return Math.PI*Math.sqrt(x);}
private double calcPerimeter(double x){
return 2*Math.PI*x;}
public double getArea(){
return this.a;}
public double getPerimeter(){
return this.p;}
public void printShape(){
System.out.println(" Shape: " + super.getName());
System.out.printf(" Radius: %.1f ", this.r);
System.out.printf(" Area: %.2f ", this.a);
System.out.printf(" Perimeter: %.2f ", this.p);
}
}
RecangleShape.java
public class RectangleShape extends NewShape{
/*
* Instance Variables
*/
private double _width= 0.0;
private double _height=0.0;
private double _area=0.0;
private double _perimeter=0.0;
/*
* Constructor
*/
public RectangleShape(String name, double height, double width){
super (name);
_height = height;
_width = width;
_area = this.calcArea(height, width);
_perimeter = this.calcPerimeter(height, width);
}
/*
* Methods
*/
private double calcArea(double height, double width){
return height*width;
}
private double calcPerimeter(double height, double width){
return (height*2)+(width*2);
}
public double getArea(){
return this._area;
}
public double getPerimeter(){
return this._perimeter;
}
public void printShape(){
System.out.println(" Shape: " + super.getName());
System.out.printf(" Height: %.1f", this._height);
System.out.printf(" Width: %.1f ", this._width);
System.out.printf(" Area: %.2f ", this._area);
System.out.printf(" Perimeter: %.2f ", this._perimeter);
}
}
main.java
public class main {
public static void main(String[] args) {
RectangleShape rectangle1 = new RectangleShape("Rectangle1", 3.0, 4.0);
rectangle1.printShape();
RectangleShape rectangle2 = new RectangleShape("Rectangle2", 4.0, 3.0);
rectangle2.printShape();
CircleShape circle1 = new CircleShape("Circle1", 4.0);
circle1.printShape();
CircleShape circle2 = new CircleShape("Circle2", 3.0);
circle2.printShape();
}
}
NewShape.java
public abstract class NewShape {
private String shapeName;
public NewShape(String name) {
shapeName = name;
}
public abstract double getArea();
public abstract double getPerimeter();
public final String getName() {
return shapeName;
}
public void printShape() {
System.out.println(" Shape: "+shapeName);
System.out.println("");
}
}
sample output
Shape: Rectangle1
Height: 3.0 Width: 4.0
Area: 12.00
Perimeter: 14.00
Shape: Rectangle2
Height: 4.0 Width: 3.0
Area: 12.00
Perimeter: 14.00
Shape: Circle1
Radius: 4.0
Area: 6.28
Perimeter: 25.13
Shape: Circle2
Radius: 3.0
Area: 5.44
Perimeter: 18.85