-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp04.java
More file actions
222 lines (205 loc) · 5.66 KB
/
App04.java
File metadata and controls
222 lines (205 loc) · 5.66 KB
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
// The commented part can be ignored. Learn it only IF YOU WANT to display the details separately and not simultaneously while taking input
import java.util.Scanner;
// import java.util.ArrayList;
// import java.util.List;
public class App04 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
// List<Employee> employees = new ArrayList<>();
// List<Student> students = new ArrayList<>();
// Employee details
for (int i = 1; i <= 5; i++) {
System.out.println("Enter details for Employee " + i);
System.out.print("Name: ");
String name = scan.nextLine();
System.out.print("Age: ");
int age = scan.nextInt();
scan.nextLine(); // Consume the newline character
System.out.print("Gender: ");
String gender = scan.nextLine();
System.out.print("Employee ID: ");
String empId = scan.nextLine();
Employee employee = new Employee(name, age, gender, empId);
// employees.add(employee); // Remove the line after this if you're using this line
employee.displayDetails();
System.out.println("------------------------");
}
// Student details
for (int i = 1; i <= 5; i++) {
System.out.println("Enter details for Student " + i);
System.out.print("Name: ");
String name = scan.nextLine();
System.out.print("Age: ");
int age = scan.nextInt();
scan.nextLine(); // Consume the newline character
System.out.print("Gender: ");
String gender = scan.nextLine();
System.out.print("Student ID: ");
String studId = scan.nextLine();
Student student = new Student(name, age, gender, studId);
// students.add(student); // Remove the line after this if you're using this line
student.displayDetails();
System.out.println("------------------------");
}
scan.close();
// System.out.println("Employee Details:");
// for (Employee employee : employees) {
// employee.displayDetails();
// System.out.println("------------------------");
// }
// System.out.println("Student Details:");
// for (Student student : students) {
// student.displayDetails();
// System.out.println("------------------------");
// }
}
}
// Base class
class Person {
String name;
int age;
String gender;
// Constructor
Person(String name, int age, String gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
// Display details method
void displayDetails() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Gender: " + gender);
}
}
// Derived class Employee
class Employee extends Person {
String empId;
// Constructor
Employee(String name, int age, String gender, String empId) {
super(name, age, gender); // Call the constructor of the base class
this.empId = empId;
}
// Display details method, overriding base class method
@Override
void displayDetails() {
super.displayDetails(); // Call the method of the base class
System.out.println("Employee ID: " + empId);
}
}
// Derived class Student
class Student extends Person {
String studId;
// Constructor
Student(String name, int age, String gender, String studId) {
super(name, age, gender); // Call the constructor of the base class
this.studId = studId;
}
// Display details method, overriding base class method
@Override
void displayDetails() {
super.displayDetails(); // Call the method of the base class
System.out.println("Student ID: " + studId);
}
}
// Output:
// Enter details for Employee 1
// Name: Rohit
// Age: 20
// Gender: M
// Employee ID: E01
// Name: Rohit
// Age: 20
// Gender: M
// Employee ID: E01
// ------------------------
// Enter details for Employee 2
// Name: Rahul
// Age: 22
// Gender: M
// Employee ID: E02
// Name: Rahul
// Age: 22
// Gender: M
// Employee ID: E02
// ------------------------
// Enter details for Employee 3
// Name: Rohita
// Age: 21
// Gender: F
// Employee ID: E03
// Name: Rohita
// Age: 21
// Gender: F
// Employee ID: E03
// ------------------------
// Enter details for Employee 4
// Name: Rishita
// Age: 19
// Gender: F
// Employee ID: E04
// Name: Rishita
// Age: 19
// Gender: F
// Employee ID: E04
// ------------------------
// Enter details for Employee 5
// Name: Ramesh
// Age: 23
// Gender: M
// Employee ID: E05
// Name: Ramesh
// Age: 23
// Gender: M
// Employee ID: E05
// ------------------------
// Enter details for Student 1
// Name: Sahil
// Age: 18
// Gender: M
// Student ID: S01
// Name: Sahil
// Age: 18
// Gender: M
// Student ID: S01
// ------------------------
// Enter details for Student 2
// Name: Sanjana
// Age: 17
// Gender: F
// Student ID: S02
// Name: Sanjana
// Age: 17
// Gender: F
// Student ID: S02
// ------------------------
// Enter details for Student 3
// Name: Sanjeev
// Age: 18
// Gender: M
// Student ID: S03
// Name: Sanjeev
// Age: 18
// Gender: M
// Student ID: S03
// ------------------------
// Enter details for Student 4
// Name: Samyukta
// Age: 18
// Gender: F
// Student ID: S04
// Name: Samyukta
// Age: 18
// Gender: F
// Student ID: S04
// ------------------------
// Enter details for Student 5
// Name: Sameer
// Age: 17
// Gender: M
// Student ID: S05
// Name: Sameer
// Age: 17
// Gender: M
// Student ID: S05
// ------------------------