Level 3 Tasks-(Java)
🔴
Level 3 – (Logic & Real-Life Simulation)
1. Library Fine System
Story:
Library charges fines for late returns.
Why: Introduces multi-level conditions.
Logic:
- If days late ≤ 7 → fine = 0.
- If ≤ 12 → fine = (daysLate - 7) × 2.
- Else → fine = (daysLate - 12) × 5 + 10.
Code :
import java.util.*;
class LibraryFine {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter days late: ");
int d = sc.nextInt(), fine = 0;
if (d <= 7) fine = 0;
else if (d <= 12) fine = (d - 7) * 2;
else fine = (d - 12) * 5 + 10;
System.out.println("Fine = " + fine);
}
}
Output :
2. Railway Platform Allotment
Story:
Trains arrive at different times. Railway must plan minimum platforms.
Why: Real-world scheduling logic.
Logic:
- Input: arrival & departure times.
- Sort times.
- Count overlapping trains → max overlap = platforms
needed.
Code :
import java.util.*;
class RailwayPlatform {
public static void main(String[] args) {
int arr[] = {900, 940, 950, 1100, 1500, 1800};
int dep[] = {910, 1200, 1120, 1130, 1900, 2000};
Arrays.sort(arr); Arrays.sort(dep);
int i=0, j=0, plat=0, maxPlat=0, n=arr.length;
while (i<n && j<n) {
if (arr[i] <= dep[j]) { plat++; i++; }
else { plat--; j++; }
maxPlat = Math.max(maxPlat, plat);
}
System.out.println("Min Platforms Needed = " + maxPlat);
}
}
Output :
3. Festival Lottery Draw
Story:
Village fair draws a winner randomly.
Why: Introduces random number generation.
Logic:
- Input: names array.
- Generate random index.
- Print winner = names[randomIndex].
Code :
import java.util.*;
class Lottery {
public static void main(String[] args) {
String names[] = {"Ravi","Sita","Kumar","Anu","Priya"};
Random r = new Random();
int win = r.nextInt(names.length);
System.out.println("Winner = " + names[win]);
}
}
Output :
4. Exam Marks Analysis
Story:
School wants average marks and topper.
Why: Practice array calculations.
Logic:
- Input: marks array.
- Loop → sum → average = sum / n.
- Track max = topper.
Code :
class MarksAnalysis {
public static void main(String[] args) {
int marks[] = {78, 92, 85, 66, 99};
int sum=0, max=marks[0], topper=0;
for (int i=0;i<marks.length;i++) {
sum+=marks[i];
if (marks[i]>max) { max=marks[i]; topper=i; }
}
double avg = (double)sum/marks.length;
System.out.println("Average = " + avg);
System.out.println("Topper = Student " + (topper+1) + " with " + max);
}
}
Output :
5. Supermarket Bill with Multiple Items
Story:
Supermarket bills customers for multiple items.
Why: Realistic array + multiplication logic.
Logic:
- Input:
Items = { Rice=50, Oil=120, Soap=40 }
Quantities = { 2, 1, 3 } - Total = sum of (price × quantity).
Code :
class SupermarketBill {
public static void main(String[] args) {
String items[] = {"Rice","Oil","Soap"};
int price[] = {50,120,40};
int qty[] = {2,1,3};
int total=0;
for (int i=0;i<items.length;i++) {
int amt = price[i]*qty[i];
System.out.println(items[i]+": "+qty[i]+" x "+price[i]+" = "+amt);
total+=amt;
}
System.out.println("Total Bill = " + total);
}
}
Output :
Comments
Post a Comment