Java Coding Programs - Chirag Khimani
Java Coding Programs - Chirag Khimani
com
“You don’t learn to walk by following rules. You learn by doing, and by falling over.”
- Richard Branson
Learning programming is like mastering any other skill: it requires practice, repetition,
OceanofPDF.com
OceanofPDF.com
Table of Contents
JAVA BASICS
PROGRAMS
OceanofPDF.com
JAVA BASIC PROGRAMS
O u tp u t
Hello World
Write a program to print “Hello” in the 1st line and “World” in the 2nd line
O u tp u t
Hello
World
// Sentence (String)
O u tp u t
127
128
500
1032423
1.5
int a = 10;
O u tp u t
10.0
10
result = a / c * b + b * a / c - a * c;
O u tp u t
int a = 5;
int b = ++a;
O u tp u t
int a = 5;
int b = a++;
O u tp u t
O u tp u t
true
import java.util.Scanner;
O u tp u t
JAVA OPERATORS
OceanofPDF.com
JAVA OPERATORS
import java.util.Scanner;
Output
10
20
Sum=30
import java.util.Scanner;
Output
10
20
import java.util.Scanner;
Output
Enter a number
10
Write a program to calculate simple interest based on principle, rate of interest and
number of years
import java.util.Scanner;
principleAmount = input.nextDouble();
Output
1000
6.5
Write a program to take age from user in years and display his age in a month, days
and minutes
import java.util.Scanner;
System. out .println(“ You are “ + age * 365 + “ days old ”);
System. out .println(“ You are “ + age * 365 * 60 + “ minutes old ”);
Output
23
Write a program to take total bill amount and discount percentage from user and print
import java.util.Scanner;
10
Output
1200
10
import java.util.Scanner;
Output
10
20
11
Write a program to swap values of two variables without using third variable using
import java.util.Scanner;
Output
10
20
Write a program to swap values of two variables without using third variable using
import java.util.Scanner;
12
// Variable Declaration
Write a program to take the temperature in fahrenheit and print it into the celsius
import java.util.Scanner;
13
Output
98.6
Write a program to get marks of three subject from user and print result in percentage
import java.util.Scanner;
Output
60
70
14
Write a program to convert mega bytes into the kilo bytes (1 MB = 1024 KB)
import java.util.Scanner;
Output
50
Write a program to get number of days from user and convert into the seconds
import java.util.Scanner;
15
Output
Write a program to find area of the circle ( Area of Circle = PI * Radius * Radius )
import java.util.Scanner;
Output
Write a program to get weight in kilogram and convert it into lbs (1 kg = 2.2 lbs)
import java.util.Scanner;
16
weightInKg = input.nextDouble();
Output
Write a program to convert distance from kilometer to miles (1 mile = 1.60934 km)
import java.util.Scanner;
Output
100
Write a program to take number from user and display its last digit
import java.util.Scanner;
17
// Variable Declaration
Output
Enter a number
1432
Write a program to take number of balls from user and convert it into the overs and
import java.util.Scanner;
18
Output
17
Write a program to calculate salary of the employee based on number of hours they
Rate as follow
___________________________________________
Data
import java.util.Scanner;
Output
Write a program to print the discount obtain by the customer based on the total bill
19
import java.util.Scanner;
Output
Write a program to print total shipping cost to the customer including tax based on
import java.util.Scanner;
20
Output
200
Write a program to get weight and height from user and calculate BMI units
import java.util.Scanner;
Output
72
172
21
Write a program to find volume of the Cuboid based on length, width and height
import java.util.Scanner;
Output
12
12
Write a program to find gross salary of the employee from the basic salary
22
import java.util.Scanner;
Output
3500
23
CONDITIONAL
STATEMENTS
OceanofPDF.com
CONDITIONAL STATEMENTS
Write a program to take number from user and print if it is divisible by 5 or not
import java.util.Scanner;
Output
10
10 is divisible by 5
import java.util.Scanner;
25
} else {
Output
55
Odd
Write a program to print greatest number out of two numbers (Assume, numbers are
not equal)
import java.util.Scanner;
Output
34
12
34 is greatest
26
import java.util.Scanner;
Output
10
Number is positive
Input Output
____________________________
1 31 day
Example
import java.util.Scanner;
27
if (month == 1) {
} else if (month == 2) {
Output
31 days
Write a program to print number of days in a given month using Switch Statement
Input Output
____________________________
1 31 days
Example
28
import java.util.Scanner;
Output
31 days
29
Write a program to print name of the day from the given number
Input Output
____________________________
0 Sunday
Example
import java.util.Scanner;
Output
12
30
Marks Grade
____________________________
90 to 100 A+
Example
import java.util.Scanner;
Output
55
C+
31
Write a program to calculate final bill of customer after giving appropriate discount
Amount Discount
_______________________________________
Example
import java.util.Scanner;
Output
9500
32
Input Output
__________________________________________
import java.util.Scanner;
Output
33
53
Vowel characters - a, e, i, o, u
Input Output
__________________________________________
Example
33
import java.util.Scanner;
char c = ‘ r ’;
Output
Not Vowel
Please brainstorm and write above program to include capital letters as well
Input Output
__________________________________________
Example
import java.util.Scanner;
34
switch ( choice ) {
case “Add”:
break ;
Output
13
15
Write a program to take username and password from user and print appropriate
dbUsername dbPassword
Data
Example
35
Output
1234
3333
Incorrect password
Rating Message
__________________________________________
5 Very Good
Example
36
import java.util.Scanner;
Output
Average
Input Output
__________________________________________
Example
37
import java.util.Scanner;
38
Output
365
Write a program to reads power consumed in units and print amount to be paid by
customer
________________________________________________________________________________
Data
Example
import java.util.Scanner;
Output
672
39
Write a program to print FIFA world cup team captain based on team name entered by
user
Team Captain
______________________________________________
Data
Example
40
case “Cameroon”:
break ;
default :
Output
Portugal
Cristiano Ronaldo
Write a program to print a welcome message to the passenger and notify them to do
security checks if they are not coming from connecting flight, and at the end print “enjoy
your flight”
Output Input
Example
41
Output
no
Write a program to print a welcome message to the traveler and notify them of a 10%
discount if they are returning customers, at the end print the message “Enjoy your stay
here”
Output Input
Example
Output
Yes
42
Write a program to get three subject marks from the user and print the total percentage
secured by the student. If the percentage is more than or equal to 70 print the message
Output
80
75
Write a program to print total shipping cost to the customer including tax based on cost
entered by user, tax rate is decided based on cost as shown in the table below
_________________________________
Data
43
Output
4300
Write a program to print obesity level of the user based on bmi value calculated from
44
weight = input.nextDouble();
Output
72
170
Write a program to find smallest number out of two numbers (Assume both numbers
45
num2 = input.nextInt();
Output
45
56
45 is smallest
Write a program to get age from user and check if user is Teenager or not
import java.util.Scanner;
Output
17
46
Write a program to find smallest number out of three numbers (Assume numbers are
not equal)
import java.util.Scanner;
Output
32
54
47
Write a program to take age and amount from user and print rate of interest of fixed
_______________________________________________________
Data
import java.util.Scanner;
Output
54
35000
7.0%
48
Write a program to take age and weight from the user and check if they are eligible for
___________________________________________________________________________________
Data
import java.util.Scanner;
Output
43
65
49
Write a program to take salary and existing loan amount from user and decide their
_______________________________________________________________________________
Data
import java.util.Scanner;
50
Output
35000
15000
25000
Write a program to calculate income tax of the employee from the total salary
_________________________________________________________________________________
Up to 3000 0%
Data
import java.util.Scanner;
51
Output
18000
52
FOR LOOP
OceanofPDF.com
FOR LOOP
O u tp u t
if (i % 2 == 0) {
1
54
}
APPROACH
O u tp u t
10
O u tp u t
10
55
int num = 5;
O u tp u t
5*1=5
5*2=10
5*3=15
5*4=20
5*5=25
if (i % 3 == 0) {
}
APPROACH
56
O u tp u t
12
15
// Because we are printing two lines in one iteration so we’ll execute loop 5 time only
O u tp u t
10
// Initialize sum variable to 0. This variable will hold the sum of the numbers.
int sum = 0;
57
sum = sum + i;
O u tp u t
55
int sum = 0;
O u tp u t
30
int sum = 0;
O u tp u t
593
58
int sum = 0;
O u tp u t
19
double sum = 0;
O u tp u t
7.980122655122655
Can you guess the answer, why we need to take i double instead of int?
int sum = 0;
59
sum = sum + i;
O u tp u t
55
Java program to find sum of 1^2 + 2^2 + 3^2 + 4^2 + 5^2 +...+ 10^2
int sum = 0;
O u tp u t
385
import java.util.Scanner;
// We will store multiplication in mul variable, and we will start with initial value 1
60
O u tp u t
120
Input Output
____________________________
6 1,2,3,6
Example
import java.util.Scanner;
O u tp u t
12
Factors: 1 2 3 4 6 12
61
Perfect number is a positive integer that is equal to the sum of its positive
Input Output
____________________________
Example
import java.util.Scanner;
O u tp u t
28
62
import java.util.Scanner;
O u tp u t
13
Prime
if (i % 2 == 1) {
63
O u tp u t
int num = 1;
O u tp u t
11
20
28
35
int num = 1;
64
O u tp u t
16
65
WHILE LOOP
OceanofPDF.com
WHILE LOOP
int i = 1;
Output
int i = 1;
67
Output
10
int i = 1;
Output
16
Write a program to print each digit of the number into separate line in reverse order
Input Output
____________________________
5232 2
Example
68
import java.util.Scanner;
Output
35434
Input Output
____________________________
5232 4
Example
import java.util.Scanner;
69
Output
45448
Write a program to print sum of each digits from the given number
Input Output
____________________________
5232 12
Example
import java.util.Scanner;
70
Output
6456789
45
Input Output
____________________________
73453 7
Example
import java.util.Scanner;
71
Output
346454
Input Output
____________________________
73453 35337
Example
import java.util.Scanner;
Output
12345
54321
72
Input Output
____________________________
12321 Palindrome
import java.util.Scanner;
Output
643534
Not Palindrom
Input Output
____________________________
20 0 1 1 2 3 5 8 13
Example
import java.util.Scanner;
73
Output
12
0 1 1 2 3 5 8
Armstrong number is a number that is equal to the sum of cubes of its digits
Input Output
_________________________________________
Example
import java.util.Scanner;
74
Output
153
Armstrong
import java.util.Scanner;
75
Output
1101
13
import java.util.Scanner;
Output
17
10001
Java program to find sum of digit of a given number until the final sum is in single digit
Input Output
_________________________________________
864 8 + 6 + 4 = 18
1 + 8
Example
import java.util.Scanner;
76
Output
34567
Given number is Magic number, if the sum of its digits are calculated till a
single digit is 1
Input Output
Example
import java.util.Scanner;
77
int sum = 0;
Output
325
It is Magic Number
A number is called a spy number if the sum and product of its digits are equal
Input Output
_________________________________________
Example
import java.util.Scanner;
78
Output
217
79
DO WHILE LOOP
OceanofPDF.com
DO WHILE LOOP
Write a program to print addition of two numbers until user enters ‘No’
Input Output
_______________________________________________________________
10 20 30
Example
import java.util.Scanner;
Output
10 20
30
81
Press 2 for No
Write a program to check given number is prime or not until user enters ‘No’
Input Output
_______________________________________________________________
20 Not prime
Example
import java.util.Scanner;
82
count++;
// If number of divisor is exactly 2 then it is prime number as it is divisible only by 1 and itself
Output
20
Not Prime
Write a program to Develop ATM machine algorithm using do while loop that supports
three operation
1. Withdraw
2. Deposit
83
import java.util.Scanner;
84
Output
10000
1. Withdraw
2. Deposit
85
NESTED LOOP
OceanofPDF.com
NESTED LOOP
12345
12345
12345
87
88
89
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
90
A A A A A
B B B B B
C C C C C
D D D D D
91
int numOfRows = 5;
char ch = ‘ A ’;
J J J J
A A A A
V V V V
A A A A
92
93
if (i == 1 || i == numOfRows || j == 1 || j == numOfRows) {
94
1 1 1 1 1
0 0 0 0 0
1 1 1 1 1
95
} else {
* * * * *
* *
* *
* *
96
* * * * *
* * * * *
* *
* *
* * * * *
* * * * *
* *
* *
97
98
99
int numOfRows = 5;
100
21
321
4321
101
1 0
1 0 0
102
12
123
1234
22
333
4444
103
int numOfRows = 5;
**
***
****
104
CH
CHI
CHIR
54
543
5432
105
int numOfRows = 5;
45
345
2345
106
11
000
1111
10
100
1000
107
int numOfRows = 5;
222
33333
4444444
108
***
*****
*******
111
00000
1111111
109
int numOfRows = 5;
121
12321
1234321
110
101
11011
1110111
111
5
5
545
545
54345
54345
5432345
454
34543
2345432
112
CHC
CHIHC
CHIRIHC
113
101
10001
1000001
114
12345
1234
123
115
*****
****
***
116
12345
2345
345
117
54321
4321
321
118
12345
1234
123
12
55555
4444
333
22
119
int numOfRows = 5;
*****
****
***
**
120
54321
5432
543
54
12345
2345
345
45
121
11111
2222
333
44
122
54321
54321
4321
4321
321
321
21
CHIRAG
CHIRA
CHIR
CHI
123
CHIRAG
HIRAG
IRAG
RAG
124
555555555
555555555
4444444
4444444
33333
33333
222
123454321
1234321
12321
121
125
000000000
1111111
00000
111
126
*********
*********
*******
*******
*****
*****
***
543212345
5432345
54345
545
127
123454321
2345432
34543
454
128
CHIRAGARIHC
CHIRARIHC
CHIRIHC
100000001
1000001
10001
101
129
int numOfRows = 5;
222
33333
4444444
130
***
*****
*******
131
111
00000
1111111
132
121
12321
1234321
133
int numOfRows = 5;
134
0
0
101
101
11011
11011
1110111
135
} else {
545
54345
5432345
136
137
CHC
CHIHC
CHIRIHC
138
139
1
1
101
101
10001
10001
1000001
140
} else {
141
STRING PROGRAM
OceanofPDF.com
STRING PROGRAM
Input Output
_____________________________________________________________________
import java.util.Scanner;
Output
Enter a String
10
Write a program to count number of vowels, digit, spaces from the string
Input Output
________________________________________________________________________________
import java.util.Scanner;
143
switch (str.charAt(i)) {
Output
Enter a String
10
Input Output
____________________________________________________________________
144
import java.util.Scanner;
Output
Enter a String
Write a program to count number of words of String without using library function
Input Output
___________________________________________________________________
import java.util.Scanner;
145
Output
Enter a String
Input Output
________________________________________________________________________________
import java.util.Scanner;
Output
Enter a String
Enter a character
146
Input Output
___________________________________________
Example
import java.util.Scanner;
}
APPROACH
Output
Elon Musk
Write a program to take full name from the user and print initials from the name
Input Output
______________________________________________
import java.util.Scanner;
147
Output
Elon Musk
Initials - E.M.
Input Output
_____________________________________________________
Example
import java.util.Scanner;
148
Output
Spaces - 4
Two strings are said to be anagrams, if they contain the same characters but
in a different order
Input Output
Example
import java.util.Scanner;
Output
Earth
149
Heart
Anagram
Input Output
Example
import java.util.Scanner;
Output
moodoom
Palindrome
Input Output
______________________________________________________
150
import java.util.Scanner;
Output
Input Output
______________________________________________________________________________
import java.util.Scanner;
151
Output
Java
and
Input Output
________________________________________________________________________________
import java.util.Scanner;
152
Output
Learning java programming is not easy but once you learn java it is easy and interesting
programming
Input Output
______________________________________________________________________________
import java.util.Scanner;
153
// If we find any word that is greater than our current max word then we will assign new
minLength = words[i].length();
minString = words[i];
Output
Learning java programming is not easy but once you learn java it is easy and interesting
is
Write a program to find length of the String without using the length() function
Input Output
_____________________________________
programming 11
Example
import java.util.Scanner;
import java.util.Scanner;
2
154
int count = 0;
Output
programming
11
import java.util.Scanner;
Output
155
java 4
revision 8
notes 5
and 3
java 4
Write a program to replace all spaces in the String with the back slash
Input Output
______________________________________________________________________________
import java.util.Scanner;
Output
Learning java programming is not easy but once you learn java it is easy and interesting
Learning\java\programming\is\not\easy\but\once\you\learn\java\it\is\easy\and\interesting
Write a program to encode the given String using Caeser Cipher Algorithm
Input Output
_____________________________________________________________________________
abcdefghijklmnopqrstuvwxyz
Example
156
import java.util.Scanner;
Output
easyjava
jfxdofaf
Input Output
______________________________________________________________________________
import java.util.Scanner;
157
Output
Learning java programming is not easy but once you learn java it is easy and interesting
gninrael avaj gnimmargorp si ton ysae tub ecno uoy nrael avaj ti si ysae dna gnitseretni
Input Output
_____________________________________________
programming puzzules r
Example
import java.util.Scanner;
158
// If value of isFound is still false then it means that we do not have unique character of String
//is empty
if (!isFound) {
Output
javajava
-1
Input Output
______________________________________________________________________________
import java.util.Scanner;
159
Output
Write a program to check given string is palindrome or not ignoring the space and case
Input Output
___________________________________________________
import java.util.Scanner;
Output
Yo banana boy
Palindrome
160
Input Output
________________________________________
aabbbccddd a2b3c2d4
Example
import java.util.Scanner;
Output
aaabcccddddd
a3
b1
c3
Input Output
______________________________________________________________________________________
Learning java is now feels easy ease feels now is java Learning
Example
161
import java.util.Scanner;
Output
162
USING ARRAY
OceanofPDF.com
USING ARRAY
Input Output
_____________________________________________________________________
import java.util.Scanner;
Output
56 24 75 43 65
24
Input Output
_____________________________________________________________________
164
import java.util.Scanner;
Output
56 24 75 43 65
75
Input Output
_____________________________________________________________________
import java.util.Scanner;
165
System .out.println(sum);
Output
56 23 74 45 65
263
Input Output
_____________________________________________________________________
import java.util.Scanner;
166
Output
56 24 75 43 65
183
80
Input Output
_____________________________________________________________________
Example
import java.util.Scanner;
Output
56 -24 -75 43 65
167
Input Output
____________________________________________________________________________
import java.util.Scanner;
Output
56 -24 -75 43 65
65
65 is in the array
168
Input Output
_____________________________________________________________________
import java.util.Scanner;
Output
54 54 54 56 56
54
56
169
Write a program to print the elements of an array present in the odd position
Input Output
_____________________________________________________________________
import java.util.Scanner;
Output
45 23 76 23 56
23
23
Input Output
__________________________________________________________________________________________
import java.util.Scanner;
170
numbers[i] = input.nextInt();
// Check each number with current number and if we find any number less than current number,
Output
45 23 76 23 56
Sorted Array
23
23
Input Output
_____________________________________________________________________
import java.util.Scanner;
171
numbers[i] = input.nextInt();
int count = 1 ;
Output
54 54 54 56 56
54
56
Write a program to segregate odd and even numbers from the array
Segregate odd numbers on left and even are on right side of the array
Input Output
Example
import java.util.Scanner;
172
numbers[i] = input.nextInt();
Output
52 54 13 77 80
Result Array
77 13 54 52 80
Write a program to find two elements in the array whose sum is equal to a given
number
Input Output
_____________________________________________________________________
Example
173
import java.util.Scanner;
Output
3 4 7 2 7
Input Output
______________________________________________________________________________________
import java.util.Scanner;
174
numbers[i] = input.nextInt();
Output
52 54 13 77 80
13
Result Array
175
Input Output
_____________________________________________________________________________________________
import java.util.Scanner;
176
Output
52 54 13 77 80
12
Write a program to convert each element of an array in reverse order for integer
array
Input Output
_________________________________________________________________________________________
Example
import java.util.Scanner;
177
Output
52 54 13 77 32
Result Array
25 45 31 77 23
Write a program to convert each element of an array in reverse order for String array
Input Output
_____________________________________________________________________________________________
Enter five String : Java Python Ruby Test Automation Result Array : avaJ nohtyP
Example
import java.util.Scanner;
Output
Result Array
178
Input Output
_____________________________________________________________________________________________
import java.util.Scanner;
Output
Test Case 1
52 54 13 77 32
179
Test Case 2
15 15 15 15 15
Input Output
__________________________________________________________________________________
import java.util.Scanner;
180
int noOfUniqueElement = 0 ;
noOfUniqueElement ++ ;
Output
56 56 76 56 43
Result Array
76
56
Input Output
_________________________________________________________________
import java.util.Scanner;
181
Output
5 4 3 2 1
Input Output
_________________________________________________________________
import java.util.Scanner;
182
secondMin = num;
Output
1 2 3 4 5
Input Output
_________________________________________________________________
import java.util.Scanner;
Output
56 56 76 56 43
43
183
Input Output
_________________________________________________________________________________________
Example
import java.util.Scanner;
Output
31 0 34 0 0
Result Array
31 34 0 0 0
184
Write a program to count number of odd and even number in the array
Input Output
____________________________________________________________________________________
Example
import java.util.Scanner;
Output
34 45 23 56 23
No of even element is 2
No of odd element is 3
Write a program to find the sum of array elements except largest and smallest
Input Output
__________________________________________________________________
Example
185
import java.util.Scanner;
Output
34 45 23 56 23
102
Write a Java program to find the length of the longest consecutive elements
Input Output
______________________________________________________________________________________
Example
186
import java.util.Scanner;
Output
3 4 7 8 9 10 43 45 46 47 9
Write a Java program to find the two elements from a given array of whose sum is
Input Output
__________________________________________________________________
Example
187
import java.util.Scanner;
Output
12 3 0 -1 1
-1 1
Write a Java program to find maximum product of two integers in a given array of
integers
Input Output
__________________________________________________________________
Example
188
import java.util.Scanner;
Output
12 3 0 -1 1
36
189
System .out.println(columnSum);
Output
Frequency=3
Write a program to find sum of each row from two dimention array
int sum = 0 ;
Output
22
13
18
190
max = data[i][j];
Output
931
191
10
USER DEFINED
METHODS
OceanofPDF.com
USER DEFINED METHODS
Input Output
_____________________________________________________________________
import java.util.Scanner;
Output
Enter a number
1441
Palindrom
if (isPalindromeNumber(i)) {
193
while (num != 0 ) {
lastDigit = num % 10 ;
Output
11 22 33 44 55 66 77 88 99 101 111 121 131 141 151 161 171 181 191
if (isPrime(i)) {
Output
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47
194
if (isArmstrongNumber(i)) {
Output
Write a user defined function fact() that takes one number and return factorial of the
given number
Input Output
_________________________________________________________________
Example
import java.util.Scanner;
195
int fact = 1 ;
fact = fact * i;
Output
Enter a number
120
Write a user defined function isEven() that takes one number and return true if given
Input Output
_________________________________________________________________
Example
import java.util.Scanner;
Output
Enter a number
23
false
196
Write a user defined function getMaxValue() that takes array of integers and return
Input Output
_________________________________________________________________
Example
import java.util.Scanner;
Output
54 76 87 23 43
87
Write a user defined function getMinValue() that takes array of integers and return
Input Output
_________________________________________________________________
Example
197
import java.util.Scanner;
Output
54 76 87 23 43
23
Write a user defined function getSum() that takes array of integers and return sum of
Input Output
_________________________________________________________________
Example
import java.util.Scanner;
198
numbers[i] = input.nextInt();
System .out.println(getMinValue(numbers));
Output
54 76 87 23 43
283
Write a program to print greatest number out of four numbers using user defined
Input Output
_________________________________________________________________
Example
import java.util.Scanner;
199
Output
67
23
Write a user defined function getRev() that takes one String value and return reverse
of that String
Input Output
_________________________________________________________________
Example
import java.util.Scanner;
200
Output
Chirag
garihC
Input Output
___________________________________________________________________________________________
import java.util.Scanner;
Output
13
11
201
11
COLLECTION
OceanofPDF.com
COLLECTION
Input Output
____________________________________________________________________________________________
import java.util.Scanner;
Output
34 54 34 56 54
34
54
Input Output
_______________________________________________________________________________
import java.util.Scanner;
203
if ( ! setOfChar.add(str.charAt(i))) {
Output
Enter a String
[ , ,r s, c, t, i, l, m, o]
Input Output
____________________________________________________________________________________________
Enter a String : This is my collection program { =4, a=1, c=2, e=1, g=1, h=1,
Example
import java.util.Scanner;
204
Output
{ =4, a=1, c=2, e=1, g=1, h=1, i=3, l=2, m=2, n=1, o=3, p=1, r=2, s=2, t=2, y=1}
Input Output
____________________________________________________________________________
import java.util.Scanner;
Output
Enter a String
[a, e, g, h, n, p, y]
Write a program to find character from the String that has a greatest frequency
Input Output
____________________________________________________________________________
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
205
Output
Enter a String
{ =3, a=5, g=2, h=3, i=2, j=1, m=3, n=1, o=1, p=2, r=2, s=1, t=1, v=1, w=1}
Write a program to get five numbers from user and sort them in ascending order
Input Output
___________________________________________________________________________________________
Enter five numbers : 45 67 23 12 43 Sorted list: [12, 23, 43, 45, 67]
Example
206
Output
45 67 23 12 43
Write a program to get five numbers from user and print all numbers in reverse
sequence
Input Output
___________________________________________________________________________________________
Example
Output
45 67 23 12 43
207
Write a program to get five numbers from user and print each number in reverse order
Input Output
___________________________________________________________________________________________
Enter five numbers : 45, 67, 23, 12, 43 Reverse elements : 54, 76, 32, 76, 34
Example
Output
45 67 23 12 43
Reverse elements:
54 76 32 21 34
Write a program to get five numbers from user twice and check if both the time they
Input Output
________________________________________________________________________
Example
208
Output
45 67 23 12 43
23 12 67 43 45
Same
Write a program to get five numbers from user twice and print common numbers
Input Output
___________________________________________________________________________________________
Example
209
Output
45 67 23 12 43
22 12 67 40 45
names.add( "Maryam" );
210
Output
Emily
Maryam
Tim
Anna
211
12
class Employee {
String employeeName;
int empId;
double salary;
boolean isEligibleForPromotion;
213
Output
Mike
9454
23500.0
true
Mike is on leave
class Bank {
String accName;
int balance;
214
account1.accName = "Mike" ;
Output
4000
10000
Write a program to demonstrate how to store list of Bank Account objects in the list
class Bank {
String accName;
int balance;
215
maxBalance = account.balance;
Output
216
13
CONSTRUCTOR
AND
OceanofPDF.com
CONSTRUCTOR AND STATIC KEYWORD
class Bank {
int accountNumber;
String accName;
int balance;
Output
1000
Mike
================
1000
218
class Bank {
int accountNumber;
String accName;
int balance;
Output
5000
Mike
================
1000
219
class Student {
void showTeacherName() {
Output
Instructor - Instructor3
Mentor - Mentor1
Instructor - Instructor3
Mentor - Mentor2
Instructor - Instructor3
220
class Student {
Output
Instructor - Instructor1
Mentor - Mentor1
221
14
INHERITANCE
AND
OceanofPDF.com
INHERITANCE AND POLYMORPHISM
// Parent Class
class BasicCalculator {
Output
sum=30
sub=-10
sum=30
Mul=200
sub=-10
223
class Bank {
Bank() {
Output
Write a program to demonstrate use of This & Super keyword with variables
class Parent{
224
Output
50
20
10
Write a program to demonstrate use of This & Super keyword with Methods
class Parent {
System .out.println(a);
Output
20
10
Write a program to demonstrate use of This & Super keyword with Constructor
class Parent {
Parent() {
225
Child() {
Output
Statement 1
Statement 2
Statement 4
Statement 3
class FlightBooking {
226
Output
class Bank {
int balance;
227
cb.displayInterestRate();
Output
5.0
7.0
class Bank {
int balance;
228
b.displayInterestRate();
Output
5.0
7.0
229
15
ENCAPSULATION
AND
OceanofPDF.com
ENCAPSULATION AND ABSTRACTION
class Bank {
231
Output
5000
28 May 1991
08 May 1990
int balance;
Output
7.0%
232
interface Page {
int PAGE_LOAD_TIME = 60 ;
233
Output
234
16
EXCEPTION HANDLING
OceanofPDF.com
EXCEPTION HANDLING
int num[] = { 32 , 54 , 21 , 62 , 34 };
Output
236
try {
} catch (Exception e) {
Output
34
237
Output
at com.java.class38.LadderIfElseExample.main(LadderIfElseExample.java:29)
method3();
Output
at com.java.class38.ThrowAndThrowsKeyword.method1(ThrowAndThrowsKeyword.java:10)
at com.java.class38.ThrowAndThrowsKeyword.method2(ThrowAndThrowsKeyword.java:14)
at com.java.class38.ThrowAndThrowsKeyword.method3(ThrowAndThrowsKeyword.java:18)
at com.java.class38.ThrowAndThrowsKeyword.main(ThrowAndThrowsKeyword.java:22)
238