Practical V
Practical V
PRACTICAL-: 1(B)
AIM-: Write a program in Java to find maximum of three
Numbers using conditional operator.
CODE-:
public class MaxOfThree {
public static void main(String[] args) {
int a = 10, b = 20, c = 30;
236470307020 PAGE:
AOOP 4340701
OUTPUT:-
236470307020 PAGE:
AOOP 4340701
PRACTICAL-: 1(C)
AIM-: Write a program in Java to reverse the digits of a
Number using while loop
CODE:-
import java.util.Scanner;
236470307020 PAGE:
AOOP 4340701
OUTPUT-:
236470307020 PAGE:
AOOP 4340701
PRACTICAL-: 2(A)
AIM-: Write a program in Java to add two 3*3 matrices.
CODE-:
public class AddMatrices {
public static void main(String[] args) {
int[][] matrixA = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
int[][] matrixB = {
{9, 8, 7},
{6, 5, 4},
{3, 2, 1}
};
236470307020 PAGE:
AOOP 4340701
236470307020 PAGE:
AOOP 4340701
OUTPUT-:
236470307020 PAGE:
AOOP 4340701
PRACTICAL-: 2(B)
AIM-: Write a program in Java to generate first N prime
numbers.
CODE-:
import java.util.Scanner;
236470307020 PAGE:
AOOP 4340701
236470307020 PAGE:
AOOP 4340701
OUTPUT-:
236470307020 PAGE:
AOOP 4340701
PRACTICAL-: 3(A)
AIM-: Write a program in Java which has a class Student having two instance
variables enrollment No and name. Create 3 objects of Student class main
method and display student’s name.
CODE-:
class Student {
// Instance variables
private String enrollmentNo;
private String name;
// Constructor
public Student(String enrollmentNo, String name) {
this.enrollmentNo = enrollmentNo;
this.name = name;
}
236470307020 PAGE:
AOOP 4340701
236470307020 PAGE:
AOOP 4340701
OUTPUT-:
236470307020 PAGE:
AOOP 4340701
PRACTICAL-: 3(B)
AIM-: Write a program in Java which has a class
Rectangle having two instance variables height and
weight. Initialize the class using constructor.
CODE-:
import java.util.Scanner;
236470307020 PAGE:
AOOP 4340701
236470307020 PAGE:
AOOP 4340701
OUTPUT-:
236470307020 PAGE:
AOOP 4340701
PRACTICAL-: 3(B)
AIM-: Write a program in Java demonstrate the use of “this”
keyword.
CODE-:
class ThisKeywordDemo {
int a;
int b;
ThisKeywordDemo(int a, int b) {
this.a = a;
this.b = b;
}
void display() {
System.out.println("Value of a: " + this.a);
System.out.println("Value of b: " + this.b);
}
236470307020 PAGE:
AOOP 4340701
OUTPUT-:
236470307020 PAGE:
AOOP 4340701
PRACTICAL-: 3(C)
AIM-: Write a program in Java to demonstrate the use of “static”
keyword.
CODE-:
class StaticKeywordDemo {
static int count = 0;
StaticKeywordDemo() {
count++;
}
236470307020 PAGE:
AOOP 4340701
OUTPUT-:
236470307020 PAGE:
AOOP 4340701
PRACTICAL-: 3(D)
AIM-:Write a program in Java to demonstrate the use of “static”
keyword.
CODE-:
public class StaticDemo {
// Static variable
static int staticCounter = 0;
// Instance variable
int instanceCounter = 0;
// Static block
static {
System.out.println("Static block is executed.");
}
// Static method
static void displayStaticMessage() {
System.out.println("This is a static method.");
// Accessing static variable inside static method
System.out.println("Static counter: " + staticCounter);
}
// Instance method
void displayInstanceMessage() {
236470307020 PAGE:
AOOP 4340701
// Constructor
public StaticDemo() {
staticCounter++; // Incrementing static variable
instanceCounter++; // Incrementing instance variable
System.out.println("Instance created. Static Counter: " + staticCounter + ",
Instance Counter: " + instanceCounter);
}
// Creating instances
StaticDemo obj1 = new StaticDemo();
StaticDemo obj2 = new StaticDemo();
236470307020 PAGE:
AOOP 4340701
236470307020 PAGE:
AOOP 4340701
PRACTICAL-: 4(A)
AIM-: Write a program in Java to demonstrate the use of "final"
keyword.
CODE-:
class FinalDemo {
final int finalVariable = 100;
236470307020 PAGE:
AOOP 4340701
OUTPUT-:
236470307020 PAGE:
AOOP 4340701
PRACTICAL-: 4(B)
AIM-: Write a program in Java which has a class Shape having 2
overloaded methods area(float radius) and area(float length, float
width). Display the area of circle and rectangle using overloaded
methods.
CODE-:
class Shape {
236470307020 PAGE:
AOOP 4340701
236470307020 PAGE:
AOOP 4340701
OUTPUT-:
236470307020 PAGE:
AOOP 4340701
PRACTICAL-: 4(C)
AIM-: Write a program in Java to demonstrate the constructor
overloading.
CODE-:
class Car {
String model;
int year;
String color;
236470307020 PAGE:
AOOP 4340701
void displayDetails() {
System.out.println("Model: " + model + ", Year: " + year + ",
Color: " + color);
}
}
236470307020 PAGE:
AOOP 4340701
car1.displayDetails();
car2.displayDetails();
car3.displayDetails();
car4.displayDetails();
}
}
236470307020 PAGE:
AOOP 4340701
OUTPUT-:
Write a java program to demonstrate use of “String” class methods
236470307020 PAGE:
AOOP 4340701
PRACTICAL-: 4(D)
AIM-: Write a java program to demonstrate use of “String” class methods
CODE-:
public class StringMethodsDemo {
public static void main(String[] args) {
String str1 = "Hello";
String str2 = "World";
String str3 = " Java Programming ";
// 1. Concatenation
String concatResult = str1.concat(str2);
System.out.println("Concatenation: " + concatResult);
// 4. Comparison
System.out.println("str1 equals str2: " + str1.equals(str2));
System.out.println("str1 equals 'Hello': " + str1.equals("Hello"));
// 5. Substring
System.out.println("Substring of str3 (from index 3 to 7): " +
str3.substring(3, 7));
236470307020 PAGE:
AOOP 4340701
// 6. Trim spaces
System.out.println("Trimmed str3: '" + str3.trim() + "'");
// 8. Replace characters
System.out.println("Replace 'l' with 'x' in str1: " + str1.replace('l', 'x'));
236470307020 PAGE:
AOOP 4340701
OUTPUT-:
236470307020 PAGE:
AOOP 4340701
PRACTICAL-: 5(A)
AIM-: Write a program in Java to demonstrate single inheritance
CODE-:
// Parent class
class Animal {
void eat() {
System.out.println("This animal eats food.");
}
}
236470307020 PAGE:
AOOP 4340701
OUTPUT-:
236470307020 PAGE:
AOOP 4340701
PRACTICAL-: 5(B)
AIM-: Write a program in Java to demonstrate multilevel inheritance
CODE-:
// Parent class
class Animal {
void eat() {
System.out.println("This animal eats food.");
}
}
236470307020 PAGE:
AOOP 4340701
236470307020 PAGE:
AOOP 4340701
OUTPUT-:
236470307020 PAGE:
AOOP 4340701
PRACTICAL-: 5(C)
AIM-: Write a program in Java to demonstrate hierarchical
inheritance.
// Parent class
class Animal {
void eat() {
System.out.println("This animal eats food.");
}
}
236470307020 PAGE:
AOOP 4340701
236470307020 PAGE:
AOOP 4340701
OUTPUT-:
236470307020 PAGE:
AOOP 4340701
PRACTICAL-: 5(D)
AIM-: Write a program in Java to demonstrate method overriding.
CODE-:
// Parent class
class Animal {
void sound() {
System.out.println("This animal makes a sound.");
}
}
// Child class overriding the sound() method
class Dog extends Animal {
@Override
void sound() {
System.out.println("The dog barks.");
}
}
public class Main {
public static void main(String[] args) {
Animal myAnimal = new Animal(); // Parent class object
Dog myDog = new Dog(); // Child class object
236470307020 PAGE:
AOOP 4340701
}
}
OUTPUT-:
236470307020 PAGE:
AOOP 4340701
PRACTICAL-: 5(E)
AIM-: Write a program in Java which has a class Car having two
instance variables topSpeed and name. Override toString() method in
Car class. Create 3 instances of Car class and print the instances.
CODE-:
class Car {
int topSpeed;
String name;
236470307020 PAGE:
AOOP 4340701
236470307020 PAGE:
AOOP 4340701
OUTPUT-:
236470307020 PAGE:
AOOP 4340701
PRACTICAL-: 6(A)
AIM-: Write a program in Java to implement multiple inheritance
using interfaces.
CODE-:
// First interface
interface Drivable {
void drive();
}
// Second interface
interface Flyable {
void fly();
}
236470307020 PAGE:
AOOP 4340701
236470307020 PAGE:
AOOP 4340701
OUTPUT-:
236470307020 PAGE:
AOOP 4340701
PRACTICAL-: 6(B)
AIM-: Write a program in Java which has an abstract class Shape
having three subclasses: Triangle, Rectangle, and Circle
CODE-:
// Abstract class
abstract class Shape {
abstract double area(); // Abstract method to be implemented by
subclasses
}
// Triangle class
class Triangle extends Shape {
double base, height;
@Override
double area() {
return 0.5 * base * height; // Area formula for triangle
}
}
236470307020 PAGE:
AOOP 4340701
// Rectangle class
class Rectangle extends Shape {
double length, width;
@Override
double area() {
return length * width; // Area formula for rectangle
}
}
// Circle class
class Circle extends Shape {
double radius;
Circle(double radius) {
this.radius = radius;
}
236470307020 PAGE:
AOOP 4340701
@Override
double area() {
return Math.PI * radius * radius; // Area formula for circle
}
}
236470307020 PAGE:
AOOP 4340701
OUTPUT-:
236470307020 PAGE:
AOOP 4340701
PRACTICAL-: 6(C)
AIM-: Define method area() in the abstract class Shape and override
area() method tocalculate the area.
CODE-:
// Abstract class with a concrete area() method
abstract class Shape {
// Concrete method to display a generic message
void display() {
System.out.println("This is a shape.");
}
// Triangle class
class Triangle extends Shape {
double base, height;
236470307020 PAGE:
AOOP 4340701
@Override
double area() {
return 0.5 * base * height; // Area formula for triangle
}
}
// Rectangle class
class Rectangle extends Shape {
double length, width;
@Override
double area() {
return length * width; // Area formula for rectangle
}
}
// Circle class
236470307020 PAGE:
AOOP 4340701
Circle(double radius) {
this.radius = radius;
}
@Override
double area() {
return Math.PI * radius * radius; // Area formula for circle
}
}
triangle.display();
System.out.println("Area of Triangle: " + triangle.area());
rectangle.display();
System.out.println("Area of Rectangle: " + rectangle.area());
236470307020 PAGE:
AOOP 4340701
circle.display();
System.out.println("Area of Circle: " + circle.area());
}
}
236470307020 PAGE:
AOOP 4340701
OUTPUT-:
236470307020 PAGE:
AOOP 4340701
PRACTICAL-: 6(D)
AIM-: Write a program in Java to demonstrate use of final Class.
CODE-:
// Final class
final class Vehicle {
String brand;
Vehicle(String brand) {
this.brand = brand;
}
void display() {
System.out.println("Brand: " + brand);
}
}
236470307020 PAGE:
AOOP 4340701
vehicle.display();
}
}
OUTPUT-:
236470307020 PAGE:
AOOP 4340701
PRACTICAL-: 6(E)
AIM-: Write a program in Java to demonstrate use of package
CODE-:
Step 1: Create a Package
1. Create a directory for the package:
o Let's call the package mypackage.
o Inside this directory, create a file named Greeting.java.
package mypackage;
public class Greeting {
public void sayHello() {
System.out.println("Hello from the package!");
} }
✅ Step 2: Use the Package in Another Class
2. Create another file in the parent directory (outside mypackage):
o Name it Main.java.
import mypackage.Greeting; // Importing the package
236470307020 PAGE:
AOOP 4340701
PRACTICAL-: 7(A)
AIM-: Write a program in Java to develop user defined exception
for'Divide by Zero' error
CODE-:
class DivideByZeroException extends Exception {
public DivideByZeroException(String message) {
super(message);
}
}
236470307020 PAGE:
AOOP 4340701
}
OUTPUT-:
236470307020 PAGE:
AOOP 4340701
PRACTICAL-: 7(B)
AIM-: Write a program in Java to develop Banking Application in
which user deposits the amount Rs 25000 and then start
withdrawing of Rs 20000, Rs 4000 and it throws exception "Not
Sufficient Fund" when user withdraws Rs. 2000 there after.
CODE-:
// Custom exception for insufficient funds
class InsufficientFundsException extends Exception {
public InsufficientFundsException(String message) {
super(message);
}
}
236470307020 PAGE:
AOOP 4340701
try {
account.deposit(0); // Deposit Rs 0 to start with the initialized balance
account.withdraw(20000); // First withdrawal of Rs 20000
account.withdraw(4000); // Second withdrawal of Rs 4000
account.withdraw(2000); // Third withdrawal of Rs 2000 (should throw
an exception)
} catch (InsufficientFundsException e) {
System.out.println(e.getMessage()); // Catch and print the exception
message
}
}
236470307020 PAGE:
AOOP 4340701
}
CODE-:
236470307020 PAGE:
AOOP 4340701
PRACTICAL-: 8(A)
AIM-: Write a program that executes two threads. One thread displays “Thread1” every 1000
milliseconds, and the other displays “Thread2” every 2000 milliseconds. Create the threads by
extending the Thread class
CODE-:
// First thread that prints "Thread1" every 1000 milliseconds
class Thread1 extends Thread {
public void run() {
try {
while (true) {
System.out.println("Thread1");
Thread.sleep(1000); // Sleep for 1000 milliseconds
}
} catch (InterruptedException e) {
System.out.println(e);
}
}
}
236470307020 PAGE:
AOOP 4340701
236470307020 PAGE:
AOOP 4340701
OUTPUT-:
236470307020 PAGE:
AOOP 4340701
PRACTICAL-: 8(B)
AIM-: Write a program that executes two threads. One thread will print the even numbers and
another thread will print odd numbers from 1 to 200.
CODE-:
// Thread to print even numbers
class EvenThread extends Thread {
public void run() {
for (int i = 2; i <= 200; i += 2) {
System.out.println("Even: " + i);
try {
Thread.sleep(100); // Optional sleep to make the output
readable
} catch (InterruptedException e) {
System.out.println(e);
}
}
}
}
236470307020 PAGE:
AOOP 4340701
236470307020 PAGE:
AOOP 4340701
OUTPUT-:
236470307020 PAGE:
AOOP 4340701
PRACTICAL-: 9(A)
AIM-: Write a program in Java to perform read and write operations on a Text file
CODE-:
import java.io.*;
236470307020 PAGE:
AOOP 4340701
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line); // Print each line from the file
}
} catch (IOException e) {
System.out.println("An error occurred while reading from the
file.");
e.printStackTrace();
}
}
236470307020 PAGE:
AOOP 4340701
OUTPUT-:
236470307020 PAGE:
AOOP 4340701
PRACTICAL-: 9(B)
AIM-: ) Write a program in Java to demonstrate use of List. 1) Create ArrayList and add weekdays
(in string form) 2) Create LinkedList and add months (in string form) Display
CODE-:
import java.util.*;
236470307020 PAGE:
AOOP 4340701
months.add("May");
months.add("June");
months.add("July");
months.add("August");
months.add("September");
months.add("October");
months.add("November");
months.add("December");
236470307020 PAGE:
AOOP 4340701
OUTPUT-:
236470307020 PAGE:
AOOP 4340701
PRACTICAL-: 10(A)
AIM-: Write a program in Java to create a new HashSet, add colors(in string form) and iterate
through all elements using for-each loop to display the collection
CODE-:
import java.util.HashSet;
236470307020 PAGE:
AOOP 4340701
}
}
OUTPUT-:
236470307020 PAGE:
AOOP 4340701
PRACTICAL-: 10(B)
AIM-: Write a Java program to create a new HashMap, add 5 students’data (enrolment no and
name). retrieve and display the student’s name from HashMap using enrolment no.
CODE-:
import java.util.HashMap;
236470307020 PAGE:
AOOP 4340701
if (students.containsKey(enrolmentNo)) {
System.out.println("Student Name for Enrolment No " +
enrolmentNo + ": " + students.get(enrolmentNo));
} else {
System.out.println("Student not found with Enrolment No " +
enrolmentNo);
}
}
}
236470307020 PAGE:
AOOP 4340701
OUTPUT-:
236470307020 PAGE: