Java Manual 2
Java Manual 2
Java Manual 2
Q1) Create Account with 1000 Rs Minimum Balance, Deposit Amount, Withdraw Amount and Also
Throws LessBalanceException. It has a Class Called LessBalanceException Which returns the
Statement that Says WithDraw Amount(_Rs) is Not
Valid. It has a Class Which Creates 2 Accounts, Both Account Deposite Money and One Account Tries
to WithDraw more Money Which Generates a LessBalanceException Take Appropriate Action for the
Same.
*/
// Custom exception class for insufficient balance
class LessBalanceException extends Exception {
public LessBalanceException(String message) {
super(message);
}
}
try {
// Withdraw Rs. 800 from Bob's account (invalid as it will breach the minimum balance)
account2.withdraw(800);
} catch (LessBalanceException e) {
System.out.println(e.getMessage());
}
class Employee {
public Employee() {
this.name = name;
this.id = id;
this.salary = salary;
Employee emp2 = new Employee("Alice", 101); // Calls constructor with name and ID
Employee emp3 = new Employee("Bob", 102, 50000.0); // Calls constructor with all parameters
System.out.println("\nEmployee Information:");
emp1.displayInfo();
emp2.displayInfo();
emp3.displayInfo();
Output:-
Q3)You have been given the list of the names of the files in a directory. You have to select Java files
from them. A file is a Java file if it’s name ends with “.java”. For e.g. File- “Names.java” is a Java file,
“FileNames.java.pdf” is not.
*/
import java.util.ArrayList;
import java.util.List;
if (file.endsWith(".java")) {
javaFiles.add(file);
return javaFiles;
System.out.println("Java Files:");
System.out.println(javaFile);
Output:-
Q4Write java program where user will enter loginid and password as input. The password should be 8
digit containing one digit and one special symbol. If user enter valid password satisfying above
criteria then show “Login Successful Message”. If user enter invalid Password then create
InvalidPasswordException stating Please enter valid password of length 8 containing one digit and
*/
import java.util.Scanner;
import java.util.regex.Pattern;
super(message);
if (password.length() != 8) {
if (!Pattern.compile("[0-9]").matcher(password).find()) {
if (!Pattern.compile("[!@#$%^&*(),.?\":{}|<>]").matcher(password).find()) {
try {
validatePassword(password);
System.out.println("Login Successful.");
} catch (InvalidPasswordException e) {
System.out.println("Please enter a valid password of length 8 containing at least one digit and
Output:-
Q5)Write a menu driven Java program which will read a number and should implement the following
methods
1. factorial()
2. testArmstrong()
3. testPalindrome()
4. testPrime()
5. fibonacciSeries()
*/
import java.util.Scanner;
long fact = 1;
fact *= i;
return fact;
int sum = 0;
while (num != 0) {
int digit = num % 10;
num /= 10;
int reversedNum = 0;
while (num != 0) {
num /= 10;
return true;
}
// Method to print the Fibonacci series up to a given number
int a = 0, b = 1;
int next = a + b;
a = b;
b = next;
System.out.println();
int choice;
do {
System.out.println("1. Factorial");
System.out.println("6. Exit");
choice = scanner.nextInt();
switch (choice) {
case 1:
break;
case 2:
if (testArmstrong(num2)) {
} else {
break;
case 3:
if (testPalindrome(num3)) {
} else {
break;
case 4:
if (testPrime(num4)) {
} else {
break;
case 5:
fibonacciSeries(num5);
break;
case 6:
break;
default:
scanner.close();
Output:-
Q6)Write menu driven program to implement recursive Functions for following tasks.
*/
import java.util.Scanner;
if (b == 0) {
return a;
if (n > 0) {
printFibonacci(n - 1, b, a + b);
}
if (num == 0) {
return reversed;
if (n <= 0) {
return 0;
int choice;
do {
System.out.println("\nMenu:");
System.out.println("1. Find GCD and LCM");
System.out.println("5. Exit");
choice = scanner.nextInt();
switch (choice) {
case 1:
break;
case 2:
int n = scanner.nextInt();
printFibonacci(n, 0, 1);
System.out.println();
break;
case 3:
break;
case 4:
break;
case 5:
System.out.println("Exiting...");
break;
default:
scanner.close();
Output:-
Q7)Print Reverse Array list in java by writing our own function.
*/
import java.util.ArrayList;
import java.util.Scanner;
reversedList.add(list.get(i));
return reversedList;
System.out.print("Enter the number of elements you want to add to the ArrayList: ");
int n = scanner.nextInt();
scanner.close();
Output:-
Q8)
/*
Implement a java program to calculate gross salary & net salary taking the following data.
DA=70% of basic
HRA=30% of basic
CCA=Rs240/-
PF=10% of basic
PT= Rs100/-
*/
import java.util.Scanner;
Output:-
Q9)Write a Java Program to create a Student Profile form using AWT controls.
*/
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public StudentProfileForm() {
// Create and add labels and text fields for name and age
add(new Label("Name:"));
add(nameField);
add(new Label("Age:"));
add(new Label("Gender:"));
genderChoice.add("Select Gender");
genderChoice.add("Male");
genderChoice.add("Female");
genderChoice.add("Other");
add(genderChoice);
add(new Label("Course:"));
courseChoice.add("Select Course");
courseChoice.add("Computer Science");
courseChoice.add("Mathematics");
courseChoice.add("Physics");
courseChoice.add("Chemistry");
add(courseChoice);
submitButton.addActionListener(this);
add(submitButton);
setSize(400, 300);
setVisible(true);
// Add a window listener to handle closing the window
addWindowListener(new java.awt.event.WindowAdapter() {
System.exit(0);
});
@Override
dialog.setLayout(new FlowLayout());
dialog.add(new Label(message));
Button okButton = new Button("OK");
dialog.add(okButton);
dialog.setSize(300, 200);
dialog.setVisible(true);
new StudentProfileForm();
Output:-