Thursday, July 17, 2014

Geometry Area Calculator Java

Class 1: Area Calc 
---------------------------------


public class areacalculator
{
    private double area;

    public double circleArea(double r)
    {
area = (r * r) * Math.PI ;
return area;
    }

    public double rectangleArea(double length, double width)
    {
area = length * width;
return area;
    }

    public double triangleArea(double base, double height)
    {
area = (base * height)/2;
return area;
    }
}

Class 2 Geometry 
---------------------
import java.util.Scanner;
public class geo
{
   
    public static void main(String[] args)
    {
int selection;
double area = 0;
areacalculator Calc = new areacalculator();
Scanner console = new Scanner(System.in);
System.out.println("Area Calculator");
System.out.println("1." + "\tWhat is the Area of a Circle?");
System.out.println("2." + "\tWhat is the Area of a Rectangle?");
System.out.println("3." + "\tWhat is the Area of a Triangle?");
System.out.println("4." +  "\tExit");

while (true)
{
     selection = console.nextInt();
     if (selection == 1) {
    System.out.println("Radius? ");
    double radius = console.nextDouble();
    area = Calc.circleArea(radius);
    System.out.println("The area is? "+ area );
     }
     else if (selection == 2)
     {
    System.out.print("Enter the length? ");
    double length = console.nextDouble();
    System.out.println("Enter the width? ");
    double width = console.nextDouble();
    area = Calc.rectangleArea(length, width);
    System.out.println("The area is? "+ area );
     }
     else if (selection == 3)
     {
    System.out.print("Enter the base? ");
    double base = console.nextDouble();
    System.out.println("Enter the height? ");
    double height = console.nextDouble();
    area = Calc.triangleArea(base, height);
    System.out.println("The area is? "+ area );
     }
   
     else if (selection == 4)
     {
    System.exit(0);
     }
     else
     {
    System.out.println(selection+" Were you born wrong? Try again.");
     }

}
    }
}

No comments:

Post a Comment