Sample Experiments-2
Sample Experiments-2
A). Write a JAVA program to display default values of all primitive data
type of java.
Expected output:
These are the basic building blocks for handling data in Java
EXERCISE -1
import java.util.Scanner;
scanner.close();
}
}
Expected output:
Enter coefficient a: 2
Enter coefficient b: 4
Enter coefficient c: 0
Discriminant (D) = 16.0
The equation has two distinct real roots:
Root 1 = 0.0
Root 2 = -2.0
Note
The discriminant helps you figure out how many solutions a quadratic equation
has and what kind they are. For an equation like:
Ax2 + bx +c = 0
The discriminant is:
Δ = b2 – 4ac
What It Means:
If Δ > 0: There are two different real solutions.
If Δ = 0: There is one real solution.
If Δ < 0: There are no real solutions (solutions are complex).
It’s a quick way to know the type and number of solutions without solving the
whole equation!
EXERCISE -2
import java.util.Arrays;
import java.util.Scanner;
if (array[mid] == target) {
return mid; // Return the index of the target
}
scanner.close();
}
}
Expected output:
Case i:
Enter the number of elements in the array: 6
Enter the elements of the array:
123456
Enter the target element to search for: 8
Sorted array: [1, 2, 3, 4, 5, 6]
Element not found in the array.
Case ii:
import java.util.Scanner;
// Mark as swapped
swapped = true;
}
}
scanner.close();
}
}
Expected output:
import java.util.Scanner;
scanner.close();
}
}
Expected output:
Expected output:
Name: Alice
Age: 25
EXERCISE -3
System.out.println("\nRectangle 2:");
rect2.display();
System.out.println("\nRectangle 3:");
rect3.display();
}
}
Expected output:
Rectangle 1:
Length: 1.0, Width: 1.0
Area: 1.0
Rectangle 2:
Length: 5.0, Width: 5.0
Area: 25.0
Rectangle 3:
Length: 4.0, Width: 6.0
Area: 24.0
EXERCISE -4
// Parent class
class Animal {
// Method in the parent class
void eat() {
System.out.println("The animal eats food.");
}
}
// Calling methods from both the parent class and child class
myDog.eat(); // Method from the Animal class
myDog.bark(); // Method from the Dog class
}
}
Expected output:
// Base class
class Animals {
// Method in the base class
void eat() {
System.out.println("The animal eats.");
}
}
Expected output:
Expected output:
Animal constructor called
Dog constructor called
Animal makes a sound
Dog barks
Superclass name: Animal
Subclass name: Dog
EXERCISE -5
Expected output:
Dog:
The dog barks
The dog lives on land
Dolphin:
The dolphin clicks
The dolphin lives in water
EXERCISE -5
Expected output:
Drawing a circle
Drawing a rectangle
EXERCISE -6
try {
// Attempt division
result = numerator / denominator;
System.out.println("Result: " + result);
} catch (ArithmeticException e) {
// Handle division by zero error
System.out.println("Error: Division by zero is not allowed.");
} finally {
// This block always executes
System.out.println("Execution completed in the 'finally' block.");
}
try {
// Attempting division by zero
result = numbers[0] / numbers[1];
System.out.println("Result of division: " + result);
Expected output:
Result of division: 2
Error: Array index is out of bounds.
Program continues after exception handling.
Result of division: 2
Accessing element at index 2: 0
Error: Attempted to access a method on a null object.
Program continues after exception handling.
EXERCISE -6
Expected output:
Expected output:
@Override
try {
System.out.println(Thread.currentThread().getName() + " is
starting.");
} catch (InterruptedException e) {
System.out.println(e);
thread1.start();
thread2.start();
try {
} catch (InterruptedException e) {
System.out.println(e);
Expected output:
Thread-0 is starting.
Thread-1 is starting
@Override
while (true) {
System.out.println(Thread.currentThread().getName() + "
(Daemon) is running in the background.");
try {
} catch (InterruptedException e) {
System.out.println(e);
@Override
public void run() {
try {
} catch (InterruptedException e) {
System.out.println(e);
daemonThread.setDaemon(true);
// Start both threads
daemonThread.start();
userThread.start();
try {
userThread.join();
} catch (InterruptedException e) {
System.out.println(e);
Expected output:
Thread-1 (User) is starting.
import java.util.LinkedList;
import java.util.Queue;
class Buffer {
private Queue<Integer> queue = new LinkedList<>();
private int capacity;
@Override
public void run() {
int value = 0;
try {
while (true) {
buffer.produce(value);
value++;
Thread.sleep(500); // Simulate time taken to produce an item
}
} catch (InterruptedException e) {
System.out.println("Producer interrupted");
}
}
}
@Override
public void run() {
try {
while (true) {
buffer.consume();
Thread.sleep(1000); // Simulate time taken to consume an item
}
} catch (InterruptedException e) {
System.out.println("Consumer interrupted");
}
}
}
public class ProducerConsumerProblem {
public static void main(String[] args) {
Buffer buffer = new Buffer(5); // Create a buffer with a capacity of 5
producer.start();
consumer.start();
}
}
Expected output:
Produced: 0
Consumed: 0
Produced: 1
Consumed: 1
Produced: 2
Produced: 3
Consumed: 2
Produced: 4
Produced: 5
Consumed: 3
Produced: 6
Produced: 7
Consumed: 4
Produced: 8
Produced: 9
Consumed: 5
Produced: 10
Buffer is full. Producer is waiting...
Consumed: 6
Produced: 11
Buffer is full. Producer is waiting...
Consumed: 7
Produced: 12
Buffer is full. Producer is waiting...
Consumed: 8
Produced: 13
Buffer is full. Producer is waiting...
Consumed: 9
Produced: 14
Buffer is full. Producer is waiting...
Consumed: 10
Produced: 15
Buffer is full. Producer is waiting...
Consumed: 11
Produced: 16
Buffer is full. Producer is waiting….
EXERCISE – 8
a) Write a java program that import and use the user defined packages
Expected output:
Sum: 15
Difference: 5
Product: 50
Quotient: 2.0