Java Practice Lab: A Gym Weekly Tracker with Loops, Arrays and Methods
One week of workout minutes {45, 0, 30, 60, 0, 90, 20} becomes a report: total 245, best day Day 6 with 90 min, badge Gold. Four small Java programs use an else-if ladder, for with continue and break, methods and an index tracker.
What you will learn
- Award a badge with an else-if ladder where the first true test wins
- Walk an array of daily minutes with for, continue and break
- Split the work into a void method and a method that returns a value
- Find the best day by tracking an index, and combine everything into one report
Before you start
- Java arrays
- Java methods and overloading
- Java break, continue and nested loops
Someone works out during the week, and our programs turn their minutes into a report with a badge. Every program in this lab uses the same seven numbers:
int[] minutesPerDay = {45, 0, 30, 60, 0, 90, 20};
Index 0 is Day 1, index 6 is Day 7. Days 2 and 5 are rest days. The finished report says: total 245 minutes, best day Day 6, badge Gold.
Program 1: award the badge
package org.studyeasy.s02l16;
public class Ex01_BadgeDecisions {
public static void main(String[] args) {
int totalMinutes = 245;
String badge;
if (totalMinutes >= 240) {
badge = "Gold";
} else if (totalMinutes >= 150) {
badge = "Silver";
} else if (totalMinutes >= 60) {
badge = "Bronze";
} else {
badge = "Keep going";
}
System.out.println("Minutes: " + totalMinutes);
System.out.println("Badge: " + badge);
}
}
Minutes: 245
Badge: Gold
Java starts at the top rung: is 245 at least 240? Yes, so the badge is Gold and the Silver and Bronze tests never run. The first true test wins (see if, else and switch). Change the first test to > 245 and Gold is skipped: the badge becomes Silver.
Program 2: walk the week
package org.studyeasy.s02l16;
public class Ex02_WeekLoops {
public static void main(String[] args) {
int[] minutesPerDay = {45, 0, 30, 60, 0, 90, 20};
int total = 0;
for (int i = 0; i < minutesPerDay.length; i++) {
total += minutesPerDay[i];
}
System.out.println("Week total: " + total);
for (int i = 0; i < minutesPerDay.length; i++) {
if (minutesPerDay[i] == 0) {
continue;
}
System.out.println("Day " + (i + 1) + ": " + minutesPerDay[i] + " min");
if (minutesPerDay[i] >= 60) {
break;
}
}
}
}
Week total: 245
Day 1: 45 min
Day 3: 30 min
Day 4: 60 min
The first loop keeps a running total: 45, 45, 75, 135, 135, 225, 245. The second adds two rules. A rest day (0 minutes) hits continue, which skips the print and moves to the next day. The first day with 60 minutes or more hits break, and that's Day 4, so Days 5 to 7 are never checked. In this week continue only fires once, on Day 2. Delete the continue line and "Day 2: 0 min" prints too (the loop still stops at Day 4).
Program 3: put it in methods
package org.studyeasy.s02l16;
public class Ex03_DayMethods {
public static void main(String[] args) {
int[] minutesPerDay = {45, 0, 30, 60, 0, 90, 20};
for (int day = 1; day <= 7; day++) {
printDay(day, minutesPerDay);
}
System.out.println("Total: " + totalMinutes(minutesPerDay) + " min");
}
static void printDay(int day, int[] minutes) {
System.out.println("Day " + day + ": " + minutes[day - 1] + " min");
}
static int totalMinutes(int[] minutes) {
int sum = 0;
for (int i = 0; i < minutes.length; i++) {
sum += minutes[i];
}
return sum;
}
}
Day 1: 45 min
Day 2: 0 min
Day 3: 30 min
Day 4: 60 min
Day 5: 0 min
Day 6: 90 min
Day 7: 20 min
Total: 245 min
printDay is void: it does its job and gives nothing back. It uses minutes[day - 1] because days start at 1 and the array starts at 0. totalMinutes returns a number, 245, to main. Call printDay(8, minutesPerDay) and there's no index 7: the program stops with an exception.
Finding the best day
Start by saying the best is Day 1 (index 0). Days 2 and 3 are smaller. Day 4 has 60, more than 45, so the marker moves. Day 6 has 90, more than 60, so it moves again. Day 7's 20 doesn't beat 90, so the marker stays on index 5.
The weekly report
package org.studyeasy.s02l16;
public class Lab2_WeeklyReport {
public static void main(String[] args) {
int[] minutesPerDay = {45, 0, 30, 60, 0, 90, 20};
int total = 0;
int best = 0;
for (int i = 0; i < minutesPerDay.length; i++) {
total += minutesPerDay[i];
if (minutesPerDay[i] > minutesPerDay[best]) {
best = i;
}
}
System.out.println("---- Weekly Report ----");
System.out.println("Total: " + total + " min");
System.out.println("Best day: Day " + (best + 1) + " with " + minutesPerDay[best] + " min");
System.out.println("Badge: " + badge(total));
}
static String badge(int total) {
if (total >= 240) {
return "Gold";
} else if (total >= 150) {
return "Silver";
} else if (total >= 60) {
return "Bronze";
}
return "Keep going";
}
}
---- Weekly Report ----
Total: 245 min
Best day: Day 6 with 90 min
Badge: Gold
One loop keeps two running values: total adds the minutes, best remembers the index of the best day. best is 5, so the report prints best + 1, Day 6. The badge is the same ladder as program 1, moved into a method where each branch returns, so the first true test ends the method.
Start with int best = 6; and the report still says Day 6: 45 beats the starting 20 on the first pass, and 90 wins later.
Recap
| Idea | Where it shows up |
|---|---|
| else-if ladder | the badge: first true test wins |
for + running total | 245 minutes |
continue / break | skip rest days, stop at the first 60+ day |
| void vs returning method | printDay vs totalMinutes and badge |
| index tracker | best moves when a day beats it |
Try it yourself: add a restDays count to the report (it should be 2), and a method that returns the average minutes per active day.
StudyEasy AI Community
Learn AI engineering, share what you build, and connect with other learners.
Free resources: lesson files, notebooks and code
Ask questions and join engineering discussions
Meet real people learning and building with AI
Related course on Udemy
FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE
Java 25, JSP, Hibernate, Spring Boot 4, REST APIs, React and Spring AI.View on Udemy ↗: FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE (paid course, opens in a new tab)Related posts
Install Java 25 (JDK) and IntelliJ IDEA, Then Run Your First Program
Install JDK 25 (Microsoft Build of OpenJDK) with JAVA_HOME, install IntelliJ IDEA, then point a run configuration at the JDK and the main class and run Hello World.
Use Claude Agent, Copilot or Junie Inside IntelliJ IDEA
Open IntelliJ's AI Chat, pick Claude Agent (the agent behind Claude Code) or Copilot and a model, ask in plain words for Main.java to print the date and time, review the change, and run it.
Java Hello World with Maven: What the JDK Does and What Maven Adds
javac compiles HelloWorld.java into bytecode and java runs it on the JVM. Maven adds one pom.xml, the same folders in every project and one build command. Then a real Maven Hello World in IntelliJ.
Java Variables, Keywords and Naming Rules
A variable is a labelled jar that holds one value at a time. Four small programs show declaring, reassigning, the names Java accepts, the keywords it reserves, and why daysUntilLaunch beats d.
