0% found this document useful (0 votes)
4 views

Code5

The document provides a comprehensive overview of creating a 'Person' class in Java, including data members, setter and getter methods, and methods for displaying details. It demonstrates data validation in setter methods and constructors to ensure valid input values. The document also illustrates how to prevent data modification by making setter methods private.

Uploaded by

Muhammad Daniyal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Code5

The document provides a comprehensive overview of creating a 'Person' class in Java, including data members, setter and getter methods, and methods for displaying details. It demonstrates data validation in setter methods and constructors to ensure valid input values. The document also illustrates how to prevent data modification by making setter methods private.

Uploaded by

Muhammad Daniyal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Class and objects

Code: E1: creating classes and objects


01 public class Person {
02 // Data members
03 private String name;
04 private int age;
05 private String address;

06 // Setter methods to set the data members


07 public void setName(String name) {
08 this.name = name;
09 }

10 public void setAge(int age) {


11 this.age = age;
12 }

13 public void setAddress(String address) {


14 this.address = address;
15 }

16 // Getter methods to get the data members


17 public String getName() {
18 return name;
19 }

20 public int getAge() {


21 return age;
22 }

23 public String getAddress() {


24 return address;
25 }

26 // Member method to display person's details


27 public void displayPersonDetails() {
28 System.out.println("Name: " + name);
29 System.out.println("Age: " + age);
30 System.out.println("Address: " + address);
31 }
32 }
01 public class Main {
02 public static void main(String[] args) {
03 // Create an object of the Person class
04 Person person = new Person();

05 // Set the data members using setter methods


06 person.setName("John Doe");
07 person.setAge(30);
08 person.setAddress("123 Main St, Anytown, USA");

09 // Display the person's details using the member method


10 person.displayPersonDetails();
11 }
12 }
Code: E2: previous code with data validation
01 public class Person {
02 // Data members
03 private String name;
04 private int age;
05 private String address;

06 // Setter methods to set the data members with validation


07 public void setName(String name) {
08 if (name != null && !name.trim().isEmpty() && name.matches("[a-zA-Z\\s]+")){
09 this.name = name;
10 } else {
11 System.out.println("Invalid name. Name cannot be null, empty, or
contain numbers or special characters.");
12 }
13 }

14 public void setAge(int age) {


15 if (age > 0 && age < 150) {
16 this.age = age;
17 } else {
18 System.out.println("Invalid age. Age must be between 1 and 149.");
19 }
20 }

21 public void setAddress(String address) {


22 if (address != null && !address.trim().isEmpty()) {
23 this.address = address;
24 } else {
25 System.out.println("Invalid address. Address cannot be null or
empty.");
26 }
27 }

28 // Getter methods to get the data members


29 public String getName() {
30 return name;
31 }

32 public int getAge() {


33 return age;
34 }

35 public String getAddress() {


36 return address;
37 }

38 // Member method to display person's details


39 public void displayPersonDetails() {
40 System.out.println("Name: " + name);
41 System.out.println("Age: " + age);
42 System.out.println("Address: " + address);
43 }
44 }
01 public class Main {
02 public static void main(String[] args) {
03 // Create an object of the Person class
04 Person person = new Person();

05 // Set the data members using setter methods


06 person.setName("John Doe");
07 person.setAge(30);
08 person.setAddress("123 Main St, Anytown, USA");

09 // Display the person's details using the member method


10 person.displayPersonDetails();

11 // Attempt to set invalid values to demonstrate validation


12 person.setName("John123"); // Invalid name
13 person.setName("John@Doe"); // Invalid name
14 person.setAge(200); // Invalid age
15 person.setAddress(null); // Invalid address

16 // Display the person's details again to show that invalid values were
not set
17 person.displayPersonDetails();
18 }
19 }
Output:
01 Name: John Doe
02 Age: 30
03 Address: 123 Main St, Anytown, USA
04 Invalid name. Name cannot be null, empty, or contain numbers or special
characters.
05 Invalid name. Name cannot be null, empty, or contain numbers or special
characters.
06 Invalid age. Age must be between 1 and 149.
07 Invalid address. Address cannot be null or empty.
08 Name: John Doe
09 Age: 30
10 Address: 123 Main St, Anytown, USA
Code: E3: Class Constructors
01 public class Person {
02 // Data members
03 private String name;
04 private int age;
05 private String address;

06 // Constructor with validation


07 public Person(String name, int age, String address) {
08 this.name = name;
09 this.age = age;
10 this.address = address;
11 }

12 // Setter methods to set the data members with validation


13 public void setName(String name) {
14 if (name != null && !name.trim().isEmpty() && name.matches("[a-zA-Z\\s]+")) {
15 this.name = name;
16 } else {
17 System.out.println("Invalid name. Name cannot be null, empty, or contain numbers or special
characters.");
18 }
19 }

20 public void setAge(int age) {


21 if (age > 0 && age < 150) {
22 this.age = age;
23 } else {
24 System.out.println("Invalid age. Age must be between 1 and 149.");
25 }
26 }

27 public void setAddress(String address) {


28 if (address != null && !address.trim().isEmpty()) {
29 this.address = address;
30 } else {
31 System.out.println("Invalid address. Address cannot be null or empty.");
32 }
33 }

34 // Getter methods to get the data members


35 public String getName() {
36 return name;
37 }

38 public int getAge() {


39 return age;
40 }

41 public String getAddress() {


42 return address;
43 }

44 // Member method to display person's details


45 public void displayPersonDetails() {
46 System.out.println("Name: " + name);
47 System.out.println("Age: " + age);
48 System.out.println("Address: " + address);
49 }
50 }
01 public class Main {
02 public static void main(String[] args) {
03 // Create an object of the Person class with valid initial values
04 Person person = new Person("John Doe", 30, "123 Main St, Anytown, USA");

05 // Display the person's details using the member method


06 person.displayPersonDetails();

07 // Attempt to set invalid values to demonstrate validation


08 person.setName("John123"); // Invalid name
09 person.setName("John@Doe"); // Invalid name
10 person.setAge(200); // Invalid age
11 person.setAddress(null); // Invalid address
12 // Display the person's details again to show that invalid values were not set
13 person.displayPersonDetails();
14 }
15 }
Code: E4: previous code with data validation in constructors
01 public class Person {
02 // Data members
03 private String name;
04 private int age;
05 private String address;

06 // Constructor with validation


07 public Person(String name, int age, String address) {
08 setName(name); // Using the setter method to validate
09 setAge(age); // Using the setter method to validate
10 setAddress(address); // Using the setter method to validate
11 }

12 // Setter methods to set the data members with validation


13 public void setName(String name) {
14 if (name != null && !name.trim().isEmpty() && name.matches("[a-zA-Z\\s]+")) {
15 this.name = name;
16 } else {
17 System.out.println("Invalid name. Name cannot be null, empty, or contain numbers or special
characters.");
18 }
19 }

20 public void setAge(int age) {


21 if (age > 0 && age < 150) {
22 this.age = age;
23 } else {
24 System.out.println("Invalid age. Age must be between 1 and 149.");
25 }
26 }

27 public void setAddress(String address) {


28 if (address != null && !address.trim().isEmpty()) {
29 this.address = address;
30 } else {
31 System.out.println("Invalid address. Address cannot be null or empty.");
32 }
33 }
34 // Getter methods to get the data members
35 public String getName() {
36 return name;
37 }

38 public int getAge() {


39 return age;
40 }

41 public String getAddress() {


42 return address;
43 }

44 // Member method to display person's details


45 public void displayPersonDetails() {
46 System.out.println("Name: " + name);
47 System.out.println("Age: " + age);
48 System.out.println("Address: " + address);
49 }
50 }
Code: E5: ‘Set’ methods and preventing data modification
01 public class Person {
02 // Data members
03 private String name;
04 private int age;
05 private String address;

06 // Constructor
07 public Person(String name, int age, String address) {
08 this.name = name;
09 this.age = age;
10 this.address = address;
11 }

12 // Setter methods to set the data members with validation


13 private void setName(String name) {
14 if (name != null && !name.trim().isEmpty() && name.matches("[a-zA-Z\\s]+")) {
15 this.name = name;
16 } else {
17 System.out.println("Invalid name. Name cannot be null, empty, or contain numbers or special
characters.");
18 }
19 }
20 private void setAge(int age) {
21 if (age > 0 && age < 150) {
22 this.age = age;
23 } else {
24 System.out.println("Invalid age. Age must be between 1 and 149.");
25 }
26 }

27 private void setAddress(String address) {


28 if (address != null && !address.trim().isEmpty()) {
29 this.address = address;
30 } else {
31 System.out.println("Invalid address. Address cannot be null or empty.");
32 }
33 }

38 // Getter methods to get the data members


39 public String getName() {
40 return name;
41 }

42 public int getAge() {


43 return age;
44 }

45 public String getAddress() {


46 return address;
47 }

48 // Member method to display person's details


49 public void displayPersonDetails() {
50 System.out.println("Name: " + name);
51 System.out.println("Age: " + age);
52 System.out.println("Address: " + address);
53 }
54 }

You might also like