Practical No.
1 & 2
• Program Code:
class pr1 {
public static void main(String args[]) {
System.out.println("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) {
System.out.print(x+" is the largest number.");
}
•
else if(y > x && y > z) {
•
System.out.print(y+" is the largest nmuber.");
•
}
•
else {
•
System.out.print(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) {
System.out.print(x +" is an even number.");
}
else {
System.out.print(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':
System.out.println(input+" is a vowel.");
break;
default:
System.out.println(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:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
break;
case 7:
System.out.println("Sunday");
break;
default:
System.out.println("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--) {
System.out.print(" ");
}
for (int k = 1; k <= 2 * i - 1; k++) {
System.out.print("*");
}
System.out.println();
}
}
}
• 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) {
System.out.println("The array
contains negative numbers.");
} else {
System.out.println("The array
does not contain negative
numbers.");
}
}
}
Practical No. 6 }
• Program Code:
import java.util.Scanner;
• Output:
class pr6 {
public static void main(String[]
args) {
Scanner scanner = new
Scanner(System.in);
int num1, num2;
boolean isValid = false;
do {
System.out.print("Enter an
even number: ");
num1 = scanner.nextInt();
System.out.print("Enter an
odd number: ");
num2 = scanner.nextInt();
isValid = (num1 % 2 == 0) &&
(num2 % 2 != 0);
if (!isValid) {
System.out.println("Invalid
input! Please enter one even
number and one odd number.");
}
} while (!isValid);
System.out.println("You
entered a valid pair of numbers: " +
num1 + " and " + num2);
scanner.close();
}
Practical No. 6
• Program Code:
class pr6a {
public static void main(String args[])
{
int x = 1;
do {
System.out.println(x);
x++;
}while(x<=50);
}
}
• Output:
Practical No. 10 System.out.println();
• Program Code: System.out.println("Details of
object created using parameterized
public class pr10 {
constructor:");
private String name;
private int age;
paramConstructorObj.displayDetails
();
public pr10() {
}
this.name = "John Doe";
}
this.age = 30;
}
public pr10(String name, int age) • Output:
{
this.name = name;
this.age = age;
}
public void displayDetails() {
System.out.println("Name: " +
name);
System.out.println("Age: " +
age);
}
public static void main(String[]
args) {
pr10 defaultConstructorObj =
new pr10();
pr10 paramConstructorObj =
new pr10("Alice", 25);
System.out.println("Details of
object created using default
constructor:");
defaultConstructorObj.displayDetail
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 =
this.real = 0.0;
new ComplexNumber(complex2);
this.imaginary = 0.0;
System.out.println("Complex
}
number 1:");
public ComplexNumber(double
complex1.display();
real, double imaginary) {
System.out.println("Complex
this.real = real;
number 2:");
this.imaginary = imaginary;
complex2.display();
}
System.out.println("Complex
public
number 3 (copy of complex number
ComplexNumber(ComplexNumber
2):");
other) {
complex3.display();
this.real = other.real;
ComplexNumber sum =
this.imaginary =
complex1.add(complex2);
other.imaginary;
System.out.println("Sum of
}
complex number 1 and complex
public ComplexNumber
number 2:");
add(ComplexNumber other) {
sum.display();
double sumReal = this.real +
}
other.real;
}
double sumImaginary =
this.imaginary + other.imaginary; • Output:
return new
ComplexNumber(sumReal,
sumImaginary);
}
public void display() {
System.out.println(real + " + " +
imaginary + "i");
}
}
Practical No. 28
• Program Code:
import java.applet.Applet;
import java.awt.Graphics;
public class pr28 extends Applet {
public void paint(Graphics g) {
g.drawString("Hello, World!",
20, 20);
}
}
/*<applet code="pr28.class" width =
"300" height = "300"></applet>*/
• Output:
Practical No. 28 • Output:
• Program Code:
import java.applet.Applet;
import java.awt.Graphics;
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++) {
g.drawRect(x, y, size, size);
x += 20;
y += 20;
size += 10;
}
}
}
/*<applet code="pr28a.class"
width="300" height="300">
</applet>*/
Practical No. 29 • Output:
• Program Code:
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Polygon;
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);
g.drawPolygon(polygon);
}
}
/*<applet code="pr29.class"
width="300" height="300">
</applet>*/
Practical No. 29 • Output:
• Program Code:
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
public class pr29a extends Applet {
public void paint(Graphics g) {
g.setColor(Color.YELLOW);
g.fillOval(100, 100, 200, 200);
g.setColor(Color.BLACK);
g.fillOval(150, 150, 30, 30);
g.fillOval(220, 150, 30, 30);
g.setColor(Color.RED);
g.fillOval(190, 200, 20, 20);
g.setColor(Color.BLACK);
g.drawArc(150, 220, 100, 80,
180, 180);
}
}
/*<applet code="pr29a.class"
width="400" height="400">
</applet>*/
Practical No. 30 • Output:
• Program Code:
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
public class pr30 extends Applet {
public void paint(Graphics g) {
g.setColor(Color.BLUE);
g.drawRect(600,100,200,200);
g.drawRect(700,200,200,200);
g.drawLine(600,100,700,200);
g.drawLine(800,100,900,200);
g.drawLine(600,300,700,400);
g.drawLine(800,300,900,400);
g.setColor(Color.RED);
g.drawOval(300,60,280,60);
g.drawLine(300,90,430,280);
g.drawLine(430,280,580,90);
}
}
/*<applet code="pr30.class"
width="400" height="400">
</applet>*/
Practical No. 30 • Output:
• Program Code:
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
public class pr30a extends Applet {
public void paint(Graphics g) {
g.setColor(Color.BLUE);
g.fillRect(50, 50, 200, 200);
g.setColor(Color.RED);
g.fillOval(50, 50, 200, 200);
}
}
/*<applet code="pr30a.class"
width="300" height="300">
</applet>*/
Practical No. 31 & 32
• Program Code: • Output:
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class pr3132 {
public static void main(String[]
args) {
// Input and output file paths
String inputFile = "input.txt";
String outputFile = "output.txt";
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 =
reader.read()) != -1) {
writer.write(character);
}
System.out.println("Characters
copied from " + inputFile + " to " +
outputFile + " successfully.");
} catch (IOException e) {
System.err.println("Error: " +
e.getMessage());
}
}
}
Practical No. 31 & 32 • Output:
• Program Code:
import java.io.FileOutputStream;
import java.io.IOException;
public class pr3132a {
public static void main(String[]
args) {
String fileName = "output.bin";
// 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
outputStream.write(data);
System.out.println("Bytes
written to " + fileName + "
successfully.");
} catch (IOException e) {
System.err.println("Error: " +
e.getMessage());
}
}
}
PR 13 . Develop a program for System.out.println("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
System.out.println("Element at index 0:
" + numbers[0]);
System.out.println("Element at index 1:
" + numbers[1]);
System.out.println("Element at index 2:
" + numbers[2]);
System.out.println("Element at index 3:
" + numbers[3]);
System.out.println("Element at index 4:
" + numbers[4]);
int length = numbers.length;
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
System.out.println("Array elements:");
for (int number : numbers) {
System.out.println(number);
}
PR 14 . Develop a program for // Display elements of the Vector after
implementation of Vector in Java. removal
• Program Code : System.out.println("\nVector elements
after removal:");
import java.util.Vector;
for (int number : numbers) {
System.out.println(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
numbers.add(10);
numbers.add(20);
numbers.add(30);
numbers.add(40);
numbers.add(50);
// Display elements of the Vector
System.out.println("Vector elements:");
for (int number : numbers) {
System.out.println(number);
// Get the size of the Vector
int size = numbers.size();
System.out.println("Size of the Vector:
" + size);
// Remove an element from the Vector
numbers.removeElement(30);
Exercise PR 14. Write a program to System.out.println("Does the Vector
use different methods of vector class. contain 'Banana'? " +
vector.contains("Banana"));
• Program Code :
import java.util.Vector;
// Remove an element by index
System.out.println("Removed element
public class VectorMethodsExample { at index 2: " + vector.remove(2));
public static void main(String[] args) {
Vector<String> vector = new // Display elements of the Vector after
Vector<>(); removal
System.out.println("Vector elements
vector.add("Apple"); after removal: " + vector);
vector.add("Banana");
vector.add("Orange"); // Remove all elements from the Vector
vector.clear();
// Display elements of the Vector System.out.println("Vector elements
after clearing: " + vector);
System.out.println("Vector elements: "
+ vector); }
// Get the size of the Vector • Output :
System.out.println("Size of the Vector:
" + vector.size());
// Check if the Vector is empty
System.out.println("Is the Vector
empty? " + vector.isEmpty());
// Access an element by index
System.out.println("Element at index 1:
" + vector.get(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() {
System.out.println("Animal makes a
sound");
// Subclass (child class)
class Dog extends Animal {
// Overriding the makeSound method
@Override
public void makeSound() {
System.out.println("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
myDog.makeSound();
}
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() {
System.out.println("Animal moves");
// Subclass (child class)
class Dog extends Animal {
// Overriding the move method using
super keyword @Override
public void move() {
super.move();
// Call the move method of the superclass
System.out.println("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
myDog.move();
}
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 myDog.sound(); // Accessing method of
parent class
class Animal {
myDog.bark(); // Accessing method of
// Method in the parent class child class
public void sound() {
System.out.println("Animal makes a // Multilevel Inheritance
sound");
Labrador myLabrador = new Labrador();
}
myLabrador.sound(); // Accessing
} method of grandparent class
myLabrador.bark(); // Accessing
// Subclass inheriting from Animal class method of parent class
(Single Inheritance) myLabrador.color(); // Accessing
class Dog extends Animal { method of child class
// Method in the subclass }
public void bark() { }
System.out.println("Dog barks"); • Output :
}
// Subclass inheriting from Dog class
(Multilevel Inheritance)
class Labrador extends Dog {
// Method in the subclass
public void color() {
System.out.println("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 = myRoom.calculateArea();
class Room {
System.out.println("Area of the room: "
double length; + area);
double width; double volume =
double height; myRoom.calculateVolume();
// Constructor to initialize room System.out.println("Volume of the
dimensions room: " + volume);
public Room(double length, double width, }
double height) { }
this.length = length;
this.width = width;
• Output :
this.height = 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();
myDog.breathe();
}
myDog.eat();
// Interface for Animal
// Call method specific to Dog class
interface Animal {
myDog.bark();
void eat();
}
}
}
// Class representing a Dog
• Output :
class Dog implements LivingBeing, Animal {
@Override
public void breathe() {
System.out.println("Dog breathes");
@Override
public void eat() {
System.out.println("Dog eats");
// Additional method specific to Dog class
public void bark() {
System.out.println("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 rectangle.calculateArea();
width) {
System.out.println("Area of Rectangle:
this.length = length; " + rectangleArea);
this.width = width;
} // Calculate and display area of Circle
public double calculateArea() { double circleArea =
return length * width; circle.calculateArea();
} System.out.println("Area of Circle: " +
circleArea);
}
}
class Circle implements Shape {
}
double radius;
• Output :
public Circle(double radius) {
this.radius = radius;
// Implementation of calculateArea
method for Circle
@Override
public double calculateArea() {
return Math.PI * 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++) {
System.out.println("Thread: " +
Thread.currentThread().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();
thread1.start();
thread2.start();
}
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();
System.out.println("Process 1 is
process1.start();
running...");
process2.start();
try {
}
Thread.sleep(1000); // Sleep for 1
second }
} catch (InterruptedException e) {
e.printStackTrace(); • Output :
}
class Process2 extends Thread {
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println("Process 2 is
running...");
try {
Thread.sleep(1500); // Sleep for
1.5 seconds
} catch (InterruptedException e) {
e.printStackTrace();