Assignment #97 and Area Calculator
Code
/// Name: Brendan Baird
/// Period: 6
/// Program Name: Area Calculator
/// File Name: AreaCalculator.java
/// Date Finished: 1/6/2016
import java.util.Scanner;
public class AreaCalculator
{
public static void main(String[] args)
{
Scanner k = new Scanner(System.in);
int shape;
do
{
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.print("Which shape: ");
shape = k.nextInt();
System.out.println();
if (shape == 1)
{
System.out.print("Base: ");
int base = k.nextInt();
System.out.print("Height: ");
int height = k.nextInt();
System.out.println();
System.out.println("The area is " + areaTriangle( base, height ) + ".");
}
else if (shape == 2)
{
System.out.print("Length: ");
int length = k.nextInt();
System.out.print("Width: ");
int width = k.nextInt();
System.out.println();
System.out.println("The area is " + areaRectangle( length, width ) + ".");
}
else if (shape == 3)
{
System.out.print("Side length: ");
int side = k.nextInt();
System.out.println();
System.out.println("The area is " + areaSquare( side ) + ".");
}
else if (shape == 4)
{
System.out.print("Radius: ");
int radius = k.nextInt();
System.out.println();
System.out.println("The area is " + areaCircle( radius ) + ".");
}
else if (shape == 5)
{
System.out.println("Goodbye.");
}
else
System.out.println("Error. Please select a number from the list of shapes.");
} while (shape != 5);
}
public static double areaCircle( int radius )
{
double a;
a = Math.PI * radius * radius;
return a;
}
public static int areaRectangle( int length, int width )
{
int a;
a = length * width;
return a;
}
public static int areaSquare( int side )
{
int a;
a = side * side;
return a;
}
public static double areaTriangle( int base, int height )
{
double a;
a = .5 * base * height;
return a;
}
}
Picture of the output