fbpx

Exercise 1
a) Program to Display Default Values of All Primitive Data Types in Java

				
					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);
    }
}

				
			
				
					byte: 0
short: 0
int: 0
long: 0
float: 0.0
double: 0.0
char: [ ]
boolean: false

				
			

b) Program to Display the Roots of a Quadratic Equation

				
					import java.util.Scanner;

public class QuadraticEquation {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a, b, and c: ");
        double a = scanner.nextDouble();
        double b = scanner.nextDouble();
        double c = scanner.nextDouble();
        double d = b * b - 4 * a * c;
        
        if (d > 0) {
            double root1 = (-b + Math.sqrt(d)) / (2 * a);
            double root2 = (-b - Math.sqrt(d)) / (2 * a);
            System.out.println("Roots are real and distinct: " + root1 + ", " + root2);
        } else if (d == 0) {
            double root = -b / (2 * a);
            System.out.println("Roots are real and equal: " + root);
        } else {
            System.out.println("Roots are complex and imaginary.");
        }
    }
}

				
			
				
					Enter a, b, and c: 1 -3 2
Roots are real and distinct: 2.0, 1.0

				
			

Exercise 2
a) Binary Search Program

				
					import java.util.Arrays;
import java.util.Scanner;

public class BinarySearch {
    public static void main(String[] args) {
        int[] arr = {10, 20, 30, 40, 50};
        Arrays.sort(arr);
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a number to search: ");
        int key = scanner.nextInt();
        int result = Arrays.binarySearch(arr, key);
        System.out.println(result >= 0 ? "Found at index " + result : "Not Found");
    }
}

				
			
				
					Enter a number to search: 30
Found at index 2

				
			

b) Bubble Sort Program

				
					public class BubbleSort {
    public static void main(String[] args) {
        int[] arr = {5, 1, 4, 2, 8};
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = 0; j < arr.length - i - 1; j++) {
                if (arr[j] > arr[j + 1]) {
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
        for (int num : arr) System.out.print(num + " ");
    }
}

				
			
				
					1 2 4 5 8 

				
			

c) StringBuffer Program (delete, remove character)

				
					public class StringBufferExample {
    public static void main(String[] args) {
        StringBuffer sb = new StringBuffer("Hello World");
        sb.delete(5, 11); // Deletes " World"
        sb.deleteCharAt(0); // Removes the first character
        System.out.println(sb.toString());
    }
}

				
			
				
					ello

				
			

Exercise 6
a) Program to Demonstrate the "super" Keyword

				
					class Animal {
    String color = "White";
}

class Dog extends Animal {
    String color = "Black";

    void printColor() {
        System.out.println("Dog color: " + color); // prints color of Dog class
        System.out.println("Animal color: " + super.color); // prints color of Animal class
    }
}

public class SuperKeywordExample {
    public static void main(String[] args) {
        Dog d = new Dog();
        d.printColor();
    }
}

				
			
				
					Dog color: Black
Animal color: White


				
			

b) Program to Implement Interface

				
					interface Animal {
    void sound();
}

class Dog implements Animal {
    public void sound() {
        System.out.println("Dog barks");
    }
}

public class InterfaceExample {
    public static void main(String[] args) {
        Animal myDog = new Dog();
        myDog.sound();
    }
}

				
			
				
					Dog barks

				
			

c) Program to Demonstrate Runtime Polymorphism

				
					class Animal {
    void sound() {
        System.out.println("Animal sound");
    }
}

class Dog extends Animal {
    void sound() {
        System.out.println("Dog barks");
    }
}

class Cat extends Animal {
    void sound() {
        System.out.println("Cat meows");
    }
}

public class RuntimePolymorphismExample {
    public static void main(String[] args) {
        Animal myAnimal = new Dog();
        myAnimal.sound();
        
        myAnimal = new Cat();
        myAnimal.sound();
    }
}

				
			
				
					Dog barks
Cat meows