Experiment – 4a
Create a Java programs using inheritance and polymorphism
(a) "Java does not support multiple inheritance but we can achieve it by
interface' Write a program to justify the above statement.
Multiple Inheritance Using Interface
interface Animal {
void sound();
}
interface Bird {
void fly();
}
class Bat implements Animal, Bird {
public void sound() {
System.out.println("Bat makes squeaking sound");
}
public void fly() {
System.out.println("Bat can fly using wings");
}
}
public class MultipleInheritanceDemo {
public static void main(String[] args) {
Bat bat = new Bat();
bat.sound();
bat.fly();
}
}
Experiment – 4a(Output)
Output:
Bat makes squeaking sound
Bat can fly using wings
Experiment – 4b
(b) Write a program in java to implement the following types of
inheritance: " Single Inheritance " Multilevel Inheritance
1. Single Inheritance
class Animal {
void eat() {
System.out.println("This animal eats food");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Dog barks");
}
}
public class SingleInheritanceDemo {
public static void main(String[] args) {
Dog dog = new Dog();
dog.eat(); // inherited method
dog.bark(); // own method
}
}
Output:
This animal eats food
Dog barks
2. Multilevel Inheritance
class Animal {
void eat() {
System.out.println("Animal eats");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Dog barks");
}
}
class Puppy extends Dog {
void weep() {
System.out.println("Puppy weeps");
}
}
public class MultilevelInheritanceDemo {
public static void main(String[] args) {
Puppy puppy = new Puppy();
puppy.eat(); // from Animal
puppy.bark(); // from Dog
puppy.weep(); // from Puppy
}
}
Output:
Animal eats
Dog barks
Puppy weeps
Experiment – 5a
Implement error-handling techniques using exception handling and
multithreading.
(a) Write a Java program to implement user defined exception handling for
negative amount entered
class NegativeAmountException extends Exception {
public NegativeAmountException(String message) {
super(message);
}
}
import java.util.Scanner;
public class NegativeAmountDemo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter an amount: ");
double amount = sc.nextDouble();
try {
if (amount < 0) {
throw new NegativeAmountException("Amount cannot be
negative.");
} else {
System.out.println("Amount entered: ₹" + amount);
}
} catch (NegativeAmountException e) {
System.out.println("Exception: " + e.getMessage());
}
}
Experiment – 5a(Output)
Enter an amount: -500
Exception: Amount cannot be negative
Experiment – 5b
(b) Write a program in java which creates two threads, "Even" thread and
"Odd" thread and print the even no using Even Thread after every |two
seconds and odd no using Odd Thread after every five second.
class EvenThread extends Thread {
public void run() {
for (int i = 0; i <= 10; i += 2) {
System.out.println("Even: " + i);
try {
Thread.sleep(2000); // 2 seconds
} catch (InterruptedException e) {
System.out.println(e);
}
}
}
}
class OddThread extends Thread {
public void run() {
for (int i = 1; i < 10; i += 2) {
System.out.println("Odd: " + i);
try {
Thread.sleep(5000); // 5 seconds
} catch (InterruptedException e) {
System.out.println(e);
}
}
}
}
public class EvenOddThreadDemo {
public static void main(String[] args) {
EvenThread even = new EvenThread();
OddThread odd = new OddThread();
even.start();
odd.start();
}
}
Experiment – 5b(Output)
Even: 0
Odd: 1
Even: 2
Even: 4
Odd: 3
Even: 6
Even: 8
Odd: 5
Even: 10
Odd: 7
Odd: 9
Experiment – 6a
Create java program with the use of java packages
(a) Create a package named "Mathematics" and add a class "Matrix"
with methods to add and subtract matrices (2x2). Write a Java progrm
Importing the Mathematics package and use the classes defined in it
Package
Mathematics/
└── Matrix.java
MainApp.java
package Mathematics;
public class Matrix {
private int[][] mat = new int[2][2];
public Matrix(int[][] values) {
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++)
this.mat[i][j] = values[i][j];
}
public Matrix add(Matrix other) {
int[][] result = new int[2][2];
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++)
result[i][j] = this.mat[i][j] + other.mat[i][j];
return new Matrix(result);
}
public Matrix subtract(Matrix other) {
int[][] result = new int[2][2];
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++)
result[i][j] = this.mat[i][j] - other.mat[i][j];
return new Matrix(result);
}
public void display() {
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++)
System.out.print(mat[i][j] + " ");
System.out.println();
}
}
}
Main
import Mathematics.Matrix;
public class MainApp {
public static void main(String[] args) {
int[][] data1 = { {1, 2}, {3, 4} };
int[][] data2 = { {5, 6}, {7, 8} };
Matrix m1 = new Matrix(data1);
Matrix m2 = new Matrix(data2);
System.out.println("Matrix 1:");
m1.display();
System.out.println("\nMatrix 2:");
m2.display();
Matrix sum = m1.add(m2);
System.out.println("\nSum of matrices:");
sum.display();
Matrix diff = m1.subtract(m2);
System.out.println("\nDifference of matrices:");
diff.display();
}
}
Output :
Matrix 1:
12
34
Matrix 2:
56
78
Sum of matrices:
68
10 12
Difference of matrices:
-4 -4
-4 -4
Experiment – 7a
Construct java program using Java /O package (a) Write a program in java to
take input from user by using all the following methods: Command Line
Arguments " DatalnputStream Class " BufferedReader Class Scanner Class "
Console Class
import java.io.*;
import java.util.Scanner;
public class InputMethodsDemo {
public static void main(String[] args) {
// 1. Command Line Arguments
if (args.length > 0) {
System.out.println("1. Command Line Argument:");
System.out.println("Hello, " + args[0]);
} else {
System.out.println("1. Command Line Argument:");
System.out.println("No command-line argument provided.");
}
try {
// 2. DataInputStream
System.out.println("\n2. DataInputStream:");
DataInputStream dis = new DataInputStream(System.in);
System.out.print("Enter your age (DataInputStream): ");
String ageStr = dis.readLine(); // Note: readLine() is deprecated but works
int age = Integer.parseInt(ageStr);
System.out.println("Your age is: " + age);
// 3. BufferedReader
System.out.println("\n3. BufferedReader:");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter your city (BufferedReader): ");
String city = br.readLine();
System.out.println("Your city is: " + city);
// 4. Scanner
System.out.println("\n4. Scanner:");
Scanner sc = new Scanner(System.in);
System.out.print("Enter your favorite number (Scanner): ");
int favNum = sc.nextInt();
System.out.println("Your favorite number is: " + favNum);
// 5. Console
System.out.println("\n5. Console:");
Console console = System.console();
if (console != null) {
String name = console.readLine("Enter your name (Console): ");
System.out.println("Your name is: " + name);
} else {
System.out.println("Console is not available (usually in IDEs).");
}
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
}
}
Output :
1. Command Line Argument:
Hello, User
2. DataInputStream:
Enter your age (DataInputStream): 20
Your age is: 20
3. BufferedReader:
Enter your city (BufferedReader): Delhi
Your city is: Delhi
4. Scanner:
Enter your favorite number (Scanner): 7
Your favorite number is: 7
5. Console:
Enter your name (Console): User
Your name is: User