JustPaste
HomeCategoriesAboutDonateContactTerms of UsePrivacy Policy
JustPaste

Free online notepad — write and share instantly

Navigate

  • Home
  • Timeline
  • Categories

Info

  • About
  • Donate
  • Contact

Legal

  • Terms of Use
  • Privacy Policy

© 2026 JustPaste.app. All rights reserved.

Made with ♥ by JustPaste

Untitled Page | JustPaste.app
about 6 hours ago4 views
👨‍💻Programming

Untitled Page

EXPERIMENT – 1

1(a) Single-Dimensional and Multi-Dimensional Arrays

Aim: Write a Java program to declare, initialize and access the elements of single-dimensional Arrays and multi-dimensional Arrays. Display the array elements using loops.

Source Code

public class ArrayExample {
    public static void main(String[] args) {

        // Single-Dimensional Array
        int[] singleArray = {10, 20, 30, 40, 50};

        System.out.println("Single-Dimensional Array Elements:");

        //accessing
        for (int i = 0; i < singleArray.length; i++) {
            //displaying
            System.out.println("Element at index " + i + ": " + singleArray[i]);
        }

        // Multi-Dimensional Array (2D Array)
        int[][] multiArray = {
            {1, 2, 3},
            {4, 5, 6},
            {7, 8, 9}
        };

        System.out.println("\nMulti-Dimensional Array Elements:");

        //accessing
        for (int i = 0; i < multiArray.length; i++) {
            for (int j = 0; j < multiArray[i].length; j++) {
                //displaying
                System.out.print(multiArray[i][j] + " ");
            }
            System.out.println();
        }
    }
}

Output

---

1(b) Arrays Using Scanner Class

Aim: Using Scanner class (user input) Write a Java program to declare, initialize and access the elements of single-dimensional Arrays and multi-dimensional Arrays. Display the array elements using loops.

Source Code

import java.util.Scanner;

public class ArrayInputExample {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        // Single-Dimensional Array
        System.out.print("Enter size of Single-Dimensional Array: ");
        int size = sc.nextInt();

        int[] singleArray = new int[size];

        System.out.println("Enter elements for Single-Dimensional Array:");

        for (int i = 0; i < size; i++) {
            singleArray[i] = sc.nextInt();
        }

        System.out.println("Single-Dimensional Array Elements:");

        for (int i = 0; i < size; i++) {
            System.out.println("Element at index " + i + ": " + singleArray[i]);
        }

        // Multi-Dimensional Array
        System.out.print("\nEnter number of rows for Multi-Dimensional Array: ");
        int rows = sc.nextInt();

        System.out.print("Enter number of columns for Multi-Dimensional Array: ");
        int cols = sc.nextInt();

        int[][] multiArray = new int[rows][cols];

        System.out.println("Enter elements for Multi-Dimensional Array:");

        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                multiArray[i][j] = sc.nextInt();
            }
        }

        System.out.println("Multi-Dimensional Array Elements:");

        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                System.out.print(multiArray[i][j] + " ");
            }
            System.out.println();
        }

        sc.close();
    }
}

Output

---

EXPERIMENT – 2

Aim: Write a Java program to demonstrate static variable, static method and static block.

Source Code

import java.lang.*;

class Student {

    int rollno;
    String name;

    static String college = "BEC"; //static variable

    Student(int r, String n) {
        rollno = r;
        name = n;
    }

    static void change() //static method
    {
        college = "BEC";
    }

    static //static block
    {
        System.out.println("\n<--Student BioData-->\n");
        System.out.println("\tRNo.\tName\tCollege");
        System.out.println("\t----\t----\t----");
    }

    void display() {
        System.out.println("\t" + rollno + "\t" + name + "\t" + college);
    }

    public static void main(String args[]) {

        Student s1 = new Student(111, "Karan");
        Student s2 = new Student(222, "Aryan");
        Student s3 = new Student(333, "Sonoo");

        Student.change();

        s1.display();
        s2.display();
        s3.display();
    }
}

Output

---

EXPERIMENT – 3

Aim: Create a Java program that demonstrates method overloading (Compile-time Polymorphism) and method overriding (Runtime Polymorphism) using inheritance.

3(a) Method Overloading

Source Code

//Prg 3(a). to demonstrate Method Overloading

import java.util.Scanner;

class Demo {

    void area(int a) {
        System.out.println("Area of Square: " + a * a);
    }

    double area(int a, int b) {
        System.out.println("Area of Rectangle: " + a * b);
        return a * b;
    }
}

class Overloading {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        Demo o = new Demo();

        double result;

        System.out.print("Enter side of a square:");
        o.area(sc.nextInt());

        System.out.print("Enter sides of a rectangle:");
        result = o.area(sc.nextInt(), sc.nextInt());

        System.out.println("result is: " + result);
    }
}

Output

---

3(b) Method Overriding

Source Code

//Prg 3(b). to demonstrate Method Overriding

import java.lang.*;
import java.util.*;

class Base {

    public int a, b;

    public void set() {

        Scanner sc = new Scanner(System.in);

        System.out.print("Enter first number:");
        a = sc.nextInt();

        System.out.println("First number:" + a);
        System.out.println();

        System.out.print("Enter second number:");
        b = sc.nextInt();

        System.out.println("second number:" + b);
        System.out.println("===================");
    }
}

class Sum extends Base {

    public void show() {
        int k = a + b;

        System.out.println("\nAddition is:" + k);
        System.out.println("===================");
    }
}

class Sub extends Base {

    public void show() {
        int k = a - b;

        System.out.println("\nSubtraction is:" + k);
        System.out.println("===================");
    }
}

class Mul extends Base {

    public void show() {
        int k = a * b;

        System.out.println("\nMultiplication is:" + k);
        System.out.println("===================");
    }
}

class Div extends Base {

    public void show() {
        float k = a / b;

        System.out.println("\nDivision is:" + k);
        System.out.println("===================");
    }
}

class Main {

    public static void main(String args[]) {

        System.out.println("<----- Menu ----->");

        //loop:
        while (true) {

            System.out.println("[0]:Exit\n[1]:Addition\n[2]:Subtraction\n[3]:Multiplication\n [4]:Division\n”);

            System.out.println("====================");

            Scanner sc = new Scanner(System.in);

            System.out.print("Enter your choice:");
            int choice = sc.nextInt();

            switch (choice) {

                case 0:
                    System.exit(0);
                    //break loop;

                case 1:
                    sum a = new sum();
                    a.set();
                    a.show();
                    break;

                case 2:
                    sub b = new sub();
                    b.set();
                    b.show();
                    break;

                case 3:
                    mul m = new mul();
                    m.set();
                    m.show();
                    break;

                case 4:
                    div d = new div();
                    d.set();
                    d.show();
                    break;

                default:
                    System.out.println("Wrong Choice...!, Plz enter correct one..");
                    System.out.println("===================");
                    break;
            }
        }
    }
}

Output

---

EXPERIMENT – 4

Aim: Write a Java program to demonstrate multiple inheritance using interfaces.

Source Code

// Prg.Program to implement the Multiple Inheritance

import java.lang.*;

interface Bank {

    float rate = 12.0f;
    int no_of_years = 3;

    void show();
}

class Customer {

    String cust_name;
    int cust_id;

    Customer(String s, int i) {
        cust_name = s;
        cust_id = i;
    }

    void display() {
        System.out.println("Customer Name = " + cust_name);
        System.out.println("Customer Id = " + cust_id);
    }
}

class Account extends Customer implements Bank {

    int acc_no;
    float acc_bal;

    Account(String s, int i, int n, float b) {
        super(s, i);
        acc_no = n;
        acc_bal = b;
    }

    public void show() {
        super.display();

        System.out.println("Account No. = " + acc_no);
        System.out.println("Account Balance = " + acc_bal);
    }

    void interest() {
        show();

        float intr = (rate * acc_bal * no_of_years) / 100;

        System.out.println("Interest = " + intr);
    }
}

class main {

    public static void main(String[] args) {

        Account ac = new Account("Sameer", 8008, 123456, 50000f);

        ac.interest();
    }
}

Output

---

EXPERIMENT – 5

Aim: Develop a Java program that demonstrates the use of all exception handling keywords: try, catch, throw, throws, and finally.

Source Code

class ExceptionDemo {

    // Method that declares it may throw an exception
    static int divide(int a, int b) throws ArithmeticException {

        if (b == 0) {

            // Explicitly throwing an exception
            throw new ArithmeticException("Division by zero is not allowed!");
        }

        return a / b;
    }

    public static void main(String[] args) {

        try {

            System.out.println("Program started...");

            // Try block to check division
            int result = divide(10, 0);  // This will throw an exception

            System.out.println("Result: " + result);

        } catch (ArithmeticException e) {

            // Catch block handles the thrown exception
            System.out.println("Caught Exception: " + e.getMessage());

        } finally {

            // Finally block always executes
            System.out.println("Finally block executed: Closing resources.");
        }

        System.out.println("Program continues...");
    }
}

Output

---

EXPERIMENT – 6

Aim: Write a Java program to create and handle one or more user-defined exceptions with meaningful messages.

Source Code

import java.util.Scanner;

// User-defined exception
class AgeNotValidException extends Exception {

    public AgeNotValidException(String message) {
        super(message);
    }
}

public class VotingExample {

    // Method to check voting eligibility
    public static void checkAge(int age) throws AgeNotValidException {

        if (age < 18) {

            // throw user-defined exception
            throw new AgeNotValidException("Age " + age + " is not valid for voting. Must be 18 or above.");

        } else {

            System.out.println("Eligible to vote!");
        }
    }

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        try {

            System.out.print("Enter your age: ");

            int age = sc.nextInt();

            checkAge(age); // may throw exception

        } catch (AgeNotValidException e) {

            System.out.println("Exception: " + e.getMessage());

        } finally {

            System.out.println("Voting eligibility check completed.");
        }

        sc.close();
    }
}

Output

---

EXPERIMENT – 7

Aim: Design a Java program to demonstrate the creation and use of packages. Include at least two classes from different packages.

Source Code

Step 1: Create package1

package mypackage1;

public class Addition {

    public int add(int a, int b) {
        return a + b;
    }
}

Save by: Addition.java

Compile:

javac -d . Addition.java

Step 2: Create package2

package mypackage2;

public class Multiplication {

    public int multiply(int a, int b) {
        return a * b;
    }
}

Save by: Multiplication.java

Compile:

javac -d . Multiplication.java

Step 3: Importing above two packages in below program

import mypackage1.Addition;
import mypackage2.Multiplication;

public class MainProgram {

    public static void main(String[] args) {

        Addition addObj = new Addition();

        Multiplication mulObj = new Multiplication();

        int sum = addObj.add(10, 20);

        int product = mulObj.multiply(5, 4);

        System.out.println("Sum = " + sum);

        System.out.println("Product = " + product);
    }
}

Save by: MainProgram.java

Compile:

javac MainProgram.java

Run:

java MainProgram

Output

---

⚠️Content was pasted as plain text and auto-formatted as a code block. Use the Code Block button in the editor for proper formatting.

← Back to timeline