Level 6 — Programming — Mar/Apr 2025 — Practical
Level 6 • March/April 2025 • Practical assessment
Work through each question before checking the answer. These revision answers are prepared for learners; they are not an official marking scheme.
Question 1
Answer
Save as ParkingLot.java.
import java.util.Scanner;
public class ParkingLot {
public static void main(String[] args) {
final int CAPACITY = 10;
int cars = 0; boolean running = true;
Scanner in = new Scanner(System.in);
while (running) {
System.out.println("1 Entry\n2 Car exit\n3 Status\n4 Quit");
int choice;
try { choice = Integer.parseInt(in.nextLine()); }
catch (NumberFormatException ex) { System.out.println("Enter 1-4"); continue; }
switch (choice) {
case 1:
if (cars < CAPACITY) { cars++; System.out.println("Car added"); }
else System.out.println("Parking lot is full");
break;
case 2:
if (cars > 0) { cars--; System.out.println("Car exited"); }
else System.out.println("Parking lot is empty");
break;
case 3:
System.out.printf("Cars: %d; available spaces: %d%n", cars, CAPACITY-cars);
break;
case 4: running = false; break;
default: System.out.println("Enter 1-4");
}
}
}
}
| Test | Expected |
|---|---|
| Exit when count0 | Empty message; count stays0 |
| 10 successful entries | Count10; available0 |
| 11th entry | Full message; count stays10 |
| Exit from10 | Count9; available1 |
| Invalid menu input | No count change; prompt again |
Question 2
Answer
| Crop | Temperature°C | Rainfallmm | Humidity% |
|---|---|---|---|
| Rice | 20–35 | 100–300 | 60–90 |
| Wheat | 10–25 | 50–100 | 40–60 |
| Maize | 18–27 | 60–150 | 50–70 |
| Sugarcane | 20–40 | 100–300 | 70–90 |
The introductory requirement says recommendations use total rainfall for15 days, while task4 asks for rainfall average. The solution calculates and displays both, and uses the total for the recommendation. Several crops can match; the paper gives no rule to rank them, so show all matches.
Save as CropRecommendation.java.
import java.util.Scanner;
public class CropRecommendation {
static double read(Scanner in, String prompt, double min, double max) {
while (true) {
System.out.print(prompt);
try {
double value = Double.parseDouble(in.nextLine());
if (Double.isFinite(value) && value >= min && value <= max) return value;
} catch (NumberFormatException ex) { }
System.out.println("Enter a finite value in the allowed range.");
}
}
static boolean between(double x, double low, double high) { return x >= low && x <= high; }
public static void main(String[] args) {
final int DAYS = 15;
double[] temperature = new double[DAYS], rainfall = new double[DAYS],
humidity = new double[DAYS];
double totalTemp = 0, totalRain = 0, totalHumidity = 0;
Scanner in = new Scanner(System.in);
for (int day = 0; day < DAYS; day++) {
System.out.println("Day " + (day + 1));
temperature[day] = read(in, "Temperature C: ", -Double.MAX_VALUE, Double.MAX_VALUE);
rainfall[day] = read(in, "Rainfall mm: ", 0, Double.MAX_VALUE);
humidity[day] = read(in, "Humidity percent: ", 0, 100);
totalTemp += temperature[day]; totalRain += rainfall[day];
totalHumidity += humidity[day];
}
double averageTemp = totalTemp/DAYS, averageRain = totalRain/DAYS,
averageHumidity = totalHumidity/DAYS;
System.out.printf("Average temperature: %.2f C%nTotal rainfall: %.2f mm%nAverage rainfall: %.2f mm/day%nAverage humidity: %.2f%%%n",
averageTemp, totalRain, averageRain, averageHumidity);
String[] crops = {"Rice", "Wheat", "Maize", "Sugarcane"};
double[][] limits = {{20,35,100,300,60,90}, {10,25,50,100,40,60},
{18,27,60,150,50,70}, {20,40,100,300,70,90}};
boolean found = false;
for (int i = 0; i < crops.length; i++) {
double[] r = limits[i];
if (between(averageTemp,r[0],r[1]) && between(totalRain,r[2],r[3]) &&
between(averageHumidity,r[4],r[5])) {
System.out.println("Matching crop: " + crops[i]); found = true;
}
}
if (!found) System.out.println("No crop matches all supplied conditions.");
}
}
- Illustration: each of15 days has24°C,10mm rainfall and65% humidity.
- Temperature average=(15×24)/15=24°C; humidity average=(15×65)/15=65%.
- Total rainfall=15×10=150mm; daily average=150/15=10mm.
- 24°C,150mm total and65% fit both Rice and Maize in the supplied table. Wheat fails humidity; Sugarcane fails the70% minimum.
This implements the assessment table, rather than a full agronomic decision model. Test inclusive boundaries, no-match conditions, invalid humidity and all15 entries.