Commit 9e15c93
committed
feat: Add Subject1 and Student1 classes to model subjects and students with encapsulation
WHAT the code does:
Defines Subject1 with private fields:
- subID, name, maxMarks, marksObtain.
- Constructor initializes subID, name, and maxMarks.
- Getters and setters provide controlled access.
- isQualified() checks if marks are ≥ 40% of max marks.
- toString() returns formatted subject details.
Defines Student1 with private fields:
- rollNo, name, dept, numOfSub, and an array of Subject references.
- Overloaded constructors initialize rollNo, name, and optionally number of subjects.
- Getters return student info and associated subjects.
- setDept() assigns department.
- setSubjects(Subject... subs) assigns subjects to the student.
- toString() formats student details.
Defines OOPsIMP with main():
- Creates an array of 3 Subject objects (DS, Algorithms, OS).
- Prints subject details.
WHY this matters:
Demonstrates object composition: a Student1 contains multiple Subject references.
Illustrates encapsulation through private fields and getters/setters.
Shows constructor overloading for flexible object creation.
Introduces array-of-objects concept in OOP design.
HOW it works:
OOPsIMP creates Subject objects and prints them using toString().
Student1 is designed to associate with multiple Subject objects, but this link is not yet used in main().
Validation logic (40% threshold) is encoded in isQualified() for subject performance checks.
Tips and gotchas:
OOPsIMP uses Subject, but the defined class is Subject1 — likely a naming mismatch that will cause compilation errors.
Student1’s sub array is declared but never initialized; setSubjects() will throw NullPointerException. It should be initialized with new Subject[numOfSub].
isQualified() uses integer division (maxMarks/10*4), which may cause precision issues; prefer (maxMarks * 40) / 100.
Currently, no setters exist for subID or name in Subject1, making them immutable after construction — which may be intentional.
Use-cases:
Educational demonstration of OOP composition, encapsulation, and constructor overloading.
Models a simple academic system linking students and subjects.
Foundation for expanding into grading systems, course registration, or transcript generation.
Short key: class-subject1 student1 oop composition encapsulation.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent df2f9bb commit 9e15c93
1 file changed
+21
-35
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
3 | | - | |
4 | | - | |
| 1 | + | |
5 | 2 | | |
6 | 3 | | |
7 | 4 | | |
8 | 5 | | |
9 | 6 | | |
10 | | - | |
11 | | - | |
| 7 | + | |
12 | 8 | | |
13 | 9 | | |
14 | 10 | | |
15 | 11 | | |
| 12 | + | |
16 | 13 | | |
17 | 14 | | |
18 | 15 | | |
19 | 16 | | |
20 | 17 | | |
21 | | - | |
22 | | - | |
| 18 | + | |
23 | 19 | | |
24 | 20 | | |
25 | | - | |
26 | | - | |
| 21 | + | |
| 22 | + | |
27 | 23 | | |
28 | 24 | | |
29 | | - | |
30 | | - | |
| 25 | + | |
| 26 | + | |
31 | 27 | | |
32 | 28 | | |
33 | | - | |
34 | | - | |
| 29 | + | |
| 30 | + | |
35 | 31 | | |
36 | 32 | | |
37 | 33 | | |
38 | 34 | | |
39 | | - | |
40 | | - | |
| 35 | + | |
41 | 36 | | |
42 | 37 | | |
43 | 38 | | |
44 | 39 | | |
45 | 40 | | |
46 | 41 | | |
47 | | - | |
48 | | - | |
49 | | - | |
| 42 | + | |
50 | 43 | | |
51 | 44 | | |
52 | | - | |
53 | 45 | | |
54 | | - | |
55 | | - | |
| 46 | + | |
| 47 | + | |
56 | 48 | | |
57 | 49 | | |
58 | 50 | | |
| |||
64 | 56 | | |
65 | 57 | | |
66 | 58 | | |
67 | | - | |
68 | | - | |
| 59 | + | |
69 | 60 | | |
70 | 61 | | |
71 | | - | |
72 | | - | |
| 62 | + | |
| 63 | + | |
73 | 64 | | |
74 | 65 | | |
75 | 66 | | |
76 | 67 | | |
77 | | - | |
78 | | - | |
| 68 | + | |
79 | 69 | | |
80 | 70 | | |
81 | | - | |
82 | 71 | | |
83 | 72 | | |
84 | | - | |
85 | | - | |
86 | | - | |
87 | | - | |
88 | | - | |
| 73 | + | |
| 74 | + | |
89 | 75 | | |
90 | 76 | | |
91 | 77 | | |
| |||
94 | 80 | | |
95 | 81 | | |
96 | 82 | | |
97 | | - | |
| 83 | + | |
0 commit comments