-
Notifications
You must be signed in to change notification settings - Fork 0
/
SLT - Java Programs.txt
667 lines (646 loc) · 16.4 KB
/
SLT - Java Programs.txt
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
1. Write a program using array to accept 10 numbers and display the numbers in ascending order.
--Program :
import java.util.Scanner;
public class Ascend
{
int[] numbers;
private int min;
public void accept()
{
numbers = new int[10];
Scanner input;
System.out.println("Enter the Integers");
for(int i = 0 ; i <= 9 ; i++)
{
input = new Scanner(System.in);
numbers[i]= input.nextInt();
}
}
public void execute()
{
for(int i = 0; i<10 ; i++)
{
for(int j = 0 ; j < 9 ; j++)
{
if (numbers [j] > numbers [j+1])
{
min = numbers [j];
numbers [j] = numbers [j+1];
numbers [j+1] = min;
}
}
}
}
public void print()
{
for(int i = 0; i < 10 ; i++)
{
System.out.println(" "+numbers [i]);
}
}
public static void main(String[] args)
{
Ascend object = new Ascend();
object.accept();
object.execute();
object.print();
}
}
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2. Write a program to accept 10 numbers using array and display the sum and average of 10 numbers.
--Program :
import java.util.Scanner;
public class Sum_Avg
{
int[] numbers;
int sum=0;
int average=0;
public void accept()
{
numbers = new int[10];
Scanner input;
System.out.println("Enter the numbers");
for(int i = 0 ; i <= 9 ; i++)
{
input = new Scanner(System.in);
numbers[i]= input.nextInt();
}
}
public void execute()
{
for(int i = 0; i<10 ; i++)
{
sum = sum+ numbers[i];
}
average = sum/numbers.length;
}
public void print()
{
System.out.println("The sum of the given numbers are : "+sum);
System.out.println("The average of the given numbers are : "+average);
}
public static void main(String[] args)
{
Sum_Avg obj = new Sum_Avg();
obj.accept();
obj.execute();
obj.print();
}
}
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
3. Write a program to accept mark of 10 students using array and display the name of the highest scorer.
--Program :
import java.util.Scanner;
public class High_Scorer
{
String[][] database = new String[2][10];
int[] array = new int[10];
public int temp = 0;
public int highest = 0;
public void acceptStudName()
{
Scanner input;
System.out.println("Enter the names of the students : ");
for(int i = 0 ; i <= 9 ; i++)
{
input = new Scanner(System.in);
database[0][i]= input.next();
}
}
public void acceptStudMark()
{
Scanner input;
System.out.println("Enter the marks of the students in the same order of previously given names : ");
for(int i = 0 ; i <= 9 ; i++)
{
input = new Scanner(System.in);
database[1][i]= input.next();
array[i] = Integer.parseInt(database[1][i]);
}
}
public void higestMark()
{
for(int i = 0; i <= 9 ; i++)
{
if(array[i] > highest)
{
temp = i;
highest = array [i];
}
}
}
public void topper()
{
System.out.println(database[0][temp]+" scored the highest mark ");
System.out.println("His mark was "+database[1][temp]);
}
public static void main(String[] args)
{
High_Scorer obj = new High_Scorer();
obj.acceptStudName();
obj.acceptStudMark();
obj.higestMark();
obj.topper();
}
}
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
4. Write a java program to accept name and salary of 5 employee
(1) Display employee of highest salary.
(2) Display employee lowest paid.
(3) Display average salary of employee.
--Program :
import java.util.Scanner;
public class Salary_Details
{
String[][] database = new String[2][10];
int[] array = new int[10];
int temp = 0;
int temp1 = 0;
int highestSal = 0;
int lowestSal = 0;
public void acceptEmpName()
{
Scanner input;
System.out.println("Enter the names of the employees");
for(int i = 0 ; i <= 9 ; i++)
{
input = new Scanner(System.in);
database[0][i]= input.next();
}
}
public void acceptEmpSal()
{
Scanner input;
System.out.println("Enter the salaries of the employees in the same order as names you entered before : ");
for(int i = 0 ; i <= 9 ; i++)
{
input = new Scanner(System.in);
database[1][i]= input.next();
array[i] = Integer.parseInt(database[1][i]);
}
}
public void highestSal()
{
for(int i = 0; i <= 9 ; i++)
{
if(array[i] > highestSal)
{
temp = i;
highestSal = array [i];
}
}
}
public void lowestSal()
{
for(int i = 0; i <= 9 ; i++)
{
if(array[i] < highestSal)
{
temp1 = i;
lowestSal = array [i];
}
}
}
public void AvgSal()
{
System.out.println(database[0][temp]+" gets the highest salary");
System.out.println("His salary is "+database[1][temp]);
System.out.println(" ");
System.out.println(database[0][temp1]+" gets the lowest salary");
System.out.println("His salary is "+database[1][temp1]);
}
public static void main(String[] args)
{
Salary_Details obj = new Salary_Details();
obj.acceptEmpName();
obj.acceptEmpSal();
obj.highestSal();
obj.AvgSal();
}
}
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
5. Write a java program to display matrix of 3 rows and 3 columns.
--Program :
import java.util.Scanner;
public class MatrixDisplay
{
int i,j;
System.out.println("Enter the number of rows and columns : ");
Scanner s = new Scanner(System.in);
int row = s.nextInt();
int column = s.nextInt();
public int[][] matrix1 = new int[3][3];
public int[][] matrix2 = new int[3][3];
public void accept()
{
Scanner input;
System.out.println("Enter the elements for the first matrix:");
for(int i = 0 ; i <= 2 ; i++)
{
for(int j = 0 ; j <= 2 ; j++ )
{
input = new Scanner(System.in);
matrix1[i][j]= input.nextInt();
}
}
System.out.println("Enter the elements for the second matrix:");
for(int i = 0 ; i <= 2 ; i++)
{
for(int j = 0 ; j <= 2 ; j++ )
{
input = new Scanner(System.in);
matrix2[i][j]= input.nextInt();
}
}
}
public void display()
{
System.out.println("the first matrix:");
for(int i = 0 ; i <= 2 ; i++)
{
for(int j = 0 ; j <= 2 ; j++ )
{
System.out.println(matrix1[i][j]+" ");
}
System.out.println();
}
System.out.println("the second matrix:");
for(int i = 0 ; i <= 2 ; i++)
{
for(int j = 0 ; j <= 2 ; j++ )
{
System.out.println(matrix2[i][j]);
}
System.out.println(" ");
}
}
public static void main(String[] args)
{
MatrixDisplay obj = new MatrixDisplay();
obj.accept();
obj.display();
}
}
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
6. Write a java program to add 2 matrics of 3 rows and 3 columns.
--Program :
import java.util.Scanner;
public class Matrix_Add
{
int i,j;
Scanner s = new Scanner(System.in);
int row = s.nextInt();
int column = s.nextInt();
public int[][] matrixAdd = new int[3][3];
public int[][] matrix1 = new int[3][3];
public int[][] matrix2 = new int[3][3];
public void addaccept()
{
Scanner input;
System.out.println("Enter the elements for the first matrix:");
for(int i = 0 ; i <= 2 ; i++)
{
for(int j = 0 ; j <= 2 ; j++ )
{
input = new Scanner(System.in);
matrix1[i][j]= input.nextInt();
}
System.out.println("Enter the elements for the second matrix:");
matrixAdd[i][j] = matrix1[i][j] + matrix2[i][j];
}
}
public void addDisplay()
{
System.out.println("the sum of the matrices:");
for(int i = 0 ; i <= 2 ; i++)
{
for(int j = 0 ; j <= 2 ; j++ )
{
System.out.println(matrixAdd[i][j]+"\t");
}
System.out.println(" ");
}
}
public static void main(String[] args)
{
Matrix_Add obj = new Matrix_Add();
//obj.display();
obj.addaccept();
obj.addDisplay();
}
}
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
7. Write a java program to do product of 2 matrix of 3 rows and 3 columns.
--Program :
import java.util.Scanner;
public class MatrixMul
{
int i,j;
Scanner s = new Scanner(System.in);
int row = s.nextInt();
int column = s.nextInt();
public int[][] matrix1 = new int[3][3];
public int[][] matrix2 = new int[3][3];
public int[][] MatrixMul = new int[3][3];
public void MulMat()
{
Scanner input;
System.out.println("Enter the elements for the first matrix:");
for(int i = 0 ; i <= 2 ; i++)
{
for(int j = 0 ; j <= 2 ; j++ )
{
input = new Scanner(System.in);
matrix1[i][j]= input.nextInt();
}
System.out.println("Enter the elements for the second matrix:");
MatrixMul[i][j] = MatrixMul[i][j] + (matrix1[i][k] * matrix2[k][j]);
}
}
public void MulDisplay()
{
Scanner input;
System.out.println("the product of the matrices:");
for(int i = 0 ; i <= 2 ; i++)
{
for(int j = 0 ; j <= 2 ; j++ )
{
System.out.println(MatrixMul[i][j]);
}
System.out.println(" ");
}
}
public static void main(String[] args)
{
MatrixMul obj = new MatrixMul();
//obj.display();
obj.MulMat();
obj.MulDisplay();
}
}
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
8. Write a program in java to create user defined exception which will allow user to perform bank transaction.
1. if user try to withdraw amount > balance program must throw insufficient fund exception
2. before withdraw or deposit user must enter pin, if pin is not valid then program must throw invalid pin exception
3. user cannot deposit > Rs.200000 without pan details are not provided program must raise exception saying invalid transaction.
--Program :
package userTransaction;
public class userAmtException extends Exception
{
public userAmtException(String message)
{
super (message);
}
}
____________________________________________
package AccountHandle;
import java.util.Scanner;
public class BankTransaction
{
public Scanner scanner;
public int i=0;
public int amount;
public int Display()
{
System.out.println("What would you like to do?");
System.out.println("1. --> Withdraw");
System.out.println("2. --> Deposit");
System.out.println("Please press 1 or 2");
scanner = new Scanner(System.in);
int selection = scanner.nextInt();
return selection;
}
public void Withdraw() throws Exception
{
System.out.println("Please enter your four Digit pin number:");
try
{
int PIN = scanner.nextInt();
checkPin(PIN);
}
catch(Exception e)
{
System.out.println("Please check the PIN number you have typed.");
}
System.out.println("Please enter the amount you wish to withdraw");
amount = scanner.nextInt();
if(amount<=76000)
{
System.out.println("Please collect your cash");
}
else
{
throw new UserAmtException("Invalid entry");
}
}
public void Deposit() throws Exception
{
System.out.println("Please enter your four Digit pin number:");
try
{
int PIN = scanner.nextInt();
checkPin(PIN);
}
catch(Exception e)
{
System.out.println("Please check the PIN number you have typed.");
}
System.out.println("Please enter the amount you wish to deposit");
amount = scanner.nextInt();
if(amount>200000)
{
System.out.println("Please enter your pan card details");
String PAN = scanner.next();
//throw new UserAmtException("Transaction will be processed, only if the PAN number is valid");
checkDatabase(PAN);
}
System.out.println("Please place your cash for deposit");
}
public void checkDatabase(String data) throws UserAmtException
{
if(data=="AQJKS3456A")
{
return;
}
else
{
throw new UserAmtException("Invalid entry");
}
}
public void checkPin(int code) throws Exception
{
try
{
while(code!=0716)
{
System.out.println("Please check the PIN number you have typed.");
code = scanner.nextInt();
i++;
if(i==3)
{
System.out.println("Your Account has been blocked temporarily.");
System.exit(0);
}
}
if(code==0716)
{
return;
}
}
catch(Exception e)
{
System.out.println("Please check the PIN number you have typed.");
}
}
public static void main(String[] args) throws Exception
{
BankTransaction object = new BankTransaction();
int choice = object.Display();
if(choice == 1)
{
object.Withdraw();
}
if(choice == 2)
{
object.Deposit();
}
}
}
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
9. Write a menu driven program for calculator using java language. Menu details are mentioned as : Addition, Subtraction, Multiplication, Division,
Percentage, Exit.
--Program :
import java.io.*;
public class calculator
{
int result = 0;
public int add(int num1, int num2)
{
result = num1+num2;
return result;
}
public int sub(int num1, int num2)
{
result = num1-num2;
return result;
}
public int mul(int num1, int num2)
{
result=num1*num2;
return result;
}
public int div(int num1, int num2)
{
result=num1/num2;
return result;
}
public int percent(int num1, int num2)
{
result=(num1+num2)/100;
return result;
}
public static void main(String[] aa)
{
int num1,num2,result;
num1=num2=result=0;
int choice;
Scanner sin = new Scanner(System.in);
calculator cobj=new calculator();
System.out.println("1.Addition");
System.out.println("2.Subtraction");
System.out.println("3.Multiplication");
System.out.println("4.Division");
System.out.println("5.Percentage");
System.out.println("6.Exit");
choice=sin.nextInt();
switch(choice)
{
case1:
result=cobj.add(num1,num2);
}
System.out.println("The result as per your choice", +result);
}
}
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
10. Write a program in java to create user defined exception which will accept employee salary as input.
1. if salary is less than 0 display the message that you are not eligible for loan.
2. if salary is greater than 10000 display message that you are eligible for loan of 1000000.
3. if salary is greater is greater than 50000 display message thay you are eligible for loan of 500000.
4. if salary is greater than 100000 display message that you are eligible for loan of 1000000.
--Program :
package userDefinedException;
public class exceptionnew extends Exception
{
public exceptionnew(String message)
{
super (message);
}
}
____________________________________________
package userDefinedException;
import java.io.*;
public class LoanEligible
{
static InputStreamReader is = new InputStreamReader(System.in);
static BufferedReader br = new BufferedReader(is);
public static int salary;
public static int Display() throws Exception
{
System.out.println("Enter your salary");
salary = Integer.parseInt(br.readLine());
return salary;
}
public static void validAge(int salary) throws exceptionnew
{
if(salary >= 10000)
{
throw new exceptionnew("eligible for a loan of 1 lakh");
}
else if(salary >= 50000)
{
throw new exceptionnew("eligible for a loan of 5 lakhs");
}
else if(salary >= 100000)
{
throw new exceptionnew("eligible for a loan of 10 lakhs");
}
if(salary<10000)
{
throw new exceptionnew("not eligible for loan");
}
}
public static void main(String[] args) throws Exception
{
Display();
validAge(salary);
}
}
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
11. Write a java program to accept a number of any digit and reverse the numbers : Ex : 6,5,4,3,2,1 to 1,2,3,4,5,6
--Program :
import java.util.Scanner;
public class ReverseNum
{
public void revnum()
{
int num;
int rev=0;
int rem;
Scanner sin=new Scanner(System.in);
System.out.println("Enter the number");
num = sin.nextInt();
while(num!=0)
{
rem=num%10;
rev=rev*10+rem;
num=num/10;
}
System.out.println("the number after reversing",+rev);
}
public static void main(String[]aa)
{
ReverseNum rnobj=new Reversenum();
rnobj.revnum();
}
}
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------