Powza
2010-04-10 07:08:43 UTC
. an invalid number for radius
. an invalid length and height for a rectangle
. an invalid base and height for a triangle.
Also have to create individual Exception classes to catch these errors. Then Prompt the user to reenter the data once the exception is caught. Any help is appreciated, Thanks
/******************** Geometry ********************/
public class Geometry {
public static float areaCircle( float radius ) {
if( radius < 0 )
System.out.println("Can not have negative numbers");
return (float) (Math.PI * ( radius * radius ));
}
public static float areaRectangle(float height, float length){
return (float) length * height;
}
public static float areaTriangle(float base, float height){
return (float) ((float) base * height * 0.5) ;
}
}
/******************** Geometry Tester ********************/
import java.util.Scanner;
class GeometryTester {
public static void main( String [] args ) {
Scanner sc = new Scanner( System.in );
boolean run = true;
do {
System.out.println("Geometry Calculator");
System.out.println(" 1. Calculate the Area of a Circle");
System.out.println(" 2. Calculate the Area of a Rectangle");
System.out.println(" 3. Calculate the Area of a Triangle");
System.out.println(" 4. Quit");
System.out.print("Please make your selection ==> ");
String selection = sc.nextLine();
int choice = Integer.parseInt( selection );
switch( choice ) {
case 1 :
System.out.println(" 1. Calculate the Area of a Circle");
System.out.print("Enter the radius of the circle ==> ");
float c = Float.parseFloat( sc.nextLine() );
System.out.println( "The area is " + Geometry.areaCircle( c ) );
break;
case 2:
System.out.println(" 2. Calculate the Area of a Rectangle");
System.out.print("Enter the Length of the Rectangle ==> ");
float l = Float.parseFloat( sc.nextLine() );
System.out.print("Enter the Width of the Rectangle ==> ");
float w = Float.parseFloat( sc.nextLine() );
System.out.println( "The area is " + Geometry.areaRectangle( l, w ) );
break;
case 3:
System.out.println(" 3. Calculate the Area of a Triangle");
System.out.print("Enter the Base of the Triangle ==> ");
float b = Float.parseFloat( sc.nextLine() );
System.out.print("Enter the Height of the Triangle ==> ");
float h = Float.parseFloat( sc.nextLine() );
System.out.println( "The area is " + Geometry.areaTriangle( b, h ) );
case 4:
run = false;
break;
default:
System.out.println("you must enter 1,2,3,or 4");
}
} while( run = false );
System.out.println("Thank you for using the program");
System.exit(0);
}
}