Inheritance and extends Overridden methods Abstract class and methods (in Shape
ID: 3730659 • Letter: I
Question
Inheritance and extends
Overridden methods
Abstract class and methods (in Shape parent class)
Inherited method (toString() and output())
super method
Create an application that, given certain parameters for a shape, will return the shape's area and perimeter in meters. This application should be extensible. There should be a generic shape class that performs the work of printing the area and perimeter values. Create the Shape generic class and only one specific shape from the list below that extends the shape class. The main method for the project should come from the specific extended class, not Shape.
Create a new project.
Create an abstract Shape class. This class will only be a model for other shapes to come.
Create a field (attribute) called type and a getter called getType(). This field will hold the type of the shapes when new, concrete shapes are created.
Create three abstract getters for area, perimeter and parameters. There is no need to create fields for these values.
Create a default constructor and a constructor that accepts the shape type as a parameter.
Create an abstract requestInput() method with return type void.
Create an toString() method that displays the following information:
Details for <the shape type goes here>
with <the parameter string goes here>:
The area is <the area value goes here> meters.
The perimeter is <the perimeter value goes here> meters.
Replace the values in the angle braces with calls to getter methods. Create an output() method that displays the toString() information, either in a JOptionPane or using System.out.println().
Create one, and only one, of the shapes from the table below or create one of your own. The new type class must inherit from the Shape class. Use the value Math.PI for . There is a Math.tan() method that will compute tangent.
Type
Input / Parameters
Area Calculation
Perimeter Calculation
Create the input parameters as attributes with appropriate types and create getters and setters for each, implementing the getters and setters from Shape. These should be standard getters and setters. DO NOT request input from the user in the getters and setters. That will be accomplished in the next step in the requestInput() method.
Implement requestInput() to ask for the input parameters, overriding Shape's requestInput() method. The getParameterString() method creates a String of input parameters and their corresponding values. Call the getter methods for each of the input parameters to print the value.
Implement the getters for area and perimeter.
DO NOT implement the output method in the child class. Let the class inherit this method and the toString() method from the Shape class. Since the getters are defined in the Shape class and overridden in the child shape, the correct value will display for the shape created.
Create a default constructor that calls the Shape class's constructor to set the type. Use the super method.
Create a main method that creates the child shape object, calls the requestInput() and output methods. Ask the user if they want to try again and loop. Make sure this is the main method selected when creating the jar file.
As required for all assignments from now on
validate the proper functionality using the example answers below and review the Concepts and Ideas checklist
format the code
add a comment at the top of the file that reads @ author and your name
build the javadocs
Verify that the jar file contains source and class files and the MANIFEST.MF file contains a Main-Class. See the Resources section for Configuring Eclipse to view Jar files.
Run the application from the jar file, either from the command line using java -jar jarfilename or from Eclipse. See the Resources section for Configuring Eclipse to run Jar files.
submit the jar file in the Assignment section of BlackBoard under View/Complete Assignment for this assignment.
Example:
The results in the following examples do not have to match exactly, though they should be accurate through two places to the right of the decimal. Only one concrete shape implementation is required for this assignment, however, you are welcome to implement more.
Circle
Please enter a radius in meters: 3
Details for circle
with radius 3.0:
The area is 28.274333882308138 meters.
The perimeter is 18.84955592153876 meters.
Rectangle
Please enter a width: 10
Please enter a height: 20
Details for rectangle
with width: 10.0 height: 20.0:
The area is 200.0 meters.
The perimeter is 60.0 meters.
RegularPolygon
Please enter number of sides: 6
Please enter side length: 1
Details for polygon
with sides: 6.0 length: 1.0:
The area is 2.598076211353316 meters.
The perimeter is 6.0 meters.
Type
Input / Parameters
Area Calculation
Perimeter Calculation
Circle radius · radius · radius 2 · · radius Rectangle width, height width · height 2 · (width + height) RegularPolygon sides, length (sides · length · length) / (4 · tan( / sides)) sides · lengthExplanation / Answer
Hello, I have a solution for you. Implemented everything as per the requirements. Defined following things in this answer.
// Shape.java
public abstract class Shape {
//type attribute
private String type;
/**
* default constructor
*/
public Shape() {
type = "";
}
/**
* constructor with one argument
* @param type - shape type
*/
public Shape(String type) {
this.type = type;
}
/**
* getter method for the type
*/
public String getType() {
return type;
}
/**
* method prototype for finding the area
* @returns the area of the shape
*/
public abstract double getArea();
/**
* method prototype for finding the perimeter
* @returns the perimeter of the shape
*/
public abstract double getPerimeter();
/**
* method prototype for retrieving the parameters
* @returns the parameters of the shape as a String
*/
public abstract String getParameters();
/**
* method prototype for prompting the user to enter input
*/
public abstract void requestInput();
@Override
public String toString() {
/**
* returning a properly formatted String
*/
return "Details for " + type + " with " + getParameters()
+ ": The area is : " + getArea()
+ " meters The perimeter is: " + getPerimeter() + " meters";
}
/**
* method to display the String returned by toString() method
*/
public void output(){
System.out.println(toString());
}
}
//Circle.java
import java.util.Scanner;
public class Circle extends Shape {
// defining a Scanner to read user input
static Scanner scanner=new Scanner(System.in);
// radius attribute
private double radius;
/**
* constructor with no parameters
*/
public Circle() {
/**
* informing the super class that this is a Circle type
*/
super("Circle");
radius = 0;
}
/**
* constructor with one parameter to set the radius
*/
public Circle(double radius) {
super("Circle");
this.radius = radius;
}
@Override
public double getArea() {
/**
* applying the formula to find the area of circle - PI * radius^2
*/
return Math.PI * radius * radius;
}
@Override
public double getPerimeter() {
/**
* applying the formula to find the circumference of circle - 2*PI *
* radius
*/
return 2 * Math.PI * radius;
}
@Override
public String getParameters() {
/**
* a circle has only radius parameter, returning it
*/
return "radius: " + radius;
}
@Override
public void requestInput() {
/**
* prompting the user to enter a radius
*/
System.out.println("Please enter a radius in meters: ");
try {
radius = Double.parseDouble(scanner.nextLine());
} catch (Exception e) {
/**
* Invalid character is entered
*/
System.out.println("Invalid input, try again");
/**
* asking again
*/
requestInput();
}
}
public static void main(String[] args) {
/**
* Defining a Circle instance
*/
Circle circle = new Circle();
String choice = "y";
/**
* looping until user wish to quit
*/
while (choice.equalsIgnoreCase("y")) {
//getting input
circle.requestInput();
//displaying output
circle.output();
System.out.println("Do you want to try again? (y/n) ");
//asking if the user wants to continue
choice = scanner.nextLine();
}
}
}
/*OUTPUT*/
Please enter a radius in meters:
3
Details for Circle
with radius: 3.0:
The area is : 28.274333882308138 meters
The perimeter is: 18.84955592153876 meters
Do you want to try again? (y/n)
y
Please enter a radius in meters:
24.3
Details for Circle
with radius: 24.3:
The area is : 1855.079046018237 meters
The perimeter is: 152.68140296446396 meters
Do you want to try again? (y/n)
n