0% found this document useful (0 votes)
5 views7 pages

Java Lab All Exercises 1 To 11

The document contains Java lab exercises covering various programming concepts, including default values of data types, quadratic equations, searching algorithms, sorting, inheritance, exception handling, threading, file I/O, and JavaFX GUI applications. Each exercise includes code examples demonstrating the respective concepts. The exercises range from basic to advanced topics, providing a comprehensive overview of Java programming techniques.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views7 pages

Java Lab All Exercises 1 To 11

The document contains Java lab exercises covering various programming concepts, including default values of data types, quadratic equations, searching algorithms, sorting, inheritance, exception handling, threading, file I/O, and JavaFX GUI applications. Each exercise includes code examples demonstrating the respective concepts. The exercises range from basic to advanced topics, providing a comprehensive overview of Java programming techniques.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Java Lab Programs - Exercises 1 to 11

Exercise 1:
a) Default values of all primitive data types
---------------------------------------------
public class DefaultValues {
byte b; short s; int i; long l;
float f; double d; char c; boolean bool;
public static void main(String[] args) {
DefaultValues obj = new DefaultValues();
System.out.println("byte: " + obj.b);
System.out.println("short: " + obj.s);
System.out.println("int: " + obj.i);
System.out.println("long: " + obj.l);
System.out.println("float: " + obj.f);
System.out.println("double: " + obj.d);
System.out.println("char: '" + obj.c + "'");
System.out.println("boolean: " + obj.bool);
}
}

b) Roots of a quadratic equation


--------------------------------
import java.util.Scanner;
public class QuadraticEquation {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double a = sc.nextDouble(), b = sc.nextDouble(), c = sc.nextDouble();
double d = b * b - 4 * a * c;
if (d > 0) System.out.println("Real and distinct roots.");
else if (d == 0) System.out.println("Real and equal roots.");
else System.out.println("Imaginary roots.");
}
}

Exercise 2:
a) Binary Search
----------------
import java.util.Scanner;
public class BinarySearch {
public static void main(String[] args) {
int[] arr = {10, 20, 30, 40, 50};
Scanner sc = new Scanner(System.in);
int key = sc.nextInt(), low = 0, high = arr.length - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (arr[mid] == key) {
System.out.println("Found at index " + mid); return;
} else if (arr[mid] < key) low = mid + 1;
else high = mid - 1;
}
System.out.println("Not found");
}
}

b) Bubble Sort
--------------
public class BubbleSort {
public static void main(String[] args) {
int[] arr = {5, 2, 9, 1, 5, 6};
for (int i = 0; i < arr.length - 1; i++) {
for (int j = 0; j < arr.length - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
int t = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = t;
}
}
}
for (int n : arr) System.out.print(n + " ");
}
}

c) StringBuffer
---------------
public class StringBufferExample {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer("Hello World");
sb.deleteCharAt(5);
System.out.println(sb);
}
}

Exercise 3:
a) Class and Methods
--------------------
class MyClass {
void show() { System.out.println("Hello from method!"); }
public static void main(String[] args) {
MyClass obj = new MyClass();
obj.show();
}
}

b) Method Overloading
---------------------
class Overload {
void display(int a) { System.out.println("Int: " + a); }
void display(double b) { System.out.println("Double: " + b); }
public static void main(String[] args) {
Overload o = new Overload();
o.display(10);
o.display(5.5);
}
}
c) Constructor
--------------
class ConstructorExample {
ConstructorExample() {
System.out.println("Constructor called");
}
public static void main(String[] args) {
new ConstructorExample();
}
}

d) Constructor Overloading
--------------------------
class ConsOverload {
ConsOverload() { System.out.println("Default"); }
ConsOverload(int a) { System.out.println("Parameterized: " + a); }
public static void main(String[] args) {
new ConsOverload();
new ConsOverload(100);
}
}

Exercise 4:
a) Single Inheritance
---------------------
class A { void display() { System.out.println("Class A"); }}
class B extends A {
public static void main(String[] args) {
B obj = new B();
obj.display();
}
}

b) Multilevel Inheritance
-------------------------
class A { void showA() { System.out.println("A"); }}
class B extends A { void showB() { System.out.println("B"); }}
class C extends B {
public static void main(String[] args) {
C obj = new C();
obj.showA(); obj.showB();
}
}

c) Abstract Class
-----------------
abstract class Shape {
abstract void area();
}
class Circle extends Shape {
void area() { System.out.println("Circle area"); }
public static void main(String[] args) {
new Circle().area();
}
}

Exercise 5:
a) super Keyword
----------------
class Parent {
int num = 100;
}
class Child extends Parent {
void display() { System.out.println(super.num); }
public static void main(String[] args) {
new Child().display();
}
}

b) Interface and Inheritance Type


---------------------------------
interface A { void methodA(); }
interface B { void methodB(); }
class C implements A, B {
public void methodA() { System.out.println("A"); }
public void methodB() { System.out.println("B"); }
public static void main(String[] args) {
new C().methodA();
new C().methodB();
}
}

c) Runtime Polymorphism
-----------------------
class Animal {
void sound() { System.out.println("Animal sound"); }
}
class Dog extends Animal {
void sound() { System.out.println("Dog barks"); }
}
class Main {
public static void main(String[] args) {
Animal obj = new Dog();
obj.sound();
}
}

Exercise 6:
Exception Handling
------------------
try {
int a = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("Error: " + e.getMessage());
}
Multiple Catch Clauses
----------------------
try {
int[] arr = new int[5];
arr[5] = 30;
} catch (ArithmeticException e) {
System.out.println("Arithmetic Exception");
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Array Index Exception");
}

User Defined Exception


----------------------
class MyException extends Exception {
MyException(String s) { super(s); }
}
class Test {
public static void main(String args[]) throws MyException {
throw new MyException("Custom Exception");
}
}

Exercise 7:
Thread Creation
---------------
class MyThread extends Thread {
public void run() {
System.out.println("Thread running");
}
public static void main(String[] args) {
new MyThread().start();
}
}

Alive and Join


--------------
class Test extends Thread {
public void run() {
System.out.println("Running");
}
public static void main(String[] args) throws InterruptedException {
Test t = new Test();
t.start();
System.out.println(t.isAlive());
t.join();
System.out.println("Finished");
}
}

Daemon Thread
-------------
class DaemonExample extends Thread {
public void run() {
if (Thread.currentThread().isDaemon())
System.out.println("Daemon thread");
else
System.out.println("User thread");
}
public static void main(String[] args) {
DaemonExample t = new DaemonExample();
t.setDaemon(true);
t.start();
}
}

Exercise 8:
a) Import and use user-defined package
--------------------------------------
// In mypackage/MyClass.java
package mypackage;
public class MyClass {
public void show() { System.out.println("From Package"); }
}
// In Main.java
import mypackage.MyClass;
public class Main {
public static void main(String[] args) {
new MyClass().show();
}
}

b) JavaFX GUI label and image (No code)


c) Tip Calculator using JavaFX (requires GUI components)

Exercise 9:
a) Java Program to Read & Write using File I/O Streams
------------------------------------------------------
import java.io.*;
public class FileReadWrite {
public static void main(String[] args) throws IOException {
FileWriter fw = new FileWriter("test.txt");
fw.write("Hello File");
fw.close();

FileReader fr = new FileReader("test.txt");


int i;
while ((i = fr.read()) != -1)
System.out.print((char) i);
fr.close();
}
}

Exercise 10:
a) Java Program Using Interfaces and Multiple Inheritance
----------------------------------------------------------
interface A { void msg(); }
interface B { void info(); }
class C implements A, B {
public void msg() { System.out.println("Hello from A"); }
public void info() { System.out.println("Info from B"); }
public static void main(String[] args) {
C obj = new C();
obj.msg();
obj.info();
}
}

Exercise 11:
a) Java Program Using JavaFX to Display a Window
-------------------------------------------------
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.stage.Stage;

public class HelloFX extends Application {


public void start(Stage stage) {
Label label = new Label("Hello, JavaFX!");
Scene scene = new Scene(label, 200, 100);
stage.setScene(scene);
stage.setTitle("JavaFX Example");
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}

You might also like