0% found this document useful (0 votes)
11 views

Java parameter program

The Java program calculates the area of different shapes: circle, rectangle, and triangle based on user input. It uses method overloading to define separate area calculation methods for each shape. The user is prompted to choose a shape and input the necessary dimensions to compute the area.

Uploaded by

kusumlatamurmu0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Java parameter program

The Java program calculates the area of different shapes: circle, rectangle, and triangle based on user input. It uses method overloading to define separate area calculation methods for each shape. The user is prompted to choose a shape and input the necessary dimensions to compute the area.

Uploaded by

kusumlatamurmu0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Java parameter program:

import java.util.Scanner;

public class AreaCalculator {

// Area of Circle
public static double area(double radius) {
return Math.PI * radius * radius;
}

// Area of Rectangle
public static double area(double length, double breadth) {
return length * breadth;
}

// Area of Triangle
public static double area(double base, double height, boolean isTriangle) {
return 0.5 * base * height;
}

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);

System.out.println("Choose shape:");
System.out.println("1. Circle");
System.out.println("2. Rectangle");
System.out.println("3. Triangle");
int choice = sc.nextInt();

switch (choice) {
case 1:
System.out.print("Enter radius: ");
double r = sc.nextDouble();
System.out.println("Area of Circle: " + area(r));
break;

case 2:
System.out.print("Enter length: ");
double l = sc.nextDouble();
System.out.print("Enter breadth: ");
double b = sc.nextDouble();
System.out.println("Area of Rectangle: " + area(l, b));
break;

case 3:
System.out.print("Enter base: ");
double base = sc.nextDouble();
System.out.print("Enter height: ");
double height = sc.nextDouble();
System.out.println("Area of Triangle: " + area(base, height, true));
break;

default:
System.out.println("Invalid choice.");
}
}
}

You might also like