-
Notifications
You must be signed in to change notification settings - Fork 0
/
OPPs Questions.java
1464 lines (1233 loc) · 44 KB
/
OPPs Questions.java
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
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*Create a class named Billing that includes three overloaded computeBill() methods for a
photo book store.
When computeBill() receives a single parameter, it represents the price of one photo book ordered. Add 8% tax, and return the total due.
When computeBill() receives two parameters, they represent the price of a photo book and the quantity ordered.
Multiply the two values, add 8% tax, and return the total due.
When computeBill() receives three parameters, they represent the price of a photo book, the quantity ordered, and a coupon value.
Multiply the quantity and price, reduce the result by the coupon value, and then add 8% tax and return the total due.
Write a main() method that tests all three overloaded methods. Save the application as Billing.java.*/
public class Billing {
private float photobookprice=0;
private int photobooks = 1;
private final int taxpercent = 8;
private float couponValuePercent=0;
private float totalDue=0;
public Billing(float photobookprice) {
this.photobookprice = photobookprice;
totalDue = calculateOneBookPrice();
}
public Billing(float photobookprice, int photobooks) {
this(photobookprice);
this.photobooks = photobooks;
totalDue = calculateBooksPrice();
}
public Billing(float photobookprice, int photobooks, float couponValuePercent) {
this(photobookprice, photobooks);
this.couponValuePercent = couponValuePercent;
totalDue = calculateBooksPriceWithCoupon();
}
private float calculateOneBookPrice() {
return photobookprice + (0.08f * photobookprice);
}
private float calculateBooksPrice() {
return calculateOneBookPrice() * photobooks;
}
private float calculateBooksPriceWithCoupon() {
return (calculateOneBookPrice() * photobooks) - couponValuePercent;
}
public static void main(String[] args) {
Billing bill1 = new Billing(250);
Billing bill2 = new Billing(250,4);
Billing bill3 = new Billing(250,12,2.5f);
System.out.println(bill1.toString());
System.out.println(bill2.toString());
System.out.println(bill3.toString());
}
public float getTotalDue() {
return totalDue;
}
public float getPhotobookprice() {
return photobookprice;
}
public void setPhotobookprice(float photobookprice) {
this.photobookprice = photobookprice;
}
public int getPhotobooks() {
return photobooks;
}
public void setPhotobooks(int photobooks) {
this.photobooks = photobooks;
}
public float getCouponValuePercent() {
return couponValuePercent;
}
public void setCouponValuePercent(float couponValuePercent) {
this.couponValuePercent = couponValuePercent;
}
public int getTaxpercent() {
return taxpercent;
}
public String toString() {
return "PhotoBookPrice=" + photobookprice + "\n" + " PhotoBooks="+ photobooks + "\n" + "CouponValuePercent=" + couponValuePercent + "\n" + "TotalDue="+ totalDue ;
}
}
/*•Define a class named Payment that contains a member variable of type double that stores the amount of the payment and appropriate accessor and mutator methods.
Also create a method named paymentDetails that outputs an English sentence to describe the amount of the payment.
•Next, define a class named CashPayment that is derived from Payment.
This class should redefine the paymentDetails method to indicate that the payment is in cash. Include appropriate constructor(s).
•Define a class named CreditCardPayment that is derived from Payment.
This class should contain member variables for the name on the card, expiration date, and credit card number. Include appropriate constructor(s).
Finally, redefine the paymentDetails method to include all credit card information in the printout.
•Create a main method that creates at least two CashPayment and two CreditCardPayment objects with different values and calls paymentDetails for each.*/
class Payment{
private double amount;
public Payment( ){
amount = 0;
}
public Payment(double amount){
this.amount = amount;
}
public void setPayment(double amount){
this.amount = amount;
}
public double getPayment( ){
return amount;
}
public void paymentDetails( ){
System.out.println("Payment amount : " + amount);
}
}
class CashPayment extends Payment{
public CashPayment( ){
super( );
}
public CashPayment(double amt){
super(amt);
}
public void paymentDetails( ){
System.out.println("Payment amount : "+ getPayment( ));
}
}
class CreditCardPayment extends Payment{
private String name;
private String expiration;
private String creditcard;
public CreditCardPayment(){
super( );
name = " ";
expiration = " ";
creditcard = "";
}
public CreditCardPayment(double amt, String name, String expiration, String creditcard){
super(amt);
this.name = name;
this.expiration = expiration;
this.creditcard = creditcard;
}
public void paymentDetails( ){
System.out.println("Credit card payment amount : " + getPayment( ));
System.out.println("Name on the Card : " + name);
System.out.println("Expiration Date : " + expiration);
System.out.println("Card Number : " + creditcard);
}
}
class OutputPayment{
public static void main(String[ ] args){
CashPayment cash1 = new CashPayment(1000), cash2 = new CashPayment(200), cash3 = new CashPayment(500);
CreditCardPayment credit1 = new CreditCardPayment(100, "Chandana" , "09/04/2030" , "1111111111");
CreditCardPayment credit2 = new CreditCardPayment(50 , "Tina", "03/11/2025" , "999999999");
CreditCardPayment credit3 = new CreditCardPayment(150 , "Cherry", "03/02/2028" , "555555555");
System.out.println("1st User Cash Details:");
cash1.paymentDetails( );
System.out.println();
System.out.println("2nd User Cash Details:");
cash2.paymentDetails( );
System.out.println();
System.out.println("3rd User Cash Details:");
cash3.paymentDetails( );
System.out.println();
System.out.println("1st User Credit Details:");
credit1.paymentDetails( );
System.out.println();
System.out.println("2nd User Credit Details:");
credit2.paymentDetails( );
System.out.println();
System.out.println("3rd User Credit Details:");
credit3.paymentDetails( );
System.out.println();
}
}
/*Imagine you are maintaining personal data of all the players in your club.
Three personal data namely name, age, and individual salary are the parameters which you are maintaining.
You have kept those data as private to a particular class named PlayerRecords.
However, you have also kept some provisions for the club owner to set and access those private data.
Write a java code fulfilling the above criteria to implement the scenario and display name, age, and their individual salary.
Be sure to get the accessibility right.
Find the name of the player whose salary is highest. Imagine a single highest paid player is there in the team.
[Hints: “some provisions” means some methods with proper access modifiers. Club owner is accessing those data from main method of another class.]*/
class PlayerRecord {
private String name;
private int age;
private double salary;
public PlayerRecord(String name, int age, double salary) {
this.name = name;
this.age = age;
this.salary = salary;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
public void setSalary(double salary) {
this.salary = salary;
}
public double getSalary() {
return salary;
}
public String toString() {
return "\n"+"Name: " + name+ "\n" + "Age: " + age+ "\n" + "Salary: " + salary+ "\n"+ "\n";
}
}
class Club {
public static void main(String[] args) {
PlayerRecord[] players = new PlayerRecord[5];
players[0] = new PlayerRecord("Chandana", 18, 100000);
players[1] = new PlayerRecord("Priya", 22, 75000);
players[2] = new PlayerRecord("Deepika", 23, 80000);
players[3] = new PlayerRecord("Shriya", 16, 76000);
players[4] = new PlayerRecord("Sanjana", 21, 83000);
System.out.println("Players details:"+"\n");
for (int i = 0; i < players.length; i++)
System.out.println(players[i]);
int max = 0;
for (int i = 0; i < players.length; i++) {
if (players[i].getSalary() > players[max].getSalary())
max = i;
}
System.out.println("Highest Paid Player : " + players[max].getName());
}
}
// Single Inheritance Question
import java.util.Scanner;
class StudentInfo {
int RegNo;
String StuName;
void input() {
Scanner sc = new Scanner(System.in);
System.out.print("Registration Number: ");
RegNo = sc.nextInt();
sc.nextLine();
System.out.print("Name: ");
StuName = sc.nextLine();
}
}
class Student extends StudentInfo {
void display() {
System.out.println("Student Registration Number is: "+RegNo+"\n");
System.out.println("Student Name is: "+StuName);
}
public static void main(String args[]) {
Student obj = new Student();
obj.input();
obj.display();
}
}
// 15 Faculty Question
class Department {
private String Dept, HOD;
private int Students, Sections;
public Department(String Dept, String HOD, int students, int sections) {
this.Dept = Dept;
this.HOD = HOD;
this.Students = Students;
this.Sections = Sections;
}
public void showDepartmentDetails() {
System.out.println("Department Name: " + Dept + "\n");
System.out.println("HOD Name: " + HOD + "\n");
System.out.println("Total Students: " + Students + "\n");
System.out.println("No of sections: " + Sections + "\n");
}
}
interface Publication {
public void setJournalcount(int journalcount);
public void setProjectcount(int projectcount);
public void setPatterncount(int patterncount);
}
class StaffMember extends Department implements Publication{
private String name, id, qualification, designation;
private int experience;
int journalcount, projectcount, patterncount;
public StaffMember(String Dept, String HOD, int Students, int Sections, String name, String id, String qualification, String designation, int experience, int journalcount, int projectcount, int patterncount) {
super(Dept, HOD, Students, Sections);
this.name = name;
this.id = id;
this.qualification = qualification;
this.designation = designation;
this.experience = experience;
setJournalcount(journalcount);
setProjectcount(projectcount);
setPatterncount(patterncount);
}
public void showStaffDetails() {
showDepartmentDetails();
System.out.println("Staff Name: " + name + "\n");
System.out.println("Staff id: " + id + "\n");
System.out.println("Qualification: " + qualification + "\n");
System.out.println("Designation: " + designation + "\n");
System.out.println("Experience: " + experience + "\n");
System.out.println("Journalcount: " + journalcount + "\n");
System.out.println("Projectcount: " + projectcount + "\n");
System.out.println("Patterncount " + patterncount + "\n");
}
public void setJournalcount(int journalcount) {
this.journalcount = journalcount;
}
public void setProjectcount(int projectcount) {
this.projectcount = projectcount;
}
public void setPatterncount(int patterncount) {
this.patterncount = patterncount;
}
}
class Inheritance {
public static void main(String[] args) {
StaffMember s = new StaffMember("CSE", "John Doe Sr", 20, 5, "Mr. James", "21E567", "Ph.D. in Computer Science", "Assistant Professor", 10, 16, 15, 13);
s.showStaffDetails();
}
}
/*Write an application named BookArray in which you create an array that holds 10 Books, some Fiction and some NonFiction.
Using a for loop, display details about all 10 books. Save the file as BookArray.java.*/
public class BookArray{
public static void main(String[] args){
Book books[] = new Book[10];
books[0] = new Fiction("Hamlet");
books[1] = new Fiction("Harry Potter");
books[2] = new Fiction("Twilight");
books[3] = new Fiction("Heirs of Fire");
books[4] = new Fiction("Assasin's Blade");
books[5] = new Fiction("One night at the Call Center");
books[6] = new NonFiction("Thomas Calculus");
books[7] = new NonFiction("A Brief History of Time");
books[8] = new NonFiction("Into Thin Air");
books[9] = new NonFiction("The Dairy of a Young Girl");
System.out.println("Details of all the books:");
for(int i = 0; i < books.length; i++){
System.out.println();
System.out.println((i + 1) + ") Name : " +books[i].getBookTitle());
System.out.println("Price : Rs. "+ books[i].getBookPrice());
}
}
}
class Book{
private String bookTitle;
private double bookPrice;
public Book(String title){
this.bookTitle = title;
bookPrice = 1000;
}
public String getBookTitle() {
return bookTitle;
}
public double getBookPrice() {
return bookPrice;
}
}
class Fiction extends Book{
public Fiction(String title) {
super(title);
}
}
class NonFiction extends Book{
public NonFiction(String title) {
super(title);
}
}
/*Get two string inputs Object Orientedand Programming lab from user.
Then find the length of those two inputs.
Further do perform concatenation and comparison.
Finally apply at least 5 more java string methods for the inputs.*/
// 1st
import java.util.Scanner;
class StringDemo{
public static void main(String[] args){
String str1, str2;
Scanner sc=new Scanner(System.in);
System.out.println("The two strings are : ");
str1=sc.next();
str2=sc.next();
System.out.println("Length of the 1st string : "+str1.length());
System.out.println("Length of the 2nd string : "+str2.length());
System.out.println("Concatenation of the two strings : "+(str1+str2));
if(str1.compareTo(str2)==0){
System.out.println("\n"+"Both the strings are equal.");
}
else{
System.out.println("\n"+"Both the strings are not equal.");
}
}
}
//2nd
import java.util.Scanner;
class StringDemo{
public static void main(String[] args){
String str1, str2;
Scanner sc=new Scanner(System.in);
System.out.println("The two strings are : ");
str1=sc.next();
str2=sc.next();
System.out.println("The 1st string in lowercase: "+str1.toLowerCase()+"\n");
System.out.println("The 2nd string in uppercase: "+str2.toUpperCase()+"\n");
System.out.println("The specific character at index 1 in the first string: "+str1.charAt(1)+"\n");
System.out.println("The index of 'a' in the second string if present: "+str1.indexOf('a'));
System.out.println("\n"+"Are both strings equal: "+str1.equals(str2)+"\n");
System.out.println("\n"+"Are both strings equal by ingnoring the cases: "+str1.equalsIgnoreCase(str2)+"\n");
}
}
/*Create a class named Poem that contains fields for the name of the poem and the number of lines in it.
Include a constructor that requires values for both fields. Also include get methods to retrieve field values.
Create three subclasses: Couplet, Limerick, and Haiku.
The constructor for each subclass requires only a title; the lines field is set using a constant value.
A couplet has two lines, a limerick has five lines, and a haiku has three lines.
Create an application that demonstrates usage of an objectof each type.
Save the files as Poem.java, Couplet.java, Limerick.java, Haiku.java, and DemoPoems.java.*/
import java.util.*;
class Poem {
private String title;
private int lines;
public Poem(String title,int lines){
this.title= title;
this.lines= lines;
}
public String getTitle() {
return title;
}
public int getLines() {
return lines;
}
}
class Couplet extends Poem{
private static final int LINES=2;
public Couplet(String title){
super(title,LINES);
}
}
class Limerick extends Poem{
private static final int LINES=5;
public Limerick(String title){
super(title,LINES);
}
}
class Haiku extends Poem {
private static final int LINES=3;
public Haiku(String title){
super(title,LINES);
}
}
class DemoPoems{
public static void main(String[] args){
Poem poem1 = new Poem("The Road not Taken",20);
Couplet poem2 = new Couplet("The Crooning Bee");
Limerick poem3 = new Limerick("Footprints in the Sand");
Haiku poem4 = new Haiku("The Old Pond");
display(poem1);
display(poem2);
display(poem3);
display(poem4);
}
public static void display(Poem p){
System.out.println("\n"+"Poem: " + p.getTitle() +"\n"+
"No. of Lines: " + p.getLines()+"\n");
}
}
//Write a java application for performing addition, subtraction and multiplication of complex numbers. Try to get input from the user side.
import java.io.*;
import java.util.*;
class Complex{
public int real, imaginary;
public Complex(int a, int b){
real = a;
imaginary = b;
}
}
class Main{
public static void addition(Complex c1, Complex c2) {
int result_a = c1.real + c2.real;
int result_b = c1.imaginary + c2.imaginary;
if(result_b > 0){
System.out.println("Addition : " + result_a + " + " + result_b + "i");
}
else{
System.out.println("Addition : " + result_a + " - "+(result_b * - 1) + "i ");
}
}
public static void subtraction(Complex c1, Complex c2) {
int result_a = c1.real - c2.real;
int result_b = c1.imaginary - c2.imaginary;
if(result_b>0){
System.out.println("Subtraction : " + result_a + " + " + result_b+"i" );
}
else{
System.out.println("Subtraction : " + result_a + " - "+(result_b * - 1) + "i ");
}
}
public static void multiplication(Complex c1, Complex c2){
int result_a = ((c1.real * c2.real) - (c1.imaginary * c2.imaginary));
int result_b = ((c1.real*c2.imaginary) + (c1.imaginary * c2.real));
if(result_b>0){
System.out.println("Multiplication : " + result_a + " + " + result_b + "i");
}
else{
System.out.println("Multiplication : " + result_a + " - "+(result_b * - 1) + "i ");
}
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int a1, b1, a2, b2;
System.out.print("Enter Real Part of Complex Number 1 : ");
a1 = sc.nextInt();
System.out.print("Enter Imaginary Part of Complex Number 1 : ");
b1 = sc.nextInt();
System.out.print("Enter Real Part of Complex Number 2 : ");
a2 = sc.nextInt();
System.out.print("Enter Imaginary Part of Complex Number 2 : ");
b2 = sc.nextInt();
Complex c1 = new Complex(a1, b1);
Complex c2 = new Complex(a2, b2);
addition(c1,c2);
subtraction(c1,c2);
multiplication(c1,c2);
}
}
/* Create a class named College Course that includes data fields that hold
the department (for example, CSE), the course number (for example, 110), the credits (for example,4), and the fee for the course (for example, Rs 360).
All of the fields are required as arguments to the constructor, except for the fee, which is calculated at Rs 120 per credit hour.
Include a display() method that displays the course data. Create a subclass named LabCourse that adds $50 to the course fee.
Override the parent class display() method to indicate that the course is a lab course and to display all the data.
Write an application named UseCourse that prompts the user for course information.
If the user enters a class in any of the following departments,
create a LabCourse: CSE, ECE, CIVIL, or IT. If the user enters any other department, create a CollegeCourse that does not include the lab fee.
Then display the course data. Save the files as CollegeCourse.java, LabCourse.java, and UseCourse.java. */
import java.util.*;
class CollegeCourse {
private String dept;
private int id;
private double credits;
private double price;
public CollegeCourse(String dept, int id, double credits) {
super();
this.dept = dept;
this.id = id;
this.credits = credits;
price = 120;
}
void display() {
System.out.println(dept + " - " + id + "\n" + "Non-lab Course " + credits +" credits"+ "\n" + " Total fee is Rs." + (price * credits));
}
}
class LabCourse extends CollegeCourse {
private String dept;
private int id;
private double credits;
private double price;
public LabCourse(String dept, int id, double credits) {
super(dept, id, credits);
this.dept = dept;
this.id = id;
this.credits = credits;
price = 120;
}
void display() {
System.out.println(dept + " - " + id + "\n" + "Lab Course " + credits + " credits" + "\n" + "Lab fee = $50" + "\n" + "Total fee is Rs." + ((price * credits) + 50));
}
}
class UseCourse {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String dept, inStr;
String[] labCourses = { "CSE", "ECE", "CIVIL", "IT" };
int id, credits;
int found = 0;
int x;
System.out.println("Enter the Department : ");
dept = input.nextLine();
dept = dept.toUpperCase();
System.out.println("Enter the id : ");
id = input.nextInt();
System.out.println("Enter the credits : ");
credits = input.nextInt();
for (x = 0; x < labCourses.length; x++) {
if (dept.equals(labCourses[x])) {
found = 1;
inStr = labCourses[x];
break;
}
else{
found = 0;
}
}
if (found == 1) {
LabCourse lab = new LabCourse(dept, id, credits);
lab.display();
}
else {
CollegeCourse course = new CollegeCourse(dept, id, credits);
course.display();
}
}
}
/* Create a class student which includes parameters roll_no, name and address and consider it as a super class.
Then create a subclass test with parameters marl1, mark2, mark3 and mark4.
Then create another sub class result with parameters total_marks, percentage and grade. Apply multilevel inheritance.
Here Student class is the super class for test class and test class is super class of result class.
Implement a java application in which you obtain the details roll_no, name, address, mark1, mark2, mark3 and mark4
from the user side and then print total_marks, percentage and grade. Student test result */
import java.util.*;
class Student{
private String rollno;
private String name;
private String address;
public void storeDetails(String no, String Name, String add){
rollno = no;
name = Name;
address = add;
}
public void showDetails(){
System.out.println("Roll No : " + rollno);
System.out.println("\n" + "Name : " + name);
System.out.println("\n" +"Address : " + address);
}
}
class Test extends Student{
protected int marks_CSE;
protected int marks_ECE;
protected int marks_Maths;
protected int marks_English;
public void storeMarks(int ml, int m2, int m3, int m4){
marks_CSE = ml;
marks_ECE = m2;
marks_Maths = m3;
marks_English = m4;
}
public void showMarks(){
System.out.println("Marks in CSE : " + marks_CSE);
System.out.println("Marks in ECE : " + marks_ECE);
System.out.println("Marks in Maths : " + marks_Maths);
System.out.println("Marks in English : " + marks_English);
}
}
class Result extends Test{
private int totalMarks;
private float percentage;
private char grade;
public void evaluateResult(){
totalMarks = marks_CSE + marks_ECE + marks_Maths + marks_English;
percentage = (totalMarks*100.00F/600.00F);
if (percentage >= 50.00F)
grade = 'D';
else
if(percentage >= 55.00F && percentage <= 60.00F)
grade = 'C';
else
if (percentage >= 61.00F && percentage <= 70.00F)
grade = 'B';
else
if (percentage >= 71.00F && percentage <= 75.00F)
grade = 'A';
else
if (percentage >= 76.00F && percentage <= 85.00F)
grade = 'H';
else
grade = 'S';
}
public void showResult(){
showDetails();
showMarks();
System.out.println("Total Marks : " + totalMarks);
System.out.println("percentage : " + percentage);
System.out.println("Grade : " + grade);
}
}
class Multilevellnheritance{
public static void main(String ar[]){
Result ob = new Result();
ob.storeDetails("21BCE8718", "Hari Chandana", "Amaravati");
ob.storeMarks(99,78,88,92);
ob.evaluateResult();
ob.showResult();
}
}
/* Create a class in the name of ComputeInterest. Use scanner class to get principal, rate and time as inputs from the user side.
Compute simple interest and compound interest by using respective formulas. Save the implemented java program as interest.java. */
import java.util.*;
class ComputeInterest {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the principal: ");
double principal = input.nextDouble();
System.out.print("Enter the rate: ");
double rate = input.nextDouble();
System.out.print("Enter the time: ");
double time = input.nextDouble();
double simpleInterest = (principal * time * rate) / 100;
System.out.println("Principal: " + principal);
System.out.println("Interest Rate: " + rate);
System.out.println("Time Duration: " + time);
System.out.println("Simple Interest: " + simpleInterest);
System.out.print("Enter number of times interest is compounded: ");
int number = input.nextInt();
double compoundInterest = principal * (Math.pow((1 + rate/100), (time * number))) - principal;
System.out.println("Principal: " + principal);
System.out.println("Interest Rate: " + rate);
System.out.println("Time Duration: " + time);
System.out.println("Number of times interest Compounded: " + number);
System.out.println("Compound Interest: " + compoundInterest);
input.close();
}
}
/*Consider that, a lab in charge maintains lab instruments. PhD scholars help students.
Professor conducts labs. Consider LabInCharge as an interface and PHDscholar as an abstract class.
LabInCharge has a method named install() and PHDscholar has a help() method respectively which are abstract in nature.
However, PHDscholar has a concrete method named labsAssigned().
LabInCharge has attributes such as labName, roomNo, and PHDscholar has attributes like scholarName, labConducted.
Professor class has a concrete method named teaches(), and fields like subject and emp_id and define the above said abstract methods also.
HOD class is the main() containing class.
Write a java code to display the attributes from each of the classes and interface creating proper instances and invoking the methods from the HOD class with proper implementation. */
public class HOD {
public static void main(String[] args) {
Prof p1 = new Prof("CSE",11);
p1.teaches();
p1.install();
PHD_Scholar s1 = new Prof("Mr.Mahesh","LAB-234");
s1.LabsAssigned();
s1.help();
}
}
interface LabInCharge{
String LabName = "LAB-102";
int Room_No = 201;
public void install();
}
abstract class PHD_Scholar{
String ScholarName;
String LabConducted;
abstract void help();
void LabsAssigned(){
System.out.println(ScholarName + " is assigned to " + LabConducted + "." + "\n");
}
}
class Prof extends PHD_Scholar implements LabInCharge{
String Subject;
int Emp_ID;
Prof(String Subject, int Emp_ID){
this.Subject = Subject;
this.Emp_ID = Emp_ID;
}
Prof(String ScholarName, String LabConducted){
this.ScholarName = ScholarName;
this.LabConducted = LabConducted;
}
public void install(){
System.out.println("Instrument installed in " + LabName + " and the Room Number is " + Room_No + "\n");
}
void help(){
System.out.println(ScholarName + " helps students during the Lab." + "\n");
}
void teaches(){
System.out.println("Professor's Employee ID is " + Emp_ID + " and he/she teaches " + Subject + "." + "\n");
}
}
/*Implement the following.
a) Create an interface named Turner, with a single method named turn().
Create a class named Leaf that implements turn() to display Changing colors.
Create a class named Page that implements turn( ) to display Going to the next page.
Create a class named Pancake that implements turn() to display Flipping.
Write an application named DemoTurners that creates one object of each of these class types and demonstrates the turn() method for each class.
Save the files as Turner.java, Leaf.java, Page.java, Pancake.java, and DemoTurners.java.
b) Think of two more objects that use turn(), create classes for them, and then add objects to the DemoTurners application, renaming it DemoTurners2. java.
Save the files, using the names of new objects that use turn().
c) Apply Dynamic method dispatch to show the power of it and name the class as DemoTurners3.java.*/
import java.util.*;
import java.lang.*;
class demoTurner1 {
public static void main (String [] args) {
leaf leaf = new leaf();
pancake pancake = new pancake();
page page = new page();
System.out.println("Demoturner1 Running >>>");
leaf.turn();
page.turn();
pancake.turn();
}
}
class demoTurner2 {
public static void main (String [] args) {
leaf leaf = new leaf();
pancake pancake = new pancake();
page page = new page();
life life = new life();
person person = new person();
System.out.println("Demoturner2 Running >>>");
leaf.turn();
page.turn();
pancake.turn();
life.turn();
person.turn();
}
}
class demoTurner3 {
public static void main (String [] args) {
leaf leaf = new leaf();
pancake pancake = new pancake();
page page = new page();
life life = new life();
person person = new person();
System.out.println("Demoturner3 Running >>>");
turner ref;
ref = leaf;
ref.turn();
ref = page;
ref.turn();
ref = pancake;
ref.turn();
ref = life;
ref.turn();
ref = person;
ref.turn();
}
}
class leaf implements turner {
public void turn() {
System.out.println("Message from leaf class - The leaf has turned yellow.");
}
}
class life implements turner {
public void turn() {
System.out.println("Message from life class - My life has turned up side down.");
}
}
class page implements turner {
public void turn() {
System.out.println("Message from page class - The page has been flipped to the next page.");
}
}
class page implements turner {
public void turn() {
System.out.println("Message from page class - The page has been flipped to the next page.");
}
}
class page implements turner {
public void turn() {
System.out.println("Message from page class - The page has been flipped to the next page.");
}
}
class pancake implements turner {
public void turn() {
System.out.println("Message from pancake class - The pancke has been flipped.");
}
}
class person implements turner {
public void turn() {
System.out.println("Message from person class - The wanted person has turned them in.");
}
}