area: Conditional Statements

Conditional Statements (area)
CS 161 - Spring 2008

Due: Monday, February 18, 2008, 10:00 pm

Goals
- to use conditional statements (ifs)

Overview
For this lab, you will write a program that asks for the name of a shape, then asks for the appropriate linear measurements and prints the area of the shape.

Choose good variable names. Use methods and parameters. Write descriptive and precise comments including at least one for each of your methods. Indent correctly. Your program does not need nor should it use loops.

Specifications
The Area program will ask the user for the name of a shape (circle, triangle, or rectangle) and then ask for the appropriate linear dimension(s): radius for the circle, base and height for the triangle, or length and width for the rectangle. The program should check that the response(s) are positive and then print a message giving the area of a shape of that size.

Use double for the type of the dimensions.

Use the equals method of the String class to compare Strings.

If you wish to use the Scanner in methods other than the one that creates it (with new Scanner(System.in)), you'll need to pass it as a parameter to that method. Do NOT create more than one Scanner.

You should use the Math.PI constant rather than defining your own.

Sample Runs (several runs of the program, user input shown in bold)

Please enter the name of the shape (circle, triangle, rectangle): 
circle
Please enter the radius: 
5
The area of a circle with radius 5.0 is 78.53981633974483.


Please enter the name of the shape (circle, triangle, rectangle): 
triangle
Please enter the base and height: 
6 7
The area of a triangle with base 6.0 and height 7.0 is 21.0.


Please enter the name of the shape (circle, triangle, rectangle): 
rectangle
Please enter the length and width: 
3.2 5.4
The area of a rectangle with length 3.2 and width 5.4 is 17.28.


Please enter the name of the shape (circle, triangle, rectangle): 
rectangle
Please enter the length and width: 
3 -4
The value for width(-4.0) is not positive.


Please enter the name of the shape (circle, triangle, rectangle): 
square
The shape(square) is not supported.