Java Laboratory Manual: String Processing with Student Class
Objective:
To understand and practice various string manipulation techniques in Java using a custom
Student class.
To learn how to perform string operations such as concatenation, comparison, searching,
replacing, and conversion with objects of the Student class.
Exercise 1: Basic String Operations in the Student Class
Task:
1. Create a Student class with fields: firstName, middleName, and phone number.
2. Initialize these fields using a constructor.
3. Perform basic string operations like concatenation, length, and substring using the
firstName, middleName, and phoneNumber.
a) Create getFullName method to return firstName followed by middleName
b) Create getFirstLetterOfMiddleName to return first letter of middle name
c) Create getFirstNameUppercase to change first name to upper case
d) Create getFirtsNameLength to return number of characters in first name
Code:
class Student {
private String firstName;
private String middleName;
private int phoneNumber;
// Constructor to initialize student
public Student(String firstName, String middleName, int age) {
this.firstName = firstName;
this.middleName = middleName;
this.age = age;
}
public String getFullName() {
return firstName + " " + middleName + " " + age;
}
public String getFirstName() {
return firstName;
}
public String getMiddleName() {
return middleName;
}
public int getPhoneNumber() {
return age;
}
public String getMiddleFirstLetter() {
return firstName.substring(0, 1); // Substring of firstName
}
public String getFirstNameUppercase() {
return firstName.toUpperCase(); // Convert firstName to uppercase
}
public String getMiddleNameLowercase() {
return middleName.toLowerCase(); // Convert middleName to lowercase
}
}
public class StringOperationsStudent {
public static void main(String[] args) {
// Create Student object using the constructor
Student student = new Student("John", "William", 20);
// Concatenate full name
System.out.println("Full Name: " + student.getFullName());
// Find the length of firstName
System.out.println("Length of First Name: " + student.getFirstName().length());
// Extract a substring from firstName
System.out.println("Substring of First Name: " + student.getFirstNameSubstring());
// Convert firstName to uppercase
System.out.println("First Name in Uppercase: " +
student.getFirstNameUppercase());
// Convert middleName to lowercase
System.out.println("Middle Name in Lowercase: " +
student.getMiddleNameLowercase());
}
}
Expected Output:
Full Name: John William 20
Length of First Name: 4
Substring of First Name: Joh
First Name in Uppercase: JOHN
Middle Name in Lowercase: william
Exercise 2: String Comparison for Student Class
Task:
1. Compare the firstName, middleName, and phoneNumber fields of two Student objects
using equals() and ==.
2. Check if the firstName and middleName of two students are the same.
3. Override equals() method to compare Student objects based on firstName, middleName, and
phone number
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
Student student = (Student) obj;
return phoneNumber.equals( student.phoneNumber) && firstName.equals(student.firstName) &&
middleName.equals(student.middleName);
}
}
Code:
class Student {
private String firstName;
private String middleName;
private String phoneNumber;
// Constructor
public Student(String firstName, String middleName, String phoneNumber) {
this.firstName = firstName;
this.middleName = middleName;
this. phoneNumber = phoneNumber;
}
public String getFirstName() {
return firstName;
}
public String getMiddleName() {
return middleName;
}
public int getAge() {
return age;
}
// Override equals() method to compare Student objects based on firstName, middleName, and age
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
Student student = (Student) obj;
return phoneNumber. equals(student.age) && firstName.equals(student.firstName) &&
middleName.equals(student.middleName);
}
}
public class CompareStudentStrings {
public static void main(String[] args) {
// Creating two student objects
Student student1 = new Student("John", "William", 20);
Student student2 = new Student("John", "William", 20);
Student student3 = new Student("Jane", "Doe", 22);
// Comparing students using equals()
System.out.println("Are student1 and student2 equal? " + student1.equals(student2)); // true
System.out.println("Are student1 and student3 equal? " + student1.equals(student3)); // false
// Comparing first names
System.out.println("Are first names of student1 and student2 equal? " +
student1.getFirstName().equals(student2.getFirstName())); // true
}
}
Expected Output:
Are student1 and student2 equal? true
Are student1 and student3 equal? false
Are first names of student1 and student2 equal? True
Exercise 3: Validate first name, middle name, phone number using regex
Modify the Student class with a method that validates the firstName, middleName, and
phoneNumber according to the given rules using regular expressions (regex).
a) Create valideName method to validate names given the following rules
First Name and Middle Name:
a. Should begin with a capital letter.
b. Should be followed by small letters or a forward slash /.
c. There should be at least two characters after the first letter.
b) Create ValidatephoneNumber method to validate phone number given the following
rules:
Phone Number:
o Should start with 09 or +2519 or 2519 or 07 or +2517 or 2517 .
o Followed by 8 digits.
o The last 8 digits should not be all zeros (e.g., 00000000 is not valid).
Code fragment for regex
String phonePattern = “[A-Za-z0-9]+";
Pattern pattern = Pattern.compile(phonePattern);
Matcher matcher = pattern.matcher(phoneNumber);
if (matcher.matches()) {
return true;
else
return false;
Modified Student Class with Validation Method:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class Student {
private String firstName;
private String middleName;
private String phoneNumber;
// Constructor to initialize student details
public Student(String firstName, String middleName, String phoneNumber) {
this.firstName = firstName;
this.middleName = middleName;
this.phoneNumber = phoneNumber;
// Getter methods
public String getFirstName() {
return firstName;
public String getMiddleName() {
return middleName;
public String getPhoneNumber() {
return phoneNumber;
// Method to validate firstName, middleName, and phoneNumber using regex
public boolean validateStudentDetails() {
return validateName(firstName) && validateName(middleName) &&
validatePhoneNumber(phoneNumber);
// Validate the name (first name or middle name)
private boolean validateName(String name) {
// Regex: First letter capital, followed by lowercase letters or '/'
String namePattern = "^[A-Z][a-z/]{2,}$";
Pattern pattern = Pattern.compile(namePattern);
Matcher matcher = pattern.matcher(name);
return matcher.matches();
// Validate the phone number using regex
private boolean validatePhoneNumber(String phoneNumber) {
// Regex: Starts with 09 or +251 followed by 8 digits, and the last 8 digits should not be all
zeros
String phonePattern = "^(\\+2519|\\+2517|2519|2517|07|09)\\d{8}$";
Pattern pattern = Pattern.compile(phonePattern);
Matcher matcher = pattern.matcher(phoneNumber);
if (matcher.matches()) {
// Check that the last 8 digits are not all zeros
String lastEightDigits = phoneNumber.substring(phoneNumber.length() - 8);
if (lastEightDigits.equals("00000000")) {
return false;
return true;
return false;
public class StudentValidation {
public static void main(String[] args) {
// Create some student objects with different data
Student student1 = new Student("John", "Doe", "0912345678");
Student student2 = new Student("Alice", "Smith", "+251923456789");
Student student3 = new Student("Bob", "A/", "0912345670");
Student student4 = new Student("John", "williams", "0910000000"); // Invalid phone
number
// Validate each student's details
System.out.println("Student 1 valid: " + student1.validateStudentDetails()); // true
System.out.println("Student 2 valid: " + student2.validateStudentDetails()); // true
System.out.println("Student 3 valid: " + student3.validateStudentDetails()); // true
System.out.println("Student 4 valid: " + student4.validateStudentDetails()); // false
}