Java 2
Java 2
2 / 482
3 / 482
4 / 482
5 / 482
6 / 482
7 / 482
8 / 482
9 / 482
10 / 482
11 / 482
12 / 482
13 / 482
14 / 482
15 / 482
16 / 482
17 / 482
18 / 482
19 / 482
20 / 482
21 / 482
22 / 482
23 / 482
24 / 482
25 / 482
26 / 482
27 / 482
28 / 482
29 / 482
30 / 482
31 / 482
32 / 482
33 / 482
34 / 482
35 / 482
36 / 482
37 / 482
38 / 482
39 / 482
40 / 482
41 / 482
42 / 482
43 / 482
44 / 482
45 / 482
46 / 482
47 / 482
48 / 482
49 / 482
50 / 482
51 / 482
52 / 482
53 / 482
54 / 482
55 / 482
56 / 482
57 / 482
58 / 482
59 / 482
60 / 482
61 / 482
62 / 482
63 / 482
64 / 482
65 / 482
66 / 482
67 / 482
68 / 482
69 / 482
70 / 482
71 / 482
72 / 482
73 / 482
0'<'
74 / 482
0'<'
75 / 482
76 / 482
77 / 482
78 / 482
79 / 482
80 / 482
81 / 482
82 / 482
83 / 482
84 / 482
85 / 482
86 / 482
87 / 482
88 / 482
89 / 482
90 / 482
91 / 482
92 / 482
93 / 482
94 / 482
95 / 482
96 / 482
97 / 482
98 / 482
99 / 482
100 / 482
101 / 482
102 / 482
103 / 482
104 / 482
105 / 482
106 / 482
107 / 482
108 / 482
109 / 482
110 / 482
111 / 482
112 / 482
113 / 482
114 / 482
115 / 482
116 / 482
117 / 482
118 / 482
119 / 482
120 / 482
121 / 482
122 / 482
123 / 482
124 / 482
125 / 482
126 / 482
127 / 482
128 / 482
129 / 482
130 / 482
131 / 482
132 / 482
133 / 482
134 / 482
135 / 482
136 / 482
137 / 482
138 / 482
139 / 482
140 / 482
141 / 482
142 / 482
143 / 482
144 / 482
145 / 482
146 / 482
147 / 482
148 / 482
149 / 482
150 / 482
151 / 482
152 / 482
153 / 482
154 / 482
155 / 482
156 / 482
157 / 482
158 / 482
159 / 482
160 / 482
161 / 482
162 / 482
163 / 482
164 / 482
165 / 482
166 / 482
167 / 482
168 / 482
169 / 482
170 / 482
171 / 482
172 / 482
173 / 482
174 / 482
175 / 482
176 / 482
177 / 482
178 / 482
179 / 482
180 / 482
181 / 482
182 / 482
183 / 482
184 / 482
185 / 482
186 / 482
187 / 482
188 / 482
189 / 482
190 / 482
191 / 482
192 / 482
193 / 482
194 / 482
195 / 482
196 / 482
197 / 482
198 / 482
199 / 482
200 / 482
201 / 482
202 / 482
203 / 482
204 / 482
205 / 482
206 / 482
207 / 482
208 / 482
209 / 482
210 / 482
211 / 482
212 / 482
213 / 482
214 / 482
215 / 482
216 / 482
217 / 482
218 / 482
219 / 482
220 / 482
221 / 482
222 / 482
223 / 482
224 / 482
225 / 482
226 / 482
227 / 482
228 / 482
229 / 482
230 / 482
231 / 482
232 / 482
233 / 482
234 / 482
235 / 482
236 / 482
237 / 482
238 / 482
239 / 482
240 / 482
241 / 482
242 / 482
243 / 482
244 / 482
245 / 482
246 / 482
247 / 482
248 / 482
249 / 482
250 / 482
251 / 482
252 / 482
253 / 482
254 / 482
255 / 482
256 / 482
257 / 482
258 / 482
259 / 482
260 / 482
261 / 482
262 / 482
263 / 482
264 / 482
265 / 482
266 / 482
267 / 482
268 / 482
269 / 482
270 / 482
271 / 482
272 / 482
273 / 482
274 / 482
275 / 482
276 / 482
277 / 482
278 / 482
279 / 482
280 / 482
281 / 482
282 / 482
283 / 482
284 / 482
285 / 482
286 / 482
287 / 482
288 / 482
289 / 482
290 / 482
291 / 482
292 / 482
293 / 482
294 / 482
295 / 482
296 / 482
297 / 482
298 / 482
299 / 482
300 / 482
301 / 482
302 / 482
303 / 482
304 / 482
305 / 482
306 / 482
307 / 482
308 / 482
309 / 482
310 / 482
311 / 482
312 / 482
313 / 482
314 / 482
315 / 482
316 / 482
317 / 482
318 / 482
319 / 482
320 / 482
321 / 482
322 / 482
323 / 482
324 / 482
325 / 482
326 / 482
327 / 482
328 / 482
329 / 482
EXCEPTION HANDLING
ERROR:
Compile Time Error:
1) Lexical Error:
class LE
{
public static void main(String[] args)
{
int i=10;
nit j=20;
[Link](+i,+j);
}
}
Output:
>javac [Link]
[Link]: error: cannot find symbol
nit j=20;
^
symbol: class nit
location: class LE
1 error
2) Syntax Error:
class SE
{
public static void main(String[] args)
{
int i=10;
j int =20;
[Link](+i,+j);
}
}
330 / 482
Output:
javac [Link]
[Link]: error: not a statement
j int =20;
^
[Link]: error: ';' expected
j int =20;
^
3) Semantic Error:
class SEE
{
public static void main(String[] args)
{
int i=10;
boolean b = true;
char c= i+b;
[Link](+c);
}
}
>javac [Link]
[Link]: error: bad operand types for binary operator '+'
char c= i+b;
^
first type: int
second type: Boolean
int x;
int y;
[Link](x);
y = 5;
[Link](y);
}
}
Output:
>javac [Link]
[Link]: error: variable x might not have been
initialized
[Link](x);
^
[Link](byteValue);
}
}
Output:
-24
332 / 482
RunTime Error:
6) Linkage Error
class LE
{
public static void main(String[] args)
{
[Link]("hello csmb");
}
}
Output:
java LE
Error: LinkageError occurred while loading main class LE
[Link]: Incompatible magic value 71528292 in
class file LE
7) StackOverFlow Error
class SOE
{
public static void m1()
{
m2();
}
333 / 482
Output:
java SOE
Exception in thread "main" [Link]
at SOE.m2([Link])
at SOE.m1([Link])
at SOE.m2([Link])
at SOE.m1([Link])
at SOE.m2([Link])
at SOE.m1([Link])
at SOE.m2([Link])
at SOE.m1([Link])
at SOE.m2([Link])
at SOE.m1([Link])
at SOE.m2([Link])
at SOE.m1([Link])
at SOE.m2([Link])
334 / 482
at SOE.m1([Link])
at SOE.m2([Link])
at SOE.m1([Link])
at SOE.m2([Link])
at SOE.m1([Link])
at SOE.m2([Link])
at SOE.m1([Link])
at SOE.m2([Link])
at SOE.m1([Link])
at SOE.m2([Link])
at SOE.m1([Link])
at SOE.m2([Link])
at SOE.m1([Link])
continues
Output:
java OOM
OutOfMemoryError: Requested array size exceeds VM limit
[Link]: Requested array size exceeds VM limit
at [Link]([Link])
class DEH
{
public static void main(String[] args)
{
doStuff();
}
336 / 482
Output:
java DEH
Exception in thread "main" [Link]: / by zero
at [Link]([Link])
at [Link]([Link])
at [Link]([Link])
Exception Handling:
A)Checked Exception:
import [Link];
import [Link];
337 / 482
import [Link];
import [Link];
}
Output:
javac [Link]
[Link]: error: unreported exception FileNotFoundException; must be
caught or declared to be thrown. ^
[Link]: error: unreported exception IOException; must be caught or
declared to be thrown
import [Link];
import [Link];
import [Link];
import [Link];
Output:
javac [Link]
[Link]: error: unreported exception FileNotFoundException; must be
caught or declared to be thrown. ^
[Link]: error: unreported exception IOException; must be caught or
declared to be thrown.
import [Link].*;
Output:
javac [Link]
340 / 482
import [Link];
import [Link];
}
Output:
javac [Link]
[Link]: error: unreported exception ParseException; must be caught or
declared to be thrown
class IE
{
public static void main(String[] args)
{
[Link](1000);
}
}
Output:
javac [Link]
[Link]: error: unreported exception InterruptedException; must be caught or
declared to be thrown
[Link](1000);
B) UnChecked Exception:
15)Arithmetic Exception
class DEH
{
public static void main(String[] args)
342 / 482
{
doStuff();
}
public static void doStuff()
{
domoreStuff();
}
public static void domoreStuff()
{
[Link](10/0);
}
}
Output:
java AE
Exception in thread "main" [Link]: / by zero
at [Link]([Link])
Output:
>java NPE
Exception in thread "main" [Link]: Cannot invoke
"[Link]()" because "<local1>" is null
at [Link]([Link])
class A
{
}
class B extends A
{
}
class CCE
{
public static void main(String[] args)
{
A a = new A();
B b =(B)a;
}
}
Output:
java CCE
Exception in thread "main" [Link]: class A cannot be
cast to class B (A and B are in unnamed module of loader 'app')
344 / 482
at [Link]([Link])
Output:
java SIOBE
StringIndexOutOfBoundsException caught: String index out of range: 10
Output:
java AIOBE1
Exception in thread "main" [Link]: Index
5 out of bounds for length 3
at [Link]([Link])
import [Link];
import [Link];
import [Link];
Output:
javac [Link]
C:\Users\Shanti\Desktop\csmb java
lab\throwable\ExceptionHandling\CheckedExceptions>java FNF
FileNotFoundException caught: C:\Users\Shanti\Desktop\csmb java
lab\ExceptionHandling\CheckedExceptions\[Link] (The system cannot
find the path specified)
Program continues after handling FileNotFoundException.
import [Link].*;
Connection mysqlConnection =
[Link](mysqlUrl, mysqlUsername, mysqlPassword);
try
{
[Link](10/0);
}
catch (ArithmeticException e)
{
[Link](10/2);
}
}
Output:
java AEH
5
contiued after handling exception
350 / 482
Output:
java SIOBE
StringIndexOutOfBoundsException caught: String index out of range: 10
Program continues after handling StringIndexOutOfBoundsException.
import [Link];
import [Link];
try {
[Link]("Enter a number: ");
int numerator = [Link]();
[Link]();
}
Output:
>java TryWithMultipleCatch
Enter a number: 4
Enter another number: 7
Result of division: 0
>java TryWithMultipleCatch
Enter a number: 2
Enter another number: g
Error: Please enter valid integers for numerator and denominator.
>java TryWithMultipleCatch
Enter a number: 6
Enter another number: 0
Error: Cannot divide by zero. Please enter a non-zero denominator.
Finally:
25) finally block to close FILE:
353 / 482
import [Link];
import [Link];
import [Link];
try {
int byteData;
while ((byteData = [Link]()) != -1) {
[Link](byteData);
}
354 / 482
try {
if (fis != null) {
[Link]();
}
if (fos != null) {
[Link]();
}
} catch (IOException e) {
[Link]("Error closing streams: " + [Link]());
}
}
}
}
Throw:
{
super(message);
}
}
class ATM {
private double balance = 1000;
public void withdraw(double amount)
{
try {
if (amount > balance)
{
throw new InsufficientBalanceException("Insufficient balance to
withdraw " + amount);
}
balance -= amount;
[Link]("Withdrawal successful! Remaining balance: " +
balance);
} catch (InsufficientBalanceException e) {
[Link]("Error: " + [Link]());
}
}
}
public class throw1
{
public static void main(String[] args)
{
ATM atm = new ATM();
[Link](1500);
356 / 482
}
}
Output:
java throw1
Error: Insufficient balance to withdraw 1500.0
Throws:
}
}
Output:
java FileOutput2
Data has been written to the file.
[Link]("InvalidNumberFormatException: " +
[Link]());
}
}
if () {
throw new NumberFormatException("Contains non-digit characters");
}
[Link]("Valid number");
}
}
Output:
Valid number
InvalidArrayIndexOutofBounds Exception: Exceeds length of 10
Try_With_Resources
try {
copyFile(inputFile, outputFile);
[Link]("File copied successfully!");
} catch (IOException e)
{
[Link]("Error: " + [Link]());
}
}
String line;
while ((line = [Link]()) != null)
{
[Link](line);
}
}
361 / 482
}
}
class SharedResource
int counter = 0;
SharedResource sharedResource;
[Link] = sharedResource;
[Link]();
// Starting threads
[Link]();
[Link]();
Output:
class SharedResource
int counter = 0;
void increment()
// Non-critical section
synchronized (lock)
// Non-critical section
SharedResource sharedResource;
[Link] = sharedResource;
394 / 482
[Link]();
// Starting threads
[Link]();
[Link]();
Output:
class SharedResource
synchronized ([Link])
[Link]();
}
396 / 482
// Starting threads
[Link]();
[Link]();
Output:
class SharedResource
int counter = 0;
try
[Link](1000);
catch (InterruptedException e)
[Link]();
counter = currentValue + 1;
[Link] = sharedResource;
[Link]();
// Starting threads
[Link]();
[Link]();
Output:
class SharedResource
try
[Link](1000);
catch (InterruptedException e)
[Link]();
}
400 / 482
[Link] = sharedResource;
[Link] = message;
[Link](message);
// Starting threads
[Link]();
401 / 482
[Link]();
[Link]();
try {
[Link]();
[Link]();
[Link]();
catch (InterruptedException e)
[Link]();
class SharedResource
try
[Link](1000);
catch (InterruptedException e)
[Link]();
[Link] = sharedResource;
[Link] = message;
[Link](message);
// Starting threads
[Link]();
[Link]();
[Link]();
try {
[Link]();
[Link]();
[Link]();
catch (InterruptedException e)
[Link]();
Output:
6) isAlive()
[Link]();
405 / 482
[Link]("main Working...");
Output:
main Working...
main Working...
main Working...
main Working...
child Working...
child Working...
main Working...
child Working...
child Working...
child Working...
406 / 482
407 / 482
408 / 482
409 / 482
410 / 482
411 / 482
412 / 482
413 / 482
414 / 482
415 / 482
416 / 482
417 / 482
418 / 482
419 / 482
420 / 482
421 / 482
422 / 482
423 / 482
424 / 482
425 / 482
426 / 482
Collections Framework:
Collection:
List:
1) ArrayList:
import [Link].*;
public class ArrayList1
{
public static void main(String args[]){
ArrayList<Integer> a=new ArrayList<Integer>();
[Link](a);
[Link](34);
[Link](0,78);
[Link](a);
[Link](5);
[Link]("Size:"+[Link]());
[Link](19);
[Link](24);
[Link](36);
[Link](48);
[Link](57);
[Link](63);
[Link](72);
[Link](86);
[Link](94);
[Link]("Size:"+[Link]());
// we cant get "capacity" for ArrayList
[Link](a);
[Link]([Link](7));
[Link]([Link](48));
[Link]([Link](1,10));
[Link](a);
[Link]([Link](5));
[Link](a);
[Link]([Link](a));
[Link](a);
}
427 / 482
Output:
[]
[78, 34]
Size:3
Size:12
[78, 34, 5, 19, 24, 36, 48, 57, 63, 72, 86, 94]
57
true
34
[78, 10, 5, 19, 24, 36, 48, 57, 63, 72, 86, 94]
36
[78, 10, 5, 19, 24, 48, 57, 63, 72, 86, 94]
true
[]
2) Vector
import [Link].*;
public class Vector1
{
public static void main(String args[]){
Vector<String> v=new Vector<String>();
[Link](v);
[Link]("Ayush");
[Link](0,"Ashish");
[Link](v);
[Link]("a");
[Link]("Size:"+[Link]());
[Link]("Capacity:"+[Link]());
428 / 482
[Link]("b");
[Link]("c");
[Link]("d");
[Link]("e");
[Link]("g");
[Link]("g");
[Link]("h");
[Link]("x");
[Link]("y");
[Link]("Size:"+[Link]());
[Link]("Capacity:"+[Link]());
[Link](v);
[Link]([Link](5));
[Link]([Link]("t"));
[Link]([Link](1,"y"));
[Link](v);
[Link]([Link](5));
[Link](v);
[Link]([Link]("g"));
[Link](v);
[Link]([Link](v));
[Link](v);
}
}
429 / 482
Output:
[]
[Ashish, Ayush]
Size:3
Capacity:10
Size:12
Capacity:20
[Ashish, Ayush, a, b, c, d, e, g, g, h, x, y]
d
false
Ayush
[Ashish, y, a, b, c, d, e, g, g, h, x, y]
d
[Ashish, y, a, b, c, e, g, g, h, x, y]
true
[Ashish, y, a, b, c, e, g, h, x, y]
true
[]
3) Stack
import [Link].*;
public class Stack1
{
public static void main(String args[]){
430 / 482
// we cant place certain value in pop method because top element only removed.
[Link](a);
[Link]([Link](5));
[Link]([Link](78));
[Link]([Link](a));
[Link](a);
}
}
Output:
[]
[78, 34]
Size:3
Capacity:10
Size:12
Capacity:20
[78, 34, 5, 19, 24, 36, 48, 57, 63, 72, 86, 94]
57
true
34
[78, 10, 5, 19, 24, 36, 48, 57, 63, 72, 86, 94]
94
86
72
[78, 10, 5, 19, 24, 36, 48, 57, 63, 72]
8
432 / 482
0
true
[]
4) LinkedList
import [Link].*;
public class LinkedList1
{
public static void main(String args[]){
LinkedList<String> v=new LinkedList<String>();
[Link](v);
[Link]("r");
[Link](0,"e");
[Link](v);
[Link]("a");
[Link]("Size:"+[Link]());
[Link]("b");
[Link]("c");
[Link]("d");
[Link]("e");
[Link]("g");
[Link]("g");
[Link]("h");
[Link]("x");
[Link]("y");
433 / 482
[Link]("Size:"+[Link]());
[Link](v);
[Link]([Link](5));
[Link]([Link]("t"));
[Link]([Link](1,"y"));
[Link](v);
[Link]([Link](5));
[Link](v);
[Link]([Link]("g"));
[Link](v);
[Link]([Link](v));
[Link](v);
}
}
Output:
[]
[e, r]
Size:3
Size:12
[e, r, a, b, c, d, e, g, g, h, x, y]
d
false
r
[e, y, a, b, c, d, e, g, g, h, x, y]
d
434 / 482
[e, y, a, b, c, e, g, g, h, x, y]
true
[e, y, a, b, c, e, g, h, x, y]
true
[]
Map:
5)HashMap
import [Link].*;
public class HashMap1
{
public static void main(String args[]){
HashMap<Integer,String> h=new HashMap<Integer,String>();
[Link](h);
[Link](1,"r");
[Link](2,"e");
[Link](h);
[Link](3,"a");
[Link]("Size:"+[Link]());
[Link](5,"b");
[Link](7,"c");
[Link](8,"d");
[Link](23,"e");
[Link](23,"e");
[Link](23,"z");
435 / 482
[Link](56,"g");
[Link](34,"g");
[Link](32,"h");
[Link](18,"x");
[Link](29,"y");
[Link]("Size:"+[Link]());
[Link](h);
[Link]([Link](23));// 23 is key to get regarding value
[Link]([Link](56));
[Link]([Link]("e"));
[Link](h);
[Link]();
[Link](h);
Output:
{}
{1=r, 2=e}
Size:3
Size:12
{32=h, 1=r, 2=e, 34=g, 18=x, 3=a, 5=b, 7=c, 23=z, 8=d, 56=g, 29=y}
436 / 482
z
true
true
{32=h, 1=r, 2=e, 34=g, 18=x, 3=a, 5=b, 7=c, 23=z, 8=d, 56=g, 29=y}
{}
6)LinkedHashMap
import [Link].*;
public class LinkedHashMap1
{
public static void main(String args[]){
LinkedHashMap<Integer,String> a=new LinkedHashMap<Integer,String>();
[Link](a);
[Link](34,"a");
[Link](5,"b");
[Link]("Size:"+[Link]());
[Link](19,"c");
[Link](24,"d");
[Link](36,"e");
[Link](48,"f");
[Link](57,"g");
[Link](63,"h");
[Link](72,"i");
[Link](a);
[Link]("Size:"+[Link]());
[Link](a);
437 / 482
[Link]([Link](57));
[Link](a);
}
}
Output:
{}
Size:2
{34=a, 5=b, 19=c, 24=d, 36=e, 48=f, 57=g, 63=h, 72=i}
Size:9
{34=a, 5=b, 19=c, 24=d, 36=e, 48=f, 57=g, 63=h, 72=i}
g
{34=a, 5=b, 19=c, 24=d, 36=e, 48=f, 63=h, 72=i}
7) TreeMap
import [Link].*;
public class TreeMap1
{
public static void main(String args[]){
TreeMap<Integer,String> a=new TreeMap<Integer,String>();
[Link](a);
[Link](34,"a");
[Link](5,"b");
[Link]("Size:"+[Link]());
[Link](78,"c");
438 / 482
[Link](3,"g");
[Link](8,"e");
[Link](7,"v");
[Link](12,"r");
[Link](1,"q");
[Link](72,"m");
[Link](a);
[Link](86,"p");
[Link](94,"z");
//values are displayed as sorted order . not in a specified way
[Link]("Size:"+[Link]());
[Link](a);
[Link]([Link](12));
[Link](a);
}
}
Output:
{}
Size:2
{1=q, 3=g, 5=b, 7=v, 8=e, 12=r, 34=a, 72=m, 78=c}
Size:11
{1=q, 3=g, 5=b, 7=v, 8=e, 12=r, 34=a, 72=m, 78=c, 86=p, 94=z}
r
{1=q, 3=g, 5=b, 7=v, 8=e, 34=a, 72=m, 78=c, 86=p, 94=z}
439 / 482
Set
8) HashSet
import [Link].*;
public class HashSet1
{
public static void main(String args[]){
HashSet<Integer> a=new HashSet<Integer>();
[Link](a);
[Link](34);
[Link](5);
[Link]("Size:"+[Link]());
[Link](19);
[Link](24);
[Link](36);
[Link](48);
[Link](57);
[Link](63);
[Link](72);
[Link](a);
[Link](72);
// try to duplicate.
[Link](a);// 72 is not stored again.
[Link](86);
[Link](94);
//values are displayed as random order . not in a specified way
440 / 482
[Link]("Size:"+[Link]());
[Link]([Link](48));
[Link](a);
[Link]([Link](57));
[Link](a);
}
}
Output:
[]
Size:2
[48, 34, 19, 36, 5, 24, 72, 57, 63]
[48, 34, 19, 36, 5, 24, 72, 57, 63]
Size:11
true
[48, 34, 19, 36, 5, 86, 24, 72, 57, 94, 63]
true
[48, 34, 19, 36, 5, 86, 24, 72, 94, 63]
9) LinkedHashSet
import [Link].*;
public class LinkedHashSet1
{
public static void main(String args[]){
LinkedHashSet<Integer> a=new LinkedHashSet<Integer>();
441 / 482
[Link](a);
[Link](34);
[Link](5);
[Link]("Size:"+[Link]());
[Link](19);
[Link](24);
[Link](36);
[Link](48);
[Link](57);
[Link](63);
[Link](72);
[Link](a);
//It follows the insertion order .
[Link]("Size:"+[Link]());
[Link]([Link](48));
[Link](a);
[Link]([Link](57));
[Link](a);
}
}
Output:
[]
Size:2
[34, 5, 19, 24, 36, 48, 57, 63, 72]
Size:9
true
442 / 482
10) TreeSet
import [Link].*;
public class TreeSet1
{
public static void main(String args[]){
TreeSet<Integer> a=new TreeSet<Integer>();
[Link](a);
[Link](34);
[Link](5);
[Link]("Size:"+[Link]());
[Link](5);
[Link](3);
[Link](8);
[Link](7);
[Link](12);
[Link](1);
[Link](72);
[Link](a);
[Link](72);
// try to duplicate.
[Link](a);// 72 is not stored again.
[Link](86);
443 / 482
[Link](94);
//values are displayed as sorted order . not in a specified way
[Link]("Size:"+[Link]());
[Link]([Link](48));
[Link](a);
[Link]([Link](57));
[Link](a);
}
}
Output:
[]
Size:2
[1, 3, 5, 7, 8, 12, 34, 72]
[1, 3, 5, 7, 8, 12, 34, 72]
Size:10
false
[1, 3, 5, 7, 8, 12, 34, 72, 86, 94]
false
[1, 3, 5, 7, 8, 12, 34, 72, 86, 94]
Queue:
11)Priority Queue
444 / 482
import [Link].*;
public class PriorityQueue1
{
public static void main(String args[]){
PriorityQueue<Integer> a=new PriorityQueue<Integer>();
[Link](a);
[Link](34);
[Link](a);
[Link](5);
[Link]("Size:"+[Link]());
[Link](19);
[Link](24);
[Link](36);
[Link](48);
[Link](57);
[Link](63);
[Link](72);
[Link](86);
[Link](94);
[Link]("Size:"+[Link]());
//the interger which is small that has highest
//priority that is stored in "head/front".
//remaining all stored in random order.
[Link](a);
[Link]([Link](48));
[Link]([Link](5));
//5 is least element deleted.
445 / 482
Output:
[]
[34]
Size:2
Size:11
[5, 24, 19, 34, 36, 48, 57, 63, 72, 86, 94]
true
true
[19, 24, 48, 34, 36, 94, 57, 63, 72, 86]
19
[24, 34, 48, 63, 36, 94, 57, 86, 72]
true
[]
446 / 482
import [Link].*;
public class ArrayDeque1
{
public static void main(String args[]){
ArrayDeque<Integer> a=new ArrayDeque<Integer>();
[Link](a);
[Link](34);
[Link](5);
[Link]("Size:"+[Link]());
[Link](19);
[Link](24);
[Link](36);
[Link](48);
[Link](57);
[Link](63);
[Link](72);
[Link](32);
[Link](86);
[Link](59);
[Link](64);
[Link](19);
[Link](94);
[Link]("Size:"+[Link]());
[Link](a);
[Link]([Link](48));
447 / 482
[Link]([Link](59));
[Link](a);
[Link]([Link]());
[Link]([Link]());
[Link](a);
[Link]([Link]());
// displayed the "front " element then deleted.
[Link]([Link]());
[Link]([Link]());
[Link](a);
[Link]([Link]());
// displayed the first element
[Link]([Link]());
[Link]([Link]());
//displayed the last element
[Link](a);
[Link]([Link](a));
[Link](a);
}
}
Output:
[]
Size:2
Size:15
448 / 482
[59, 86, 63, 57, 34, 5, 19, 24, 36, 48, 72, 32, 64, 19, 94]
true
true
[86, 63, 57, 34, 5, 19, 24, 36, 48, 72, 32, 64, 19, 94]
86
94
[63, 57, 34, 5, 19, 24, 36, 48, 72, 32, 64, 19]
63
57
19
[34, 5, 19, 24, 36, 48, 72, 32, 64]
34
34
64
[34, 5, 19, 24, 36, 48, 72, 32, 64]
true
[]
13)LinkedList
import [Link].*;
public class LinkedList2
{
public static void main(String args[]){
LinkedList<String> a=new LinkedList<String>();
[Link](a);
[Link]("a");
449 / 482
[Link]("b");
[Link]("Size:"+[Link]());
[Link]("c");
[Link]("d");
[Link]("f");
[Link]("g");
[Link]("h");
[Link]("i");
[Link]("j");
[Link]("k");
[Link]("l");
[Link]("m");
[Link]("n");
[Link]("o");
[Link]("p");
[Link]("Size:"+[Link]());
//stored in a specified order.
[Link](a);
[Link]([Link]("k"));
[Link]([Link]("i"));
[Link](a);
[Link]([Link]());
[Link]([Link]());
[Link](a);
[Link]([Link]());
// displayed the "front " element then deleted.
[Link]([Link]());
450 / 482
[Link]([Link]());
[Link](a);
[Link]([Link]());
// displayed the first element
[Link]([Link]());
[Link]([Link]());
//displayed the last element
[Link](a);
[Link]([Link](a));
[Link](a);
}
}
Output:
[]
Size:2
Size:15
[m, l, i, h, a, b, c, d, f, g, j, k, n, o, p]
true
true
[m, l, h, a, b, c, d, f, g, j, k, n, o, p]
m
p
[l, h, a, b, c, d, f, g, j, k, n, o]
l
h
o
451 / 482
[a, b, c, d, f, g, j, k, n]
a
a
n
[a, b, c, d, f, g, j, k, n]
true
[]
452 / 482
import [Link];
@Override
public void withdraw(double amount) {
if (balance + overdraftLimit >= amount) {
balance -= amount;
[Link]("Withdrawn: " + amount);
} else {
[Link]("Overdraft limit exceeded!");
}
}
}
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
}
}
output:
---------
Account Number: S123
Balance: 5000.0
Enter deposit amount for Savings Account: 5000
Deposited: 5000.0
Interest added: 500.0
Enter withdrawal amount for Savings Account: 6700
Withdrawn: 6700.0
Account Number: S123
Balance: 3800.0
1. Sample Java program to check whether the given number is prime palindrome or
not.
import [Link];
while (num != 0) {
int digit = num % 10;
reverse = reverse * 10 + digit;
num /= 10;
}
C:\Users\Shanti\Desktop>java PrimePalindrome
Enter a number: 7
7 is a prime palindrome.
-------------------------------------------------------------------------------------------
457 / 482
2. Write a java program which reads marks of five subjects through command line and
print the total and average.
int total = 0;
for (String arg : args) {
total += [Link](arg);
}
output:
-----------
java MarksCalculator
Please enter marks for five subjects.
java MarksCalculator 34 56 78 97 65
Total Marks: 330
Average Marks: 66.0
--------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------
Design a “farm animals” java application with the details of animals like cow, pig,
horse.
Consider the following details like where they stay, what they eat, the sound they make
by using classes and objects
String sound;
// Constructor
Animal(String name, String habitat, String food, String sound) {
[Link] = name;
[Link] = habitat;
[Link] = food;
[Link] = sound;
}
Animal: Pig
Habitat: Sty
Food: Vegetables
Sound: Oink
Animal: Horse
Habitat: Stable
Food: Hay
Sound: Neigh
----------------------------------------------------------------------------------------
class Order {
String date;
int number;
@Override
void confirm() {
[Link]("Special Order " + number + " confirmed on " + date);
}
@Override
void close() {
[Link]("Special Order " + number + " is closed.");
}
}
@Override
void confirm() {
[Link]("Normal Order " + number + " confirmed on " + date);
}
@Override
void close() {
[Link]("Normal Order " + number + " is closed.");
}
}
// Class: Customer
class Customer {
String name;
String location;
Output
Alice from New York has sent an order.
Special Order 101 confirmed on 2024-09-08
Special Order 101 dispatched on 2024-09-08
Special Order 101 is closed.
Normal Order 102 confirmed on 2024-09-08
Normal Order 102 dispatched with status: In Progress
Normal Order 102 received.
Normal Order 102 is closed.
Alice from New York has received the order.
463 / 482
5. Constructor overloading
Output:
Employee Information:
Name: John Doe
ID: 1001
Designation: Employee
Salary: 50000.0
Promotion Status: false
Employee Information:
Name: Jane Smith
ID: 2001
Designation: Manager
Salary: 80000.0
Promotion Status: true
Employee Information:
Name: Robert Johnson
ID: 3001
Designation: Executive
Salary: 120000.0
Promotion Status: true
465 / 482
6. Strings
Write a program to find the longest Substring without Repeating Characters
Input: abcabcbb
output:3 string: abc
Input: pwwkew
output:3 string: wke
Note: pwke is not a substring , it is a subsequence
Output:
Input: abcabcbb
Longest Substring: abc
Length of Longest Substring: 3
Input: pwwkew
Longest Substring: wke
Length of Longest Substring: 3
7. Method overriding
All the banks operating in India are controlled by RBI. (e.g. minimum interest rate,
minimum balance allowed, maximum withdrawal limit etc) which all banks must
follow. For example, suppose RBI has set minimum interest rate applicable to a saving
bank account to be 4% annually. however, banks are free to use 4% interest rate or to
set any rates above it. Write a JAVA program to implement bank functionality in the
above scenario and demonstrate the dynamic polymorphism concept. Note: Create few
classes namely Customer, Account, RBI (Base Class) and few derived classes (SBI,
ICICI, PNB etc). Assume and implement required member variables and functions in
each class.
Testcase1: Enter the Bank name to find the rate of Interest : RBI
RBI rate of interest is : 4%
Testcase2: Enter the Bank name to find the rate of Interest : SBI
RBI rate of interest is : 7%
import [Link];
}
}
[Link]("Enter the Bank name to find the rate of Interest (RBI, SBI, ICICI,
PNB): ");
String bankName = [Link]().toUpperCase();
[Link]();
}
}
469 / 482
Output:
C:\Users\Shanti\Desktop>java BankDemo
John's Bank: RBI
Interest Rate: 4.0%
Enter the Bank name to find the rate of Interest (RBI, SBI, ICICI, PNB): icici
John's Bank: ICICI
Interest Rate: 6.0%
8. Interfaces:
import [Link];
@Override
public void calculateSalary() {
totalAmount = BASIC_PAY + HRA + TA;
}
@Override
470 / 482
@Override
public void calculateSalary() {
totalAmount = BASIC_PAY + TA;
}
@Override
public void displaySalary() {
[Link]("Salary Details:");
[Link]("Basic Pay: " + BASIC_PAY);
[Link]("T.A: " + TA);
[Link]("Total Amount: " + totalAmount);
}
}
Salary employee;
if ([Link]("regular")) {
employee = new RegularEmployee();
} else if ([Link]("contract")) {
employee = new ContractEmployee();
} else {
471 / 482
[Link]();
[Link]();
[Link]();
}
}
Output:
Enter Employee Id: 678
Enter Employee Type (Regular/Contract): regular
Salary Details:
Basic Pay: 25000
HRA: 15000
T.A: 5000
Total Amount: 45000
Test Case-2:
Enter Employee Id: 897
Enter Employee Type (Regular/Contract): contract
Salary Details:
Basic Pay: 12000
T.A: 3000
Total Amount: 15000
9. Packages
Define a package number and in that define Roman class and implement
romanToInteger() and import the method in another class.
Input: "LVIII"
Output: 58
Explanation: L = 50, V= 5, III = 3.
// File: number/[Link]
package number;
values['X'] = 10;
values['L'] = 50;
values['C'] = 100;
values['D'] = 500;
values['M'] = 1000;
int total = 0;
int prevValue = 0;
Output:
javac number/[Link]
>javac [Link]
>java RomanConverter
Input: LVIII
Output: 58
473 / 482
Write the below text in the file called [Link] and then find the frequency count of
the patterns ‘pe’, and ‘pi’ Peter Piper picked a peck of pickled peppers A peck of
pickled peppers Peter Piper picked If Peter Piper picked a peck of pickled peppers
Where’s the peck of pickled peppers Peter Piper picked?
Expected Output: ‘pe’ – no of occurrences - 20 ‘pi’ – no of occurrences – 12
import [Link];
import [Link];
import [Link];
import [Link];
// Print results
for (int i = 0; i < [Link]; i++) {
[Link]("'" + patterns[i] + "' – no of occurrences - " + counts[i]);
}
} catch (IOException e) {
[Link]();
}
}
return count;
}
}
Output:
Content written to file successfully.
'pe' â?? no of occurrences - 16
'pi' â?? no of occurrences – 8
Input a mobile number and check the given number is valid mobile number or not.
● A valid mobile number is a combination of (0-9) digits of length exactly 10.
● If the given Number Exceeds length of 10 raise Invalid Mobile
NumberArrayIndexOutofBounds Exception
● If the given Number less than the length of 10 raise Invalid Mobile Number –
LengthNotSufficientException
● If the given Number contain any character other than digit raise Invalid Mobile
Number –NumberFormatException
Sample Input
Expected Output – 1 9885089465 Valid number
98567890121 Invalid Mobile Number-ArrayIndexOutofBounds Exception
475 / 482
validateMobileNumber("9885089465");
validateMobileNumber("98567890121");
validateMobileNumber("88664433");
validateMobileNumber("98ab@123");
}
catch (InvalidArrayIndexException e)
{
[Link]("InvalidArrayIndexOutofBounds Exception: " + [Link]());
}
catch (LengthNotSufficientException e)
{
[Link]("Invalid LengthNotSufficientException: " + [Link]());
}
catch (NumberFormatException e)
{
[Link]("InvalidNumberFormatException: " + [Link]());
}
}
if ()
{
throw new NumberFormatException("Contains non-digit characters");
}
476 / 482
[Link]("Valid number");
}
}
Output:
Valid number
InvalidArrayIndexOutofBounds Exception: Exceeds length of 10
12. Multi-Threading
Implement a Reservation system which allows the persons to book seats. Define reserve
method initially with 100 seats. Now create one or more person threads to book seats. At
any time it should allow only one person thread to access the reserve method.
Expected Output: Person-1 entered. Available seats: 10 Requested seats: 5 Seat
Available. Reserve now :-) 5 seats reserved. Person-1 leaving.
----------------------------------------------
Person-2 entered. Available seats: 5 Requested seats: 2 Seat Available. Reserve now :-) 2
seats reserved. Person-2 leaving.
----------------------------------------------
Person-3 entered. Available seats: 3 Requested seats: 4 Requested seats not available :-)
Person-3 leaving.
---------------------------------------------
477 / 482
class ReservationSystem
{
private static int availableSeats = 100;
@Override
public void run()
{
[Link](personName, requestedSeats);
}
}
{
public static void main(String[] args)
{
ReservationSystem reservationSystem = new ReservationSystem();
Output:
-----------
Person-1 entered.
Available seats: 100 Requested seats: 5
Seat Available. Reserve now :-)
5 seats reserved.
Person-1 leaving.
Person-4 entered.
Available seats: 95 Requested seats: 90
Seat Available. Reserve now :-)
90 seats reserved.
Person-4 leaving.
Person-3 entered.
Available seats: 5 Requested seats: 10
Not enough seats available. Reservation failed.
Person-3 leaving.
Person-2 entered.
Available seats: 5 Requested seats: 20
Not enough seats available. Reservation failed.
Person-2 leaving.
479 / 482
Given a string containing digits from 2-9 inclusive, return all possible letter
combinations that the number could represent. A mapping of digit to letters (just like
on the telephone buttons) is given below. Note that 1 does not map to any letters.
import [Link];
//It is used to store the resulting letter combinations.
import [Link];
//It is used to create a mapping between digits and their corresponding letters.
import [Link];
//It is used to represent a list of letter combinations.
import [Link];
//It is used to create a mapping between digits and their corresponding letters.
{
List<String> result = new ArrayList<>();
//Create a new ArrayList named result to store the resulting letter
combinations.
[Link]('2', "abc");
[Link]('3', "def");
[Link]('4', "ghi");
[Link]('5', "jkl");
[Link]('6', "mno");
[Link]('7', "pqrs");
[Link]('8', "tuv");
[Link]('9', "wxyz");
Output:
[ad, ae, af, bd, be, bf, cd, ce, cf]
14. Arrays
Write a program to find the Valid Parentheses Given a string containing just the
characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. An input string
is valid if: 1. Open brackets must be closed by the same type of brackets. 2. Open
brackets must be closed in the correct order.
Note that an empty string is also considered valid.
Input: ( )
Output: valid
Input: ( { ) }
Output: Not valid
import [Link];
}
}
Output:
true
false