FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Java/07-Java-Programs/030-Menu-Driven-Program.java at main · shaikbasha-dev/Java · GitHub
shaikbasha-dev
/
Java
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
1
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
Java
/
07-Java-Programs
/
030-Menu-Driven-Program.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
139 lines (107 loc) · 4.33 KB
Breadcrumbs
Java
/
07-Java-Programs
/
030-Menu-Driven-Program.java
Copy path
File metadata and controls
139 lines (107 loc) · 4.33 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
* ============================================================================
* Program Name : Menu Driven Program
* File Name : 030-Menu-Driven-Program.java
* Class Name : MenuDrivenProgram
*
* Description:
* This program demonstrates a simple menu-driven application using the
* switch statement. The user can perform basic arithmetic operations by
* selecting an option from the menu.
*
* Objective:
* - Understand menu-driven programming in Java.
* - Learn how to use the switch statement with user input.
* - Perform arithmetic operations based on the selected menu option.
*
* Menu:
* 1. Addition
* 2. Subtraction
* 3. Multiplication
* 4. Division
* 5. Exit
*
* Author : Shaik Mahaboob Basha
* Repository : 01-Core-Java
* Folder : 08-Java-Programs
* ============================================================================
*/
import
java
.
util
.
Scanner
;
public
class
MenuDrivenProgram
{
// The main() method is the entry point of every Java application.
public
static
void
main
(
String
[]
args
) {
// Create a Scanner object to read input from the keyboard.
Scanner
scanner
=
new
Scanner
(
System
.
in
);
// Display the application title.
System
.
out
.
println
(
"========== MENU DRIVEN PROGRAM =========="
);
// Display the available menu options.
System
.
out
.
println
(
"1. Addition"
);
System
.
out
.
println
(
"2. Subtraction"
);
System
.
out
.
println
(
"3. Multiplication"
);
System
.
out
.
println
(
"4. Division"
);
System
.
out
.
println
(
"5. Exit"
);
// Ask the user to choose an option.
System
.
out
.
print
(
"Enter Your Choice: "
);
// Read the user's menu choice.
int
choice
=
scanner
.
nextInt
();
// Check whether the selected option is Exit.
if
(
choice
==
5
) {
// Display the exit message.
System
.
out
.
println
(
"Thank You! Program Exited Successfully."
);
// Example Output: Thank You! Program Exited Successfully.
// Close the Scanner object.
scanner
.
close
();
// Terminate the program.
return
;
}
// Ask the user to enter the first number.
System
.
out
.
print
(
"Enter the First Number: "
);
// Read the first number.
double
firstNumber
=
scanner
.
nextDouble
();
// Ask the user to enter the second number.
System
.
out
.
print
(
"Enter the Second Number: "
);
// Read the second number.
double
secondNumber
=
scanner
.
nextDouble
();
// Execute the selected operation.
switch
(
choice
) {
// Perform addition.
case
1
:
// Display the addition result.
System
.
out
.
println
(
"Result: "
+ (
firstNumber
+
secondNumber
));
// Example Output: Result: 30.0
break
;
// Perform subtraction.
case
2
:
// Display the subtraction result.
System
.
out
.
println
(
"Result: "
+ (
firstNumber
-
secondNumber
));
// Example Output: Result: 10.0
break
;
// Perform multiplication.
case
3
:
// Display the multiplication result.
System
.
out
.
println
(
"Result: "
+ (
firstNumber
*
secondNumber
));
// Example Output: Result: 200.0
break
;
// Perform division.
case
4
:
// Check whether the second number is zero.
if
(
secondNumber
==
0
) {
// Display an error message.
System
.
out
.
println
(
"Error! Division by zero is not allowed."
);
// Example Output: Error! Division by zero is not allowed.
}
else
{
// Display the division result.
System
.
out
.
println
(
"Result: "
+ (
firstNumber
/
secondNumber
));
// Example Output: Result: 5.0
}
break
;
// Execute this block for an invalid menu option.
default
:
// Display an invalid choice message.
System
.
out
.
println
(
"Invalid Choice! Please select a valid menu option."
);
// Example Output: Invalid Choice! Please select a valid menu option.
}
// Close the Scanner object to release system resources.
scanner
.
close
();
}
}
Back
|
FazBrowse Home
|
New Git URL