Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class AreaCalculator{
- public static void main(String[] args){
- Scanner kb = new Scanner(System.in);
- //invoke the menu method to print the menu
- //the user's int choice is returned and stored in choice
- int choice = menu();
- if( choice == 1 ){
- System.out.print("Base: ");
- int b = kb.nextInt();
- System.out.print("Height: ");
- int h = kb.nextInt();
- System.out.println("The area is " + areaTriangle(b,h) );
- }
- }
- //Method to calculate area of a Triangle
- //calculation is done by .5 * b * h
- public static double areaTriangle( int base, int height ){
- return .5 * base * height;
- }
- //Method to choose which shape to calculate the area of
- //returns the user's choice as an int
- public static int menu(){
- Scanner kb = new Scanner(System.in);
- //Prompt the user and print his/her options
- 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: ");
- //get the user's int choice and return it
- int choice = kb.nextInt();
- return choice;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement