0% found this document useful (0 votes)
23 views7 pages

JAVA PRGMS SWITCH CASE BRK St.

The document describes 3 Java programs that use switch-case statements to calculate different mathematical operations and area calculations based on user input. Program 1 calculates arithmetic operations (addition, subtraction, multiplication, division) based on a menu choice. Program 2 calculates simple or compound interest based on a menu choice. Program 3 will calculate the area of different shapes (triangle, square, rectangle, circle) based on a menu choice. All programs use a buffered reader to input values and switch-case statements to perform the correct calculation.

Uploaded by

sameerakhan8912
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)
23 views7 pages

JAVA PRGMS SWITCH CASE BRK St.

The document describes 3 Java programs that use switch-case statements to calculate different mathematical operations and area calculations based on user input. Program 1 calculates arithmetic operations (addition, subtraction, multiplication, division) based on a menu choice. Program 2 calculates simple or compound interest based on a menu choice. Program 3 will calculate the area of different shapes (triangle, square, rectangle, circle) based on a menu choice. All programs use a buffered reader to input values and switch-case statements to perform the correct calculation.

Uploaded by

sameerakhan8912
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/ 7

JAVA PROGRAMS using ‘SWITCH, CASE, BREAK & DEFAULT’ statement

/* PROGRAM 1. Write a JAVA program using class name ‘Calculate’ to perform or calculate either ADDITION or
SUBTRACTION or MULTIPLICATION or DIVISION using ‘SWITCH, CASE, BREAK & DEFAULT’ statement depending upon
the user’s ( inputted ) choice using ‘BUFFER’ mode of inputting the values */
import java.io.*; // header file used for the input buffer variable & other variables
class Calculate
(// opening of the class ‘Calculate’
public static void main(String args[ ])throws IOException
{ // opening of the ‘main( )’ function
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
// this part is to declare the variables and input the values
int ch, a, b, c = 0;
String msg;
System.out.println(“Enter the value for ‘a’ :-“);
a = Integer.parseInt(br.readLine( ));
System.out.println(“Enter the value for ‘b’ :-“);
b = Integer.parseInt(br.readLine( ));
// this part is to display a MENU based table which shows the option for the user
System.out.println(“ M E N U“);
System.out.println(“1. ADDITION “);
System.out.println(“2. SUBTRACTION “);
System.out.println(“3. MULTIPLICATION “);
System.out.println(“4. DIVISION “);
System.out.println(“CHOOSE THE OPTION either 1 or 2 or 3 or 4 :-“);
ch = Integer.parseInt(br.readLine( ));
switch(ch)
{ // opening of the ‘switch( )’ block
case 1:
c = a + b;
msg = “The Sum of a + b =”;
break; // this ends or terminates the ‘switch( )’ block
case 2:
if ( a > b )
c = a – b;
msg = “The Difference of a – b =”;
else
c = b – a;
msg = “The Difference of b – a =”;
break; // this ends or terminates the ‘switch( )’ block
case 3:
c = a * b;
msg = “The Product of a * b =”;
break; // this ends or terminates the ‘switch( )’ block
case 4:
if ( a > b )
c = a / b;
msg = “The Quotient of a / b =”;
else
c = b / a;
msg = “The Quotient of b / a =”;
break; // this ends or terminates the ‘switch( )’ block
default:
System.out.println(“WRONG OPTION”);
} // end of the ‘switch( )’ block
// this part is to display the output
System.out.println(“The value of a =”+a);
System.out.println(“The value of b =”+b);
System.out.println(+msg+c);
} // end of the ‘main( )’ function
} // end of the class ‘Calculate’
-- 2 --
VARIABLE DESCRIPTION :-

Sl. No. Variable Name Type Description


1. br String ‘br’ variable is used as a buffer variable
which controls the inflow and outflow
of the data
2. ch Int ( Integer ) ‘ch’ variable is used to input the option
1/2/3/4 for the related calculations
3. a Int ( Integer ) ‘a’ variable is used to input the 1st value
4. b Int ( Integer ) ‘b’ variable is used to input the 2nd
value
5. c Int ( Integer ) ‘c’ variable is used to store the
calculated value depending upon the
user’s choice
6. msg String ‘msg’ variable is used to store the
message depending upon the
calculations calculated

/* PROGRAM 2. Write a JAVA program using class name ‘Interest’ to perform or calculate either SIMPLE INTEREST
or COMPOUND INTEREST using ‘SWITCH, CASE, BREAK & DEFAULT’ statement depending upon the user’s ( inputted )
choice using ‘BUFFER’ mode of inputting the values */
import java.io.*; // header file used for the input buffer variable
class Interest
(// opening of the class ‘Interest’
public static void main(String args[ ])throws IOException
{ // opening of the ‘main( )’ function
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
// this part is to declare the variables and input the values
int ch, p, t, r;
float sci = 0.0;
String = msg;
System.out.println(“Enter the value for Principle Interest :- ”);
p = Integer.parseInt(br.readLine( ));
System.out.println(“Enter the value for Time ‘no. of years’ :- ”);
t = Integer.parseInt(br.readLine( ));
System.out.println(“Enter the value for Rate of Interest :- ”);
r = Integer.parseInt(br.readLine( ));
// this part is to display a MENU based table which shows the option for the user
System.out.println(“ M E N U“);
System.out.println(“1. SIMPLE INTEREST “);
System.out.println(“2. COMPOUND INTEREST “);
System.out.println(“CHOOSE THE OPTION either 1 or 2 :-“);
ch = Integer.parseInt(br.readLine( ));
switch(ch)
{ // opening of the ‘switch( )’ block
case 1:
sci = ( p * t * r ) / 100;
msg = “The Simple Interest calculated = ”;
break; // this ends or terminates the ‘switch( )’ block
case 2:
sci = 1 + ( Math.pow ( r / 100 ), n) – p;
msg = “The Compound Interest calculated =”;
break; // this ends or terminates the ‘switch( )’ block
default:
System.out.println(“WRONG OPTION”);
} // end of the ‘switch( )’ block
-- 3 --
// this part is to display the output
System.out.println(“The Principle Interest :- ”+p);
System.out.println(“The Time ‘no. of years’ :- ”+t);
System.out.println(“The Rate of Interest :- ”+r);
System.out.println(+msg+sci);
} // end of the ‘main( )’ function
} // end of the class ‘Interest’

VARIABLE DESCRIPTION :-
Sl. No. Variable Name Type Description
1. br String ‘br’ variable is used as a buffer variable
which controls the inflow and outflow
of the data
2. ch Int ( Integer ) ‘ch’ variable is used to input the option
1/2 for the related calculations
3. p Int ( Integer ) ‘p’ variable is used to input the
Principle amount
4. t Int ( Integer ) ‘t’ variable is used to input the Time
i.e., no. of years
5. r Int ( Integer ) ‘r’ variable is used to input the Rate of
Interest
6. msg String ‘msg’ variable is used to store the
message depending upon the
calculations calculated
7. sci float ‘sci’ variable is used to calculate and
store the value of either Simple or
Compound Interest on the user’s choice

/* PROGRAM 3. Write a JAVA program using class name ‘Area2’ to perform or calculate either AREA OF THE
TRIANGLE or AREA OF THE SQUARE or AREA OF THE RECTANGLE or AREA OF THE CIRCLE using ‘SWITCH, CASE, BREAK
& DEFAULT’ statement depending upon the user’s ( inputted ) choice using ‘BUFFER’ mode of inputting the values */
import java.io.*; // header file used for the input buffer variable
class Area2
(// opening of the class ‘Area2’
public static void main(String args[ ])throws IOException
{ // opening of the ‘main( )’ function
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
// this part is to declare the variables and input the values
int ch;
// this part is to display a MENU based table which shows the option for the user
System.out.println(“ M E N U“);
System.out.println(“1. AREA OF THE TRIANGLE “);
System.out.println(“2. AREA OF THE SQUARE “);
System.out.println(“3. AREA OF THE RECTANGLE “);
System.out.println(“4. AREA OF THE CIRCLE “);
System.out.println(“CHOOSE THE OPTION either 1 or 2 or 3 or 4 :-“);
ch = Integer.parseInt(br.readLine( ));
switch(ch)
{ // opening of the ‘switch( )’ block
case 1:
System.out.println(“1. AREA OF THE TRIANGLE “);
int b, h;
float atr = 0.0;
System.out.println(“Enter the value for base :-“);
b = Integer.parseInt(br.readLine( ));
System.out.println(“Enter the value for height :-“);
h = Integer.parseInt(br.readLine( ));
-- 4 --
atr = ( ½ * ( b * h ) );
System.out.println(“Area of the Triangle :- ”+atr+” for Base = “+b+” Height = “+h);
break; // this ends or terminates the ‘switch( )’ block
case 2:
System.out.println(“2. AREA OF THE SQUARE “);
int s, asd = 0;
System.out.println(“Enter the value for side :-“);
s = Integer.parseInt(br.readLine( ));
asd = ( s * s );
System.out.println(“Area of the Square :-“+asd+” for sides = “+s););
break; // this ends or terminates the ‘switch( )’ block
case 3:
System.out.println(“3. AREA OF THE RECTANGLE “);
int l, b, arct = 0;
System.out.println(“Enter the value for Length :-“);
l = Integer.parseInt(br.readLine( ));
System.out.println(“Enter the value for breadth :-“);
b = Integer.parseInt(br.readLine( ));
arct = ( l * b );
System.out.println(“Area of the Rectangle :-”+arct+” for Length = “+l+” Breadth = “+b);
break; // this ends or terminates the ‘switch( )’ block
case 4:
System.out.println(“4. AREA OF THE CIRCLE “);
int r;
float pie = ( 22/7 );
float acr = 0.0;
System.out.println(“Enter the value for radius :-“);
r = Integer.parseInt(br.readLine( ));
acr = ( pie *( Math.pow( r,2 ) ) );
System.out.println(“Area of the Circle :-“+acr+” for Radius = “r);
break; // this ends or terminates the ‘switch( )’ block
default:
System.out.println(“WRONG OPTION”);
} // end of the ‘switch( )’ block
} // end of the ‘main( )’ function
} // end of the class ‘Area2’

VARIABLE DESCRIPTION :-
Sl. No. Variable Name Type Description
1. br String ‘br’ variable is used as a buffer variable
which controls the inflow and outflow
of the data
2. ch int ( Integer ) ‘ch’ variable is used to input the option
1/2/3/4 for the related calculations
3. h int ( Integer ) ‘h’ variable is used to input the height
of the Triangle
4. b int ( Integer ) ‘b’ variable is used to input the base of
the Triangle ( or ) breadth of the
Rectangle
5. s int ( Integer ) ‘s’ variable is used to input the side of
the Square
6. l int ( Integer ) ‘l’ variable is used to input the length of
the Rectangle
7. r int ( Integer ) ‘r’ variable is used to input the radius of
the Circle
8. atr float ‘atr’ variable is used to calculate and
store the Area of the Triangle
9. asd int ( Integer ) ‘asd’ variable is used to calculate and
store the Area of the Side
10. arct int ( Integer ) ‘arct’ variable is used to calculate and
store the Area of the Rectangle
11. acr float ‘acr’ variable is used to calculate and
store the Area of the Circle

/* PROGRAM 4. Write a JAVA program using class name ‘ElectBill’ to perform or calculate either DOMESTIC
type or BUSINESS type or INDUSTRIAL type of Consumers using ‘SWITCH, CASE, BREAK & DEFAULT’ statement
depending upon the user’s ( inputted ) choice using ‘BUFFER’ mode of inputting the values for the ‘TELANGANA STATE
ELECTRICITY BOARD’, TELANGANA */
import java.io.*; // header file used for the input buffer variable
class ElectBill
{ // opening of class ‘ElectBill’
public static void main(String args[])throws IOException
{ // opening of the ‘main()’ function
// this part is to create the buffer variable ‘br’
BufferedReader br = new BufferedReader (new InputStreamReader(System.in));
// this part is to declare and initialize the variables
String nm; //’nm’ variable is used to input the name of the consumer
int cono, mtrno, ir, fr; // ‘cono’ variable is used for consumer number
double uc=0; // ‘mtrno’ variable is used for meter number
// ‘ir’ variable is used for initial readings of the meter
// ‘fr’ variable is used for final readings of the meter
// ‘uc’ variable is used for units consumed i.e., final reading – initial readings
double bill = 0; // ‘bill’ variable is used to calculate the bill on the readings
double ba = 0; // ‘ba’ variable is to calculate the original billable amount with service tax
int ch; // ‘ch’ variable is used to input the choice
// this part is to show the MENU which gives the option to the user to choose either any ONE of
// the OPTION ( category )
System.out.println("\n TELANGANA STATE ELECTRICITY BOARD");
System.out.println("\n ABIDS, HYDERABAD.");
System.out.println("\n M E N U");
System.out.println("\n 1. To calculate for DOMESTIC type of CONSUMERS");
System.out.println("\n 2. To calculate for BUSINESS type of CONSUMERS");
System.out.println("\n 3. To calculate for INDUSTRIAL type of CONSUMERS");
System.out.println("\n 4. To terminate or EXIT the options");
System.out.println("\n ENTER the option ( CHOICE ) either 1 or 2 or 3 or 4 :-");
ch = Integer.parseInt(br.readLine());
switch(ch)
{ // opening of the ‘switch()’ block
case 1 :
System.out.println("\n 1. To calculate for DOMESTIC type of CONSUMERS");
// this part id to input the values for variables
System.out.println("\n ENTER the Consumer’s Name :- ");
nm = br.readLine();
System.out.println("\n ENTER the Consumer’s Number :- ");
cono = Integer.parseInt(br.readLine());
System.out.println("\n ENTER the Meter number :- ");
mtrno = Integer.parseInt(br.readLine());
System.out.println("\n ENTER the Initial Readings :- ");
ir = Integer.parseInt(br.readLine());
System.out.println("\n ENTER the final Readings :-");
fr = Integer.parseInt(br.readLine());
// this part id to calculate the bill
uc = fr - ir;
if ( uc > 500 )
bill = uc * 2.25;
else if ( uc <= 499 && uc >= 300 )
bill = uc * 1.75;
-- 6 --
else if ( uc <= 299 && uc >= 100 )
bill = uc * 1.00;
else if ( uc <= 99 && uc >= 1 )
bill = uc * 0.75;
ba = bill + 150;
// this part is to display the bill
System.out.println("\n TELANGANA STATE ELECTRICITY BOARD");
System.out.println("\n ABIDS, HYDERABAD.");
System.out.println("\n To display bill for DOMESTIC type of CONSUMERS");
System.out.println("\n BILL");
System.out.println("\n Consumer Name :-"+nm);
System.out.println("\n Consumer Number :-"+cono);
System.out.println("\n Consumer Meter Number :-"+mtrno);
System.out.println("\n Initial Reading :-"+ir);
System.out.println("\n Final Reading :-"+fr);
System.out.println("\n Units Consumed :-"+uc);
System.out.println("\n Bill calculated as per rule :-"+bill);
System.out.println("\n Billable amount with service tax of Rs.150/- :-"+ba);
break;
case 2:
System.out.println("\n 2. To calculate for BUSINESS type of CONSUMERS");
// this part id to input the values for variables
System.out.println("\n ENTER the Consumer’s Name :- ");
nm = br.readLine();
System.out.println("\n ENTER the Consumer’s Number :- ");
cono = Integer.parseInt(br.readLine());
System.out.println("\n ENTER the Meter number :- ");
mtrno = Integer.parseInt(br.readLine());
System.out.println("\n ENTER the Initial Readings :- ");
ir = Integer.parseInt(br.readLine());
System.out.println("\n ENTER the final Readings :- ");
fr = Integer.parseInt(br.readLine());
// this part id to calculate the bill
uc = fr - ir;
if ( uc > 500 )
bill = uc * 3.75;
else if ( uc <= 499 && uc >= 300 )
bill = uc * 2.50;
else if ( uc <= 299 && uc >= 100 )
bill = uc * 1.75;
else if ( uc <= 99 && uc >= 1 )
bill = uc * 1.25;
ba = bill + 250;
// this part is to display the bill
System.out.println("\n TELANGANA STATE ELECTRICITY BOARD");
System.out.println("\n ABIDS, HYDERABAD.");
System.out.println("\n To display bill for BUSINESS type of CONSUMERS");
System.out.println("\n BILL");
System.out.println("\n Consumer Name :-"+nm);
System.out.println("\n Consumer Number :-"+cono);
System.out.println("\n Consumer Meter Number :-"+mtrno);
System.out.println("\n Initial Reading :-"+ir);
System.out.println("\n Final Reading :-"+fr);
System.out.println("\n Units Consumed :-"+uc);
System.out.println("\n Bill calculated as per rule :-"+bill);
System.out.println("\n Billable amount with service tax of Rs.250/- :-"+ba);
break;
-- 7 --
case 3:
System.out.println("\n 3. To calculate for INDUSTRIAL type of CONSUMERS");
// this part id to input the values for variables
System.out.println("\n ENTER the Consumer’s Name :- ");
nm = br.readLine();
System.out.println("\n ENTER the Consumer’s Number :- ");
cono = Integer.parseInt(br.readLine());
System.out.println("\n ENTER the Meter number :-");
mtrno = Integer.parseInt(br.readLine());
System.out.println("\n ENTER the Initial Readings :- ");
ir = Integer.parseInt(br.readLine());
System.out.println("\n ENTER the final Readings :- ");
fr = Integer.parseInt(br.readLine());
// this part id to calculate the bill
uc = fr - ir;
if ( uc > 500 )
bill = uc * 5.75;
else if ( uc <= 499 && uc >= 300 )
bill = uc * 3.50;
else if ( uc <= 299 && uc >= 100 )
bill = uc * 2.50;
else if ( uc <= 99 && uc >= 1 )
bill = uc * 1.75;
ba = bill + 450;
// this part is to display the bill
System.out.println("\n TELANGANA STATE ELECTRICITY BOARD");
System.out.println("\n ABIDS, HYDERABAD.");
System.out.println("\n To display bill for INDUSTRIAL type of CONSUMERS");
System.out.println("\n BILL");
System.out.println("\n Consumer Name :-"+nm);
System.out.println("\n Consumer Number :-"+cono);
System.out.println("\n Consumer Meter Number :-"+mtrno);
System.out.println("\n Initial Reading :-"+ir);
System.out.println("\n Final Reading :-"+fr);
System.out.println("\n Units Consumed :-"+uc);
System.out.println("\n Bill calculated as per rule :-"+bill);
System.out.println("\n Billable amount with service tax of Rs.450/- :-"+ba);
break;
default:
System.out.println("WRONG CHOICE input must be in between 1 & 3");
} // end of ‘switch()’ block
} // end of ‘main()’ function
} // end of the class ‘ElectBill’

NOTE :- Write the VARIABLE DESCRIPTION for the above program.

You might also like