Answered step by step
Verified Expert Solution
Link Copied!

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 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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Databases 11th British National Conference On Databases Bncod 11 Keele Uk July 7 9 1993 Proceedings Lncs 696

Authors: Michael F. Worboys ,Anna F. Grundy

1993rd Edition

3540569219, 978-3540569213

More Books

Students also viewed these Databases questions

Question

into vertex form and state the coordi y=x^(2)+6x-16

Answered: 1 week ago

Question

a valuing of personal and psychological privacy;

Answered: 1 week ago