|
| 1 | +//Q10. WAP to show the overloading of area function. |
| 2 | +package JAVA_Lab_File; |
| 3 | + |
| 4 | +import java.util.Scanner; |
| 5 | + |
| 6 | +public class Q10_Overload_Area_fun { |
| 7 | + static void area(double radius){ |
| 8 | + double area = Math.PI * radius * radius; |
| 9 | + System.out.print("Area of Circle is: " + String.format("%.5f", area) + " sq. units"); |
| 10 | + } |
| 11 | + static void area(double base, double height){ |
| 12 | + double area = 0.5*base*height; |
| 13 | + System.out.print("Area of Triangle is: " + String.format("%.5f", area) + " sq. units"); |
| 14 | + } |
| 15 | + public static void main(String[] args) { |
| 16 | + Scanner sc = new Scanner(System.in); |
| 17 | + System.out.println("Calculate Area: \n1.Circle\n2.Triangle\nChoice: "); |
| 18 | + int choice = sc.nextInt(); |
| 19 | + if(choice == 1){ |
| 20 | + System.out.print("Enter the radius of the circle: "); |
| 21 | + double radius = sc.nextDouble(); |
| 22 | + area(radius); |
| 23 | + } else if (choice == 2) { |
| 24 | + System.out.print("Enter the base and height of the triangle: "); |
| 25 | + double base = sc.nextDouble(); |
| 26 | + double height = sc.nextDouble(); |
| 27 | + area(base, height); |
| 28 | + }else { |
| 29 | + System.out.println("Invalid choice"); |
| 30 | + } |
| 31 | + } |
| 32 | +} |
0 commit comments