Java Code Notes
Java Code Notes
import java.util.Scanner;
System.out.println("Enter an integer:");
int intValue = scanner.nextInt();
String intAsString = Integer.toString(intValue);
System.out.println("Integer converted to string: " + intAsString);
// vi. Count number of vowels, digits, special character, lower and upper alphabets, words in
an input string
int vowels = 0, digits = 0, specialCharacters = 0, lowerCaseChars = 0, upperCaseChars = 0,
words = 0;
for (char ch : inputString.toCharArray()) {
if (Character.isLetter(ch)) {
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ||
ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') {
vowels++;
} else {
if (Character.isUpperCase(ch)) {
upperCaseChars++;
} else {
lowerCaseChars++;
}
}
} else if (Character.isDigit(ch)) {
digits++;
} else {
specialCharacters++;
}
}
words = inputString.split("\\s+").length;
System.out.println("Number of vowels: " + vowels);
System.out.println("Number of digits: " + digits);
System.out.println("Number of special characters: " + specialCharacters);
System.out.println("Number of lowercase characters: " + lowerCaseChars);
System.out.println("Number of uppercase characters: " + upperCaseChars);
System.out.println("Number of words: " + words);
scanner.close();
}
}
Output:
Question5:
// Define the Shape class
abstract class Shape {
// Abstract methods
abstract double getPeri();
abstract double getArea();
}
// Constructor
public Circle(double radius) {
this.radius = radius;
}
// Constructor
public Bank() {
balance = 0.0;
}
// Deposit method
public void deposit(double amount) {
balance += amount;
System.out.println("Amount deposited: Rs. " + amount);
System.out.println("Current balance: Rs. " + balance);
}
// Withdraw method
public void withdraw(double amount) {
if (balance >= amount) {
balance -= amount;
System.out.println("Amount withdrawn: Rs. " + amount);
System.out.println("Current balance: Rs. " + balance);
} else {
System.out.println("Withdrawal not possible, insufficient funds");
}
}
}
// Create a Bubble object and sort the array using Bubble Sort
Sortable bubbleSort = new Bubble();
bubbleSort.sort(array);
System.out.println("Array sorted using Bubble Sort:");
printArray(array);
// Create a Selection object and sort the array using Selection Sort
Sortable selectionSort = new Selection();
selectionSort.sort(array);
System.out.println("Array sorted using Selection Sort:");
printArray(array);
}
// Calculate percentage
double percentage = 0;
try {
percentage = (double) marksObtained / maxMarks * 100;
} catch (ArithmeticException e) {
System.out.println("Error: Division by zero is not allowed.");
return;
}
// Display the calculated percentage
System.out.println("Percentage: " + percentage + "%");
} catch (NumberFormatException e) {
System.out.println("Error: Invalid input. Please provide valid integer for max-marks.");
} catch (Exception e) {
System.out.println("An unexpected error occurred: " + e.getMessage());
}
}
}
Output:
Question10:
import java.util.Scanner;
// EmployeeDetails class
public class EmployeeDetails {
// Method to accept and validate employee details
public void acceptDetails() {
try {
Scanner scanner = new Scanner(System.in);
// Accept employee id
System.out.print("Enter employee id: ");
int empId = scanner.nextInt();
scanner.nextLine(); // Consume newline
// Accept department id
System.out.print("Enter department id (1-5): ");
int deptId = scanner.nextInt();
if (deptId < 1 || deptId > 5) {
throw new InvalidEmployeeDetailsException("Department id should be an integer
between 1 and 5.");
}