Skip to content

Commit 9e15c93

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

File tree

1 file changed

+21
-35
lines changed

1 file changed

+21
-35
lines changed
Lines changed: 21 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,50 @@
1-
//Pending solve it again
2-
3-
class Subject1
4-
{
1+
class Subject1 {
52
private String subID;
63
private String name;
74
private int maxMarks;
85
private int marksObtain;
96

10-
public Subject1(String subID,String name,int maxMarks)
11-
{
7+
public Subject1(String subID,String name,int maxMarks) {
128
this.subID=subID;
139
this.name=name;
1410
this.maxMarks=maxMarks;
1511
}
12+
1613
public String getSubID(){return subID;}
1714
public String getName(){return name;}
1815
public int getMaxMarks(){return maxMarks;}
1916
public int getMarksObtain(){return marksObtain;}
2017

21-
public void setMaxMarks(int mm)
22-
{
18+
public void setMaxMarks(int mm) {
2319
maxMarks=mm;
2420
}
25-
public void setMarksObtain(int m)
26-
{
21+
22+
public void setMarksObtain(int m) {
2723
marksObtain=m;
2824
}
29-
boolean isQualified()
30-
{
25+
26+
boolean isQualified() {
3127
return marksObtain>=maxMarks/10*4;
3228
}
33-
public String toString()
34-
{
29+
30+
public String toString() {
3531
return "\nSubject ID:"+subID+"\nName :"+name+"\nMarks Obtained: "+marksObtain;
3632
}
3733
}
3834

39-
class Student1
40-
{
35+
class Student1 {
4136
private String rollNo;
4237
private String name;
4338
private String dept;
4439
private int numOfSub;
4540
private Subject sub[];
4641

47-
48-
public Student1(String roll,String name)
49-
{
42+
public Student1(String roll,String name) {
5043
this.rollNo=roll;
5144
this.name=name;
52-
5345
}
54-
public Student1(String roll,String name,int ns)
55-
{
46+
47+
public Student1(String roll,String name,int ns) {
5648
this.rollNo=roll;
5749
this.name=name;
5850
this.numOfSub=ns;
@@ -64,28 +56,22 @@ public Student1(String roll,String name,int ns)
6456
public int getNoOfSubjects(){return numOfSub;}
6557
public Subject[] getSubjects(){return sub;}
6658

67-
public void setDept(String dept)
68-
{
59+
public void setDept(String dept) {
6960
this.dept=dept;
7061
}
71-
public void setSubjects(Subject ...subs)
72-
{
62+
63+
public void setSubjects(Subject ...subs) {
7364
for(int i=0;i<subs.length;i++)
7465
sub[i]=subs[i];
7566
}
7667

77-
public String toString()
78-
{
68+
public String toString() {
7969
return "Roll :" +rollNo+"\nName :"+name+"\nDept :"+dept;
8070
}
81-
8271
}
8372

84-
public class OOPsIMP
85-
{
86-
87-
public static void main(String[] args)
88-
{
73+
public class OOPsIMP {
74+
public static void main(String[] args) {
8975
Subject subs[]=new Subject[3];
9076
subs[0]=new Subject("s101","DS",100);
9177
subs[1]=new Subject("s102","Algorithms",100);
@@ -94,4 +80,4 @@ public static void main(String[] args)
9480
for(Subject s:subs)
9581
System.out.println(s);
9682
}
97-
}
83+
}

0 commit comments

Comments
 (0)