ECE Java Skill Course Lab Experiments
ECE Java Skill Course Lab Experiments
1|Page
a. Exception handling mechanism
b. Multiple catch clauses
c. Finally
d. Creating user defined exceptions
Exercise – 8 (Threads)
a) Write a JAVA program that creates threads by extending Thread class. First thread displays
“Good Morning” every 1 sec, the second thread displays “Hello” every 2 seconds and the third
display “Welcome” every 3 seconds,(Repeat the same by implementing Runnable)
b) Write a program illustrating isAlive and join ()
c) Write a Program illustrating Daemon Threads.
Exercise – 9 (Packages)
a) Create a user defined package and demonstrate different ways of importing packages
Exercise - 10 (Applet)
a) Write a JAVA program to paint like paint brush in applet.
b) Write a JAVA program to create different shapes and fill colors using Applet.
2|Page
Exercise 1 (Basics)
a) Display default values of all primitive data types in Java:
System.out.println("Default values:");
System.out.println("byte: " + byteDefault);
System.out.println("short: " + shortDefault);
System.out.println("int: " + intDefault);
System.out.println("long: " + longDefault);
System.out.println("float: " + floatDefault);
System.out.println("double: " + doubleDefault);
System.out.println("char: " + charDefault);
System.out.println("boolean: " + booleanDefault);
}
}
Output:
Default values:
byte: 0
short: 0
int: 0
long: 0
float: 0.0
double: 0.0
char:
boolean: false
3|Page
b) Display roots of a quadratic equation and describe their nature:
import java.util.Scanner;
double discriminant = b * b - 4 * a * c;
if (discriminant > 0) {
double root1 = (-b + Math.sqrt(discriminant)) / (2 * a);
double root2 = (-b - Math.sqrt(discriminant)) / (2 * a);
System.out.println("Two distinct real roots: " + root1 + " and " + root2);
} else if (discriminant == 0) {
double root = -b / (2 * a);
System.out.println("One real root: " + root);
} else {
System.out.println("No real roots (complex roots).");
}
}
}
Output:
Enter the values of a, b, and c: 1 2 3
No real roots (complex roots).
--------------------------------------------------------------------------------------------------------------
4|Page
Exercise 2 (Operations, Expressions, Control-flow, Strings)
a) Search for an element in a list using binary search:
import java.util.Arrays;
if (result >= 0) {
System.out.println("Element " + target + " found at index " + result);
} else {
System.out.println("Element " + target + " not found in the array.");
}
}
}
Output:
Element 6 found at index 5
5|Page
Output:
Sorted array using Bubble Sort:
123569
public static void merge(int[] arr, int left, int middle, int right) {
int n1 = middle - left + 1;
int n2 = right - middle;
int i = 0, j = 0, k = left;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
} else {
arr[k] = R[j];
6|Page
j++;
}
k++;
}
Output:
Sorted array using Merge Sort:
123569
Output:
World!
----------------------------------------------------------------------------------------------------------------
7|Page
Exercise 3 (Class, Objects)
a) Implement a Java program using the class mechanism:
public class MyClass {
public static void main(String[] args) {
MyObject myObj = new MyObject();
myObj.displayMessage();
}
}
class MyObject {
public void displayMessage() {
System.out.println("Hello from MyObject class!");
}
}
Output:
Hello from MyObject class!
class MyObject {
private String message;
Output:
Hello, Constructor
8|Page
c) Implement a Java program using constructor overloading:
public class ConstructorOverloadingExample {
public static void main(String[] args) {
MyObject obj1 = new MyObject();
MyObject obj2 = new MyObject("Hello, Constructor!");
obj1.displayMessage();
obj2.displayMessage();
}
}
class MyObject {
private String message;
// Default constructor
public MyObject() {
message = "Default Message";
}
// Parameterized constructor
public MyObject(String msg) {
message = msg;
}
Output:
Default Message
Hello, Constructor!
d) Implement a Java program using method overloading:
public class MethodOverloadingExample {
public static void main(String[] args) {
MyObject obj = new MyObject();
obj.displayMessage("Hello, Method Overloading!");
obj.displayMessage(42);
}
}
class MyObject {
public void displayMessage(String message) {
9|Page
System.out.println("String message: " + message);
}
----------------------------------------------------------------------------------------------------------
Exercise 4 (Inheritance)
a) Implement a Java program using single inheritance:
class Parent {
void displayParent() {
System.out.println("This is the parent class.");
}
}
10 | P a g e
void displayParent() {
System.out.println("This is the parent class.");
}
}
circle.draw();
11 | P a g e
square.draw();
}
}
Output:
Drawing a Circle
Drawing a Square
----------------------------------------------------------------------------------------------------------------
void display() {
System.out.println(message);
}
}
void display() {
super.display(); // Using "super" to call the parent class method
System.out.println(message);
}
Output:
Hello from Parent
Hello from Child
12 | P a g e
b) Implement a Java program using interfaces:
interface Shape {
void draw();
}
circle.draw();
rectangle.draw();
}
}
Output:
Drawing a Circle
Drawing a Rectangle
----------------------------------------------------------------------------------------------------------------
13 | P a g e
Exercise 6 (Runtime Polymorphism)
a) Write a Java program that implements runtime polymorphism (method overriding):
class Animal {
void makeSound() {
System.out.println("Some sound");
}
}
Output:
Woof! Woof!
Meow!
----------------------------------------------------------------------------------------------------------------
14 | P a g e
Exercise 7 (Exception)
a) Implement a Java program using exception handling:
public class ExceptionHandlingExample {
public static void main(String[] args) {
try {
int result = 10 / 0; // Attempt to divide by zero
System.out.println("Result: " + result);
} catch (ArithmeticException e) {
System.out.println("Exception caught: " + e.getMessage());
}
}
}
Output:
Exception caught: / by zero
Output:
Array index out of bounds: Index 7 out of bounds for length 5
15 | P a g e
} finally {
System.out.println("Finally block executed.");
}
}
}
Output:
Arithmetic exception: / by zero
Finally block executed.
Output:
Custom Exception: Age is below 18.
-------------------------------------------------------------------------------------------------------------
16 | P a g e
Exercise 8 (Threads)
a) Create threads by extending the Thread class to display messages at different
intervals:
class DisplayThread extends Thread {
private String message;
private int interval;
thread1.start();
thread2.start();
thread3.start();
}
}
Output:
Welcome
Good Morning
Hello
Good Morning
Hello
17 | P a g e
Good Morning
Welcome
Good Morning
Hello
Good Morning
Good Morning
^C
thread1.start();
thread2.start();
try {
thread1.join();
thread2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
18 | P a g e
Output:
Thread 1 is running.
Thread 2 is running.
Both threads have completed their execution.
daemonThread.setDaemon(true);
daemonThread.start();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
------------------------------------------------------------------------------------------------------------
19 | P a g e
Exercise 9 (Packages)
a) Create a user-defined package and demonstrate different ways of importing
packages:
Import the package and use the class in another Java file:
import myPackage.MyClass;
Output:
Hello from MyClass in myPackage.
----------------------------------------------------------------------------------------------------------------
20 | P a g e
Exercise 10 (Applet)
a) Write a Java program to paint like a paintbrush in an applet:
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
x = e.getX();
y = e.getY();
}
});
}
Output:
Applet
21 | P a g e
b) Write a Java program to create different shapes and fill colors using an applet:
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
Output:
----------------------------------------------------------------------------------------------------------------
22 | P a g e