KnowledgeBoat Logo

Computer Applications

Write a program using a method called area() to compute area of the following:

(a) Area of circle = (22/7) * r * r

(b) Area of square= side * side

(c) Area of rectangle = length * breadth

Display the menu to display the area as per the user's choice.

Java

User Defined Methods

ICSE 2005

111 Likes

Answer

import java.util.Scanner;

public class KboatMenuArea
{
    public void area() {
        
        Scanner in = new Scanner(System.in);
        
        System.out.println("Enter a to calculate area of circle");
        System.out.println("Enter b to calculate area of square");
        System.out.println("Enter c to calculate area of rectangle");
        System.out.print("Enter your choice: ");
        char choice = in.next().charAt(0);
        
        switch(choice) {
            case 'a':
                System.out.print("Enter radius of circle: ");
                double r = in.nextDouble();
                double ca = (22 / 7.0) * r * r;
                System.out.println("Area of circle = " + ca);
                break;
                
            case 'b':
                System.out.print("Enter side of square: ");
                double side = in.nextDouble();
                double sa = side * side;
                System.out.println("Area of square = " + sa);
                break;
                
            case 'c':
                System.out.print("Enter length of rectangle: ");
                double l = in.nextDouble();
                System.out.print("Enter breadth of rectangle: ");
                double b = in.nextDouble();
                double ra = l * b;
                System.out.println("Area of rectangle = " + ra);
                break;
                
            default:
                System.out.println("Wrong choice!");
        }
    }
}

Variable Description Table

Program Explanation

Output

BlueJ output of Write a program using a function called area() to compute area of the following: (a) Area of circle = (22/7) * r * r (b) Area of square= side * side (c) Area of rectangle = length * breadth Display the menu to display the area as per the user's choice.BlueJ output of Write a program using a function called area() to compute area of the following: (a) Area of circle = (22/7) * r * r (b) Area of square= side * side (c) Area of rectangle = length * breadth Display the menu to display the area as per the user's choice.BlueJ output of Write a program using a function called area() to compute area of the following: (a) Area of circle = (22/7) * r * r (b) Area of square= side * side (c) Area of rectangle = length * breadth Display the menu to display the area as per the user's choice.

Answered By

34 Likes


Related Questions