0% found this document useful (0 votes)
11 views11 pages

Labrecordprograms810

The document contains three Java programs that implement menu-driven applications using switch-case statements. The first program calculates the volume of a cube, sphere, and cuboid; the second calculates the area of different types of triangles; and the third performs basic arithmetic operations. Each program includes user input handling, error messages for invalid choices, and sample outputs demonstrating their functionality.

Uploaded by

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

Labrecordprograms810

The document contains three Java programs that implement menu-driven applications using switch-case statements. The first program calculates the volume of a cube, sphere, and cuboid; the second calculates the area of different types of triangles; and the third performs basic arithmetic operations. Each program includes user input handling, error messages for invalid choices, and sample outputs demonstrating their functionality.

Uploaded by

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

PROGRAM 8

Write a menu driven program using switch case to find the volume of a cube, a sphere and
cuboid using Scanner class methods. For an incorrect choice, an appropriate error message
should be displayed.
1. Volume of cube
2. Volume of a sphere
3. Volume of a cuboid
4. Exit

import java.util.*;
class VolumeCalculator
{
void calculate()
{
//Creating a Scanner object
Scanner sc = new Scanner(System.in);

//Displaying the Menu on the terminal window


System.out.println("***********************");
System.out.println("\tMENU");
System.out.println("***********************");
System.out.println("1.Volume of Cube");
System.out.println("2.Volume of Sphere");
System.out.println("3.Volume of Cuboid");
System.out.println("4.Exit");
System.out.println("***********************");

//Accepting the choice from the user


System.out.println("Enter your choice:");
int choice=sc.nextInt( );

//Switching the choice to select and perform the task as opted by the user
switch(choice)
{
case 1:
//Accepting the side length of a cube and calculating its volume
System.out.println("Enter the side length of the cube:");
double side=sc.nextDouble();
double cubeVolume=Math.pow(side,3);
System.out.println("The Volume of the cube is: "+cubeVolume);
break;
case 2:
//Accepting the radius of a sphere and calculating its volume
System.out.println("Enter the radius of the sphere:");
double radius=sc.nextDouble( );
double sphereVolume=4/3f*3.14*Math.pow(radius,3);
System.out.println("The Volume of the sphere is: "+sphereVolume);
break;

case 3:
//Accepting the dimensions of a cuboid and calculating its volume
System.out.println("Enter the length of the cuboid:");
double length=sc.nextDouble( );
System.out.println("Enter the breadth of the cuboid:");
double breadth=sc.nextDouble( );
System.out.println("Enter the height of the cuboid:");
double height=sc.nextDouble( );
double cuboidVolume=length*breadth*height;
System.out.println("The Volume of the sphere is: "+cuboidVolume);
break;

case 4:
//Exiting the program
System.out.println("Exiting the Program.....");
System.out.println("Thank you");
System.exit(0);

default:
//Handling Invalid choices
System.out.println("Invalid choice!");
System.out.println("Please enter a number between 1 and 4.");

}//End of switch case


}//End of method
}//End of class
VARIABLE DESCRIPTON

Variable Data type Description

choice int Stores the option number entered by the user

side double Stores the side length of a cube

cubeVolume double Stores the volume of the cube

radius double Stores the radius of the sphere

sphereVolume double Stores the volume of the sphere

double length double Stores the length of the cuboid

double breadth double Stores the breadth of the cuboid

double height double Stores the height of the cuboid

cuboidVolume double Stores the volume of the cuboid

SAMPLE OUTPUT

***********************
MENU
***********************
1.Volume of Cube
2.Volume of Sphere
3.Volume of Cuboid
4.Exit
***********************
Enter your choice:
1
Enter the side length of the cube:
4.5
The Volume of the cube is: 91.125
PROGRAM 9
Using switch case write a menu driven program to find the area of an Equilateral triangle,
Isosceles triangle and a Scalene triangle as per the user’s choice. For an incorrect choice, an
appropriate error message should be displayed.

√3 2
1. Area of an equilateral triangle = 𝑠 where s is side of a triangle
4
1
2. Area of an isosceles triangle = 𝑏√4𝑎2 − 𝑏 2
4
𝑎+𝑏+𝑐
3. Area of a scalene triangle = √𝑠(𝑠 − 𝑎)(𝑠 − 𝑏)(𝑠 − 𝑐) s =
2
4. Exit
where a, b and c are three sides of a triangle

import java.util.*;
class AreaCalculator
{
void calculate()
{
//Creating a Scanner object
Scanner sc = new Scanner(System.in);

//Displaying the Menu on the terminal window


System.out.println("************************************************");
System.out.println("\tMENU");
System.out.println("************************************************");
System.out.println("1.Calculate area of an Equilateral triangle");
System.out.println("2.Calculate area of an Isosceles triangle");
System.out.println("3.Calculate area of a Scalene triangle");
System.out.println("4.Exit");
System.out.println("************************************************");

//Accepting the choice from the user


System.out.println("Enter your choice:");
int choice=sc.nextInt( );
//Switching the choice to select and perform the task as opted by the user
switch (choice)
{
case 1:
// Accepting the side length of an Equilateral Triangle and calculating its area
System.out.print("Enter the side length of an equilateral triangle: ");
double side = sc.nextDouble( );
double equilateralArea = (Math.sqrt(3) / 4) * Math.pow(side, 2);
System.out.println("Area of Equilateral Triangle: " + equilateralArea);
break;

case 2:
// Accepting the base and height of an Isosceles triangle and calculating its area
System.out.print("Enter the base length of the isosceles triangle: ");
double base = sc.nextDouble( );
System.out.print("Enter the height of the isosceles triangle: ");
double height = sc.nextDouble( );
double isoscelesArea = 1/2f * base * height;
System.out.println("Area of Isosceles Triangle: " + isoscelesArea);
break;

case 3:
// Accepting the length of sides of a Scalene triangle
System.out.print("Enter the length of side 1 of the scalene triangle:");
double a = sc.nextDouble( );
System.out.print("Enter the length of side 2 of the scalene triangle:");
double b= sc.nextDouble( );
System.out.print("Enter the length of side 3 of the scalene triangle:");
double c = sc.nextDouble( );

// Calculating the semi-perimeter of the Scalene triangle


double s = (a+ b + c) / 2;

// Calculating the area of the Scalene triangle using Heron's formula


double scaleneArea = Math.sqrt(s*(s-a) *(s-b) * (s-c));
System.out.println("Area of Scalene Triangle: " + scaleneArea);
break;
case 4:
//Exiting the program
System.out.println("Exiting the Program.....");
System.out.println("Thank you");
System.exit(0);

default:
//Handling Invalid choices
System.out.println("Invalid choice!");
System.out.println("Please enter a number between 1 and 4.");
}//End of switch case
}//End of method
}//End of class

VARIABLE DESCRIPTON

Variable Data type Description

choice int Stores the option number entered by the user

side double Stores the side length of the equilateral triangle

equilateralArea double Stores the area of the equilateral triangle

base double Stores the base of the Isosceles triangle

height double Stores the height of the Isosceles triangle

isoscelesArea double Stores the area of the Isosceles triangle

a double Stores the length of side 1 of the scalene triangle

b double Stores the length of side 2 of the scalene triangle

c double Stores the length of side 3 of the scalene triangle

s double Stores the semi-perimeter of the scalene triangle

scaleneArea double Stores the area of the scalene triangle


SAMPLE OUTPUT

************************************************
MENU
************************************************
1.Calculate area of an Equilateral triangle
2.Calculate area of an Isosceles triangle
3.Calculate area of a Scalene triangle
4.Exit
************************************************
Enter your choice:
2
Enter the base length of the isosceles triangle: 3.5
Enter the height of the isosceles triangle: 5.5
Area of Isosceles Triangle: 9.625
PROGRAM 10
Write a menu driven program using switch case to accept any two integer values and user’s
choice using Scanner class methods to perform the following.
a) Addition
b) Subtraction
c) Multiplication
d) Division
e) Exit

import java.util.*;
class ArithematicCalculator
{
void calculate( )
{
//Creating a Scanner object
Scanner sc = new Scanner(System.in);

//Declaring and initializing two integers


int num1=0;
int num2=0;

//Displaying the Menu on the terminal window


System.out.println("************************************************");
System.out.println("\tMENU");
System.out.println("************************************************");
System.out.println("a.ADDITION");
System.out.println("b.SUBTRACTION");
System.out.println("c.MULTIPLICATION");
System.out.println("d.DIVISION");
System.out.println("e.EXIT");
System.out.println("************************************************");

//Accepting the choice from the user


System.out.println("Enter your choice:");
int choice=sc.next( ).charAt(0);
//Reading two integer values from the user if the choice is between ‘a’ and ‘d’
if (choice >= 'a' && choice <= 'd')
{
System.out.print("Enter the first number: ");
num1 = sc.nextInt( );
System.out.print("Enter the second number: ");
num2 = sc.nextInt();
}

//Switching the choice to select and perform the task as opted by the user
switch (choice)
{
case 'a':
// Performing Addition on the two integers
int sum = num1 + num2;
System.out.println("The sum of "+num1+ " and " +num2 + " is: " + sum);
break;

case 'b':
// Performing Subtraction on the two integers
int difference = num1 - num2;
System.out.println("The difference between " +num1 + " and " + num2 + " is: " + difference);
break;

case 'c':
// Performing Multiplication on the two integers
int product = num1 * num2;
System.out.println("The product of "+num1+ " and " + num2 + " is: " + product);
break;

case 'd':
// Performing Division on the two integers
double quotient = (double) num1 / num2;
System.out.println("The quotient of " +num1 + " divided by " + num2 + " is: " + quotient);
break;

case 'e':
//Exiting the program
System.out.println("Exiting the Program.....");
System.out.println("Thank you");
System.exit(0);
default:
//Handling Invalid choices
System.out.println("Invalid choice!");
System.out.println("Please enter a number between ‘a’ and ‘e’.");
}//End of switch case
}//End of method
}//End of class

VARIABLE DESCRIPTON

Variable Data type Description

choice int Stores the option number entered by the user

num1 int Stores the value of first number entered by the user

num2 int Stores the value of second number entered by the user

sum int Stores the sum of the two integer numbers

difference int Stores the difference of the two integer numbers

product int Stores the product of the two integer numbers


Stores the quotient of the first number divided by the
quotient double
second number
SAMPLE OUTPUT

************************************************
MENU
************************************************
a.ADDITION
b.SUBTRACTION
c.MULTIPLICATION
d.DIVISION
e.EXIT
************************************************
Enter your choice:
c
Enter the first number: 5
Enter the second number: 6
The product of 5 and 6 is: 30

You might also like