Skip to content

Commit 5c4d448

Browse files
committed
feat: Add Student class with methods for total, average, grade, and details
WHAT the code does: Defines Student class with public fields: - rollno, name, course, and three marks (m1, m2, m3). Implements methods: - total(): returns sum of marks. - average(): returns average as float. - grade(): assigns grade based on average (A, B, or C). - Details(): returns formatted string with student information. Defines StudentTest with main(): - Creates a Student object, sets properties manually. - Prints total, average, and details. WHY this matters: Demonstrates basic OOP design with attributes (fields) and behaviors (methods). Encapsulates common student operations like total, average, and grading into methods. Illustrates how methods can reuse each other (average() calls total()). Provides a real-world example of class modeling for education-related applications. HOW it works: Student object is created and initialized with rollno=1, name="Don", course="CSE", marks=70,60,50. total() → 180. average() → 60.0. grade() would return 'B' (though unused in main). Details() prints student info, but concatenates marks without spaces (705060). Tips and gotchas: Field names are public, breaking encapsulation; prefer private fields with getters/setters. Details() concatenates marks directly; adding spaces or formatting would improve readability. grade() is implemented but never called in StudentTest. Naming conventions: Course should be lowercase course, and method names should start with lowercase (details()). For multiple students, using arrays or lists would make this more scalable. Use-cases: Educational example of combining fields and methods in a class. Foundation for a grading or student management system. Practical demonstration of OOP concepts for beginners. Short key: class-student methods total-average-grade details. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent dc61946 commit 5c4d448

File tree

1 file changed

+16
-19
lines changed

1 file changed

+16
-19
lines changed

Section11ObjectOrientedProgramming/src/Student.java

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ class Student {
44
public String Course;
55
public int m1, m2, m3;
66

7-
//Now declares the methods .you above declares all the properties now declares behaivors means Methods.
8-
7+
//Now declares the methods .you above declares all the properties now declares behaviors means Methods.
98
public int total() {
109
return m1 + m2 + m3;
1110
}
@@ -27,24 +26,22 @@ public String Details() {
2726
}
2827
}
2928

30-
class StudentTest {
31-
public static void main(String[] args) {
32-
Student s = new Student();
33-
34-
s.rollno=1;
35-
s.name="Don";
36-
s.Course="CSE";
37-
38-
s.m1=70;
39-
s.m2=60;
40-
s.m3=50;
29+
class StudentTest {
30+
public static void main(String[] args) {
31+
Student s = new Student();
4132

42-
s.average();
33+
s.rollno=1;
34+
s.name="Don";
35+
s.Course="CSE";
4336

44-
System.out.println("Total : "+s.total());
45-
System.out.println("Average: "+s.average());
37+
s.m1=70;
38+
s.m2=60;
39+
s.m3=50;
4640

47-
System.out.println("Details:\n"+s.Details());
41+
s.average();
4842

49-
}
50-
}
43+
System.out.println("Total : "+s.total());
44+
System.out.println("Average: "+s.average());
45+
System.out.println("Details:\n"+s.Details());
46+
}
47+
}

0 commit comments

Comments
 (0)