Write a program to add two numbers.
import [Link]; class Addition
public static void main(String[] args)
[Link]("Addition without class and object");
int a=30, b=40, ans; ans=a+b;
[Link]("Addition is:"+ ans);
int x,y,sum;
Scanner myObj=new Scanner([Link]); [Link]("Type a
number:");
x=[Link]();
[Link]("Type another number:"); y=[Link]();
sum=x+y;
[Link]("The Sum is:"+sum);
}
Output:
Write a program to add two numbers using scanner class.
import [Link]; class
AdditionClass
int num1; int num2;
public void setNumbers(int n1, int n2)
num1 = n1; num2 = n2;
public int calculateSum()
return num1 + num2;
class AddTwoNumbers
public static void main(String[] args)
Scanner input = new Scanner([Link]); AdditionClass myObj = new
AdditionClass();
[Link]("Enter the first integer:"); int firstNum = [Link]();
[Link]("Enter the second integer:");
int secondNum = [Link]();
[Link](firstNum, secondNum); int sum =
[Link]();
[Link]("The sum is: " + sum); [Link]();
}
Output:
Write a program to check whether the number is prime or not.
class PrimeNumber
int value; boolean isPrime()
if(value<=1) return false;
for (int i=2; i<=[Link](value);i++)
if(value%i==0)
return false;
return true;
}
}
class PrimeNo
public static void main(String[] args)
PrimeNumber num=new PrimeNumber(); [Link]=20;
boolean ans=[Link](); if (ans==true)
[Link]([Link]+"is Prime");
else
[Link]([Link]+"is Not Prime");
[Link]("Another value"); [Link]=5;
if ([Link]())
[Link]([Link]+"is Prime");
else
[Link]([Link]+"is Not Prime");
}
Output:
Write a program to find the maximum of three numbers.
import [Link]; class MaxClass
int num1;
int num2;
int num3;
public void setNumbers(int n1, int n2, int n3)
num1 = n1; num2 =
n2; num3 = n3;
public int Searchmax()
int Max=num1; if
(num2>Max)
{Max=num2;
if(num3>Max)
Max=num3;
return Max;
class ThreeMaxNo
public static void main(String[] args)
Scanner input = new Scanner([Link]); MaxClass myObj = new MaxClass();
[Link]("Enter the first integer:"); int firstNum = [Link]();
[Link]("Enter the second integer:");
int secondNum = [Link]();
[Link]("Enter the third integer:"); int thirdNum = [Link]();
[Link](firstNum, secondNum,thirdNum);
int maxNo =[Link]();
[Link]("The Maximun number is: "+ maxNo);
[Link]();
Output:
Write a program to find and display even and odd numbers from 1 to 50.
class EvenOddNumbers
{
public static void main(String[] args)
{
[Link]("Even and Odd numbers from 1 to 50:");
[Link]("Even nos"); for (int i=1; i<=50; i++)
{
if(i % 2 == 0)
{
[Link]("\t"+i);
}
}
[Link]();
[Link]("Odd nos");
for (int i=1; i<=50;i++)
{
if(i%2!=0)
{
[Link]("\t"+i);
Output:
Write a program to demonstrate single level inheritance.
class Teacher
{
void teach()
{
[Link]("Teaching Subject");
}
}
class Student extends Teacher
{
void listen()
{
[Link]("Student listening");
class Inheritancecheck
public static void main(String[] args){
Student s1=new Student(); [Link]();
[Link]();//Calling Parent Class method
Output:
Write a program to demonstrate multilevel inheritance.
//Level 1-Super class class Shape
protected String color; public Shape(String
color)
[Link]=color;
public void display()
[Link]("This is a " + color + "
shape");
//Level 2-Parent class
class Rectangle extends Shape
{
protected int length,width;
public Rectangle(String color,int length,int width)
super(color);
[Link]=length; [Link]=width;
@Override
public void display()
[Link]("This is a " + color + " length is: " + length + " and
width is: " + width);
public int getarea()
{
return length * width;
//Level 3-Child class
class Square extends Rectangle
{
public int side;
public Square(String color,int side)
super(color,side,side);
}
@Override
public void display()
[Link]("This is a: " + color + " sqare with sides: " + side);
public int getperimeter()
return 4*length;
class Multilevelinheritance
public static void main(String[] args)
Shape shape=new Shape("Blue"); Rectangle rectangle=new
Rectangle("Blue",5,3);
Square square=new Square("Blue",6);
//Level 1-Shape
[Link]("Level 1"); [Link]();
[Link]();
//Level 2-Rectangle
[Link]("Level 2"); [Link]();
[Link]("Area of rectangle is:" + [Link]());
[Link]();
//Level 3-Square
[Link]("Level 3"); [Link]();
[Link]("Area of square is:" + [Link]());
//Inheritated from Rectangle class
[Link]("Perimeter of square is:" + [Link]());
[Link]();
//Polymorphism
[Link]("Polymorphism");
Shape[] shapes={shape,rectangle,square}; for(Shape s: shapes)
[Link]();
}
Output:
Write a program to demonstrate hierarchical inheritance.
//Parent class class
Fruit
String name; String color;
public Fruit(String name,String color)
{
[Link]=name; [Link]=color;
}
public void displayInfo()
{
[Link]("Fruit: " + name + ", Color: " + color);
public void eat()
{
[Link]("Eating " + name + " - it is delicious!");
public void ripen()
{
[Link](name + " is ripening");
}
}
//Child class 1
class Apple extends Fruit
public Apple(String color)
super("Apple",color);
public void crunch()
{
[Link](name + " - makes a crunchy sound when eaten");
@Override
public void eat()
[Link]("Eating " + color +" " + name + " - so crunchy");
//Child class 2
class Orange extends Fruit
{
public Orange()
super("Orange","Orange");
public void peel()
{
[Link]("Peeling the " + name + " to eat");
@Override
public void eat()
{
[Link]("Eating juicy " + name + " - full of vitamin c!");
}
//Child class 3
class Banana extends Fruit
public Banana()
super("Banana","Yellow");
public void peel(){
[Link]("Peeling the " + name + " to eat");
}
@Override
public void eat()
{
[Link]("Eating soft " + name + " - great source of potassium!");
}
}
//Child class 4
class Mango extends Fruit
public Mango()
{
super("Mango","Yellow-Orange");
}
public void slice()
{
[Link]("slicing the " + name + " into pieces");
@Override
public void eat()
[Link]("Eating sweet " + name + " - tropical and juicy!");
class Hierarchicalcheck
public static void main(String[] args)
Apple apple=new Apple("Red"); Orange orange=new
Orange(); Banana banana=new Banana(); Mango mango=new
Mango();
//Apple Demonstration
[Link]("Apple Class");
[Link](); //Inherited method [Link](); //Overriden Method
[Link](); //Own Method [Link]();
//Orange Demonstration
[Link]("Orange Class");
[Link](); //Inherited method [Link](); //Overriden Method
[Link](); //Own Method [Link]();
//Banana Demonstration
[Link]("Banana Class"); [Link](); //Inherited method
[Link](); //Overriden Method [Link](); //Own Method
[Link]();
//Mango Demonstration
[Link]("Mango Class");
[Link](); //Inherited method [Link](); //Overriden Method
[Link](); //Own Method [Link]();
//Polymorphism
[Link]("All fruits are ripening"); Fruit[]
fruits={apple,orange,banana,mango};
for (Fruit fruit : fruits)
[Link]();
[Link]();
[Link]("All fruits being eaten"); for (Fruit fruit : fruits){
[Link]();
}
Output:
Write a program to demonstrate the details of an employee.
// Override displayInfo @Override
public void displayInfo()
[Link](); // Call Developer's displayInfo [Link]("Team Lead: " +
(isTeamLead ? "Yes" : "No"));
[Link]("Specialization: " + specialization);
[Link]("Position: Senior Developer");
}
// Senior developers get leadership bonus @Override
public double getAnnualSalary()
double leadBonus = isTeamLead ? Salary * 0.15 * 12 : 0; /
/ 15% leadership bonus
return [Link]() + leadBonus;
}
// Main class to demonstrate inheritance class EmployeeInheritanceDemo
public static void main(String[] args)
[Link]("=== Employee Management System - Inheritance Example
===\n");
// Create objects of different classes
Employee genericEmployee = new Employee("Mr.
A", 1001, 3000, "General");
Manager projectManager = new Manager("Mr. B", 1002, 8000, "IT", 8, "E-commerce
Platform");
Developer javaDeveloper = new Developer("Mr. C", 1003, 6000, "IT", "Java", 4);
Intern collegeIntern = new Intern("Mr. D", 1004, 1500, "Marketing", "State University",
6);
SeniorDeveloper techLead = new
SeniorDeveloper("Mr. E", 1005, 9000, "IT", "Python", 8, true, "Machine Learning");
// Demonstrate inheritance with Manager [Link]("--- Manager
Example ---"); [Link]();
[Link]();
[Link]();
[Link]();
[Link]("Annual Salary: Rs/-" + [Link]());
[Link]();
// Demonstrate inheritance with Developer [Link]("--- Developer
Example ---"); [Link]();
[Link]();
[Link](); [Link]();
[Link](); // Inherited method
[Link]("Annual Salary: Rs/-" + [Link]());
[Link]();
// Demonstrate inheritance with Intern [Link]("--- Intern
Example ---"); [Link]();
[Link]();
[Link]();
[Link](); [Link]();
// Demonstrate multi-level inheritance with SeniorDeveloper
[Link]("--- Senior Developer Example -
--");
[Link](); [Link]();
[Link](); // Inherited from Developer
[Link](); // SeniorDeveloper specific method
[Link]();
[Link]("Annual Salary: Rs/-" + [Link]());
[Link]();
// Demonstrate polymorphism
[Link]("--- Polymorphism Example ---
");
Employee[] employees = {genericEmployee,
projectManager, javaDeveloper, collegeIntern, techLead};
[Link]("All employees working:"); for (Employee emp : employees)
[Link]("- ");
[Link](); // Different behavior based on actual object type
[Link]();
// Salary comparison
[Link]("Annual Salary Comparison:"); for (Employee emp : employees)
[Link]("- " + [Link] + ": Rs/- " + [Link]());
}
Output:
Write a program to demonstrate the use of an abstract class.
abstract class Employee
protected String name; protected int id;
public Employee(String name, int id)
[Link]=name; [Link]=id;
public abstract double calculateSalary(); public void displayInfo()
[Link]("Employee: " + name + ", ID: " + id);
class FullTimeEmployee extends Employee
{
private double monthlySalary;
public FullTimeEmployee(String name, int
id,double monthlySalary)
super(name,id);
[Link]=monthlySalary;
@Override
public double calculateSalary()
return monthlySalary;
class PartTimeEmployee extends Employee
{
private double hourlyRate; private int hoursWorked;
public PartTimeEmployee(String name, int id,double hourlyRate,int hoursWorked)
{
super(name,id);
[Link]=hourlyRate;
[Link]=hoursWorked;
@Override
public double calculateSalary()
return hourlyRate*hoursWorked;
class AbstractClassMain
public static void main(String[] args)
Employee[] employees= { new
FullTimeEmployee("ADIL",101,50000),
newPartTimeEmployee("Adnan",102,20,80)};
for (Employee emp : employees)
[Link]();
[Link]("Salary: " + [Link]());
[Link]();
}
Output:
Write a program to demonstrate the use of a final class.
final class Vehicle //Final Class
{
private String model; private String brand;
public Vehicle(String model, String brand)
[Link] = model; [Link] = brand;
}
public void displayDetails()
[Link]("Model: " + model + ", Brand: " + brand);
}
}
class Circle
private final double PI = 3.14159; //Final Variable private double radius;
public Circle(double radius)
[Link] = radius;
}
public double calculateArea()
return PI * radius * radius;
}
public final void MyFinalMethod()
[Link]("Final Method Calling");
}
class Main
public static void main(String[] args)
Vehicle myCar = new Vehicle("Model XYZ", "Tesla");
[Link]();
Circle circle = new Circle(5.0);
double area = [Link]();
[Link]("Area of the circle: " + area); [Link]();
}Output: