(Area Calculator) Java Programming by Indranil Bhattachajee.
Welcome to Study Global
This the output Chose 5 exit the program.
import java.util.Scanner;
public class Area_Calculator {
public static void main(String args[]) {
int shape, result;
double a;
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n");
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 = sc.nextInt();
System.out.println();
switch (shape) {
case 1:
System.out.print("Base: ");
int b = sc.nextInt();
System.out.print("Height: ");
int h = sc.nextInt();
area_triangle(b, h);
a = area_triangle(b, h);
System.out.printf("\nThe area is %.0f.\n\n", a);
break;
case 2:
System.out.print("Lenght: ");
int l = sc.nextInt();
System.out.print("Width: ");
int w = sc.nextInt();
area_rectangle(l, w);
result = area_rectangle(l, w);
System.out.printf("\nThe area is %d.\n\n", result);
break;
case 3:
System.out.print("Side lenght: ");
int s = sc.nextInt();
area_square(s);
int area = area_square(s);
System.out.printf("\nThe area is %d.\n\n", area);
break;
case 4:
System.out.print("Radius: ");
int radius = sc.nextInt();
area_circle(radius);
double res = area_circle(radius);
System.out.printf("\nThe area is %.4f.\n\n", res);
break;
}
if (shape == 5) {
System.out.println("Goodbye.");
break;
}
}
}
public static double area_triangle(int base, int height) {
double a;
a = 0.5 * base * height;
return a;
}
public static int area_rectangle(int lenght, int width) {
int b;
b = lenght * width;
return b;
}
public static int area_square(int side) {
int c;
c = side * side;
return c;
}
public static double area_circle(int radius) {
double res;
res = Math.PI * radius * radius;
return res;
}
}
Comments
Post a Comment