Java Question Bank
Complete Solutions with User Input
MSE-1 | 108 Programs | Object-Oriented Java
Topics Covered
Q 1 - 13 Method Overloading – Arithmetic, Shapes, Banking, Shopping, etc.
Q 14 - 23 Classes and Objects – Banking, Students, Library, Hospital
Q 24 - 33 Constructors – Default and Parameterized
Q 34 - 40 Static Methods – Calculator, Temperature, Primes, Factorial
Q 41 - 47 String Operations – Reverse, Palindrome, Vowels, Replace
Q 48 - 55 Constructor Overloading – Employee, Rectangle, Complex Numbers
Q 60 - 73 Mathematical Programs – Interest, Distance, Shapes, Series
Q 74 - 99 Arrays and Matrices – Sorting, Searching, Statistics
Q 100 - 108 Miscellaneous – Primes, Perfect Numbers, Time Addition
Q1 Addition of Numbers – Method Overloading
import [Link];
public class Calculator {
static int add(int a, int b) {
return a + b;
}
static int add(int a, int b, int c) {
return a + b + c;
}
static double add(double a, double b) {
return a + b;
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter two integers: ");
int x = [Link]();
int y = [Link]();
[Link]("Sum of two integers: " + add(x, y));
[Link]("Enter three integers: ");
int a = [Link]();
int b = [Link]();
int c = [Link]();
[Link]("Sum of three integers: " + add(a, b, c));
[Link]("Enter two decimal numbers: ");
double p = [Link]();
double q = [Link]();
[Link]("Sum of two decimals: " + add(p, q));
[Link]();
}
}
Q2 Area of Different Shapes – Method Overloading
import [Link];
public class AreaCalculator {
static int area(int side) {
return side * side;
}
static int area(int length, int breadth) {
return length * breadth;
}
static double area(double radius) {
return 3.14159 * radius * radius;
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter side of square: ");
int side = [Link]();
[Link]("Area of Square: " + area(side));
[Link]("Enter length and breadth of rectangle: ");
int l = [Link]();
int b = [Link]();
[Link]("Area of Rectangle: " + area(l, b));
[Link]("Enter radius of circle: ");
double r = [Link]();
[Link]("Area of Circle: " + area(r));
[Link]();
}
}
Q3 Student Average Marks – Method Overloading
import [Link];
public class StudentMarks {
static double average(int m1, int m2) {
return (m1 + m2) / 2.0;
}
static double average(int m1, int m2, int m3) {
return (m1 + m2 + m3) / 3.0;
}
static double average(int m1, int m2, int m3, int m4) {
return (m1 + m2 + m3 + m4) / 4.0;
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter marks of 2 subjects: ");
int a = [Link](), b = [Link]();
[Link]("Average of 2 subjects: " + average(a, b));
[Link]("Enter marks of 3 subjects: ");
int x = [Link](), y = [Link](), z = [Link]();
[Link]("Average of 3 subjects: " + average(x, y, z));
[Link]("Enter marks of 4 subjects: ");
int p = [Link](), q = [Link](), r = [Link](), s = [Link]();
[Link]("Average of 4 subjects: " + average(p, q, r, s));
[Link]();
}
}
Q4 Volume Calculation – Method Overloading
import [Link];
public class VolumeCalculator {
static int volume(int side) {
return side * side * side;
}
static int volume(int length, int breadth, int height) {
return length * breadth * height;
}
static double volume(double radius, int height) {
return 3.14159 * radius * radius * height;
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter side of cube: ");
int s = [Link]();
[Link]("Volume of Cube: " + volume(s));
[Link]("Enter length, breadth, height of cuboid: ");
int l = [Link](), b = [Link](), h = [Link]();
[Link]("Volume of Cuboid: " + volume(l, b, h));
[Link]("Enter radius and height of cylinder: ");
double r = [Link]();
int ht = [Link]();
[Link]("Volume of Cylinder: " + volume(r, ht));
[Link]();
}
}
Q5 Display Student Information – Method Overloading
import [Link];
public class StudentInfo {
static void display(String name) {
[Link]("Student Name: " + name);
}
static void display(String name, int age) {
[Link]("Name: " + name + ", Age: " + age);
}
static void display(String name, int age, String department) {
[Link]("Name: " + name + ", Age: " + age + ", Department: " + department);
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter student name: ");
String name = [Link]();
display(name);
[Link]("Enter name and age: ");
String name2 = [Link]();
int age = [Link]();
[Link]();
display(name2, age);
[Link]("Enter name: ");
String name3 = [Link]();
[Link]("Enter age: ");
int age3 = [Link]();
[Link]();
[Link]("Enter department: ");
String dept = [Link]();
display(name3, age3, dept);
[Link]();
}
}
Q6 Banking System – Deposit Methods
import [Link];
public class BankAccount {
double balance = 0;
void deposit(double amount) {
balance += amount;
[Link]("Cash deposited: " + amount);
[Link]("Updated Balance: " + balance);
}
void deposit(double amount, String chequeNumber) {
balance += amount;
[Link]("Cheque No: " + chequeNumber + " | Amount: " + amount);
[Link]("Updated Balance: " + balance);
}
void deposit(double amount, String upiId, boolean isUPI) {
balance += amount;
[Link]("UPI ID: " + upiId + " | Amount: " + amount);
[Link]("Updated Balance: " + balance);
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
BankAccount acc = new BankAccount();
[Link]("Enter cash deposit amount: ");
double cash = [Link]();
[Link](cash);
[Link]("Enter cheque amount: ");
double chequeAmt = [Link]();
[Link]();
[Link]("Enter cheque number: ");
String chequeNo = [Link]();
[Link](chequeAmt, chequeNo);
[Link]("Enter UPI transfer amount: ");
double upiAmt = [Link]();
[Link]();
[Link]("Enter UPI ID: ");
String upiId = [Link]();
[Link](upiAmt, upiId, true);
[Link]();
}
}
Q7 ATM Withdrawal System
import [Link];
public class ATM {
double balance = 10000;
int correctPin = 1234;
void withdraw(double amount) {
if (balance >= amount) {
balance -= amount;
[Link]("Withdrawn: " + amount);
[Link]("Remaining Balance: " + balance);
} else {
[Link]("Insufficient balance.");
}
}
void withdraw(double amount, int pin) {
if (pin != correctPin) {
[Link]("Wrong PIN. Transaction failed.");
return;
}
withdraw(amount);
}
void withdraw(double amount, int pin, String transactionType) {
[Link]("Transaction Type: " + transactionType);
withdraw(amount, pin);
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
ATM atm = new ATM();
[Link]("Enter amount to withdraw (no PIN): ");
double amt1 = [Link]();
[Link](amt1);
[Link]("Enter amount and PIN: ");
double amt2 = [Link]();
int pin = [Link]();
[Link](amt2, pin);
[Link]("Enter amount, PIN and transaction type: ");
double amt3 = [Link]();
int pin2 = [Link]();
[Link]();
String type = [Link]();
[Link](amt3, pin2, type);
[Link]();
}
}
Q8 Online Shopping Discount System
import [Link];
public class DiscountCalculator {
static double calculateDiscount(double price) {
double discount = price * 0.05;
return price - discount;
}
static double calculateDiscount(double price, String customerType) {
double rate = [Link]("Prime") ? 0.10 : 0.05;
return price - (price * rate);
}
static double calculateDiscount(double price, String customerType, boolean festivalOffer) {
double rate = [Link]("Prime") ? 0.10 : 0.05;
if (festivalOffer) rate += 0.05;
return price - (price * rate);
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter product price: ");
double price = [Link]();
[Link]("Final Price (default): " + calculateDiscount(price));
[Link]();
[Link]("Enter customer type (Regular/Prime): ");
String type = [Link]();
[Link]("Final Price with customer type: " + calculateDiscount(price, type));
[Link]("Festival offer available? (true/false): ");
boolean festival = [Link]();
[Link]("Final Price with all offers: " + calculateDiscount(price, type, festival));
[Link]();
}
}
Q9 Courier Delivery Charge System
import [Link];
public class CourierService {
static double calculateCharge(double weight) {
return weight * 10;
}
static double calculateCharge(double weight, double distance) {
return (weight * 10) + (distance * 2);
}
static double calculateCharge(double weight, double distance, boolean express) {
double charge = (weight * 10) + (distance * 2);
if (express) charge += 50;
return charge;
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter package weight (kg): ");
double weight = [Link]();
[Link]("Charge by weight only: Rs." + calculateCharge(weight));
[Link]("Enter distance (km): ");
double dist = [Link]();
[Link]("Charge with distance: Rs." + calculateCharge(weight, dist));
[Link]("Express delivery? (true/false): ");
boolean express = [Link]();
[Link]("Total Delivery Charge: Rs." + calculateCharge(weight, dist, express));
[Link]();
}
}
Q10 Mobile Recharge System
import [Link];
public class MobileRecharge {
static void recharge(String mobile, double amount) {
[Link]("Recharge of Rs." + amount + " done for " + mobile);
}
static void recharge(String mobile, double amount, String couponCode) {
double discount = 0;
if ([Link]("SAVE10")) discount = 10;
double finalAmount = amount - discount;
[Link]("Coupon Applied. Final Recharge: Rs." + finalAmount + " for " + mobile);
}
static void recharge(String mobile, double amount, boolean upi) {
String method = upi ? "UPI" : "Card";
[Link]("Payment via " + method + ". Recharge of Rs." + amount + " done for " +
mobile);
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]();
[Link]("Enter mobile number: ");
String mobile = [Link]();
[Link]("Enter recharge amount: ");
double amount = [Link]();
recharge(mobile, amount);
[Link]();
[Link]("Enter coupon code: ");
String coupon = [Link]();
recharge(mobile, amount, coupon);
[Link]("Using UPI? (true/false): ");
boolean upi = [Link]();
recharge(mobile, amount, upi);
[Link]();
}
}
Q11 Electricity Bill Calculator
import [Link];
public class ElectricityBillCalc {
static double calculateBill(int units) {
return units * 5.0;
}
static double calculateBill(int units, double tariffRate) {
return units * tariffRate;
}
static double calculateBill(int units, double tariffRate, double lateFee) {
return (units * tariffRate) + lateFee;
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter units consumed: ");
int units = [Link]();
[Link]("Bill (default rate): Rs." + calculateBill(units));
[Link]("Enter tariff rate per unit: ");
double rate = [Link]();
[Link]("Bill with custom rate: Rs." + calculateBill(units, rate));
[Link]("Enter late fee: ");
double late = [Link]();
[Link]("Total Bill with late fee: Rs." + calculateBill(units, rate, late));
[Link]();
}
}
Q12 Loan Interest Calculator
import [Link];
public class LoanInterest {
static double calculateInterest(double principal) {
return principal * 0.05 * 1;
}
static double calculateInterest(double principal, double rate) {
return principal * (rate / 100) * 1;
}
static double calculateInterest(double principal, double rate, int time) {
return principal * (rate / 100) * time;
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter principal amount: ");
double p = [Link]();
[Link]("Interest (default 5%, 1 yr): Rs." + calculateInterest(p));
[Link]("Enter interest rate (%): ");
double r = [Link]();
[Link]("Interest with custom rate: Rs." + calculateInterest(p, r));
[Link]("Enter time period (years): ");
int t = [Link]();
[Link]("Total Interest Payable: Rs." + calculateInterest(p, r, t));
[Link]();
}
}
Q13 Online Grocery Store
import [Link];
public class GroceryStore {
static double calculateTotal(double itemPrice) {
return itemPrice;
}
static double calculateTotal(double itemPrice, int quantity) {
return itemPrice * quantity;
}
static double calculateTotal(double itemPrice, int quantity, double deliveryCharge) {
return (itemPrice * quantity) + deliveryCharge;
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter item price: ");
double price = [Link]();
[Link]("Total (1 item): Rs." + calculateTotal(price));
[Link]("Enter quantity: ");
int qty = [Link]();
[Link]("Total for quantity: Rs." + calculateTotal(price, qty));
[Link]("Enter delivery charge: ");
double delivery = [Link]();
[Link]("Final Amount: Rs." + calculateTotal(price, qty, delivery));
[Link]();
}
}
Q14 Bank Account System – Classes and Objects
import [Link];
public class BankAccountSystem {
String accountNumber;
String accountHolderName;
double balance;
BankAccountSystem(String accNo, String name, double bal) {
accountNumber = accNo;
accountHolderName = name;
balance = bal;
}
void deposit(double amount) {
balance += amount;
[Link]("Deposited Rs." + amount + ". New Balance: Rs." + balance);
}
void withdraw(double amount) {
if (amount <= balance) {
balance -= amount;
[Link]("Withdrawn Rs." + amount + ". Remaining: Rs." + balance);
} else {
[Link]("Insufficient balance.");
}
}
void displayBalance() {
[Link]("Account: " + accountNumber + " | Holder: " + accountHolderName + " | Balance:
Rs." + balance);
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
BankAccountSystem acc1 = new BankAccountSystem("ACC001", "Alice", 5000);
BankAccountSystem acc2 = new BankAccountSystem("ACC002", "Bob", 3000);
[Link]();
[Link]("Enter deposit amount for Alice: ");
[Link]([Link]());
[Link]();
[Link]("Enter withdrawal amount for Bob: ");
[Link]([Link]());
[Link]();
}
}
Q15 Student Result System
import [Link];
public class StudentResult {
int rollNumber;
String name;
int m1, m2, m3;
StudentResult(int roll, String name, int m1, int m2, int m3) {
[Link] = roll;
[Link] = name;
this.m1 = m1;
this.m2 = m2;
this.m3 = m3;
}
int calculateTotal() {
return m1 + m2 + m3;
}
double calculateAverage() {
return calculateTotal() / 3.0;
}
void displayResult() {
[Link]("Roll: " + rollNumber + " | Name: " + name);
[Link]("Total: " + calculateTotal() + " | Average: " + calculateAverage());
[Link]("---------------------------");
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
StudentResult[] students = new StudentResult[3];
for (int i = 0; i < 3; i++) {
[Link]("Enter roll number, name, and 3 marks for student " + (i + 1) + ": ");
int roll = [Link]();
String name = [Link]();
int a = [Link](), b = [Link](), c = [Link]();
students[i] = new StudentResult(roll, name, a, b, c);
}
[Link]("\nStudent Results:");
for (StudentResult s : students) [Link]();
[Link]();
}
}
Q16 Library Book Management
import [Link];
public class LibraryBook {
int bookID;
String title, author;
boolean availabilityStatus;
LibraryBook(int id, String title, String author) {
[Link] = id;
[Link] = title;
[Link] = author;
[Link] = true;
}
void issueBook() {
if (availabilityStatus) {
availabilityStatus = false;
[Link](title + " has been issued.");
} else {
[Link](title + " is not available.");
}
}
void returnBook() {
availabilityStatus = true;
[Link](title + " has been returned.");
}
void displayBookDetails() {
[Link]("ID: " + bookID + " | Title: " + title + " | Author: " + author + " |
Available: " + availabilityStatus);
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter book ID, title, author: ");
int id = [Link]();
String title = [Link]();
String author = [Link]();
LibraryBook book = new LibraryBook(id, title, author);
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
}
}
Q17 Mobile Phone Details
import [Link];
public class MobilePhone {
String brand, model;
double price;
int batteryLevel;
MobilePhone(String brand, String model, double price, int battery) {
[Link] = brand;
[Link] = model;
[Link] = price;
[Link] = battery;
}
void makeCall() {
if (batteryLevel > 10)
[Link]("Calling from " + brand + " " + model);
else
[Link]("Low battery. Cannot make call.");
}
void chargeBattery() {
batteryLevel = 100;
[Link](brand + " " + model + " fully charged.");
}
void displayDetails() {
[Link]("Brand: " + brand + " | Model: " + model + " | Price: Rs." + price + " |
Battery: " + batteryLevel + "%");
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter brand, model, price, battery level: ");
String brand = [Link](), model = [Link]();
double price = [Link]();
int battery = [Link]();
MobilePhone phone = new MobilePhone(brand, model, price, battery);
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
}
}
Q18 Car Rental System
import [Link];
public class CarRental {
String carNumber, model;
double rentalPricePerDay;
boolean availability;
CarRental(String carNo, String model, double price) {
[Link] = carNo;
[Link] = model;
[Link] = price;
[Link] = true;
}
void rentCar(int days) {
if (availability) {
availability = false;
[Link]("Car " + model + " rented for " + days + " days.");
[Link]("Total Cost: Rs." + (rentalPricePerDay * days));
} else {
[Link]("Car not available.");
}
}
void returnCar() {
availability = true;
[Link]("Car " + model + " returned.");
}
void displayCarDetails() {
[Link]("Car No: " + carNumber + " | Model: " + model + " | Price/Day: Rs." +
rentalPricePerDay + " | Available: " + availability);
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter car number, model, price per day: ");
String carNo = [Link](), model = [Link]();
double price = [Link]();
CarRental car = new CarRental(carNo, model, price);
[Link]();
[Link]("Enter rental days: ");
int days = [Link]();
[Link](days);
[Link]();
[Link]();
[Link]();
}
}
Q19 Online Shopping Product
import [Link];
public class ShoppingProduct {
int productID;
String productName;
double price;
int stockQuantity;
ShoppingProduct(int id, String name, double price, int stock) {
[Link] = id;
[Link] = name;
[Link] = price;
[Link] = stock;
}
void updateStock(int qty) {
stockQuantity += qty;
[Link]("Stock updated. New Stock: " + stockQuantity);
}
void purchaseProduct(int quantity) {
if (quantity <= stockQuantity) {
stockQuantity -= quantity;
[Link]("Purchased " + quantity + " units. Total: Rs." + (price * quantity));
} else {
[Link]("Not enough stock.");
}
}
void displayProductDetails() {
[Link]("ID: " + productID + " | Name: " + productName + " | Price: Rs." + price + " |
Stock: " + stockQuantity);
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter product ID, name, price, stock: ");
int id = [Link]();
String name = [Link]();
double price = [Link]();
int stock = [Link]();
ShoppingProduct p = new ShoppingProduct(id, name, price, stock);
[Link]();
[Link]("Enter purchase quantity: ");
[Link]([Link]());
[Link]("Enter restock quantity: ");
[Link]([Link]());
[Link]();
[Link]();
}
}
Q20 Employee Salary System
import [Link];
public class EmployeeSalary {
int employeeID;
String name;
double basicSalary;
EmployeeSalary(int id, String name, double salary) {
[Link] = id;
[Link] = name;
[Link] = salary;
}
double calculateSalary() {
double hra = basicSalary * 0.20;
double da = basicSalary * 0.10;
return basicSalary + hra + da;
}
void displayEmployeeDetails() {
[Link]("ID: " + employeeID + " | Name: " + name);
[Link]("Basic: Rs." + basicSalary + " | Gross Salary: Rs." + calculateSalary());
[Link]("---------------------------");
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int n = 2;
EmployeeSalary[] employees = new EmployeeSalary[n];
for (int i = 0; i < n; i++) {
[Link]("Enter ID, name, basic salary for employee " + (i + 1) + ": ");
int id = [Link]();
String name = [Link]();
double sal = [Link]();
employees[i] = new EmployeeSalary(id, name, sal);
}
for (EmployeeSalary e : employees) [Link]();
[Link]();
}
}
Q21 Movie Ticket Booking
import [Link];
public class MovieTicket {
String movieName;
int seatNumber;
double ticketPrice;
boolean booked;
MovieTicket(String movie, int seat, double price) {
[Link] = movie;
[Link] = seat;
[Link] = price;
[Link] = false;
}
void bookTicket() {
if (!booked) {
booked = true;
[Link]("Ticket booked for " + movieName + " | Seat: " + seatNumber);
} else {
[Link]("Seat already booked.");
}
}
void cancelTicket() {
booked = false;
[Link]("Ticket for seat " + seatNumber + " cancelled.");
}
void displayTicketDetails() {
[Link]("Movie: " + movieName + " | Seat: " + seatNumber + " | Price: Rs." +
ticketPrice + " | Status: " + (booked ? "Booked" : "Available"));
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter movie name, seat number, ticket price: ");
String movie = [Link]();
int seat = [Link]();
double price = [Link]();
MovieTicket ticket = new MovieTicket(movie, seat, price);
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
}
}
Q22 Hospital Patient Record
import [Link];
public class HospitalPatient {
int patientID;
String name, disease, doctorAssigned;
HospitalPatient(int id, String name, String disease) {
[Link] = id;
[Link] = name;
[Link] = disease;
[Link] = "Not Assigned";
}
void assignDoctor(String doctor) {
[Link] = doctor;
[Link]("Dr." + doctor + " assigned to patient " + name);
}
void displayPatientDetails() {
[Link]("ID: " + patientID + " | Name: " + name + " | Disease: " + disease + " |
Doctor: " + doctorAssigned);
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter patient ID, name, disease: ");
int id = [Link]();
String name = [Link](), disease = [Link]();
HospitalPatient p = new HospitalPatient(id, name, disease);
[Link]();
[Link]("Enter doctor name to assign: ");
String doc = [Link]();
[Link](doc);
[Link]();
[Link]();
}
}
Q23 Electricity Bill System
import [Link];
public class ElectricBill {
int consumerNumber;
String consumerName;
int unitsConsumed;
ElectricBill(int num, String name, int units) {
[Link] = num;
[Link] = name;
[Link] = units;
}
double calculateBill() {
if (unitsConsumed <= 100) return unitsConsumed * 3.0;
else if (unitsConsumed <= 300) return (100 * 3.0) + (unitsConsumed - 100) * 5.0;
else return (100 * 3.0) + (200 * 5.0) + (unitsConsumed - 300) * 7.0;
}
void displayBill() {
[Link]("Consumer: " + consumerName + " | Units: " + unitsConsumed + " | Bill: Rs." +
calculateBill());
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter consumer number, name, units consumed: ");
int num = [Link]();
String name = [Link]();
int units = [Link]();
ElectricBill bill = new ElectricBill(num, name, units);
[Link]();
[Link]();
}
}
Q24 Bank Account – Parameterized Constructor
import [Link];
public class BankAccountConstructor {
String accountNumber, accountHolderName;
double balance;
BankAccountConstructor(String accNo, String name, double bal) {
[Link] = accNo;
[Link] = name;
[Link] = bal;
}
void displayDetails() {
[Link]("Account No: " + accountNumber + " | Name: " + accountHolderName + " |
Balance: Rs." + balance);
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
BankAccountConstructor[] accounts = new BankAccountConstructor[3];
for (int i = 0; i < 3; i++) {
[Link]("Enter account number, name, balance for customer " + (i + 1) + ": ");
String accNo = [Link](), name = [Link]();
double bal = [Link]();
accounts[i] = new BankAccountConstructor(accNo, name, bal);
}
[Link]("\nAccount Details:");
for (BankAccountConstructor a : accounts) [Link]();
[Link]();
}
}
Q25 Student Record – Default Constructor
import [Link];
public class StudentDefault {
int rollNumber;
String name, department;
StudentDefault() {
rollNumber = 0;
name = "Unknown";
department = "Not Assigned";
}
void setDetails(int roll, String name, String dept) {
[Link] = roll;
[Link] = name;
[Link] = dept;
}
void displayDetails() {
[Link]("Roll: " + rollNumber + " | Name: " + name + " | Department: " + department);
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
StudentDefault s1 = new StudentDefault();
[Link]("Default Values:");
[Link]();
[Link]("Enter roll number, name, department: ");
int roll = [Link]();
String name = [Link](), dept = [Link]();
[Link](roll, name, dept);
[Link]("Updated Values:");
[Link]();
[Link]();
}
}
Q26 Mobile Phone Store – Constructor
import [Link];
public class MobileStore {
String brand, model;
double price;
MobileStore(String brand, String model, double price) {
[Link] = brand;
[Link] = model;
[Link] = price;
}
void displayDetails() {
[Link]("Brand: " + brand + " | Model: " + model + " | Price: Rs." + price);
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int n = 2;
[Link]("Enter details for " + n + " phones:");
MobileStore[] phones = new MobileStore[n];
for (int i = 0; i < n; i++) {
[Link]("Brand, Model, Price: ");
String brand = [Link](), model = [Link]();
double price = [Link]();
phones[i] = new MobileStore(brand, model, price);
}
for (MobileStore p : phones) [Link]();
[Link]();
}
}
Q27 Book Library System – Constructor
import [Link];
public class BookLibrary {
int bookID;
String title, author;
BookLibrary(int id, String title, String author) {
[Link] = id;
[Link] = title;
[Link] = author;
}
void displayDetails() {
[Link]("Book ID: " + bookID + " | Title: " + title + " | Author: " + author);
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter details for 5 books:");
BookLibrary[] books = new BookLibrary[5];
for (int i = 0; i < 5; i++) {
[Link]("ID, Title, Author: ");
int id = [Link]();
String title = [Link](), author = [Link]();
books[i] = new BookLibrary(id, title, author);
}
[Link]("\nBook Details:");
for (BookLibrary b : books) [Link]();
[Link]();
}
}
Q28 Employee Salary – Constructor
import [Link];
public class EmployeeInit {
int employeeID;
String name;
double salary;
EmployeeInit(int id, String name, double salary) {
[Link] = id;
[Link] = name;
[Link] = salary;
}
void displayInfo() {
[Link]("Employee ID: " + employeeID + " | Name: " + name + " | Salary: Rs." +
salary);
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter employee ID, name, salary: ");
int id = [Link]();
String name = [Link]();
double sal = [Link]();
EmployeeInit emp = new EmployeeInit(id, name, sal);
[Link]();
[Link]();
}
}
Q29 Rectangle Area Calculator – Constructor
import [Link];
public class RectangleArea {
double length, width;
RectangleArea(double length, double width) {
[Link] = length;
[Link] = width;
}
double calculateArea() {
return length * width;
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter length and width of rectangle: ");
double l = [Link](), w = [Link]();
RectangleArea rect = new RectangleArea(l, w);
[Link]("Area of Rectangle: " + [Link]());
[Link]();
}
}
Q30 Car Showroom System – Constructor
import [Link];
public class CarShowroom {
String carBrand, model;
double price;
CarShowroom(String brand, String model, double price) {
[Link] = brand;
[Link] = model;
[Link] = price;
}
void displayDetails() {
[Link]("Brand: " + carBrand + " | Model: " + model + " | Price: Rs." + price);
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter car brand, model, price: ");
String brand = [Link](), model = [Link]();
double price = [Link]();
CarShowroom car = new CarShowroom(brand, model, price);
[Link]();
[Link]();
}
}
Q31 Electricity Bill – Constructor and Method
import [Link];
public class ElectricityBillConstructor {
String consumerName;
int unitsConsumed;
ElectricityBillConstructor(String name, int units) {
[Link] = name;
[Link] = units;
}
double calculateBill() {
return unitsConsumed * 5.0;
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter consumer name and units consumed: ");
String name = [Link]();
int units = [Link]();
ElectricityBillConstructor bill = new ElectricityBillConstructor(name, units);
[Link]("Consumer: " + [Link] + " | Bill: Rs." + [Link]());
[Link]();
}
}
Q32 Movie Ticket Booking – Constructor
import [Link];
public class MovieTicketConstructor {
String movieName;
int seatNumber;
double ticketPrice;
MovieTicketConstructor(String movie, int seat, double price) {
[Link] = movie;
[Link] = seat;
[Link] = price;
}
void displayInfo() {
[Link]("Movie: " + movieName + " | Seat: " + seatNumber + " | Price: Rs." +
ticketPrice);
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter movie name, seat number, ticket price: ");
String movie = [Link]();
int seat = [Link]();
double price = [Link]();
MovieTicketConstructor ticket = new MovieTicketConstructor(movie, seat, price);
[Link]();
[Link]();
}
}
Q33 Product Inventory System – Constructor
import [Link];
public class ProductInventory {
int productID;
String productName;
double price;
ProductInventory(int id, String name, double price) {
[Link] = id;
[Link] = name;
[Link] = price;
}
void displayDetails() {
[Link]("ID: " + productID + " | Name: " + productName + " | Price: Rs." + price);
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int n = 3;
ProductInventory[] products = new ProductInventory[n];
for (int i = 0; i < n; i++) {
[Link]("Enter product ID, name, price: ");
int id = [Link]();
String name = [Link]();
double price = [Link]();
products[i] = new ProductInventory(id, name, price);
}
for (ProductInventory p : products) [Link]();
[Link]();
}
}
Q34 Calculator Using Static Methods
import [Link];
public class StaticCalculator {
static double add(double a, double b) { return a + b; }
static double subtract(double a, double b) { return a - b; }
static double multiply(double a, double b) { return a * b; }
static double divide(double a, double b) {
if (b == 0) { [Link]("Division by zero!"); return 0; }
return a / b;
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter two numbers: ");
double a = [Link](), b = [Link]();
[Link]("Addition: " + add(a, b));
[Link]("Subtraction: " + subtract(a, b));
[Link]("Multiplication: " + multiply(a, b));
[Link]("Division: " + divide(a, b));
[Link]();
}
}
Q35 Celsius to Fahrenheit – Static Method
import [Link];
public class TempConverter {
static double celsiusToFahrenheit(double c) {
return (c * 9.0 / 5.0) + 32;
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter temperature in Celsius: ");
double celsius = [Link]();
double fahrenheit = celsiusToFahrenheit(celsius);
[Link](celsius + " Celsius = " + fahrenheit + " Fahrenheit");
[Link]();
}
}
Q36 Area of Circle – Static Method
import [Link];
public class CircleArea {
static double calculateArea(double radius) {
return 3.14159 * radius * radius;
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter radius of circle: ");
double radius = [Link]();
[Link]("Area of Circle: " + calculateArea(radius));
[Link]();
}
}
Q37 Student Registration Counter – Static Variable
import [Link];
public class StudentCounter {
static int count = 0;
String name;
StudentCounter(String name) {
[Link] = name;
count++;
[Link]("Student registered: " + name + " | Total: " + count);
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("How many students to register? ");
int n = [Link]();
for (int i = 0; i < n; i++) {
[Link]("Enter student name: ");
String name = [Link]();
new StudentCounter(name);
}
[Link]("Total Students Registered: " + [Link]);
[Link]();
}
}
Q38 Factorial – Static Method
import [Link];
public class Factorial {
static long findFactorial(int n) {
if (n == 0 || n == 1) return 1;
long result = 1;
for (int i = 2; i <= n; i++) result *= i;
return result;
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int n = [Link]();
[Link](n + "! = " + findFactorial(n));
[Link]();
}
}
Q39 Prime Numbers up to 100 – Static Method
public class PrimeNumbers {
static boolean isPrime(int n) {
if (n < 2) return false;
for (int i = 2; i <= [Link](n); i++) {
if (n % i == 0) return false;
}
return true;
}
static void displayPrimes() {
[Link]("Prime numbers up to 100:");
for (int i = 2; i <= 100; i++) {
if (isPrime(i)) [Link](i + " ");
}
[Link]();
}
public static void main(String[] args) {
displayPrimes();
}
}
Q40 Reverse a Number – Static Method
import [Link];
public class ReverseNumber {
static int reverse(int num) {
int reversed = 0;
while (num != 0) {
reversed = reversed * 10 + num % 10;
num /= 10;
}
return reversed;
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int num = [Link]();
[Link]("Reversed Number: " + reverse(num));
[Link]();
}
}
Q41 Reverse a String
import [Link];
public class ReverseString {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a string: ");
String input = [Link]();
String reversed = new StringBuilder(input).reverse().toString();
[Link]("Reversed String: " + reversed);
[Link]();
}
}
Q42 Palindrome Check – String
import [Link];
public class PalindromeString {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a string: ");
String input = [Link]().toLowerCase();
String reversed = new StringBuilder(input).reverse().toString();
if ([Link](reversed))
[Link](input + " is a Palindrome.");
else
[Link](input + " is NOT a Palindrome.");
[Link]();
}
}
Q43 Count Vowels in a String
import [Link];
public class CountVowels {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a string: ");
String input = [Link]().toLowerCase();
int count = 0;
for (char c : [Link]()) {
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')
count++;
}
[Link]("Number of vowels: " + count);
[Link]();
}
}
Q44 Convert String to Uppercase
import [Link];
public class ToUpperCase {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a string: ");
String input = [Link]();
[Link]("Uppercase: " + [Link]());
[Link]();
}
}
Q45 Convert String to Lowercase
import [Link];
public class ToLowerCase {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a string: ");
String input = [Link]();
[Link]("Lowercase: " + [Link]());
[Link]();
}
}
Q46 Remove All Spaces from a String
import [Link];
public class RemoveSpaces {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a string: ");
String input = [Link]();
String result = [Link](" ", "");
[Link]("String without spaces: " + result);
[Link]();
}
}
Q47 Replace Character in a String
import [Link];
public class ReplaceChar {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a string: ");
String input = [Link]();
[Link]("Enter character to replace: ");
char oldChar = [Link]().charAt(0);
[Link]("Enter new character: ");
char newChar = [Link]().charAt(0);
String result = [Link](oldChar, newChar);
[Link]("Result: " + result);
[Link]();
}
}
Q48 Employee – Parameterized Constructor
import [Link];
public class EmployeeConstructor {
int empId;
String empName;
double salary;
EmployeeConstructor(int id, String name, double salary) {
[Link] = id;
[Link] = name;
[Link] = salary;
}
void display() {
[Link]("ID: " + empId + " | Name: " + empName + " | Salary: Rs." + salary);
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter employee ID, name, salary: ");
int id = [Link]();
String name = [Link]();
double salary = [Link]();
EmployeeConstructor emp = new EmployeeConstructor(id, name, salary);
[Link]();
[Link]();
}
}
Q49 Rectangle – Constructor Overloading
import [Link];
public class RectangleOverload {
double length, width;
RectangleOverload(double side) {
[Link] = side;
[Link] = side;
}
RectangleOverload(double length, double width) {
[Link] = length;
[Link] = width;
}
double calculateArea() {
return length * width;
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter side for square rectangle: ");
double side = [Link]();
RectangleOverload r1 = new RectangleOverload(side);
[Link]("Area (square): " + [Link]());
[Link]("Enter length and width: ");
double l = [Link](), w = [Link]();
RectangleOverload r2 = new RectangleOverload(l, w);
[Link]("Area (rectangle): " + [Link]());
[Link]();
}
}
Q50 Car – Constructor with Details
import [Link];
public class CarDetails {
String brand, model;
double price;
CarDetails(String brand, String model, double price) {
[Link] = brand;
[Link] = model;
[Link] = price;
}
void displayInfo() {
[Link]("Brand: " + brand + " | Model: " + model + " | Price: Rs." + price);
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter brand, model, price: ");
String brand = [Link](), model = [Link]();
double price = [Link]();
CarDetails car = new CarDetails(brand, model, price);
[Link]();
[Link]();
}
}
Q51 BankAccount – Constructor
import [Link];
public class BankAccountInit {
String accountNumber, accountHolderName;
double balance;
BankAccountInit(String accNo, String name, double balance) {
[Link] = accNo;
[Link] = name;
[Link] = balance;
}
void displayDetails() {
[Link]("Account: " + accountNumber + " | Name: " + accountHolderName + " | Balance:
Rs." + balance);
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter account number, holder name, balance: ");
String accNo = [Link](), name = [Link]();
double bal = [Link]();
BankAccountInit acc = new BankAccountInit(accNo, name, bal);
[Link]();
[Link]();
}
}
Q52 Factorial in Constructor
import [Link];
public class FactorialConstructor {
int number;
long factorial;
FactorialConstructor(int n) {
[Link] = n;
factorial = 1;
for (int i = 2; i <= n; i++) factorial *= i;
[Link](n + "! = " + factorial);
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int n = [Link]();
new FactorialConstructor(n);
[Link]();
}
}
Q53 Complex Number Addition – Constructor
import [Link];
public class ComplexNumber {
double real, imaginary;
ComplexNumber(double real, double imaginary) {
[Link] = real;
[Link] = imaginary;
}
ComplexNumber add(ComplexNumber other) {
return new ComplexNumber([Link] + [Link], [Link] + [Link]);
}
void display() {
[Link](real + " + " + imaginary + "i");
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter real and imaginary of first complex number: ");
double r1 = [Link](), i1 = [Link]();
[Link]("Enter real and imaginary of second complex number: ");
double r2 = [Link](), i2 = [Link]();
ComplexNumber c1 = new ComplexNumber(r1, i1);
ComplexNumber c2 = new ComplexNumber(r2, i2);
ComplexNumber sum = [Link](c2);
[Link]("Sum: ");
[Link]();
[Link]();
}
}
Q54 Circle – Area and Circumference
import [Link];
public class CircleCalc {
double radius;
CircleCalc(double radius) {
[Link] = radius;
[Link]("Area: " + (3.14159 * radius * radius));
[Link]("Circumference: " + (2 * 3.14159 * radius));
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter radius: ");
double r = [Link]();
new CircleCalc(r);
[Link]();
}
}
Q55 Box – Volume Using Constructor
import [Link];
public class BoxVolume {
double length, width, height;
BoxVolume(double length, double width, double height) {
[Link] = length;
[Link] = width;
[Link] = height;
}
double computeVolume() {
return length * width * height;
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter length, width, height: ");
double l = [Link](), w = [Link](), h = [Link]();
BoxVolume box = new BoxVolume(l, w, h);
[Link]("Volume of Box: " + [Link]());
[Link]();
}
}
Q60 Simple and Compound Interest
import [Link];
public class InterestCalculator {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter principal, rate (%), time (years): ");
double p = [Link](), r = [Link](), t = [Link]();
double si = (p * r * t) / 100;
double ci = p * [Link](1 + r / 100, t) - p;
[Link]("Simple Interest: Rs." + si);
[Link]("Compound Interest: Rs." + [Link]("%.2f", ci));
[Link]();
}
}
Q61 Volume and Surface Area of Cylinder
import [Link];
public class CylinderCalculator {
double radius, height;
CylinderCalculator(double radius, double height) {
[Link] = radius;
[Link] = height;
}
double volume() {
return 3.14159 * radius * radius * height;
}
double surfaceArea() {
return 2 * 3.14159 * radius * (radius + height);
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter radius and height of cylinder: ");
double r = [Link](), h = [Link]();
CylinderCalculator cyl = new CylinderCalculator(r, h);
[Link]("Volume: " + [Link]("%.2f", [Link]()));
[Link]("Surface Area: " + [Link]("%.2f", [Link]()));
[Link]();
}
}
Q62 Euclidean Distance Between Two Points
import [Link];
public class EuclideanDistance {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter x1, y1: ");
double x1 = [Link](), y1 = [Link]();
[Link]("Enter x2, y2: ");
double x2 = [Link](), y2 = [Link]();
double dist = [Link]([Link](x2 - x1, 2) + [Link](y2 - y1, 2));
[Link]("Euclidean Distance: " + [Link]("%.2f", dist));
[Link]();
}
}
Q63 Volume and Surface Area of Box – Constructor
import [Link];
public class BoxCalculator {
double length, width, height;
BoxCalculator(double l, double w, double h) {
[Link] = l;
[Link] = w;
[Link] = h;
}
double volume() {
return length * width * height;
}
double surfaceArea() {
return 2 * (length * width + width * height + height * length);
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter length, width, height: ");
double l = [Link](), w = [Link](), h = [Link]();
BoxCalculator box = new BoxCalculator(l, w, h);
[Link]("Volume: " + [Link]());
[Link]("Surface Area: " + [Link]());
[Link]();
}
}
Q64 Triangle Type Check
import [Link];
public class TriangleCheck {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter three sides of triangle: ");
double a = [Link](), b = [Link](), c = [Link]();
if (a + b > c && b + c > a && a + c > b) {
[Link]("Valid Triangle.");
if (a == b && b == c)
[Link]("Type: Equilateral");
else if (a == b || b == c || a == c)
[Link]("Type: Isosceles");
else
[Link]("Type: Scalene");
} else {
[Link]("Invalid Triangle.");
}
[Link]();
}
}
Q65 Volume of Sphere – Constructor
import [Link];
public class SphereVolume {
double radius;
SphereVolume(double r) {
[Link] = r;
}
double volume() {
return (4.0 / 3.0) * 3.14159 * radius * radius * radius;
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter radius of sphere: ");
double r = [Link]();
SphereVolume sphere = new SphereVolume(r);
[Link]("Volume of Sphere: " + [Link]("%.2f", [Link]()));
[Link]();
}
}
Q66 Menu-Driven Calculator
import [Link];
public class MenuCalculator {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter two numbers: ");
double a = [Link](), b = [Link]();
[Link]("1. Addition");
[Link]("2. Subtraction");
[Link]("3. Multiplication");
[Link]("4. Division");
[Link]("Enter your choice: ");
int choice = [Link]();
switch (choice) {
case 1: [Link]("Result: " + (a + b)); break;
case 2: [Link]("Result: " + (a - b)); break;
case 3: [Link]("Result: " + (a * b)); break;
case 4:
if (b != 0) [Link]("Result: " + (a / b));
else [Link]("Division by zero is not allowed.");
break;
default: [Link]("Invalid choice.");
}
[Link]();
}
}
Q67 Area and Perimeter of Triangle – Constructor
import [Link];
public class TriangleCalc {
double a, b, c;
TriangleCalc(double a, double b, double c) {
this.a = a;
this.b = b;
this.c = c;
}
double perimeter() {
return a + b + c;
}
double area() {
double s = perimeter() / 2;
return [Link](s * (s - a) * (s - b) * (s - c));
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter three sides of triangle: ");
double a = [Link](), b = [Link](), c = [Link]();
TriangleCalc t = new TriangleCalc(a, b, c);
[Link]("Perimeter: " + [Link]());
[Link]("Area: " + [Link]("%.2f", [Link]()));
[Link]();
}
}
Q68 Fibonacci Series
import [Link];
public class FibonacciSeries {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter number of terms: ");
int n = [Link]();
int a = 0, b = 1;
[Link]("Fibonacci Series: " + a + " " + b);
for (int i = 2; i < n; i++) {
int next = a + b;
[Link](" " + next);
a = b;
b = next;
}
[Link]();
[Link]();
}
}
Q69 Prime Number Check
import [Link];
public class PrimeCheck {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int n = [Link]();
boolean prime = true;
if (n < 2) prime = false;
else {
for (int i = 2; i <= [Link](n); i++) {
if (n % i == 0) { prime = false; break; }
}
}
[Link](n + (prime ? " is a Prime number." : " is NOT a Prime number."));
[Link]();
}
}
Q70 Sum of Cosine Series
import [Link];
public class CosineSeries {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter angle in degrees: ");
double degrees = [Link]();
[Link]("Enter number of terms: ");
int n = [Link]();
double x = [Link](degrees);
double sum = 0;
double term = 1;
sum = term;
for (int i = 1; i < n; i++) {
term *= -1 * x * x / ((2 * i - 1) * (2 * i));
sum += term;
}
[Link]("cos(" + degrees + ") = " + [Link]("%.4f", sum));
[Link]("[Link] = " + [Link]("%.4f", [Link](x)));
[Link]();
}
}
Q71 Area and Perimeter of Rectangle – Constructor
import [Link];
public class RectanglePerimeter {
double length, width;
RectanglePerimeter(double l, double w) {
[Link] = l;
[Link] = w;
}
double area() { return length * width; }
double perimeter() { return 2 * (length + width); }
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter length and width: ");
double l = [Link](), w = [Link]();
RectanglePerimeter rect = new RectanglePerimeter(l, w);
[Link]("Area: " + [Link]());
[Link]("Perimeter: " + [Link]());
[Link]();
}
}
Q72 Rotate Array Clockwise by Two Positions
import [Link];
public class RotateArray {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter number of elements: ");
int n = [Link]();
int[] arr = new int[n];
[Link]("Enter elements: ");
for (int i = 0; i < n; i++) arr[i] = [Link]();
for (int r = 0; r < 2; r++) {
int last = arr[n - 1];
for (int i = n - 1; i > 0; i--) arr[i] = arr[i - 1];
arr[0] = last;
}
[Link]("Array after 2 clockwise rotations: ");
for (int x : arr) [Link](x + " ");
[Link]();
[Link]();
}
}
Q73 Volume of Cone – Constructor
import [Link];
public class ConeVolume {
double radius, height;
ConeVolume(double r, double h) {
[Link] = r;
[Link] = h;
}
double volume() {
return (1.0 / 3.0) * 3.14159 * radius * radius * height;
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter radius and height of cone: ");
double r = [Link](), h = [Link]();
ConeVolume cone = new ConeVolume(r, h);
[Link]("Volume of Cone: " + [Link]("%.2f", [Link]()));
[Link]();
}
}
Q74 Sort Array in Descending Order
import [Link];
import [Link];
import [Link];
public class DescendingSort {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter number of integers: ");
int n = [Link]();
Integer[] arr = new Integer[n];
[Link]("Enter elements: ");
for (int i = 0; i < n; i++) arr[i] = [Link]();
[Link](arr, [Link]());
[Link]("Sorted in Descending Order: ");
for (int x : arr) [Link](x + " ");
[Link]();
[Link]();
}
}
Q75 Employee Information – Array of Objects
import [Link];
public class EmployeeInfo {
String name, employeeCode;
double salary;
EmployeeInfo(String name, String code, double salary) {
[Link] = name;
[Link] = code;
[Link] = salary;
}
void display() {
[Link]("Name: " + name + " | Code: " + employeeCode + " | Salary: Rs." + salary);
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter number of employees: ");
int n = [Link]();
EmployeeInfo[] employees = new EmployeeInfo[n];
for (int i = 0; i < n; i++) {
[Link]("Enter name, code, salary for employee " + (i + 1) + ": ");
String name = [Link](), code = [Link]();
double sal = [Link]();
employees[i] = new EmployeeInfo(name, code, sal);
}
[Link]("\nEmployee Details:");
for (EmployeeInfo e : employees) [Link]();
[Link]();
}
}
Q76 Mean, Variance and Standard Deviation
import [Link];
public class Statistics {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter number of integers: ");
int n = [Link]();
double[] arr = new double[n];
[Link]("Enter elements: ");
for (int i = 0; i < n; i++) arr[i] = [Link]();
double sum = 0;
for (double x : arr) sum += x;
double mean = sum / n;
double varSum = 0;
for (double x : arr) varSum += [Link](x - mean, 2);
double variance = varSum / n;
double stdDev = [Link](variance);
[Link]("Mean: " + [Link]("%.2f", mean));
[Link]("Variance: " + [Link]("%.2f", variance));
[Link]("Standard Deviation: " + [Link]("%.2f", stdDev));
[Link]();
}
}
Q77 Book Information – Array of Objects
import [Link];
public class BookInfo {
String name;
int pages;
double price;
BookInfo(String name, int pages, double price) {
[Link] = name;
[Link] = pages;
[Link] = price;
}
void display() {
[Link]("Book: " + name + " | Pages: " + pages + " | Price: Rs." + price);
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter number of books: ");
int n = [Link]();
BookInfo[] books = new BookInfo[n];
for (int i = 0; i < n; i++) {
[Link]("Enter name, pages, price: ");
String name = [Link]();
int pages = [Link]();
double price = [Link]();
books[i] = new BookInfo(name, pages, price);
}
[Link]("\nBook Details:");
for (BookInfo b : books) [Link]();
[Link]();
}
}
Q78 Check Symmetric Matrix
import [Link];
public class SymmetricMatrix {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter matrix size n: ");
int n = [Link]();
int[][] mat = new int[n][n];
[Link]("Enter matrix elements:");
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
mat[i][j] = [Link]();
boolean symmetric = true;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if (mat[i][j] != mat[j][i]) { symmetric = false; break; }
[Link]("Matrix is " + (symmetric ? "" : "NOT ") + "Symmetric.");
[Link]();
}
}
Q79 Transpose of Square Matrix
import [Link];
public class TransposeMatrix {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter matrix size n: ");
int n = [Link]();
int[][] mat = new int[n][n];
[Link]("Enter matrix elements:");
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
mat[i][j] = [Link]();
for (int i = 0; i < n; i++)
for (int j = i + 1; j < n; j++) {
int temp = mat[i][j];
mat[i][j] = mat[j][i];
mat[j][i] = temp;
}
[Link]("Transposed Matrix:");
for (int[] row : mat) {
for (int x : row) [Link](x + " ");
[Link]();
}
[Link]();
}
}
Q80 Armstrong Number Check
import [Link];
public class ArmstrongNumber {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a 3-digit number: ");
int n = [Link]();
int a = n / 100, b = (n / 10) % 10, c = n % 10;
int sum = a * a * a + b * b * b + c * c * c;
if (sum == n)
[Link](n + " is an Armstrong number.");
else
[Link](n + " is NOT an Armstrong number.");
[Link]();
}
}
Q81 Add Two Complex Numbers
import [Link];
public class ComplexAddition {
double real, imag;
ComplexAddition(double r, double i) {
[Link] = r;
[Link] = i;
}
ComplexAddition add(ComplexAddition other) {
return new ComplexAddition([Link] + [Link], [Link] + [Link]);
}
void display() {
[Link](real + " + " + imag + "i");
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter first complex number (real imaginary): ");
double r1 = [Link](), i1 = [Link]();
[Link]("Enter second complex number (real imaginary): ");
double r2 = [Link](), i2 = [Link]();
ComplexAddition c1 = new ComplexAddition(r1, i1);
ComplexAddition c2 = new ComplexAddition(r2, i2);
ComplexAddition result = [Link](c2);
[Link]("Sum: ");
[Link]();
[Link]();
}
}
Q82 Palindrome Number Check
import [Link];
public class PalindromeNumber {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter an integer: ");
int n = [Link]();
int original = n, reversed = 0;
while (n != 0) {
reversed = reversed * 10 + n % 10;
n /= 10;
}
[Link](original + (original == reversed ? " is a Palindrome." : " is NOT a
Palindrome."));
[Link]();
}
}
Q83 Add Two Distances (Feet and Inches)
import [Link];
public class DistanceAddition {
int feet, inches;
DistanceAddition(int feet, int inches) {
[Link] = feet;
[Link] = inches;
}
DistanceAddition add(DistanceAddition other) {
int totalInches = [Link] + [Link];
int totalFeet = [Link] + [Link] + totalInches / 12;
return new DistanceAddition(totalFeet, totalInches % 12);
}
void display() {
[Link](feet + " feet " + inches + " inches");
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter first distance (feet inches): ");
int f1 = [Link](), i1 = [Link]();
[Link]("Enter second distance (feet inches): ");
int f2 = [Link](), i2 = [Link]();
DistanceAddition d1 = new DistanceAddition(f1, i1);
DistanceAddition d2 = new DistanceAddition(f2, i2);
[Link]("Total Distance: ");
[Link](d2).display();
[Link]();
}
}
Q84 GCD of Two Numbers
import [Link];
public class GCDCalc {
static int gcd(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter two integers: ");
int a = [Link](), b = [Link]();
[Link]("GCD of " + a + " and " + b + " = " + gcd(a, b));
[Link]();
}
}
Q85 nCr Calculation
import [Link];
public class NCR {
static long factorial(int n) {
long f = 1;
for (int i = 2; i <= n; i++) f *= i;
return f;
}
static long nCr(int n, int r) {
return factorial(n) / (factorial(r) * factorial(n - r));
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter n and r: ");
int n = [Link](), r = [Link]();
[Link]("C(" + n + ", " + r + ") = " + nCr(n, r));
[Link]();
}
}
Q87 Search Element and Find Frequency
import [Link];
public class SearchFrequency {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter number of elements: ");
int n = [Link]();
int[] arr = new int[n];
[Link]("Enter elements: ");
for (int i = 0; i < n; i++) arr[i] = [Link]();
[Link]("Enter element to search: ");
int key = [Link]();
int count = 0;
for (int x : arr) if (x == key) count++;
if (count > 0)
[Link](key + " found " + count + " time(s).");
else
[Link](key + " not found.");
[Link]();
}
}
Q88 Sum of All Matrix Elements
import [Link];
public class MatrixSum {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter rows and columns: ");
int r = [Link](), c = [Link]();
int[][] mat = new int[r][c];
[Link]("Enter matrix elements:");
int sum = 0;
for (int i = 0; i < r; i++)
for (int j = 0; j < c; j++) {
mat[i][j] = [Link]();
sum += mat[i][j];
}
[Link]("Sum of all elements: " + sum);
[Link]();
}
}
Q89 Sum of Diagonal Elements
import [Link];
public class DiagonalSum {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter matrix size n: ");
int n = [Link]();
int[][] mat = new int[n][n];
[Link]("Enter matrix elements:");
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
mat[i][j] = [Link]();
int sum = 0;
for (int i = 0; i < n; i++) sum += mat[i][i];
[Link]("Sum of diagonal elements: " + sum);
[Link]();
}
}
Q90 Badminton Player Information
import [Link];
public class BadmintonPlayer {
String name;
int matchesPlayed, gamesWon;
BadmintonPlayer(String name, int played, int won) {
[Link] = name;
[Link] = played;
[Link] = won;
}
void display() {
[Link]("Player: " + name + " | Played: " + matchesPlayed + " | Won: " + gamesWon);
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter number of players: ");
int n = [Link]();
BadmintonPlayer[] players = new BadmintonPlayer[n];
for (int i = 0; i < n; i++) {
[Link]("Enter name, matches played, games won: ");
String name = [Link]();
int played = [Link](), won = [Link]();
players[i] = new BadmintonPlayer(name, played, won);
}
[Link]("\nPlayer Details:");
for (BadmintonPlayer p : players) [Link]();
[Link]();
}
}
Q91 Sum of Boundary Elements of Matrix
import [Link];
public class BoundarySum {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter rows and columns: ");
int r = [Link](), c = [Link]();
int[][] mat = new int[r][c];
[Link]("Enter matrix elements:");
for (int i = 0; i < r; i++)
for (int j = 0; j < c; j++)
mat[i][j] = [Link]();
int sum = 0;
for (int i = 0; i < r; i++)
for (int j = 0; j < c; j++)
if (i == 0 || i == r - 1 || j == 0 || j == c - 1)
sum += mat[i][j];
[Link]("Sum of boundary elements: " + sum);
[Link]();
}
}
Q92 Addition and Subtraction of Two Matrices
import [Link];
public class MatrixAddSub {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter rows and columns: ");
int r = [Link](), c = [Link]();
int[][] a = new int[r][c], b = new int[r][c];
[Link]("Enter first matrix:");
for (int i = 0; i < r; i++)
for (int j = 0; j < c; j++) a[i][j] = [Link]();
[Link]("Enter second matrix:");
for (int i = 0; i < r; i++)
for (int j = 0; j < c; j++) b[i][j] = [Link]();
[Link]("Addition:");
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) [Link]((a[i][j] + b[i][j]) + " ");
[Link]();
}
[Link]("Subtraction:");
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) [Link]((a[i][j] - b[i][j]) + " ");
[Link]();
}
[Link]();
}
}
Q93 Median of n Integers
import [Link];
import [Link];
public class MedianCalc {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter number of integers: ");
int n = [Link]();
int[] arr = new int[n];
[Link]("Enter elements: ");
for (int i = 0; i < n; i++) arr[i] = [Link]();
[Link](arr);
double median;
if (n % 2 == 0)
median = (arr[n / 2 - 1] + arr[n / 2]) / 2.0;
else
median = arr[n / 2];
[Link]("Median: " + median);
[Link]();
}
}
Q94 Smallest and Second Smallest
import [Link];
public class SmallestTwo {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter number of integers: ");
int n = [Link]();
int[] arr = new int[n];
[Link]("Enter elements: ");
for (int i = 0; i < n; i++) arr[i] = [Link]();
int first = Integer.MAX_VALUE, second = Integer.MAX_VALUE;
for (int x : arr) {
if (x < first) { second = first; first = x; }
else if (x < second) second = x;
}
[Link]("Smallest: " + first);
[Link]("Second Smallest: " + second);
[Link]();
}
}
Q95 Add Two Distances (km and m)
import [Link];
public class KmDistance {
int km, meters;
KmDistance(int km, int m) {
[Link] = km;
[Link] = m;
}
KmDistance add(KmDistance other) {
int totalM = [Link] + [Link];
int totalKm = [Link] + [Link] + totalM / 1000;
return new KmDistance(totalKm, totalM % 1000);
}
void display() {
[Link](km + " km " + meters + " m");
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter first distance (km m): ");
int k1 = [Link](), m1 = [Link]();
[Link]("Enter second distance (km m): ");
int k2 = [Link](), m2 = [Link]();
KmDistance d1 = new KmDistance(k1, m1);
KmDistance d2 = new KmDistance(k2, m2);
[Link]("Total: ");
[Link](d2).display();
[Link]();
}
}
Q96 Largest and Second Largest
import [Link];
public class LargestTwo {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter number of integers: ");
int n = [Link]();
int[] arr = new int[n];
[Link]("Enter elements: ");
for (int i = 0; i < n; i++) arr[i] = [Link]();
int first = Integer.MIN_VALUE, second = Integer.MIN_VALUE;
for (int x : arr) {
if (x > first) { second = first; first = x; }
else if (x > second) second = x;
}
[Link]("Largest: " + first);
[Link]("Second Largest: " + second);
[Link]();
}
}
Q97 Delete Element from Array
import [Link];
public class DeleteElement {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter number of elements: ");
int n = [Link]();
int[] arr = new int[n];
[Link]("Enter elements: ");
for (int i = 0; i < n; i++) arr[i] = [Link]();
[Link]("Enter position to delete (1-based): ");
int pos = [Link]() - 1;
for (int i = pos; i < n - 1; i++) arr[i] = arr[i + 1];
n--;
[Link]("Array after deletion: ");
for (int i = 0; i < n; i++) [Link](arr[i] + " ");
[Link]();
[Link]();
}
}
Q98 Patient Information – Array of Objects
import [Link];
public class PatientInfo {
String name;
int patientNumber, age;
PatientInfo(String name, int num, int age) {
[Link] = name;
[Link] = num;
[Link] = age;
}
void display() {
[Link]("Name: " + name + " | Patient No: " + patientNumber + " | Age: " + age);
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter number of patients: ");
int n = [Link]();
PatientInfo[] patients = new PatientInfo[n];
for (int i = 0; i < n; i++) {
[Link]("Enter name, patient number, age: ");
String name = [Link]();
int num = [Link](), age = [Link]();
patients[i] = new PatientInfo(name, num, age);
}
[Link]("\nPatient Details:");
for (PatientInfo p : patients) [Link]();
[Link]();
}
}
Q99 Insert Element into Array
import [Link];
public class InsertElement {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter number of elements: ");
int n = [Link]();
int[] arr = new int[n + 1];
[Link]("Enter elements: ");
for (int i = 0; i < n; i++) arr[i] = [Link]();
[Link]("Enter value to insert: ");
int value = [Link]();
[Link]("Enter position (1-based): ");
int pos = [Link]() - 1;
for (int i = n; i > pos; i--) arr[i] = arr[i - 1];
arr[pos] = value;
n++;
[Link]("Array after insertion: ");
for (int i = 0; i < n; i++) [Link](arr[i] + " ");
[Link]();
[Link]();
}
}
Q100 3-Digit Prime Numbers in Descending Order
public class PrimesDescending {
static boolean isPrime(int n) {
for (int i = 2; i <= [Link](n); i++)
if (n % i == 0) return false;
return true;
}
public static void main(String[] args) {
[Link]("3-digit prime numbers in descending order:");
for (int i = 999; i >= 100; i--)
if (isPrime(i)) [Link](i + " ");
[Link]();
}
}
Q102 Perfect Numbers up to 1000
public class PerfectNumbers {
public static void main(String[] args) {
[Link]("Perfect numbers up to 1000:");
for (int n = 2; n <= 1000; n++) {
int sum = 0;
for (int i = 1; i < n; i++)
if (n % i == 0) sum += i;
if (sum == n) [Link](n + " ");
}
[Link]();
}
}
Q103 Factors of a Number
import [Link];
public class FindFactors {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int n = [Link]();
[Link]("Factors of " + n + ": ");
for (int i = 1; i <= n; i++)
if (n % i == 0) [Link](i + " ");
[Link]();
[Link]();
}
}
Q104 Area and Perimeter – Constructor Overloading (Rectangle and Circle)
import [Link];
public class ShapeCalculator {
double length, width, radius;
String shape;
ShapeCalculator(double length, double width) {
[Link] = "Rectangle";
[Link] = length;
[Link] = width;
}
ShapeCalculator(double radius) {
[Link] = "Circle";
[Link] = radius;
}
double area() {
if ([Link]("Rectangle")) return length * width;
return 3.14159 * radius * radius;
}
double perimeter() {
if ([Link]("Rectangle")) return 2 * (length + width);
return 2 * 3.14159 * radius;
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter length and width of rectangle: ");
double l = [Link](), w = [Link]();
ShapeCalculator rect = new ShapeCalculator(l, w);
[Link]("Rectangle - Area: " + [Link]() + " | Perimeter: " + [Link]());
[Link]("Enter radius of circle: ");
double r = [Link]();
ShapeCalculator circle = new ShapeCalculator(r);
[Link]("Circle - Area: %.2f | Perimeter: %.2f%n", [Link](), [Link]());
[Link]();
}
}
Q105 GCD and LCM of Two Numbers
import [Link];
public class GCDAndLCM {
static int gcd(int a, int b) {
while (b != 0) { int t = b; b = a % b; a = t; }
return a;
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter two integers: ");
int a = [Link](), b = [Link]();
int g = gcd(a, b);
int lcm = (a * b) / g;
[Link]("GCD: " + g);
[Link]("LCM: " + lcm);
[Link]();
}
}
Q106 Area and Perimeter – Method Overloading (Rectangle and Circle)
import [Link];
public class ShapeOverload {
static double area(double length, double width) {
return length * width;
}
static double area(double radius) {
return 3.14159 * radius * radius;
}
static double perimeter(double length, double width) {
return 2 * (length + width);
}
static double perimeter(double radius) {
return 2 * 3.14159 * radius;
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter length and width of rectangle: ");
double l = [Link](), w = [Link]();
[Link]("Rectangle Area: " + area(l, w));
[Link]("Rectangle Perimeter: " + perimeter(l, w));
[Link]("Enter radius of circle: ");
double r = [Link]();
[Link]("Circle Area: %.2f%n", area(r));
[Link]("Circle Perimeter: %.2f%n", perimeter(r));
[Link]();
}
}
Q107 Marks in PCM – Total and Average
import [Link];
public class PCMMarks {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter marks in Physics, Chemistry, Mathematics: ");
int physics = [Link](), chemistry = [Link](), maths = [Link]();
int total = physics + chemistry + maths;
double average = total / 3.0;
[Link]("Total: " + total);
[Link]("Average: " + [Link]("%.2f", average));
[Link]();
}
}
Q108 Add Two Times (hh:mm:ss)
import [Link];
public class TimeAddition {
int hours, minutes, seconds;
TimeAddition(int h, int m, int s) {
[Link] = h;
[Link] = m;
[Link] = s;
}
TimeAddition add(TimeAddition other) {
int s = [Link] + [Link];
int m = [Link] + [Link] + s / 60;
int h = [Link] + [Link] + m / 60;
return new TimeAddition(h % 24, m % 60, s % 60);
}
void display() {
[Link]("%02d:%02d:%02d%n", hours, minutes, seconds);
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter first time (hh mm ss): ");
int h1 = [Link](), m1 = [Link](), s1 = [Link]();
[Link]("Enter second time (hh mm ss): ");
int h2 = [Link](), m2 = [Link](), s2 = [Link]();
TimeAddition t1 = new TimeAddition(h1, m1, s1);
TimeAddition t2 = new TimeAddition(h2, m2, s2);
[Link]("Total Time: ");
[Link](t2).display();
[Link]();
}
}