Skip to content

Commit e474d62

Browse files
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

File tree

1 file changed

+19
-27
lines changed

1 file changed

+19
-27
lines changed
Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
//You have to wtite a 2 classes one is subject and students.
2-
//you have to write a class for subject and subjID and name and maxMarks and maxobtain. you have to decide the data type and properties methods and constructers. how many constructer are possible.
3-
//data type Should be a string for subID and name also and MaxMarks is Int and MarksObtain is als.
4-
//Property methods get method for all of them... what about the set method
5-
//maxmarks can change everytime so marksobtain are also use the set method
6-
//constructer you can not have non parameterized cons.. subject(sid, name) and subject(",",max marks, marks obtain)
7-
8-
9-
class Subject
10-
{
1+
/*
2+
You have to write a 2 classes one is subject and students.
3+
you have to write a class for subject and subjID and name and maxMarks and maxobtain.
4+
you have to decide the data type and properties methods and constructers. how many constructer are possible.
5+
data type Should be a string for subID and name also and MaxMarks is Int and MarksObtain is als.
6+
Property methods get method for all of them... what about the set method
7+
maxmarks can change everytime so marksobtain are also use the set method
8+
constructer you can not have non parameterized cons.. subject(sid, name) and subject(",",max marks, marks obtain)
9+
*/
10+
11+
class Subject {
1112
private String subID;
1213
private String name;
1314
private int maxMarks;
1415
private int marksObtains;
1516

16-
public Subject(String subID,String name,int maxMarks)
17-
{
17+
public Subject(String subID,String name,int maxMarks) {
1818
this.subID=subID;
1919
this.name=name;
2020
this.maxMarks=maxMarks;
@@ -25,33 +25,25 @@ public Subject(String subID,String name,int maxMarks)
2525
public int getMaxMarks(){return maxMarks;}
2626
public int getMarksObtains(){return marksObtains;}
2727

28-
public void setMaxMarks(int mm)
29-
{
28+
public void setMaxMarks(int mm) {
3029
maxMarks=mm;
3130
}
3231

33-
public void setMarksObtain(int m)
34-
{
32+
public void setMarksObtain(int m) {
3533
marksObtains=m;
3634
}
3735

38-
boolean isQualified()
39-
{
36+
boolean isQualified() {
4037
return marksObtains>=maxMarks/10*4;
4138
}
4239

43-
public String toString()
44-
{
40+
public String toString() {
4541
return"\n SubjectID: "+subID+"\n Name "+name+"\n MarksObtained "+marksObtains;
4642
}
47-
4843
}
4944

50-
public class ArrayOfObject
51-
{
52-
53-
public static void main(String[] args)
54-
{
45+
public class ArrayOfObject {
46+
public static void main(String[] args) {
5547
//Array of objects
5648
//Subs Reference to an object
5749
//Pointers or References 0, 1, 2. there are no pointers in JAVA.
@@ -67,4 +59,4 @@ public static void main(String[] args)
6759
for(Subject s:subs)
6860
System.out.println(s);
6961
}
70-
}
62+
}

0 commit comments

Comments
 (0)