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() {
[Link]("Bat makes squeaking sound");
}
public void fly() {
[Link]("Bat can fly using wings");
}
}
public class MultipleInheritanceDemo {
public static void main(String[] args) {
Bat bat = new Bat();
[Link]();
[Link]();
}
}
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() {
[Link]("This animal eats food");
}
}
class Dog extends Animal {
void bark() {
[Link]("Dog barks");
}
}
public class SingleInheritanceDemo {
public static void main(String[] args) {
Dog dog = new Dog();
[Link](); // inherited method
[Link](); // own method
}
}
Output:
This animal eats food
Dog barks
2. Multilevel Inheritance
class Animal {
void eat() {
[Link]("Animal eats");
}
}
class Dog extends Animal {
void bark() {
[Link]("Dog barks");
}
}
class Puppy extends Dog {
void weep() {
[Link]("Puppy weeps");
}
}
public class MultilevelInheritanceDemo {
public static void main(String[] args) {
Puppy puppy = new Puppy();
[Link](); // from Animal
[Link](); // from Dog
[Link](); // 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 [Link];
public class NegativeAmountDemo {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter an amount: ");
double amount = [Link]();
try {
if (amount < 0) {
throw new NegativeAmountException("Amount cannot be
negative.");
} else {
[Link]("Amount entered: ₹" + amount);
}
} catch (NegativeAmountException e) {
[Link]("Exception: " + [Link]());
}
}
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) {
[Link]("Even: " + i);
try {
[Link](2000); // 2 seconds
} catch (InterruptedException e) {
[Link](e);
}
}
}
}
class OddThread extends Thread {
public void run() {
for (int i = 1; i < 10; i += 2) {
[Link]("Odd: " + i);
try {
[Link](5000); // 5 seconds
} catch (InterruptedException e) {
[Link](e);
}
}
}
}
public class EvenOddThreadDemo {
public static void main(String[] args) {
EvenThread even = new EvenThread();
OddThread odd = new OddThread();
[Link]();
[Link]();
}
}
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/
└── [Link]
[Link]
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++)
[Link][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] = [Link][i][j] + [Link][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] = [Link][i][j] - [Link][i][j];
return new Matrix(result);
}
public void display() {
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++)
[Link](mat[i][j] + " ");
[Link]();
}
}
}
Main
import [Link];
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);
[Link]("Matrix 1:");
[Link]();
[Link]("\nMatrix 2:");
[Link]();
Matrix sum = [Link](m2);
[Link]("\nSum of matrices:");
[Link]();
Matrix diff = [Link](m2);
[Link]("\nDifference of matrices:");
[Link]();
}
}
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 [Link].*;
import [Link];
public class InputMethodsDemo {
public static void main(String[] args) {
// 1. Command Line Arguments
if ([Link] > 0) {
[Link]("1. Command Line Argument:");
[Link]("Hello, " + args[0]);
} else {
[Link]("1. Command Line Argument:");
[Link]("No command-line argument provided.");
}
try {
// 2. DataInputStream
[Link]("\n2. DataInputStream:");
DataInputStream dis = new DataInputStream([Link]);
[Link]("Enter your age (DataInputStream): ");
String ageStr = [Link](); // Note: readLine() is deprecated but works
int age = [Link](ageStr);
[Link]("Your age is: " + age);
// 3. BufferedReader
[Link]("\n3. BufferedReader:");
BufferedReader br = new BufferedReader(new InputStreamReader([Link]));
[Link]("Enter your city (BufferedReader): ");
String city = [Link]();
[Link]("Your city is: " + city);
// 4. Scanner
[Link]("\n4. Scanner:");
Scanner sc = new Scanner([Link]);
[Link]("Enter your favorite number (Scanner): ");
int favNum = [Link]();
[Link]("Your favorite number is: " + favNum);
// 5. Console
[Link]("\n5. Console:");
Console console = [Link]();
if (console != null) {
String name = [Link]("Enter your name (Console): ");
[Link]("Your name is: " + name);
} else {
[Link]("Console is not available (usually in IDEs).");
}
} catch (Exception e) {
[Link]("Error: " + [Link]());
}
}
}
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