Assignemnt #97 Area
Code
/// Name: Caelan Dorstad
/// Period: 6
/// Program Name: area
/// File Name: area.java
/// Date Finished: 4/28/2016
import java.util.Scanner;
import java.util.InputMismatchException;
public class area{
static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args){
String read = "";
float finalArea,n1,n2 = 0;
do{
finalArea = 0;
System.out.println("=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=");
System.out.println("1) Triangle");
System.out.println("2) Rectangle");
System.out.println("3) Square");
System.out.println("4) Circle");
System.out.println("5) Quit");
System.out.println("=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=");
System.out.print("Choice: ");
read = keyboard.next();
try{
switch(read){
case "1":
System.out.print("Base: ");
n1 = keyboard.nextFloat();
System.out.print("Height: ");
n2 = keyboard.nextFloat();
finalArea = triangleArea(n1,n2);
break;
case "2":
System.out.print("Base: ");
n1 = keyboard.nextFloat();
System.out.print("Height: ");
n2 = keyboard.nextFloat();
finalArea = rectangleArea(n1, n2);
break;
case "3":
System.out.print("Side: ");
n1 = keyboard.nextFloat();
finalArea = squareArea(n1);
break;
case "4":
System.out.print("Radius: ");
n1 = keyboard.nextFloat();
finalArea = circleArea(n1);
break;
default:
read = "ERROR";
}
}
catch (InputMismatchException e)
{
read = "ERROR";
}
if (!read.equals("error") && !read.equals("5")) System.out.println("\nThe area is "+finalArea+".\n");
if (read.equals("error")) System.out.println("WRONG INPUT, TRY AGAIN!!!");
}
while (!read.equals("5"));
System.out.println("BYE");
}
static float triangleArea(float b , float h){
return b*h*.5f;
}
static float rectangleArea(float l, float w){
return l*w;
}
static float squareArea(float s){
return s*s;
}
static float circleArea(float r){
return r*r*(float)Math.PI;
}
}
Picture of the output