(OOPL5) Strings
🪢

(OOPL5) Strings

 

Employee Name & Email

import java.util.Scanner; class Employee { String name; String email; public Employee(String name) { this.name = name; this.formatEmployeeName(); this.generateEmail(); System.out.println("Name and Email generated."); } void formatEmployeeName() { StringBuffer nameBuffer = new StringBuffer(this.name.length()); // split 'name' into words. for (String stringPart : this.name.split(" ")) { if (stringPart.length() > 1) { // append first capital letter, then the rest. nameBuffer.append(stringPart.substring(0, 1).toUpperCase()) .append(stringPart.substring(1).toLowerCase()); } else { // just append the character. nameBuffer.append(stringPart.toUpperCase()); } // adding a space after each word. nameBuffer.append(" "); } this.name = nameBuffer.toString(); } void generateEmail() { StringBuffer emailBuffer = new StringBuffer(); // split 'name' into parts. String[] nameParts = this.name.split(" "); // append the first letter, then the last name. emailBuffer.append(nameParts[0].substring(0, 1)) .append(nameParts[nameParts.length - 1]) .append("@example.com"); this.email = emailBuffer.toString().toLowerCase(); } void display() { System.out.println("Name: " + this.name); System.out.println("Email: " + this.email); } } public class EmployeeInput { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Enter employee name: "); Employee employee = new Employee(scanner.nextLine()); employee.display(); scanner.close(); } }
PS F:\oopsies> java EmployeeInput Enter employee name: Abhigyan Tripathi Name and Email generated. Name: Abhigyan Tripathi Email: atripathi@example.com

Student Initials & Sorting

import java.util.*; class Student { String name; String compressedName; String initials; public Student(String name) { this.name = name; this.getInitials(); this.removeWhitespace(); this.display(); } void getInitials() { StringBuffer initialBuffer = new StringBuffer(); for (String stringPart : this.name.split(" ")) { initialBuffer.append(stringPart.substring(0, 1).toUpperCase() + "."); } this.initials = initialBuffer.toString(); } void removeWhitespace() { StringBuffer compressedBuffer = new StringBuffer(); for (String stringPart : this.name.split(" ")) { compressedBuffer.append(stringPart); } this.compressedName = compressedBuffer.toString(); } void display() { System.out.println(); System.out.println("Name: " + this.name); System.out.println("Initials: " + this.initials); System.out.println("Compressed Name: " + this.compressedName); System.out.println(); } static void findSubstring(List<Student> list, String subString) { List<Student> substrList = new ArrayList<>(); for (Student student : list) { if (student.name.contains(subString)) { substrList.add(student); } } System.out.println("\n" + substrList.toArray().length + " elements found."); for (Student student : substrList) { student.display(); } } static void sortList(List<Student> list) { List<Student> sortedList = new ArrayList<>(list); Collections.sort(sortedList, (s1, s2) -> s1.name.compareTo(s2.name)); System.out.println("\nThe sorted list is:"); for (Student student : sortedList) { student.display(); } } } public class StudentInput { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String choice; List<Student> students = new ArrayList<>(); do { System.out.println("Enter student name: "); students.add(new Student(scanner.nextLine())); System.out.print("Add another? [y/N] "); choice = scanner.nextLine(); } while (choice.equals("y")); System.out.println("Enter substring to find: "); Student.findSubstring(students, scanner.nextLine()); Student.sortList(students); scanner.close(); } }
PS F:\oopsies> java StudentInput Enter student name: Abhigyan Tripathi Name: Abhigyan Tripathi Initials: A.T. Compressed Name: AbhigyanTripathi Add another? [y/N] y Enter student name: Butternaan Chapati Name: Butternaan Chapati Initials: B.C. Compressed Name: ButternaanChapati Add another? [y/N] y Enter student name: Abhishek Tripathi Doesn't Exist Name: Abhishek Tripathi Doesn't Exist Initials: A.T.D.E. Compressed Name: AbhishekTripathiDoesn'tExist Add another? [y/N] n Enter substring to find: Tripathi 2 elements found. Name: Abhigyan Tripathi Initials: A.T. Compressed Name: AbhigyanTripathi Name: Abhishek Tripathi Doesn't Exist Initials: A.T.D.E. Compressed Name: AbhishekTripathiDoesn'tExist The sorted list is: Name: Abhigyan Tripathi Initials: A.T. Compressed Name: AbhigyanTripathi Name: Abhishek Tripathi Doesn't Exist Initials: A.T.D.E. Compressed Name: AbhishekTripathiDoesn'tExist Name: Butternaan Chapati Initials: B.C. Compressed Name: ButternaanChapati

Student Records & Functions

(This combines Questions 3, 4, and 5.)
import java.text.*; import java.util.*; class StudentRecord { int regNo; String name; String joinDateString; Calendar joinDate = new GregorianCalendar(); short semester; float gpa, cgpa; static int number = 0; StudentRecord(Scanner scanner) { System.out.print("Enter name: "); this.name = scanner.nextLine(); System.out.print("Enter joining date (dd/MM/yy): "); this.joinDateString = scanner.nextLine(); System.out.print("Enter semester: "); this.semester = scanner.nextShort(); System.out.print("Enter GPA: "); this.gpa = scanner.nextFloat(); System.out.print("Enter CGPA: "); this.cgpa = scanner.nextFloat(); scanner.nextLine(); try { DateFormat dateFormat = new SimpleDateFormat("dd/MM/yy"); Date date = dateFormat.parse(this.joinDateString); this.joinDate.setTime(date); } catch (ParseException ex) { ex.printStackTrace(); } number++; this.regNo = ((this.joinDate.get(Calendar.YEAR) % 100) * 100) + number; this.display(); } void display() { System.out.println(); System.out.println("Registration Number: " + regNo); System.out.println("Name: " + name); System.out.println("Joining Date: " + joinDateString); System.out.println("Semester: " + semester); System.out.println("GPA: " + gpa); System.out.println("CGPA: " + cgpa); System.out.println(); } void changeName() { String[] names = this.name.split(" "); StringBuffer initials = new StringBuffer(); for (int i = 0; i < names.length - 1; i++) { initials.append(names[i].charAt(0)).append(". "); } this.name = initials.toString().trim() + " " + names[names.length - 1]; this.display(); } String getName() { return this.name; } short getSemester() { return this.semester; } float getCgpa() { return this.cgpa; } static void sortBySemCgpa(List<StudentRecord> records) { records.sort(Comparator.comparing(StudentRecord::getSemester) .thenComparing(StudentRecord::getCgpa)); System.out.println("\n--- SORT BY SEM AND CGPA ---"); for (StudentRecord record : records) { record.display(); } } static void sortByName(List<StudentRecord> records) { records.sort(Comparator.comparing(StudentRecord::getName)); System.out.println("\n--- SORT BY NAME ---"); for (StudentRecord record : records) { record.display(); } } static void filterByCharacter(List<StudentRecord> records, char initial) { System.out.println("\n--- FILTER BY CHAR ---"); for (StudentRecord record : records) { if (record.name.charAt(0) == initial) { record.display(); } } } static void filterBySubstr(List<StudentRecord> records, String substring) { System.out.println("\n--- FILTER BY SUBSTR ---"); for (StudentRecord record : records) { if (record.name.contains(substring)) { record.display(); } } } } public class StudentRecordInput { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int num; do { System.out.println("Enter number of students (min. 5): "); num = scanner.nextInt(); scanner.nextLine(); } while (num < 5); List<StudentRecord> studentRecords = new ArrayList<StudentRecord>(num); for (int i = 0; i < num; i++) { studentRecords.add(new StudentRecord(scanner)); System.out.println("----\n"); } StudentRecord.sortBySemCgpa(studentRecords); StudentRecord.sortByName(studentRecords); StudentRecord.filterByCharacter(studentRecords, 'A'); StudentRecord.filterBySubstr(studentRecords, "Tripathi"); System.out.println("\n--- CHANGING NAMES ---"); for (StudentRecord record : studentRecords) { record.changeName(); } scanner.close(); } }
PS F:\oopsies> java StudentRecordInput Enter number of students (min. 5): 5 Enter name: Abhigyan Tripathi Enter joining date (dd/MM/yy): 21/05/10 Enter semester: 3 Enter GPA: 8.48 Enter CGPA: 8.5 Registration Number: 1001 Name: Abhigyan Tripathi Joining Date: 21/05/10 Semester: 3 GPA: 8.48 CGPA: 8.5 ---- Enter name: Abhishek Tripathi Enter joining date (dd/MM/yy): 15/11/18 Enter semester: 7 Enter GPA: 5 Enter CGPA: 4 Registration Number: 1802 Name: Abhishek Tripathi Joining Date: 15/11/18 Semester: 7 GPA: 5.0 CGPA: 4.0 ---- Enter name: Butternaan Chapati Enter joining date (dd/MM/yy): 16/05/17 Enter semester: 8 Enter GPA: 5 Enter CGPA: 5 Registration Number: 1703 Name: Butternaan Chapati Joining Date: 16/05/17 Semester: 8 GPA: 5.0 CGPA: 5.0 ---- Enter name: Hostel Biryani Enter joining date (dd/MM/yy): 17/08/20 Enter semester: 5 Enter GPA: 8.8 Enter CGPA: 7.8 Registration Number: 2004 Name: Hostel Biryani Joining Date: 17/08/20 Semester: 5 GPA: 8.8 CGPA: 7.8 ---- Enter name: Rishikesh Bhatt Enter joining date (dd/MM/yy): 17/11/21 Enter semester: 2 Enter GPA: 8.8 Enter CGPA: 8.8 Registration Number: 2105 Name: Rishikesh Bhatt Joining Date: 17/11/21 Semester: 2 GPA: 8.8 CGPA: 8.8 ---- --- SORT BY SEM AND CGPA --- Registration Number: 2105 Name: Rishikesh Bhatt Joining Date: 17/11/21 Semester: 2 GPA: 8.8 CGPA: 8.8 Registration Number: 1001 Name: Abhigyan Tripathi Joining Date: 21/05/10 Semester: 3 GPA: 8.48 CGPA: 8.5 Registration Number: 2004 Name: Hostel Biryani Joining Date: 17/08/20 Semester: 5 GPA: 8.8 CGPA: 7.8 Registration Number: 1802 Name: Abhishek Tripathi Joining Date: 15/11/18 Semester: 7 GPA: 5.0 CGPA: 4.0 Registration Number: 1703 Name: Butternaan Chapati Joining Date: 16/05/17 Semester: 8 GPA: 5.0 CGPA: 5.0 --- SORT BY NAME --- Registration Number: 1001 Name: Abhigyan Tripathi Joining Date: 21/05/10 Semester: 3 GPA: 8.48 CGPA: 8.5 Registration Number: 1802 Name: Abhishek Tripathi Joining Date: 15/11/18 Semester: 7 GPA: 5.0 CGPA: 4.0 Registration Number: 1703 Name: Butternaan Chapati Joining Date: 16/05/17 Semester: 8 GPA: 5.0 CGPA: 5.0 Registration Number: 2004 Name: Hostel Biryani Joining Date: 17/08/20 Semester: 5 GPA: 8.8 CGPA: 7.8 Registration Number: 2105 Name: Rishikesh Bhatt Joining Date: 17/11/21 Semester: 2 GPA: 8.8 CGPA: 8.8 --- FILTER BY CHAR --- Registration Number: 1001 Name: Abhigyan Tripathi Joining Date: 21/05/10 Semester: 3 GPA: 8.48 CGPA: 8.5 Registration Number: 1802 Name: Abhishek Tripathi Joining Date: 15/11/18 Semester: 7 GPA: 5.0 CGPA: 4.0 --- FILTER BY SUBSTR --- Registration Number: 1001 Name: Abhigyan Tripathi Joining Date: 21/05/10 Semester: 3 GPA: 8.48 CGPA: 8.5 Registration Number: 1802 Name: Abhishek Tripathi Joining Date: 15/11/18 Semester: 7 GPA: 5.0 CGPA: 4.0 --- CHANGING NAMES --- Registration Number: 1001 Name: A. Tripathi Joining Date: 21/05/10 Semester: 3 GPA: 8.48 CGPA: 8.5 Registration Number: 1802 Name: A. Tripathi Joining Date: 15/11/18 Semester: 7 GPA: 5.0 CGPA: 4.0 Registration Number: 1703 Name: B. Chapati Joining Date: 16/05/17 Semester: 8 GPA: 5.0 CGPA: 5.0 Registration Number: 2004 Name: H. Biryani Joining Date: 17/08/20 Semester: 5 GPA: 8.8 CGPA: 7.8 Registration Number: 2105 Name: R. Bhatt Joining Date: 17/11/21 Semester: 2 GPA: 8.8 CGPA: 8.8

Comma-Punctuated Numbers

import java.util.Scanner; public class CommaNumbers { public static String addCommas(String input) { int len = input.length(); StringBuffer result = new StringBuffer(); int commaPosition = 0; for (int i = len - 1; i >= 0; i--) { result.insert(0, input.charAt(i)); commaPosition++; if (commaPosition == 3 && i != 0) { result.insert(0, ","); commaPosition = 0; } } return result.toString(); } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Input String: "); String input = scanner.nextLine(); System.out.print("Output String: " + addCommas(input)); } }
PS F:\oopsies> java CommaNumbers Input String: 12345678 Output String: 12,345,678