Commit e474d62
committed
feat: Add Subject class and ArrayOfObject demo for handling multiple objects in arrays
WHAT the code does:
Defines a Subject class with fields:
- subID, name, maxMarks, marksObtains.
Constructor initializes subID, name, and maxMarks.
Provides getters and setters for encapsulated access.
Implements isQualified() to check if marks obtained are at least 40% of max marks.
Overrides toString() to display subject details.
Defines ArrayOfObject with main():
- Creates an array of Subject objects.
- Initializes three subjects (DS, Algorithms, Operating Systems).
- Iterates over the array and prints each Subject.
WHY this matters:
Demonstrates how arrays can store multiple objects in Java.
Encapsulates object state with private fields and exposes controlled access through methods.
Highlights object initialization via constructors and usage of overridden toString() for formatted output.
Introduces a simple grading rule (40% threshold) through isQualified().
HOW it works:
Subject subs[] = new Subject[3] creates an array of references.
Each index is assigned a new Subject object.
Enhanced for loop iterates through the array, printing details via toString().
Output shows subject IDs, names, and marksObtains (default 0 unless explicitly set).
Tips and gotchas:
marksObtains defaults to 0 because it’s never set before printing; consider using setMarksObtain().
isQualified() currently uses integer division in maxMarks/10*4, which may behave unexpectedly for non-multiples of 10; use (maxMarks * 40) / 100 instead.
Overriding toString() is effective, but adding subject maxMarks could improve clarity.
For real systems, prefer List<Subject> instead of fixed arrays for flexibility.
Use-cases:
Educational example of arrays of objects in Java.
Useful for modeling student-subject relationships in simple academic systems.
Foundation for extending into collections-based applications like gradebooks or course management tools.
Short key: class-subject arrayofobject encapsulation object-array.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 8fa6ed0 commit e474d62
1 file changed
+19
-27
lines changedLines changed: 19 additions & 27 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
3 | | - | |
4 | | - | |
5 | | - | |
6 | | - | |
7 | | - | |
8 | | - | |
9 | | - | |
10 | | - | |
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
11 | 12 | | |
12 | 13 | | |
13 | 14 | | |
14 | 15 | | |
15 | 16 | | |
16 | | - | |
17 | | - | |
| 17 | + | |
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
| |||
25 | 25 | | |
26 | 26 | | |
27 | 27 | | |
28 | | - | |
29 | | - | |
| 28 | + | |
30 | 29 | | |
31 | 30 | | |
32 | 31 | | |
33 | | - | |
34 | | - | |
| 32 | + | |
35 | 33 | | |
36 | 34 | | |
37 | 35 | | |
38 | | - | |
39 | | - | |
| 36 | + | |
40 | 37 | | |
41 | 38 | | |
42 | 39 | | |
43 | | - | |
44 | | - | |
| 40 | + | |
45 | 41 | | |
46 | 42 | | |
47 | | - | |
48 | 43 | | |
49 | 44 | | |
50 | | - | |
51 | | - | |
52 | | - | |
53 | | - | |
54 | | - | |
| 45 | + | |
| 46 | + | |
55 | 47 | | |
56 | 48 | | |
57 | 49 | | |
| |||
67 | 59 | | |
68 | 60 | | |
69 | 61 | | |
70 | | - | |
| 62 | + | |
0 commit comments