Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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 : 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? csr: c
CIRCLE:
Enter radius:
Area:
Continue? yn: y
Calculate area of a circle, square, or rectangle? csr: s
SQUARE:
Enter width:
Area:
Continue? yn: y
Calculate area of a circle, square, or rectangle? csr: r
RECTANGLE:
Enter width:
Enter height:
Area:
Continue? yn: n
Project : 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 Circledouble radius
public double getRadius
public void setRadiusdouble radius
public double getArea
Create a class named Square that inherits the Shape class and contains these constructors and methods:
public Squaredouble width
public double getWidth
public void setWidthdouble width
public double getArea
Create a class named Rectangle that inherits the Square class and contains these constructors and methods:
public Rectangledouble width, double height
public double getHeight
public void setHeightdouble height
public double getArea
Use the following formulas to calculate area:
Circle: Area radius pi
Square: Area width
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 ScannerSystemin;
public static String getStringString prompt
System.out.printprompt;
return scnextLine;
public static int getIntString prompt
while true
System.out.printprompt;
try
return Integer.parseIntscnextLine;
catchNumberFormatException e
System.out.printlnError Invalid integer value.";
public static int getIntString prompt, int min, int max
while true
int value getIntprompt;
if value min && value max
return value;
else
System.out.printlnError Number must be greater than
min and less than max ;
public static double getDoubleString prompt
while true
System.out.printprompt;
try
return Double.parseDoublescnextLine;
catchNumberFormatException e
System.out.printlnError Invalid integer value.";
public static double getDoubleString prompt, double min, double max
while true
double value getDoubleprompt;
if value min && value max
return value;
else
System.out.printlnError Number must be greater than
min and less than max ;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started