0% found this document useful (0 votes)
8 views27 pages

My Java Practical

Have ece ajp practice

Uploaded by

Yashraj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views27 pages

My Java Practical

Have ece ajp practice

Uploaded by

Yashraj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Write a program to add two numbers.

import java.util.Scanner; class Addition

public static void main(String[] args)

System.out.println("Addition without class and object");

int a=30, b=40, ans; ans=a+b;

System.out.println("Addition is:"+ ans);


int x,y,sum;

Scanner myObj=new Scanner(System.in); System.out.println("Type a


number:");

x=myObj.nextInt();

System.out.println("Type another number:"); y=myObj.nextInt();

sum=x+y;

System.out.println("The Sum is:"+sum);

}
Output:
Write a program to add two numbers using scanner class.

import java.util.Scanner; 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(System.in); AdditionClass myObj = new


AdditionClass();
System.out.println("Enter the first integer:"); int firstNum = input.nextInt();
System.out.println("Enter the second integer:");

int secondNum = input.nextInt();


myObj.setNumbers(firstNum, secondNum); int sum =
myObj.calculateSum();
System.out.println("The sum is: " + sum); input.close();

}
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<=Math.sqrt(value);i++)

if(value%i==0)

return false;

return true;

}
}

class PrimeNo

public static void main(String[] args)

PrimeNumber num=new PrimeNumber(); num.value=20;


boolean ans=num.isPrime(); if (ans==true)

System.out.println(num.value+"is Prime");

else

System.out.println(num.value+"is Not Prime");

System.out.println("Another value"); num.value=5;

if (num.isPrime())

System.out.println(num.value+"is Prime");

else

System.out.println(num.value+"is Not Prime");

}
Output:
Write a program to find the maximum of three numbers.

import java.util.Scanner; 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(System.in); MaxClass myObj = new MaxClass();


System.out.println("Enter the first integer:"); int firstNum = input.nextInt();
System.out.println("Enter the second integer:");

int secondNum = input.nextInt();

System.out.println("Enter the third integer:"); int thirdNum = input.nextInt();

myObj.setNumbers(firstNum, secondNum,thirdNum);

int maxNo =myObj.Searchmax();

System.out.println("The Maximun number is: "+ maxNo);

input.close();

Output:
Write a program to find and display even and odd numbers from 1 to 50.

class EvenOddNumbers

{
public static void main(String[] args)

{
System.out.println("Even and Odd numbers from 1 to 50:");
System.out.println("Even nos"); for (int i=1; i<=50; i++)
{
if(i % 2 == 0)
{
System.out.print("\t"+i);
}
}
System.out.println();
System.out.println("Odd nos");
for (int i=1; i<=50;i++)

{
if(i%2!=0)

{
System.out.print("\t"+i);

Output:
Write a program to demonstrate single level inheritance.

class Teacher

{
void teach()
{
System.out.println("Teaching Subject");
}

}
class Student extends Teacher

{
void listen()

{
System.out.println("Student listening");

class Inheritancecheck

public static void main(String[] args){

Student s1=new Student(); s1.listen();

s1.teach();//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)

this.color=color;

public void display()

System.out.println("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);

this.length=length; this.width=width;

@Override

public void display()

System.out.println("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()

System.out.println("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

System.out.println("Level 1"); shape.display();

System.out.println();
//Level 2-Rectangle

System.out.println("Level 2"); rectangle.display();


System.out.println("Area of rectangle is:" + rectangle.getarea());

System.out.println();

//Level 3-Square
System.out.println("Level 3"); square.display();

System.out.println("Area of square is:" + square.getarea());

//Inheritated from Rectangle class

System.out.println("Perimeter of square is:" + square.getperimeter());

System.out.println();

//Polymorphism

System.out.println("Polymorphism");

Shape[] shapes={shape,rectangle,square}; for(Shape s: shapes)

s.display();

}
Output:
Write a program to demonstrate hierarchical inheritance.

//Parent class class


Fruit

String name; String color;

public Fruit(String name,String color)

{
this.name=name; this.color=color;
}

public void displayInfo()

{
System.out.println("Fruit: " + name + ", Color: " + color);

public void eat()

{
System.out.println("Eating " + name + " - it is delicious!");

public void ripen()

{
System.out.println(name + " is ripening");
}
}
//Child class 1

class Apple extends Fruit

public Apple(String color)

super("Apple",color);

public void crunch()

{
System.out.println(name + " - makes a crunchy sound when eaten");

@Override
public void eat()

System.out.println("Eating " + color +" " + name + " - so crunchy");

//Child class 2

class Orange extends Fruit

{
public Orange()

super("Orange","Orange");

public void peel()

{
System.out.println("Peeling the " + name + " to eat");

@Override

public void eat()

{
System.out.println("Eating juicy " + name + " - full of vitamin c!");
}

//Child class 3

class Banana extends Fruit

public Banana()

super("Banana","Yellow");

public void peel(){

System.out.println("Peeling the " + name + " to eat");


}

@Override
public void eat()

{
System.out.println("Eating soft " + name + " - great source of potassium!");
}

}
//Child class 4

class Mango extends Fruit

public Mango()

{
super("Mango","Yellow-Orange");
}

public void slice()


{

System.out.println("slicing the " + name + " into pieces");

@Override

public void eat()

System.out.println("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

System.out.println("Apple Class");

apple.displayInfo(); //Inherited method apple.eat(); //Overriden Method


apple.crunch(); //Own Method System.out.println();

//Orange Demonstration
System.out.println("Orange Class");

orange.displayInfo(); //Inherited method orange.eat(); //Overriden Method


orange.peel(); //Own Method System.out.println();

//Banana Demonstration

System.out.println("Banana Class"); banana.displayInfo(); //Inherited method


banana.eat(); //Overriden Method banana.peel(); //Own Method

System.out.println();

//Mango Demonstration

System.out.println("Mango Class");

mango.displayInfo(); //Inherited method mango.eat(); //Overriden Method


mango.slice(); //Own Method System.out.println();

//Polymorphism

System.out.println("All fruits are ripening"); Fruit[]


fruits={apple,orange,banana,mango};

for (Fruit fruit : fruits)

fruit.ripen();

System.out.println();

System.out.println("All fruits being eaten"); for (Fruit fruit : fruits){

fruit.eat();

}
Output:
Write a program to demonstrate the details of an employee.

// Override displayInfo @Override

public void displayInfo()

super.displayInfo(); // Call Developer's displayInfo System.out.println("Team Lead: " +


(isTeamLead ? "Yes" : "No"));

System.out.println("Specialization: " + specialization);

System.out.println("Position: Senior Developer");

}
// Senior developers get leadership bonus @Override

public double getAnnualSalary()

double leadBonus = isTeamLead ? Salary * 0.15 * 12 : 0; /


/ 15% leadership bonus
return super.getAnnualSalary() + leadBonus;

}
// Main class to demonstrate inheritance class EmployeeInheritanceDemo

public static void main(String[] args)

System.out.println("=== 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 System.out.println("--- Manager


Example ---"); projectManager.displayInfo();

projectManager.work();

projectManager.conductMeeting();
projectManager.assignTasks();
System.out.println("Annual Salary: Rs/-" + projectManager.getAnnualSalary());

System.out.println();

// Demonstrate inheritance with Developer System.out.println("--- Developer


Example ---"); javaDeveloper.displayInfo();

javaDeveloper.work();

javaDeveloper.debugCode(); javaDeveloper.reviewCode();
javaDeveloper.takeLunchBreak(); // Inherited method

System.out.println("Annual Salary: Rs/-" + javaDeveloper.getAnnualSalary());

System.out.println();

// Demonstrate inheritance with Intern System.out.println("--- Intern


Example ---"); collegeIntern.displayInfo();

collegeIntern.work();

collegeIntern.attendTraining();
collegeIntern.learnSkills(); System.out.println();

// Demonstrate multi-level inheritance with SeniorDeveloper


System.out.println("--- Senior Developer Example -
--");

techLead.displayInfo(); techLead.work();
techLead.debugCode(); // Inherited from Developer

techLead.mentorJuniors(); // SeniorDeveloper specific method

techLead.designArchitecture();

System.out.println("Annual Salary: Rs/-" + techLead.getAnnualSalary());

System.out.println();

// Demonstrate polymorphism

System.out.println("--- Polymorphism Example ---


");

Employee[] employees = {genericEmployee,


projectManager, javaDeveloper, collegeIntern, techLead};

System.out.println("All employees working:"); for (Employee emp : employees)

System.out.print("- ");
emp.work(); // Different behavior based on actual object type

System.out.println();

// Salary comparison

System.out.println("Annual Salary Comparison:"); for (Employee emp : employees)

System.out.println("- " + emp.name + ": Rs/- " + emp.getAnnualSalary());

}
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)

this.name=name; this.id=id;

public abstract double calculateSalary(); public void displayInfo()

System.out.println("Employee: " + name + ", ID: " + id);

class FullTimeEmployee extends Employee


{

private double monthlySalary;

public FullTimeEmployee(String name, int


id,double monthlySalary)

super(name,id);

this.monthlySalary=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);

this.hourlyRate=hourlyRate;

this.hoursWorked=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)

emp.displayInfo();

System.out.println("Salary: " + emp.calculateSalary());

System.out.println();

}
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)

this.model = model; this.brand = brand;

}
public void displayDetails()

System.out.println("Model: " + model + ", Brand: " + brand);

}
}

class Circle

private final double PI = 3.14159; //Final Variable private double radius;


public Circle(double radius)

this.radius = radius;

}
public double calculateArea()

return PI * radius * radius;

}
public final void MyFinalMethod()

System.out.println("Final Method Calling");


}

class Main

public static void main(String[] args)

Vehicle myCar = new Vehicle("Model XYZ", "Tesla");

myCar.displayDetails();

Circle circle = new Circle(5.0);

double area = circle.calculateArea();

System.out.println("Area of the circle: " + area); circle.MyFinalMethod();

}Output:

You might also like