Level 2 Tasks-(Java)

 

🟡 Level 2 – (Arrays & Strings)


1. Fruit Shop Stock

Story:
A shopkeeper wants to know total apples in all baskets.
Why: Introduces array sum & max.
Logic:

  • Input: [10, 20, 30, 40].
  • Loop → add all = total apples.
  • Track max using if.
Code :

import java.util.Scanner;
public class FruitStock 
{
  public static void main(String[] args) 
  {
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter basket counts separated by space: ");
   
    String line = sc.nextLine().trim();
    String[] parts = line.split("\\s+");
    int total = 0;
    int max = Integer.MIN_VALUE;
    for (String p : parts) 
    {
      int v = Integer.parseInt(p);
      total += v;
      if (v > max) max = v;
    }
    System.out.println("Total apples = " + total);
    System.out.println("Max in a basket = " + max);
    sc.close();
  }
}

Output :





2. Cinema Ticket Booking

Story:
Cinema hall has limited seats. Booking system must prevent overbooking.
Why: Teaches condition checking.
Logic:

  • Input: tickets requested.
  • If requested ≤ available seats → confirm booking.
  • Else → "House Full".
Code :

import java.util.Scanner;
public class CinemaBooking 
{
  public static void main(String[] args) 
  {
    final int availableSeats = 50;
    Scanner sc = new Scanner(System.in);
    System.out.print("Tickets requested: ");
    int req = sc.nextInt();
    if (req <= 0) System.out.println("Enter a positive number");
    else if (req <= availableSeats) System.out.println("Booking confirmed for " + req + " tickets.");
    else System.out.println("House Full");
    sc.close();
  }
}

Output :





3. Bank ATM Withdrawal

Story:
A customer withdraws money from ATM.
Why: Helps in balance calculations.
Logic:

  • Input: balance and withdrawal.
  • If withdrawal ≤ balance → deduct.
  • Else → "Insufficient Funds".
Code :

import java.util.Scanner;
public class ATMWithdrawal 
{
  public static void main(String[] args) 
  {
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter balance: ");
    double balance = sc.nextDouble();
    System.out.print("Enter withdrawal amount: ");
    double w = sc.nextDouble();
    if (w <= 0) System.out.println("Enter a positive withdrawal amount.");
    else if (w <= balance)
     {
      balance -= w;
      System.out.printf("Withdrawal successful. Remaining balance = %.2f%n", balance);
    } 
    else System.out.println("Insufficient Funds.");
    sc.close();
  }
}

Output :





4. Train Reservation Waiting List

Story:
Trains have limited confirmed + waiting seats. Extra passengers must be rejected.
Why: Practice with conditions.
Logic:

  • Confirmed = 5, Waiting = 3.
  • Input = number of passengers.
  • If ≤5 → confirmed.
  • If ≤8 → waiting list.
  • Else → "No Seats Available".
Code :

import java.util.Scanner;
public class TrainReservation
 {
  public static void main(String[] args) 
  {
    final int CONFIRMED = 5;
    final int WAITING = 3; 
    Scanner sc = new Scanner(System.in);
    System.out.print("Number of passengers: ");
    int n = sc.nextInt();
    if (n <= CONFIRMED) System.out.println("All confirmed (" + n + " seats).");
    else if (n <= CONFIRMED + WAITING) 
    {
      int wcount = n - CONFIRMED;
      System.out.println("Confirmed = " + CONFIRMED + ", Waiting = " + wcount);
    } else 
    {
      System.out.println("No Seats Available");
    }
    sc.close();
  }
}

Output :





5. Election Vote Counting

Story:
In a small election, votes are cast for candidates A, B, C.
Why: Great for counting array/string frequency.
Logic:

  • Input: votes list.
  • Loop → count each candidate’s votes.
  • Compare to find winner.

Code :


import java.util.Scanner;
public class VoteCount 
{
  public static void main(String[] args) 
  {
    Scanner sc = new Scanner(System.in);
    System.out.print("Number of votes: ");
    int n = sc.nextInt();
    System.out.println("Enter votes (A or B or C), one per line:");
    int a=0,b=0,c=0;
    for (int i=0;i<n;i++) 
    {
      String vote = sc.next().trim().toUpperCase();
      if (vote.equals("A")) a++;
      else if (vote.equals("B")) b++;
      else if (vote.equals("C")) c++;
      else System.out.println("Invalid vote ignored: " + vote);
    }
    System.out.println("Votes - A: " + a + ", B: " + b + ", C: " + c);
    if (a> b && a > c) System.out.println("Winner: A");
    else if (b > a && b > c) System.out.println("Winner: B");
    else if (c > a && c > b) System.out.println("Winner: C");
    else System.out.println("Result: Tie or no clear winner");
    sc.close();
  }
}

Output :






Comments

Popular posts from this blog