ECE Java Skill Course Lab Experiments
ECE Java Skill Course Lab Experiments
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);
}
}
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).");
}
}
}
--------------------------------------------------------------------------------------------------------------
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.");
}
}
}
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];
j++;
}
k++;
}
----------------------------------------------------------------------------------------------------------------
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!");
}
}
class MyObject {
private String message;
obj1.displayMessage();
obj2.displayMessage();
}
}
class MyObject {
private String message;
// Default constructor
public MyObject() {
message = "Default Message";
}
// Parameterized constructor
public MyObject(String msg) {
message = msg;
}
class MyObject {
public void displayMessage(String message) {
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.");
}
}
circle.draw();
square.draw();
}
}
----------------------------------------------------------------------------------------------------------------
Exercise 5 (Inheritance - Continued)
a) Implement a Java program using the "super" keyword:
class Parent {
String message = "Hello from Parent";
void display() {
System.out.println(message);
}
}
void display() {
super.display(); // Using "super" to call the parent class method
System.out.println(message);
}
circle.draw();
rectangle.draw();
}
}
----------------------------------------------------------------------------------------------------------------
Exercise 6 (Runtime Polymorphism)
a) Write a Java program that implements runtime polymorphism (method overriding):
class Animal {
void makeSound() {
System.out.println("Some sound");
}
}
----------------------------------------------------------------------------------------------------------------
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());
}
}
}
-------------------------------------------------------------------------------------------------------------
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();
}
}
thread1.start();
thread2.start();
try {
thread1.join();
thread2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
daemonThread.setDaemon(true);
daemonThread.start();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
------------------------------------------------------------------------------------------------------------
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;
----------------------------------------------------------------------------------------------------------------
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();
}
});
}
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;
----------------------------------------------------------------------------------------------------------------