Wednesday, 11 March 2015

[JAVA] [ICSE 2005 Answer 6] Calculating Area By Function Overloading

Question:
ICSE 2005 Question 6
Write a program using a function called area() to compute the area of a:
(i)      Circle(II * r)where II = 3.14
(ii)     Square(side*side)
(iii)    Rectange(length * breadth)
Display the menu to output the area as per user's choice

Source Code:
import java.util.*;
public class ICSE2005_6
{
    public static void main(String args[])throws Exception
    {
        Scanner read=new Scanner(System.in);
        System.out.print("What do you want to find?\nEnter:\n1 for area of circle\n2 for area of square\n3 for area of rectange\nEnter your choice:");
        int choice=read.nextInt();
        double a;
        switch(choice)
        {
            case 1:
            System.out.print("Enter the length of radius of the circle:");
            double r=read.nextDouble();
            a=areac(r);
            System.out.println("Area of the circle is "+a+".");
            break;
            case 2:
            System.out.print("Enter the length of side of the square:");
            double s=read.nextDouble();
            a=areas(s);
            System.out.println("Area of the square is "+a+".");
            break;
            case 3:
            System.out.print("Enter the length of first side of the rectange:");
            double l=read.nextDouble();
            System.out.print("Enter the length of second side of the rectange:");
            double b=read.nextDouble();
            a=arear(l, b);
            System.out.println("Area of the rectangle is "+a+".");
            break;
            default:
            System.out.println("Invalid option entered!:(");
        }
    }

    private static double areac(double r)
    {
        return ((3.14)*r*r);
    }

    private static double areas(double s)
    {
        return (s*s);
    }

    private static double arear(double l, double b)
    {
        return (l*b);
    }
}


Result 1:
OUTPUT:
What do you want to find?
Enter:
1 for area of circle
2 for area of square
3 for area of rectange
Enter your choice:1
Enter the length of radius of the circle:4.67
Area of the circle is 68.479946.
Result 2:
OUTPUT:
What do you want to find?
Enter:
1 for area of circle
2 for area of square
3 for area of rectange
Enter your choice:2
Enter the length of side of the square:98
Area of the square is 9604.0.
Result 3:
OUTPUT:
What do you want to find?
Enter:
1 for area of circle
2 for area of square
3 for area of rectange
Enter your choice:3
Enter the length of first side of the rectange:5.4
Enter the length of second side of the rectange:6.87
Area of the rectangle is 37.098000000000006.
Result 4:
OUTPUT:
What do you want to find?
Enter:
1 for area of circle
2 for area of square
3 for area of rectange
Enter your choice:4
Invalid option entered!:(

1 comment:

  1. Betway India: Get a bonus up to ₹4,600 - JTG Hub
    Check out 구미 출장안마 the 양산 출장샵 best deals on Betway 양산 출장안마 India: Get a bonus up 경기도 출장안마 to ₹4,600 to ₹4,600 in the latest Promotions. 원주 출장마사지

    ReplyDelete