import java.util.
Scanner;
public class ZodiacSign {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String[] months = {
"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
};
String[] zodiacs = {
"Capricornus / Aquarius", // Jan
"Aquarius / Pisces", // Feb
"Pisces / Aries", // Mar
"Aries / Taurus", // Apr
"Taurus / Gemini", // May
"Gemini / Cancer", // Jun
"Cancer / Leo", // Jul
"Leo / Virgo", // Aug
"Virgo / Libra", // Sep
"Libra / Scorpius", // Oct
"Scorpius / Sagittarius", // Nov
"Sagittarius / Capricornus" // Dec
};
System.out.print("Enter month number (1-12): ");
int month = sc.nextInt();
if (month >= 1 && month <= 12) {
System.out.println("Month -> " + months[month - 1]);
System.out.println("Zodiac Signs -> " + zodiacs[month - 1]);
} else {
System.out.println("Invalid month number.")
sc.close();
}}
STRING SUBSTRING REVERSE UPPER
import java.util.Scanner;
public class ReverseSubstringAuto {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Get full string
System.out.print("Enter a string: ");
String str = sc.nextLine();
// Create a substring from index 1 to second-last character (just an example)
if (str.length() >= 3) {
String sub = str.substring(1, str.length() - 1);
StringBuilder rev = new StringBuilder(sub.toUpperCase()).reverse();
System.out.println("Reversed Substring: " + rev);
System.out.println("Length: " + sub.length());
} else {
System.out.println("String too short to create a substring.");
sc.close();
}
FACTORIAL
import java.util.Scanner;
class Main{
public static long FactRec(int n){
if(n==1 || n==0){
return 1;
}else{
return n*FactRec(n-1);
public static long FactIter(int n){
long factorial = 1;
for(int i=1; i<=n; i++){
factorial*=i;
return factorial;
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("Enter a positive number: ");
int x = sc.nextInt();
long frec = FactRec(x);
long fiter = FactIter(x);
System.out.println("the factorial by recurrsion is: "+ frec);
System.out.println("the factorial by iteration is: "+ fiter);
}
ABSTRACT
import java.util.Scanner;
abstract class operation{
abstract double calc(double a, double b);
class add extends operation{
double calc(double a, double b){
return a+b;
class sub extends operation{
double calc(double a, double b){
return a-b;
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter first number: ");
double a = scanner.nextInt();
double b = scanner.nextInt();
add ADD = new add();
sub SUB = new sub();
System.out.println("The addition is: "+ ADD.calc(a,b));
System.out.println("The subtraction is: "+ SUB.calc(a,b));
}
COMPLEXXXX ADD
class Complex {
int real;
int imag;
// Constructor
Complex(int real, int imag) {
this.real = real;
this.imag = imag;
// Method to add two complex numbers
void add(Complex c) {
int r = this.real + c.real;
int i = this.imag + c.imag;
System.out.println("Result after addition: " + r + " + " + i + "i");
// Method to start the addition, using 'this' to pass object
void performAddition(Complex another) {
another.add(this); // Pass this object as argument
// Main class
public class Main {
public static void main(String[] args) {
Complex c1 = new Complex(4, 5); // 4 + 5i
Complex c2 = new Complex(3, 2); // 3 + 2i
// Using 'this' to pass and receive
c1.performAddition(c2); // Adds c1 to c2
}}
METHOD OVERLOADING
class Calculator {
void add(int a, int b) {
System.out.println("Sum (int): " + (a + b));
void add(double a, double b) {
System.out.println("Sum (double): " + (a + b));
void add(int a, int b, int c) {
System.out.println("Sum (3 int): " + (a + b + c));
public class Main {
public static void main(String[] args) {
Calculator c = new Calculator();
c.add(5, 10);
c.add(3.5, 2.5);
c.add(1, 2, 3);
}
METHOD OVERRIDING
class Animal {
void sound() {
System.out.println("Animal makes a sound");
class Dog extends Animal {
@Override
void sound() {
System.out.println("Dog barks");
public class Main {
public static void main(String[] args) {
Animal a = new Animal();
Dog d = new Dog();
a.sound(); // calls Animal's sound
d.sound(); // calls Dog's overridden sound
CONSTRUCTOR OVERLOADING
class User {
String name;
int age;
String city;
// Constructor 1: Only name
User(String name) {
this.name = name;
System.out.println("Welcome, " + name + "!");
// Constructor 2: Name and age
User(String name, int age) {
this.name = name;
this.age = age;
System.out.println(name + " is " + age + " years old.");
// Constructor 3: Name, age, and city
User(String name, int age, String city) {
this.name = name;
this.age = age;
this.city = city;
System.out.println(name + " is " + age + " years old and lives in " + city + ".");
public class Main {
public static void main(String[] args) {
User u1 = new User("Tanishka"); // only name
User u2 = new User("Aarav", 20); // name + age
User u3 = new User("Simran", 22, "Vellore"); // name + age + city
}}
STUDENT MARKS
import java.util.Scanner;
class Main{
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
System.out.println("Enter student's name: ");
String name = scanner.nextLine();
System.out.println("Enter student's roll no.: ");
String roll = scanner.nextLine();
int total = 0; // Declare total once outside the loop
int num = 6;
for(int i = 1; i < num; i++){
System.out.println("Enter marks of subject " + i + ": ");
int marks = scanner.nextInt();
total = total + marks; // Add to total without redeclaring it
double avg = total/5.0;
System.out.println("Average marks scored by "+name+" is "+avg+".");
EMPLOYEE QUE
// Base class
class Employee {
int id;
String name;
double salary;
Employee(int id, String name, double salary) {
this.id = id;
this.name = name;
this.salary = salary;
void display() {
System.out.println("ID: " + id);
System.out.println("Name: " + name);
System.out.println("Salary: ₹" + salary);
// Intermediate class
class Manager extends Employee {
int teamSize;
Manager(int id, String name, double salary, int teamSize) {
super(id, name, salary);
this.teamSize = teamSize;
@Override
void display() {
super.display();
System.out.println("Team Size: " + teamSize);
// Final derived class
class Director extends Manager {
String department;
Director(int id, String name, double salary, int teamSize, String department) {
super(id, name, salary, teamSize);
this.department = department;
}
@Override
void display() {
super.display();
System.out.println("Department: " + department);
// Main class
public class Main {
public static void main(String[] args) {
Employee e = new Employee(1, "Aanya", 30000);
Manager m = new Manager(2, "Raj", 50000, 8);
Director d = new Director(3, "Simran", 80000, 15, "Tech");
System.out.println("--- Employee Info ---");
e.display();
System.out.println("\n--- Manager Info ---");
m.display();
System.out.println("\n--- Director Info ---");
d.display();
LIBRARY BOOKS
class books{
String title;
String author;
int id;
static int totalbooks = 0;
books(String title,String author, int id){
this.title = title;
this. author = author;
this.id = id;
totalbooks++;
void display(){
System.out.println("Title: "+title);
System.out.println("Author: "+author);
System.out.println("ID: "+id);
static void displaytotal(){
System.out.println("Total books in library: "+totalbooks);
class Main {
public static void main(String[] args) {
books b1 = new books("seven secrets", "Enid Blyton",10937);
books b2 = new books("elements of electromagnetic theory", "sadiku",80327);
b1.display();
System.out.println();
b2.display();
System.out.println();
books.displaytotal();
System.out.println();
Abstract Class: An abstract class in Java:
Cannot be instantiated (you can't create objects directly from it).
Can have both abstract and non-abstract methods.
Acts as a blueprint for other classes.
Is useful when you want to provide some common functionality while leaving
specific implementations to subclasses.
✅ Why use abstract classes?
To enforce a common structure for all subclasses.
To implement code reusability.
Importance of Using Inheritance in Java
Inheritance is one of the key concepts of Object-Oriented Programming (OOP) in Java. It allows a
class to acquire properties and behaviors (methods) from another class.
✅ Why use inheritance?
Code reusability – reuse common code in child classes.
Method overriding – change or extend behavior from the parent class.
Improves code organization – promotes cleaner, modular code.
BILLING SYSTEM
import java.util.Scanner;
class Product {
int id;
String name;
double price;
Product(int id, String name, double price) {
this.id = id;
this.name = name;
this.price = price;
void displayProduct() {
System.out.println("ID: " + id + " | Name: " + name + " | Price: ₹" + price);
public class BillingSystem {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Create product catalogue
Product[] products = {
new Product(101, "Notebook", 45.00),
new Product(102, "Pen", 10.00),
new Product(103, "Pencil", 5.00),
new Product(104, "Eraser", 4.50),
new Product(105, "Sharpener", 7.00)
};
System.out.println("===== PRODUCT CATALOGUE =====");
for (Product p : products) {
p.displayProduct();
double total = 0.0;
System.out.println("\nEnter number of items you want to buy:");
int itemCount = sc.nextInt();
for (int i = 0; i < itemCount; i++) {
System.out.print("\nEnter Product ID: ");
int id = sc.nextInt();
System.out.print("Enter Quantity: ");
int qty = sc.nextInt();
boolean found = false;
for (Product p : products) {
if (p.id == id) {
double cost = qty * p.price;
total += cost;
System.out.println("Added: " + p.name + " x " + qty + " = ₹" + cost);
found = true;
break;
if (!found) {
System.out.println("Product ID " + id + " not found.");
}
}
System.out.println("\n========== BILL ==========");
System.out.println("Total Amount: ₹" + total);
System.out.println("==========================");
sc.close();
import java.util.Scanner;
import java.io.FileWriter;
import java.io.IOException;
public class EasyLibraryCatalogueWithFile {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("How many books do you want to add? ");
int n = sc.nextInt();
sc.nextLine(); // clear newline
String[] titles = new String[n];
String[] authors = new String[n];
String[] ids = new String[n];
for (int i = 0; i < n; i++) {
System.out.println("\nEnter details for Book " + (i + 1) + ":");
System.out.print("Title: ");
titles[i] = sc.nextLine();
System.out.print("Author: ");
authors[i] = sc.nextLine();
System.out.print("Book ID: ");
ids[i] = sc.nextLine();
// Writing to EXAMPLE.txt
try {
FileWriter writer = new FileWriter("EXAMPLE.txt");
writer.write("Total number of books: " + n + "\n\n");
for (int i = 0; i < n; i++) {
writer.write("Book " + (i + 1) + ":\n");
writer.write("Title: " + titles[i] + "\n");
writer.write("Author: " + authors[i] + "\n");
writer.write("Book ID: " + ids[i] + "\n");
writer.write("--------------------------\n");
}
writer.close();
System.out.println("\nDetails successfully written to EXAMPLE.txt!");
} catch (IOException e) {
System.out.println("An error occurred while writing to the file.");
sc.close();
TAX DEDUCTION
import java.util.Scanner;
class Employee {
String id;
String name;
double basePay;
double hra;
double da;
double investments;
// Constructor
public Employee(String id, String name, double basePay, double hra, double da, double investments) {
this.id = id;
this.name = name;
this.basePay = basePay;
this.hra = hra;
this.da = da;
this.investments = investments;
// Method to display employee details
public void displayDetails() {
System.out.println("\nEmployee ID : " + id);
System.out.println("Name : " + name);
System.out.println("Base Pay : " + basePay);
System.out.println("HRA : " + hra);
System.out.println("DA : " + da);
System.out.println("Investments : " + investments);
public class EmployeeTaxDetails {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Employee[] employees = new Employee[2];
for (int i = 0; i < 2; i++) {
System.out.println("\nEnter details for Employee " + (i + 1) + ":");
System.out.print("ID: ");
String id = sc.nextLine();
System.out.print("Name: ");
String name = sc.nextLine();
System.out.print("Base Pay: ");
double basePay = sc.nextDouble();
System.out.print("HRA: ");
double hra = sc.nextDouble();
System.out.print("DA: ");
double da = sc.nextDouble();
System.out.print("Investments: ");
double investments = sc.nextDouble();
sc.nextLine(); // consume leftover newline
employees[i] = new Employee(id, name, basePay, hra, da, investments);
System.out.println("\n--- Employee Tax Deduction Details ---");
for (Employee emp : employees) {
emp.displayDetails();
System.out.println("--------------------------------------");
sc.close();
}}
MATRIXXXXXX
import java.util.Scanner;
public class MatrixMaxSum {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the size of the matrix (n x n): ");
int n = sc.nextInt();
int[][] matrix = new int[n][n];
// Input matrix elements
System.out.println("\nEnter elements of the matrix:");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.print("Element [" + i + "][" + j + "]: ");
matrix[i][j] = sc.nextInt();
int maxRowSum = Integer.MIN_VALUE;
int maxColSum = Integer.MIN_VALUE;
int mainDiagonalSum = 0;
int antiDiagonalSum = 0;
// Row-wise and main/anti-diagonal sums
for (int i = 0; i < n; i++) {
int rowSum = 0;
int colSum = 0;
for (int j = 0; j < n; j++) {
rowSum += matrix[i][j];
colSum += matrix[j][i];
if (i == j) {
mainDiagonalSum += matrix[i][j];
if (i + j == n - 1) {
antiDiagonalSum += matrix[i][j];
if (rowSum > maxRowSum) maxRowSum = rowSum;
if (colSum > maxColSum) maxColSum = colSum;
// Find max among all
int maxSum = Math.max(Math.max(maxRowSum, maxColSum), Math.max(mainDiagonalSum, antiDiagonalSum));
System.out.println("\nMaximum Row Sum : " + maxRowSum);
System.out.println("Maximum Column Sum : " + maxColSum);
System.out.println("Main Diagonal Sum : " + mainDiagonalSum);
System.out.println("Anti Diagonal Sum : " + antiDiagonalSum);
System.out.println("Maximum of all sums : " + maxSum);
sc.close();