Practice 4.
1) Soln:
import java.util.Scanner;
public class Employee {
private final String name;
private final String username;
private final String email;
private String password;
public Employee() {
name = setName();
username = setUserName(name);
email = setEmail(username);
password = setPassword(username);
// Method to generate Employee Details
@Override
public String toString() {
return "Employee Details\n" +
"Name : " + name + "\n" +
"Username : " + username + "\n" +
"Email : " + email + "\n" +
"Initial Password : " + password;
// Method to count occurrences of a char in a String
private int countChars(String str, char ch) {
int count = 0;
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == ch) {
count++;
}
}
return count;
// Method to set the Employee name
private String setName() {
Scanner scanner = new Scanner(System.in);
String name;
do {
System.out.println("Enter the employee's first and last name (e.g., John Doe): ");
name = scanner.nextLine();
} while (countChars(name, ' ') != 1);
return name;
// Method to generate username
private String setUserName(String name) {
String[] parts = name.split(" ");
return (parts[0] + "." + parts[1]).toLowerCase();
// Method to generate email address
private String setEmail(String username) {
String[] parts = username.split("\\.");
return parts[0].charAt(0) + parts[1] + "@oracleacademy.Test";
// Method to generate the password
private String setPassword(String username) {
// Replace vowels with asterisks
String password = username.replaceAll("[AEIOUaeiou]", "*");
// Ensure password length is 8 characters
if (password.length() < 8) {
while (password.length() < 8) {
password += "*";
} else {
password = password.substring(0, 8);
// Ensure at least one uppercase letter is present
boolean hasUpper = false;
for (char c : password.toCharArray()) {
if (Character.isLetter(c) && Character.isUpperCase(c)) {
hasUpper = true;
break;
if (!hasUpper) {
for (int i = 0; i < password.length(); i++) {
if (Character.isLetter(password.charAt(i))) {
password = password.substring(0, i) +
Character.toUpperCase(password.charAt(i)) +
password.substring(i + 1);
break;
return password;
}
2) Soln:
public String reverse(String str) {
String strRev = "";
for (int i = str.length() - 1; i >= 0; i--) {
strRev += str.charAt(i);
return strRev;