1.
HotelBooking Class
import java.util.Scanner;
class HotelBooking {
String roomType;
int days;
double bill;
HotelBooking() {
roomType = "";
days = 0;
bill = 0.0;
}
void accept() {
Scanner sc = new Scanner(System.in);
System.out.print("Enter room type
(Deluxe/Standard/Economy): ");
roomType = sc.next();
System.out.print("Enter number of days: ");
days = sc.nextInt();
}
void calculate() {
switch (roomType) {
case "Deluxe":
bill = days * 2000;
break;
case "Standard":
bill = days * 1500;
break;
case "Economy":
bill = days * 1000;
break;
default:
System.out.println("Invalid room type.");
bill = 0.0;
}
}
void display() {
System.out.println("Room Type: " + roomType);
System.out.println("Days Stayed: " + days);
System.out.println("Total Bill: ₹" + bill);
}
public static void main(String[] args) {
HotelBooking hb = new HotelBooking();
hb.accept();
hb.calculate();
hb.display();
}
}
2. WordProcessor Class
import java.util.Scanner;
class WordProcessor {
String sentence;
WordProcessor() {
sentence = "";
}
void accept() {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a sentence: ");
sentence = sc.nextLine();
}
void wordCount() {
String[] words = sentence.split(" ");
System.out.println("Number of words: " + words.length);
}
void reverseWords() {
String[] words = sentence.split(" ");
for (String word : words) {
StringBuilder sb = new StringBuilder(word);
System.out.print(sb.reverse().toString() + " ");
}
System.out.println();
}
public static void main(String[] args) {
WordProcessor wp = new WordProcessor();
wp.accept();
wp.wordCount();
wp.reverseWords();
}
}
3. MatrixOperations Class
import java.util.Scanner;
class MatrixOperations {
int[][] matrix;
MatrixOperations() {
matrix = new int[3][3];
}
void accept() {
Scanner sc = new Scanner(System.in);
System.out.println("Enter elements of the 3x3 matrix:");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
matrix[i][j] = sc.nextInt();
}
}
}
void calculateSum() {
int sum = 0;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
sum += matrix[i][j];
}
}
System.out.println("Sum of all elements: " + sum);
}
void displayDiagonal() {
System.out.println("Main diagonal elements:");
for (int i = 0; i < 3; i++) {
System.out.print(matrix[i][i] + " ");
}
System.out.println();
}
public static void main(String[] args) {
MatrixOperations mo = new MatrixOperations();
mo.accept();
mo.calculateSum();
mo.displayDiagonal();
}
}
4. Fibonacci Class
import java.util.Scanner;
class Fibonacci {
int fibonacci(int n) {
if (n <= 1) {
return n;
}
return fibonacci(n - 1) + fibonacci(n - 2);
}
void display(int N) {
for (int i = 0; i < N; i++) {
System.out.print(fibonacci(i) + " ");
}
System.out.println();
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of Fibonacci numbers to
display: ");
int N = sc.nextInt();
Fibonacci fib = new Fibonacci();
fib.display(N);
}
}
5. Library Class
import java.util.Scanner;
class Library {
String bookTitle;
String author;
double price;
Library() {
bookTitle = "";
author = "";
price = 0.0;
}
void accept() {
Scanner sc = new Scanner(System.in);
System.out.print("Enter book title: ");
bookTitle = sc.nextLine();
System.out.print("Enter author: ");
author = sc.nextLine();
System.out.print("Enter price: ");
price = sc.nextDouble();
}
void applyDiscount() {
if (price > 500) {
price *= 0.9; // 10% discount
}
}
void display() {
System.out.println("Book Title: " + bookTitle);
System.out.println("Author: " + author);
System.out.println("Final Price: ₹" + price);
}
public static void main(String[] args) {
Library lib = new Library();
lib.accept();
lib.applyDiscount();
lib.display();
}
}
6. FileHandler Class
import java.io.*;
import java.util.Scanner;
class FileHandler {
void readFile() throws IOException {
BufferedReader br = new BufferedReader(new
FileReader("input.txt"));
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
}
void writeFile() throws IOException {
BufferedWriter bw = new BufferedWriter(new
FileWriter("output.txt"));
Scanner sc = new Scanner(System.in);
System.out.println("Enter text to write to the file:");
String text = sc.nextLine();
bw.write(text);
bw.close();
}
void copyFile() throws IOException {
BufferedReader br = new BufferedReader(new
FileReader("input.txt"));
BufferedWriter bw = new BufferedWriter(new
FileWriter("copy.txt"));
String line;
while ((line = br.readLine()) != null) {
bw.write(line);
bw.newLine();
}
br.close();
bw.close();
}
public static void main(String[] args) throws IOException {
FileHandler fh = new FileHandler();
fh.readFile();
fh.writeFile();
fh.copyFile();
}
}
7. ComplexOperations Class
import java.util.Scanner;
class ComplexOperations {
double real;
double imaginary;
ComplexOperations() {
real = 0.0;
imaginary = 0.0;
}
void accept() {
Scanner sc = new Scanner(System.in);
System.out.print("Enter real part: ");
real = sc.nextDouble();
System.out.print("Enter imaginary part: ");
imaginary = sc.nextDouble();
}
ComplexOperations subtract(ComplexOperations c) {
ComplexOperations result = new ComplexOperations();
result.real = this.real - c.real;
result.imaginary = this.imaginary - c.imaginary;
return result;
}
void display() {
System.out.println(real + " + " + imaginary + "i");
}
public static void main(String[] args) {
ComplexOperations c1 = new ComplexOperations();
ComplexOperations c2 = new ComplexOperations();
System.out.println("Enter first complex number:");
c1.accept();
System.out.println("Enter second complex number:");
c2.accept();
ComplexOperations result = c1.subtract(c2);
System.out.println("Result of subtraction:");
result.display();
}
}
8. ArraySearch Class
import java.util.Scanner;
class ArraySearch {
int[] arr;
ArraySearch() {
arr = new int[10];
}
void accept() {
Scanner sc = new Scanner(System.in);
System.out.println("Enter 10 integers:");
for (int i = 0; i < 10; i++) {
arr[i] = sc.nextInt();
}
}
int linearSearch(int key) {
for (int i = 0; i < arr.length; i++) {
if (arr[i] == key) {
return i;
}
}
return -1;
}
void display() {
System.out.println("Array elements:");
for (int i : arr) {
System.out.print(i + " ");
}
System.out.println();
}
public static void main(String[] args) {
ArraySearch as = new ArraySearch();
as.accept();
as.display();
Scanner sc = new Scanner(System.in);
System.out.print("Enter the element to search: ");
int key = sc.nextInt();
int index = as.linearSearch(key);
if (index != -1) {
System.out.println("Element found at index: " + index);
} else {
System.out.println("Element not found.");
}
}
}
9. PatternPrinter Class
import java.util.Scanner;
class PatternPrinter {
String input;
PatternPrinter() {
input = "";
}
void accept() {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a string: ");
input = sc.nextLine();
}
void printPattern() {
for (int i = 0; i < input.length(); i++) {
for (int j = 0; j <= i; j++) {
System.out.print(input.charAt(j));
}
System.out.println();
}
}
public static void main(String[] args) {
PatternPrinter pp = new PatternPrinter();
pp.accept();
pp.printPattern();
}
}
10. BankAccount Class
import java.util.Scanner;
class BankAccount {
String accountNumber;
String accountHolderName;
double balance;
BankAccount() {
accountNumber = "";
accountHolderName = "";
balance = 0.0;
}
void accept() {
Scanner sc = new Scanner(System.in);
System.out.print("Enter account number: ");
accountNumber = sc.next();
System.out.print("Enter account holder name: ");
accountHolderName = sc.next();
System.out.print("Enter initial balance: ");
balance = sc.nextDouble();
}
void deposit(double amount) {
balance += amount;
System.out.println("Amount deposited: ₹" + amount);
}
void withdraw(double amount) {
if (amount <= balance) {
balance -= amount;
System.out.println("Amount withdrawn: ₹" + amount);
} else {
System.out.println("Insufficient balance");
}
}
void display() {
System.out.println("Account Number: " + accountNumber);
System.out.println("Account Holder Name: " +
accountHolderName);
System.out.println("Current Balance: ₹" + balance);
}
public static void main(String[] args) {
BankAccount ba = new BankAccount();
ba.accept();
Scanner sc = new Scanner(System.in);
System.out.print("Enter amount to deposit: ");
double depositAmount = sc.nextDouble();
ba.deposit(depositAmount);
System.out.print("Enter amount to withdraw: ");
double withdrawAmount = sc.nextDouble();
ba.withdraw(withdrawAmount);
ba.display();
}
}