Question: use the console class for validation and create a main classs Project 3 : Area Calculator Create an application that calculates the area of various

use the console class for validation and create a main classs Project 3: Area Calculator
Create an application that calculates the area of various shapes.
Console
Welcome to the Area Calculator
Calculate area of a circle, square, or rectangle? (c/s/r): c
CIRCLE:
Enter radius: 3
Area: 28.274333882308138
Continue? (y/n): y
Calculate area of a circle, square, or rectangle? (c/s/r): s
SQUARE:
Enter width: 3
Area: 9.0
Continue? (y/n): y
Calculate area of a circle, square, or rectangle? (c/s/r): r
RECTANGLE:
Enter width: 3
Enter height: 4
Area: 12.0
Continue? (y/n): n
Project 4: Area Calculator (continued)
Specifications
Create an abstract class named Shape that contains:
An abstract method named getArea()
A default method that overrides the toString() method and returns a string that includes the value returned by the getArea() method
Create a class named Circle that inherits the Shape class and contains these constructors and methods:
public Circle(double radius)
public double getRadius()
public void setRadius(double radius)
public double getArea()
Create a class named Square that inherits the Shape class and contains these constructors and methods:
public Square(double width)
public double getWidth()
public void setWidth(double width)
public double getArea()
Create a class named Rectangle that inherits the Square class and contains these constructors and methods:
public Rectangle(double width, double height)
public double getHeight()
public void setHeight(double height)
public double getArea()
Use the following formulas to calculate area:
Circle: Area = radius2* pi
Square: Area = width2
Rectangle: Area = width * height
Use the Console class (download from Blackboard, or see in Appendix below) or an enhanced version of it to get and validate the users entries.
Assume the user will enter a valid shape type.
Appendix:
console.java:
import java.util.Scanner;
public class Console {
private static final Scanner sc = new Scanner(System.in);
public static String getString(String prompt){
System.out.print(prompt);
return sc.nextLine();
}
public static int getInt(String prompt){
while (true){
System.out.print(prompt);
try {
return Integer.parseInt(sc.nextLine());
} catch(NumberFormatException e){
System.out.println("Error! Invalid integer value.");
}
}
}
public static int getInt(String prompt, int min, int max){
while (true){
int value = getInt(prompt);
if (value > min && value < max){
return value;
} else {
System.out.println("Error! Number must be greater than "
+ min +" and less than "+ max +".");
}
}
}
public static double getDouble(String prompt){
while (true){
System.out.print(prompt);
try {
return Double.parseDouble(sc.nextLine());
} catch(NumberFormatException e){
System.out.println("Error! Invalid integer value.");
}
}
}
public static double getDouble(String prompt, double min, double max){
while (true){
double value = getDouble(prompt);
if (value > min && value < max){
return value;
} else {
System.out.println("Error! Number must be greater than "
+ min +" and less than "+ max +".");
}
}
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!