Skip to content

Commit df2f9bb

Browse files
committed
feat: Add Student2 class with encapsulation, validation, and constructors
WHAT the code does: Defines Student2 with private fields: - rollNo, name, age, and course. Implements: - Default constructor: initializes with safe default values. - Parameterized constructor: uses setters for validation during initialization. Provides getters and setters with validation: - rollNo must be positive. - name and course must not be null or empty. - age must be between 16 and 60. Adds getDetails() to return formatted student information. Defines GetAndSetMethodSolve with main(): - Demonstrates object creation via default and parameterized constructors. - Updates properties with setters. - Tests validation by providing invalid inputs. - Prints full details and individual properties using getters. WHY this matters: Encapsulates object data for safety and consistency. Introduces input validation to enforce domain-specific rules (e.g., age range, positive roll number). Demonstrates constructor overloading for flexible object creation. Shows practical usage of getters and setters beyond simple boilerplate by embedding validation logic. HOW it works: s1 is created with default values, then updated using setters. s2 is created directly with valid values through the parameterized constructor. s3 demonstrates validation: invalid inputs trigger error messages, leaving defaults unchanged. getDetails() prints the internal state in a human-readable format. Tips and gotchas: Validation currently only prints warnings; in real systems, throwing exceptions or rejecting construction might be better. Using immutable design (final fields, constructor-only initialization) can eliminate the need for setters in some cases. Overriding toString() instead of using getDetails() would integrate better with Java’s object system. Validation logic is basic; could be extended with regex for names, or course catalogs for valid courses. Use-cases: Educational example of encapsulation, constructors, and validation in Java. Foundation for academic systems like student records or enrollment systems. Demonstrates robust setter/getter design beyond trivial access. Short key: class-student2 encapsulation validation constructor-overloading. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent e1f3ab4 commit df2f9bb

File tree

1 file changed

+122
-1
lines changed

1 file changed

+122
-1
lines changed
Lines changed: 122 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,126 @@
1+
/**
2+
* This class demonstrates the proper implementation of getters and setters
3+
* for encapsulating and protecting object data
4+
*/
5+
6+
class Student2 {
7+
// Private data members - hidden from direct access
8+
private int rollNo;
9+
private String name;
10+
private int age;
11+
private String course;
12+
13+
// Default constructor
14+
public Student2() {
15+
rollNo = 0;
16+
name = "Unknown";
17+
age = 18;
18+
course = "Not Assigned";
19+
}
20+
21+
// Parameterized constructor
22+
public Student2(int rollNo, String name, int age, String course) {
23+
setRollNo(rollNo); // Using setter for validation
24+
setName(name);
25+
setAge(age);
26+
setCourse(course);
27+
}
28+
29+
// Getter for rollNo
30+
public int getRollNo() {
31+
return rollNo;
32+
}
33+
34+
// Setter for rollNo with validation
35+
public void setRollNo(int rollNo) {
36+
if (rollNo > 0) {
37+
this.rollNo = rollNo;
38+
} else {
39+
System.out.println("Invalid Roll Number. Must be positive.");
40+
}
41+
}
42+
43+
// Getter for name
44+
public String getName() {
45+
return name;
46+
}
47+
48+
// Setter for name with validation
49+
public void setName(String name) {
50+
if (name != null && !name.trim().isEmpty()) {
51+
this.name = name;
52+
} else {
53+
System.out.println("Invalid Name. Cannot be empty.");
54+
}
55+
}
56+
57+
// Getter for age
58+
public int getAge() {
59+
return age;
60+
}
61+
62+
// Setter for age with validation
63+
public void setAge(int age) {
64+
if (age >= 16 && age <= 60) {
65+
this.age = age;
66+
} else {
67+
System.out.println("Invalid Age. Must be between 16 and 60.");
68+
}
69+
}
70+
71+
// Getter for course
72+
public String getCourse() {
73+
return course;
74+
}
75+
76+
// Setter for course with validation
77+
public void setCourse(String course) {
78+
if (course != null && !course.trim().isEmpty()) {
79+
this.course = course;
80+
} else {
81+
System.out.println("Invalid Course. Cannot be empty.");
82+
}
83+
}
84+
85+
// Display student information
86+
public String getDetails() {
87+
return "Student [Roll No: " + rollNo +
88+
", Name: " + name +
89+
", Age: " + age +
90+
", Course: " + course + "]";
91+
}
92+
}
93+
194
public class GetAndSetMethodSolve {
295
public static void main(String[] args) {
3-
96+
// Create student using default constructor
97+
Student2 s1 = new Student2();
98+
System.out.println("Default Student: " + s1.getDetails());
99+
100+
// Set properties using setters
101+
s1.setRollNo(101);
102+
s1.setName("John Doe");
103+
s1.setAge(20);
104+
s1.setCourse("Computer Science");
105+
System.out.println("Updated Student: " + s1.getDetails());
106+
107+
// Create student using parameterized constructor
108+
Student2 s2 = new Student2(102, "Jane Smith", 22, "Mathematics");
109+
System.out.println("Second Student: " + s2.getDetails());
110+
111+
// Test validation for invalid inputs
112+
Student2 s3 = new Student2();
113+
s3.setRollNo(-5); // Invalid roll number
114+
s3.setName(""); // Invalid name
115+
s3.setAge(15); // Invalid age
116+
s3.setCourse(""); // Invalid course
117+
System.out.println("Student with invalid inputs: " + s3.getDetails());
118+
119+
// Accessing properties using getters
120+
System.out.println("\nAccessing individual properties of second student:");
121+
System.out.println("Roll No: " + s2.getRollNo());
122+
System.out.println("Name: " + s2.getName());
123+
System.out.println("Age: " + s2.getAge());
124+
System.out.println("Course: " + s2.getCourse());
4125
}
5126
}

0 commit comments

Comments
 (0)