Practical No.
1 & 2
• Program Code:
class pr1 {
public static void main(String args[]) {
[Link]("Java test run");
}
}
• Output:
Practical No. 3
• Program Code:
class pr2a {
public static void main(String args[]) {
int x = 13, y = 7, z = 96;
if(x > y && x > z) {
[Link](x+" is the largest number.");
}
•
else if(y > x && y > z) {
•
[Link](y+" is the largest nmuber.");
•
}
•
else {
•
[Link](z+" is the largest number.");
•
}
• Output:
Practical No. 3
• Program Code:
class pr2 {
public static void main(String args[]) {
int x = 17;
if(x % 2 == 0) {
[Link](x +" is an even number.");
}
else {
[Link](x +" is an odd number.");
}
}
}
• Output:
Practical No. 4
Program Code:
class pr4 {
public static void main(String args[]) {
char input = 'Z';
switch(input) {
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
[Link](input+" is a vowel.");
break;
default:
[Link](input+" is not a vowel.");
• Output:
Practical No. 4 • Output:
• Program Code:
class pr4 {
public static void main(String args[])
{
int input = 4;
switch(input) {
case 1:
[Link]("Monday");
break;
case 2:
[Link]("Tuesday");
break;
case 3:
[Link]("Wednesday");
break;
case 4:
[Link]("Thursday");
break;
case 5:
[Link]("Friday");
break;
case 6:
[Link]("Saturday");
break;
case 7:
[Link]("Sunday");
break;
default:
[Link]("Invalid Input");
}
}
}
Practical No. 5
• Program Code:
public class pr5a {
public static void main(String[] args) {
int numRows = 5;
for (int i = 1; i <= numRows; i++) {
for (int j = numRows - i; j > 0; j--) {
[Link](" ");
}
for (int k = 1; k <= 2 * i - 1; k++) {
[Link]("*");
}
[Link]();
}
}
}
• Output:
Practical No. 5 • Output:
• Program Code:
class pr5 {
public static void main(String[]
args) {
// Initialize an array with
predefined values
int[] numbers = {5, 8, -3, 10, 2};
// Check if any of the numbers
are negative
boolean hasNegative = false;
for (int number : numbers) {
if (number < 0) {
hasNegative = true;
break;
}
}
// Display the result
if (hasNegative) {
[Link]("The array
contains negative numbers.");
} else {
[Link]("The array
does not contain negative
numbers.");
}
}
}
Practical No. 6 }
• Program Code:
import [Link];
• Output:
class pr6 {
public static void main(String[]
args) {
Scanner scanner = new
Scanner([Link]);
int num1, num2;
boolean isValid = false;
do {
[Link]("Enter an
even number: ");
num1 = [Link]();
[Link]("Enter an
odd number: ");
num2 = [Link]();
isValid = (num1 % 2 == 0) &&
(num2 % 2 != 0);
if (!isValid) {
[Link]("Invalid
input! Please enter one even
number and one odd number.");
}
} while (!isValid);
[Link]("You
entered a valid pair of numbers: " +
num1 + " and " + num2);
[Link]();
}
Practical No. 6
• Program Code:
class pr6a {
public static void main(String args[])
{
int x = 1;
do {
[Link](x);
x++;
}while(x<=50);
}
}
• Output:
Practical No. 10 [Link]();
• Program Code: [Link]("Details of
object created using parameterized
public class pr10 {
constructor:");
private String name;
private int age;
[Link]
();
public pr10() {
}
[Link] = "John Doe";
}
[Link] = 30;
}
public pr10(String name, int age) • Output:
{
[Link] = name;
[Link] = age;
}
public void displayDetails() {
[Link]("Name: " +
name);
[Link]("Age: " +
age);
}
public static void main(String[]
args) {
pr10 defaultConstructorObj =
new pr10();
pr10 paramConstructorObj =
new pr10("Alice", 25);
[Link]("Details of
object created using default
constructor:");
[Link]
s();
Practical No. 10 class Pr10a {
public static void main(String[]
• Program Code: args) {
ComplexNumber complex1 =
class ComplexNumber {
new ComplexNumber();
private double real;
ComplexNumber complex2 =
private double imaginary;
new ComplexNumber(2.5, 3.7);
public ComplexNumber() {
ComplexNumber complex3 =
[Link] = 0.0;
new ComplexNumber(complex2);
[Link] = 0.0;
[Link]("Complex
}
number 1:");
public ComplexNumber(double
[Link]();
real, double imaginary) {
[Link]("Complex
[Link] = real;
number 2:");
[Link] = imaginary;
[Link]();
}
[Link]("Complex
public
number 3 (copy of complex number
ComplexNumber(ComplexNumber
2):");
other) {
[Link]();
[Link] = [Link];
ComplexNumber sum =
[Link] =
[Link](complex2);
[Link];
[Link]("Sum of
}
complex number 1 and complex
public ComplexNumber
number 2:");
add(ComplexNumber other) {
[Link]();
double sumReal = [Link] +
}
[Link];
}
double sumImaginary =
[Link] + [Link]; • Output:
return new
ComplexNumber(sumReal,
sumImaginary);
}
public void display() {
[Link](real + " + " +
imaginary + "i");
}
}
Practical No. 28
• Program Code:
import [Link];
import [Link];
public class pr28 extends Applet {
public void paint(Graphics g) {
[Link]("Hello, World!",
20, 20);
}
}
/*<applet code="[Link]" width =
"300" height = "300"></applet>*/
• Output:
Practical No. 28 • Output:
• Program Code:
import [Link];
import [Link];
public class pr28a extends Applet {
public void paint(Graphics g) {
int x = 10;
int y = 10;
int size = 20;
for (int i = 0; i < 10; i++) {
[Link](x, y, size, size);
x += 20;
y += 20;
size += 10;
}
}
}
/*<applet code="[Link]"
width="300" height="300">
</applet>*/
Practical No. 29 • Output:
• Program Code:
import [Link];
import [Link];
import [Link];
public class pr29 extends Applet {
public void paint(Graphics g) {
int[] xPoints = {100, 150, 200,
200, 150, 100};
int[] yPoints = {50, 100, 50, 150,
200, 150};
int nPoints = 6;
Polygon polygon = new
Polygon(xPoints, yPoints, nPoints);
[Link](polygon);
}
}
/*<applet code="[Link]"
width="300" height="300">
</applet>*/
Practical No. 29 • Output:
• Program Code:
import [Link];
import [Link];
import [Link];
public class pr29a extends Applet {
public void paint(Graphics g) {
[Link]([Link]);
[Link](100, 100, 200, 200);
[Link]([Link]);
[Link](150, 150, 30, 30);
[Link](220, 150, 30, 30);
[Link]([Link]);
[Link](190, 200, 20, 20);
[Link]([Link]);
[Link](150, 220, 100, 80,
180, 180);
}
}
/*<applet code="[Link]"
width="400" height="400">
</applet>*/
Practical No. 30 • Output:
• Program Code:
import [Link];
import [Link];
import [Link];
public class pr30 extends Applet {
public void paint(Graphics g) {
[Link]([Link]);
[Link](600,100,200,200);
[Link](700,200,200,200);
[Link](600,100,700,200);
[Link](800,100,900,200);
[Link](600,300,700,400);
[Link](800,300,900,400);
[Link]([Link]);
[Link](300,60,280,60);
[Link](300,90,430,280);
[Link](430,280,580,90);
}
}
/*<applet code="[Link]"
width="400" height="400">
</applet>*/
Practical No. 30 • Output:
• Program Code:
import [Link];
import [Link];
import [Link];
public class pr30a extends Applet {
public void paint(Graphics g) {
[Link]([Link]);
[Link](50, 50, 200, 200);
[Link]([Link]);
[Link](50, 50, 200, 200);
}
}
/*<applet code="[Link]"
width="300" height="300">
</applet>*/
Practical No. 31 & 32
• Program Code: • Output:
import [Link];
import [Link];
import [Link];
public class pr3132 {
public static void main(String[]
args) {
// Input and output file paths
String inputFile = "[Link]";
String outputFile = "[Link]";
try (FileReader reader = new
FileReader(inputFile);
FileWriter writer = new
FileWriter(outputFile)) {
int character;
// Read characters from the
input file and write them to the
output file
while ((character =
[Link]()) != -1) {
[Link](character);
}
[Link]("Characters
copied from " + inputFile + " to " +
outputFile + " successfully.");
} catch (IOException e) {
[Link]("Error: " +
[Link]());
}
}
}
Practical No. 31 & 32 • Output:
• Program Code:
import [Link];
import [Link];
public class pr3132a {
public static void main(String[]
args) {
String fileName = "[Link]";
// Name of the output file
try (FileOutputStream
outputStream = new
FileOutputStream(fileName)) {
// Data to be written to the
file (example: byte array)
byte[] data = {72, 101, 108,
108, 111, 32, 87, 111, 114, 108,
100};
// Write the bytes to the file
[Link](data);
[Link]("Bytes
written to " + fileName + "
successfully.");
} catch (IOException e) {
[Link]("Error: " +
[Link]());
}
}
}
PR 13 . Develop a program for [Link]("Length of the array:
implementation of Array in Java. " + length);
• Program Code : }
public class ArrayExample { }
public static void main(String[] args) {
// Declaring an array of integers • Output :
int[] numbers;
// Allocating memory for 5 integers
numbers = new int[5];
// Assigning values to the array
elements
numbers[0] = 10;
numbers[1] = 20;
numbers[2] = 30;
numbers[3] = 40;
numbers[4] = 50;
// Accessing and printing array
elements
[Link]("Element at index 0:
" + numbers[0]);
[Link]("Element at index 1:
" + numbers[1]);
[Link]("Element at index 2:
" + numbers[2]);
[Link]("Element at index 3:
" + numbers[3]);
[Link]("Element at index 4:
" + numbers[4]);
int length = [Link];
Exercise PR 13. Write a program to
display array elements using for each
loop . • Output :
• Program Code :
public class ForEachLoopExample {
public static void main(String[] args) {
// Declare and initialize an array of
integers
int[] numbers = {10, 20, 30, 40, 50};
// Display array elements using a for-
each loop
[Link]("Array elements:");
for (int number : numbers) {
[Link](number);
}
PR 14 . Develop a program for // Display elements of the Vector after
implementation of Vector in Java. removal
• Program Code : [Link]("\nVector elements
after removal:");
import [Link];
for (int number : numbers) {
[Link](number);
public class VectorExample {
}
public static void main(String[] args) {
}
// Create a Vector of integers
}
Vector<Integer> numbers = new
Vector<>(); • Output :
// Add elements to the Vector
[Link](10);
[Link](20);
[Link](30);
[Link](40);
[Link](50);
// Display elements of the Vector
[Link]("Vector elements:");
for (int number : numbers) {
[Link](number);
// Get the size of the Vector
int size = [Link]();
[Link]("Size of the Vector:
" + size);
// Remove an element from the Vector
[Link](30);
Exercise PR 14. Write a program to [Link]("Does the Vector
use different methods of vector class. contain 'Banana'? " +
[Link]("Banana"));
• Program Code :
import [Link];
// Remove an element by index
[Link]("Removed element
public class VectorMethodsExample { at index 2: " + [Link](2));
public static void main(String[] args) {
Vector<String> vector = new // Display elements of the Vector after
Vector<>(); removal
[Link]("Vector elements
[Link]("Apple"); after removal: " + vector);
[Link]("Banana");
[Link]("Orange"); // Remove all elements from the Vector
[Link]();
// Display elements of the Vector [Link]("Vector elements
after clearing: " + vector);
[Link]("Vector elements: "
+ vector); }
// Get the size of the Vector • Output :
[Link]("Size of the Vector:
" + [Link]());
// Check if the Vector is empty
[Link]("Is the Vector
empty? " + [Link]());
// Access an element by index
[Link]("Element at index 1:
" + [Link](1));
// Check if the Vector contains an
element
PR 17 . Develop a program for which
implements the concept of overriding in
java. • Output :
• Program Code :
// Parent class
class Animal {
// Method to make sound
public void makeSound() {
[Link]("Animal makes a
sound");
// Subclass (child class)
class Dog extends Animal {
// Overriding the makeSound method
@Override
public void makeSound() {
[Link]("Dog barks");
public class OverridingExample {
public static void main(String[] args) {
// Create an object of the subclass
Dog myDog = new Dog();
// Call the makeSound method of the
subclass
[Link]();
}
Exercise PR 17. Develop a program to • Output :
extend ‘dog’ from ‘animal’ to override
‘move()’ method using super keyword.
• Program Code :
// Parent class
class Animal {
// Method to move
public void move() {
[Link]("Animal moves");
// Subclass (child class)
class Dog extends Animal {
// Overriding the move method using
super keyword @Override
public void move() {
[Link]();
// Call the move method of the superclass
[Link]("Dog moves using
four legs");
public class OverridingWithSuperExample {
public static void main(String[] args) {
// Create an object of the subclass
Dog myDog = new Dog();
// Call the move method of the subclass
[Link]();
}
PR 18. Develop a program for public class InheritanceExample {
implementation of Single and
public static void main(String[] args) {
Multilevel inheritance.
// Single Inheritance
• Program Code :
Dog myDog = new Dog();
// Parent class [Link](); // Accessing method of
parent class
class Animal {
[Link](); // Accessing method of
// Method in the parent class child class
public void sound() {
[Link]("Animal makes a // Multilevel Inheritance
sound");
Labrador myLabrador = new Labrador();
}
[Link](); // Accessing
} method of grandparent class
[Link](); // Accessing
// Subclass inheriting from Animal class method of parent class
(Single Inheritance) [Link](); // Accessing
class Dog extends Animal { method of child class
// Method in the subclass }
public void bark() { }
[Link]("Dog barks"); • Output :
}
// Subclass inheriting from Dog class
(Multilevel Inheritance)
class Labrador extends Dog {
// Method in the subclass
public void color() {
[Link]("Labrador is black in
color");
}
Exercise PR 18. Develop a program to }
calculate the room area and volume to
public class RoomExample {
illustrate the concept of single
inheritance . public static void main(String[] args) {
• Program Code : RoomWithVolume myRoom = new
RoomWithVolume(5.0, 4.0, 3.0);
// Parent class
double area = [Link]();
class Room {
[Link]("Area of the room: "
double length; + area);
double width; double volume =
double height; [Link]();
// Constructor to initialize room [Link]("Volume of the
dimensions room: " + volume);
public Room(double length, double width, }
double height) { }
[Link] = length;
[Link] = width;
• Output :
[Link] = height;
// Method to calculate and return the
area of the room
public double calculateArea() {
return 2 * (length * width + width *
height + height * length);
class RoomWithVolume extends Room {
public RoomWithVolume(double length,
double width, double height) {
super(length, width, height); // Call the
constructor of the parent class
public double calculateVolume() {
return length * width * height;
}
PR 19. Develop a program for public class MultipleInheritanceExample {
implementation of multipe
public static void main(String[] args) {
inheritance.
// Create an object of Dog
• Program Code :
Dog myDog = new Dog();
// Interface for LivingBeing
interface LivingBeing {
// Call methods from interfaces
void breathe();
[Link]();
}
[Link]();
// Interface for Animal
// Call method specific to Dog class
interface Animal {
[Link]();
void eat();
}
}
}
// Class representing a Dog
• Output :
class Dog implements LivingBeing, Animal {
@Override
public void breathe() {
[Link]("Dog breathes");
@Override
public void eat() {
[Link]("Dog eats");
// Additional method specific to Dog class
public void bark() {
[Link]("Dog barks");
}
Exercise PR 19. Develop a program to }
find area of rectangle and circle using
}
interfaces.
• Program code :
public class AreaCalculationExample {
// Interface for Shape
public static void main(String[] args) {
interface Shape {
// Create objects of Rectangle and
double calculateArea(); Circle
}
Rectangle rectangle = new Rectangle(5,
// Class representing Rectangle 4);
class Rectangle implements Shape { Circle circle = new Circle(3);
double length;
double width; // Calculate and display area of
Rectangle
// Constructor
double rectangleArea =
public Rectangle(double length, double [Link]();
width) {
[Link]("Area of Rectangle:
[Link] = length; " + rectangleArea);
[Link] = width;
} // Calculate and display area of Circle
public double calculateArea() { double circleArea =
return length * width; [Link]();
} [Link]("Area of Circle: " +
circleArea);
}
}
class Circle implements Shape {
}
double radius;
• Output :
public Circle(double radius) {
[Link] = radius;
// Implementation of calculateArea
method for Circle
@Override
public double calculateArea() {
return [Link] * radius * radius;
PR 21 & 22. Develop a program
for implementation of
• Output :
multithreading operation.
• Program code :
class MyThread extends Thread {
public void run() {
for (int i = 0; i < 5; i++) {
[Link]("Thread: " +
[Link]().getId() + " Value: " +
i);
public class MultithreadingExample {
public static void main(String[] args) {
// Create and start two threads
MyThread thread1 = new MyThread();
MyThread thread2 = new MyThread();
[Link]();
[Link]();
}
Exercise PR 21& 22. Implement
multithreading to perform
public class MultithreadingExample {
simultaneous processes.
public static void main(String[] args) {
• Program :
// Create and start two threads for each
class Process1 extends Thread { process
public void run() {
Process1 process1 = new Process1();
for (int i = 0; i < 5; i++) { Process2 process2 = new Process2();
[Link]("Process 1 is
[Link]();
running...");
[Link]();
try {
}
[Link](1000); // Sleep for 1
second }
} catch (InterruptedException e) {
[Link](); • Output :
}
class Process2 extends Thread {
public void run() {
for (int i = 0; i < 5; i++) {
[Link]("Process 2 is
running...");
try {
[Link](1500); // Sleep for
1.5 seconds
} catch (InterruptedException e) {
[Link]();