LPU Java Unit 2 Lecture 10 COD RB | LPUCOLAB

LPU Java Unit 2 Lecture 10 COD RB | LPUCOLAB

LPU_Java_Unit 2_Lecture 10_COD_RB

1. Problem Statement

Danny is developing a scheduling application that needs to determine the next meeting day based on the current day.

The application allows users to input the current day, and it then determines and displays the next day for scheduling meetings. Write the program that uses the enum concept to implement this feature.

Input format:
The input consists of a single string representing the day of the week.

Output format:
If the input is a valid day of the week, print the name of the next day in upper case.
If the input is not a valid day of the week, print:
Invalid Input

Solution:

import java.util.Scanner;

enum Day {
    MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY;

    public Day nextDay() {
        return values()[(this.ordinal() + 1) % values().length];
    }
}

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String input = sc.nextLine().toUpperCase();

        try {
            Day currentDay = Day.valueOf(input);
            System.out.println(currentDay.nextDay());
        } catch (IllegalArgumentException e) {
            System.out.println("Invalid Input");
        }
    }
}

2. Problem Statement

Sharon is creating a program that finds the first repeated element in an integer array. The program should efficiently identify the first element that appears more than once in the given array. If no such element is found, it should appropriately display a message.

Help Sharon to complete the program.

Input format:
The first line of input consists of an integer n, representing the number of elements in the array.
The second line consists of n space-separated integers, representing the array elements.

Output format:
If a repeated element is found, print the first element that appears more than once.
If no repeated element is found, print:
No repeated element found in the array

Solution:

public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] arr = new int[n];

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

        boolean found = false;
        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < n; j++) {
                if (arr[i] == arr[j]) {
                    System.out.println(arr[i]);
                    found = true;
                    break;
                }
            }
            if (found) {
                break;
            }
        }

        if (!found) {
            System.out.println("No repeated element found in the array");
        }
    }