import [Link].
Scanner;
public class ZodiacSign {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
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
};
[Link]("Enter month number (1-12): ");
int month = [Link]();
if (month >= 1 && month <= 12) {
[Link]("Month -> " + months[month - 1]);
[Link]("Zodiac Signs -> " + zodiacs[month - 1]);
} else {
[Link]("Invalid month number.")
[Link]();
}}
STRING SUBSTRING REVERSE UPPER
import [Link];
public class ReverseSubstringAuto {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
// Get full string
[Link]("Enter a string: ");
String str = [Link]();
// Create a substring from index 1 to second-last character (just an example)
if ([Link]() >= 3) {
String sub = [Link](1, [Link]() - 1);
StringBuilder rev = new StringBuilder([Link]()).reverse();
[Link]("Reversed Substring: " + rev);
[Link]("Length: " + [Link]());
} else {
[Link]("String too short to create a substring.");
[Link]();
}
FACTORIAL
import [Link];
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([Link]);
[Link]("Enter a positive number: ");
int x = [Link]();
long frec = FactRec(x);
long fiter = FactIter(x);
[Link]("the factorial by recurrsion is: "+ frec);
[Link]("the factorial by iteration is: "+ fiter);
}
ABSTRACT
import [Link];
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([Link]);
[Link]("Enter first number: ");
double a = [Link]();
double b = [Link]();
add ADD = new add();
sub SUB = new sub();
[Link]("The addition is: "+ [Link](a,b));
[Link]("The subtraction is: "+ [Link](a,b));
}
COMPLEXXXX ADD
class Complex {
int real;
int imag;
// Constructor
Complex(int real, int imag) {
[Link] = real;
[Link] = imag;
// Method to add two complex numbers
void add(Complex c) {
int r = [Link] + [Link];
int i = [Link] + [Link];
[Link]("Result after addition: " + r + " + " + i + "i");
// Method to start the addition, using 'this' to pass object
void performAddition(Complex another) {
[Link](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
[Link](c2); // Adds c1 to c2
}}
METHOD OVERLOADING
class Calculator {
void add(int a, int b) {
[Link]("Sum (int): " + (a + b));
void add(double a, double b) {
[Link]("Sum (double): " + (a + b));
void add(int a, int b, int c) {
[Link]("Sum (3 int): " + (a + b + c));
public class Main {
public static void main(String[] args) {
Calculator c = new Calculator();
[Link](5, 10);
[Link](3.5, 2.5);
[Link](1, 2, 3);
}
METHOD OVERRIDING
class Animal {
void sound() {
[Link]("Animal makes a sound");
class Dog extends Animal {
@Override
void sound() {
[Link]("Dog barks");
public class Main {
public static void main(String[] args) {
Animal a = new Animal();
Dog d = new Dog();
[Link](); // calls Animal's sound
[Link](); // calls Dog's overridden sound
CONSTRUCTOR OVERLOADING
class User {
String name;
int age;
String city;
// Constructor 1: Only name
User(String name) {
[Link] = name;
[Link]("Welcome, " + name + "!");
// Constructor 2: Name and age
User(String name, int age) {
[Link] = name;
[Link] = age;
[Link](name + " is " + age + " years old.");
// Constructor 3: Name, age, and city
User(String name, int age, String city) {
[Link] = name;
[Link] = age;
[Link] = city;
[Link](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 [Link];
class Main{
public static void main(String[] args){
Scanner scanner = new Scanner([Link]);
[Link]("Enter student's name: ");
String name = [Link]();
[Link]("Enter student's roll no.: ");
String roll = [Link]();
int total = 0; // Declare total once outside the loop
int num = 6;
for(int i = 1; i < num; i++){
[Link]("Enter marks of subject " + i + ": ");
int marks = [Link]();
total = total + marks; // Add to total without redeclaring it
double avg = total/5.0;
[Link]("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) {
[Link] = id;
[Link] = name;
[Link] = salary;
void display() {
[Link]("ID: " + id);
[Link]("Name: " + name);
[Link]("Salary: ₹" + salary);
// Intermediate class
class Manager extends Employee {
int teamSize;
Manager(int id, String name, double salary, int teamSize) {
super(id, name, salary);
[Link] = teamSize;
@Override
void display() {
[Link]();
[Link]("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);
[Link] = department;
}
@Override
void display() {
[Link]();
[Link]("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");
[Link]("--- Employee Info ---");
[Link]();
[Link]("\n--- Manager Info ---");
[Link]();
[Link]("\n--- Director Info ---");
[Link]();
LIBRARY BOOKS
class books{
String title;
String author;
int id;
static int totalbooks = 0;
books(String title,String author, int id){
[Link] = title;
this. author = author;
[Link] = id;
totalbooks++;
void display(){
[Link]("Title: "+title);
[Link]("Author: "+author);
[Link]("ID: "+id);
static void displaytotal(){
[Link]("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);
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
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 [Link];
class Product {
int id;
String name;
double price;
Product(int id, String name, double price) {
[Link] = id;
[Link] = name;
[Link] = price;
void displayProduct() {
[Link]("ID: " + id + " | Name: " + name + " | Price: ₹" + price);
public class BillingSystem {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
// 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)
};
[Link]("===== PRODUCT CATALOGUE =====");
for (Product p : products) {
[Link]();
double total = 0.0;
[Link]("\nEnter number of items you want to buy:");
int itemCount = [Link]();
for (int i = 0; i < itemCount; i++) {
[Link]("\nEnter Product ID: ");
int id = [Link]();
[Link]("Enter Quantity: ");
int qty = [Link]();
boolean found = false;
for (Product p : products) {
if ([Link] == id) {
double cost = qty * [Link];
total += cost;
[Link]("Added: " + [Link] + " x " + qty + " = ₹" + cost);
found = true;
break;
if (!found) {
[Link]("Product ID " + id + " not found.");
}
}
[Link]("\n========== BILL ==========");
[Link]("Total Amount: ₹" + total);
[Link]("==========================");
[Link]();
import [Link];
import [Link];
import [Link];
public class EasyLibraryCatalogueWithFile {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("How many books do you want to add? ");
int n = [Link]();
[Link](); // clear newline
String[] titles = new String[n];
String[] authors = new String[n];
String[] ids = new String[n];
for (int i = 0; i < n; i++) {
[Link]("\nEnter details for Book " + (i + 1) + ":");
[Link]("Title: ");
titles[i] = [Link]();
[Link]("Author: ");
authors[i] = [Link]();
[Link]("Book ID: ");
ids[i] = [Link]();
// Writing to [Link]
try {
FileWriter writer = new FileWriter("[Link]");
[Link]("Total number of books: " + n + "\n\n");
for (int i = 0; i < n; i++) {
[Link]("Book " + (i + 1) + ":\n");
[Link]("Title: " + titles[i] + "\n");
[Link]("Author: " + authors[i] + "\n");
[Link]("Book ID: " + ids[i] + "\n");
[Link]("--------------------------\n");
}
[Link]();
[Link]("\nDetails successfully written to [Link]!");
} catch (IOException e) {
[Link]("An error occurred while writing to the file.");
[Link]();
TAX DEDUCTION
import [Link];
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) {
[Link] = id;
[Link] = name;
[Link] = basePay;
[Link] = hra;
[Link] = da;
[Link] = investments;
// Method to display employee details
public void displayDetails() {
[Link]("\nEmployee ID : " + id);
[Link]("Name : " + name);
[Link]("Base Pay : " + basePay);
[Link]("HRA : " + hra);
[Link]("DA : " + da);
[Link]("Investments : " + investments);
public class EmployeeTaxDetails {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
Employee[] employees = new Employee[2];
for (int i = 0; i < 2; i++) {
[Link]("\nEnter details for Employee " + (i + 1) + ":");
[Link]("ID: ");
String id = [Link]();
[Link]("Name: ");
String name = [Link]();
[Link]("Base Pay: ");
double basePay = [Link]();
[Link]("HRA: ");
double hra = [Link]();
[Link]("DA: ");
double da = [Link]();
[Link]("Investments: ");
double investments = [Link]();
[Link](); // consume leftover newline
employees[i] = new Employee(id, name, basePay, hra, da, investments);
[Link]("\n--- Employee Tax Deduction Details ---");
for (Employee emp : employees) {
[Link]();
[Link]("--------------------------------------");
[Link]();
}}
MATRIXXXXXX
import [Link];
public class MatrixMaxSum {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter the size of the matrix (n x n): ");
int n = [Link]();
int[][] matrix = new int[n][n];
// Input matrix elements
[Link]("\nEnter elements of the matrix:");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
[Link]("Element [" + i + "][" + j + "]: ");
matrix[i][j] = [Link]();
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 = [Link]([Link](maxRowSum, maxColSum), [Link](mainDiagonalSum, antiDiagonalSum));
[Link]("\nMaximum Row Sum : " + maxRowSum);
[Link]("Maximum Column Sum : " + maxColSum);
[Link]("Main Diagonal Sum : " + mainDiagonalSum);
[Link]("Anti Diagonal Sum : " + antiDiagonalSum);
[Link]("Maximum of all sums : " + maxSum);
[Link]();