Skip to content

Week 7: Java – ArrayList & OOP


Q1. Student List Operations Using ArrayList

Objective: Manage a list of students using ArrayList—insert, update, remove, search, and reverse.

import java.util.ArrayList;
import java.util.Scanner;

class q1 {
    static ArrayList<String> stud = new ArrayList<>();
    static Scanner sc = new Scanner(System.in);

    static void insertstud() {
        System.out.println("Enter 5 student names:");
        for (int i = 0; i < 5; i++) {
            System.out.print("Enter name " + (i + 1) + ": ");
            stud.add(sc.nextLine());
        }
    }

    static void displaystud() {
        System.out.println("Initial Student List: " + stud);
    }

    static void removestud() {
        if (stud.size() >= 3) {
            stud.remove(2); // remove 3rd student
            System.out.println("After Removing 3rd Name: " + stud);
        }
    }

    static void updatestud() {
        System.out.print("Enter a new student name to add at 2nd place: ");
        String newstud = sc.nextLine();
        stud.add(1, newstud);
        System.out.println("After Adding " + newstud + " at 2nd Position: " + stud);
    }

    static void replacestud() {
        System.out.print("Enter a name to update at last: ");
        String newName = sc.nextLine();
        if (!stud.isEmpty()) {
            stud.set(stud.size() - 1, newName);
            System.out.println("After updating Last Name to " + newName + ": " + stud);
        }
    }

    static void findstud() {
        System.out.print("Enter a name to search: ");
        String searchName = sc.nextLine();
        if (stud.contains(searchName)) {
            System.out.println(searchName + " is present in the list.");
        } else {
            System.out.println(searchName + " does not exist");
        }
    }

    static void revstud() {
        System.out.println("Final Student List (Reverse):");
        for (int i = stud.size() - 1; i >= 0; i--) {
            System.out.println(stud.get(i));
        }
    }

    public static void main(String args[]) {
        insertstud();
        displaystud();
        removestud();
        updatestud();
        replacestud();
        findstud();
        revstud();
        sc.close();
    }
}

Key Points:

  • ArrayList supports dynamic resizing and convenient methods: add(), remove(), set(), contains().
  • Indexing in ArrayList starts from 0.
  • Reversing is done via a loop from size()-1 to 0.

Q2. Employee Management with Objects

Objective: Manage employee data using a custom class and ArrayList.

import java.util.*;

class Employee {
    int empId;
    String name;
    int daysPresent;

    Employee(int empId, String name, int daysPresent) {
        this.empId = empId;
        this.name = name;
        this.daysPresent = daysPresent;
    }
}

public class q2 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        ArrayList<Employee> employees = new ArrayList<>();

        employees.add(new Employee(101, "Alice", 22));
        employees.add(new Employee(102, "Bob", 18));
        employees.add(new Employee(103, "Charlie", 25));
        employees.add(new Employee(104, "David", 15));
        employees.add(new Employee(105, "Eva", 20));

        // Display all employees
        System.out.println("All Employees:");
        for (Employee e : employees) {
            System.out.println(e.empId + " " + e.name + " " + e.daysPresent);
        }

        // Search employee by ID
        System.out.print("Enter Employee ID to search: ");
        int searchId = sc.nextInt();
        boolean found = false;
        for (Employee e : employees) {
            if (e.empId == searchId) {
                System.out.println("Employee Found: " + e.name + " - Days Present: " + e.daysPresent);
                found = true;
                break;
            }
        }
        if (!found) System.out.println("Employee not found.");

        // Display employees with attendance < 20
        System.out.println("Employees with attendance less than 20 days:");
        for (Employee e : employees) {
            if (e.daysPresent < 20) {
                System.out.println(e.empId + " " + e.name + " " + e.daysPresent);
            }
        }

        sc.close();
    }
}

Key Points:

  • Define a custom class for structured data (empId, name, daysPresent).
  • ArrayList<Employee> stores objects.
  • Loop through the list with for-each for easy access to object properties.
  • Conditional checks can filter objects (attendance < 20 days).

Week 7 Concepts:

  1. ArrayList Operations: add, remove, set, contains, get, size.
  2. OOP Basics: classes, objects, constructors, attributes.
  3. Searching and Filtering: using loops and if conditions on object properties.
  4. Reversing list elements with a simple for-loop.

Comments