Single logo
  • Courses
  • Blog
  • About
Join on SkoolJoin on Skool

In this topic

Java
  • Install Java 25 (JDK) and IntelliJ IDEA, Then Run Your First Program
  • Use Claude Agent, Copilot or Junie Inside IntelliJ IDEA
  • Java Hello World with Maven: What the JDK Does and What Maven Adds
  • Java Variables, Keywords and Naming Rules
  • Maven in IntelliJ: pom.xml, Dependencies and the Build Lifecycle
  • Java Primitive Types and Memory: What int x = 5 Really Does
  • Java Strings and Text Blocks: Quotes, Concatenation and parseInt
  • Java Operators: Arithmetic, a++ vs ++a, Precedence, && || and the Ternary
  • Java Type Casting: Widening, Narrowing and Converting Text to Numbers
  • Java Practice Lab: A Cafe Bill with Variables, Operators and Casting
  • Java if, else and switch: Braces, Else-If Ladders and the Modern Switch
  • Java Loops: for, while, do-while and for-each
  • Java break, continue and Nested Loops
  • What's New in Java 21 to 25: Compact Source Files and Sequenced Collections
  • Java Arrays: Indexes, Default Values, Loops and 2D Grids
  • Java Methods and Overloading: Parameters, return and Pass-by-Value
  • Java Scope, Code Blocks and the Call Stack
  • Read User Input in Java with Scanner (and the Leftover Enter Trap)
  • Java Practice Lab: A Gym Weekly Tracker with Loops, Arrays and Methods
  • Debugging Java in IntelliJ: Stack Traces, Breakpoints and a Better AI Prompt
  • Java var: Let Java Infer the Type
  • Push and Pull in IntelliJ: Two Developers, One GitHub Repository, a Rejected Push and a Conflict
  • Java Classes and Objects: One Blueprint, Many Cars
  • Java Constructors and the this Keyword
  • Java Instance Methods, Getters, Setters and toString
  • Java static: Fields, Methods and Blocks Shared by the Whole Class
  • Java Packages and Imports: Every Class Has an Address
  • Java Access Modifiers and Encapsulation: Private Data, Public Methods
  • Java final and Immutable Objects: Assign Once, Share Safely
  • Java Strings as Objects: The String Pool, == vs equals, StringBuilder and printf
  • Java Practice Lab: A Library Book Catalogue with final Fields and StringBuilder
  • Java Inheritance: extends, super and the Order Objects Are Built
  • Java Method Overriding, @Override and the Object Class
  • Java Polymorphism: Upcasting, Dynamic Dispatch and Downcasting
  • Java Composition: Has-A vs Is-A
  • Java Abstract Classes: A Parent That Leaves One Blank
  • Java Interfaces: Contracts and Multiple Inheritance
  • Java Nested, Inner and Anonymous Classes
  • Java Enums with Fields, Methods and switch
  • Java Practice Lab: Payment Processing with an Interface, an Abstract Class and an Enum
  • Java Records: A Whole Data Class in One Line
  • Java Sealed Classes and Interfaces
  • Java Pattern Matching with instanceof and switch
  • Java Record Patterns: Unpacking a Record
  • Java equals and hashCode: Why a HashSet Needs Both
  • From Arrays to Collections: The Java Collections Framework Map
  • Java Wrapper Classes and Autoboxing
  • Java ArrayList: The Stretchy Array
  • Java LinkedList vs ArrayList: Nodes or an Array
  • Java Sets: HashSet, LinkedHashSet and TreeSet
  • Java Sorting with Comparable and Comparator
  • Java Maps: HashMap, LinkedHashMap and TreeMap
  • Java Stacks, Queues and Deques: ArrayDeque and PriorityQueue
  • Choosing a Java Collection: Immutable and Sequenced Collections
  • Java Exception Handling: try, catch and finally
  • Java throw, throws, Checked vs Unchecked and Custom Exceptions
  • Java try-with-resources and Clean Error Handling
  • Java Generics: Type-Safe Containers
  • Java Generic Methods, Bounded Types and Wildcards
  • Java Lambdas and Functional Interfaces
  • Java Built-in Functional Interfaces: Predicate, Function, Consumer, Supplier
  • Java Optional: Saying Maybe Without null
  • Java Streams I: filter, map and reduce
  • Java Streams II: Collectors, groupingBy and flatMap
  • Why Concurrency: Processes, Threads and the Main Thread in Java
  • Creating Threads in Java: Thread, Runnable and Lambdas
  • Java Thread Lifecycle: sleep, join and interrupt
  • Java Race Conditions and synchronized
  • Java volatile and Atomic Variables
  • Java ExecutorService and Thread Pools
  • Java Callable, Future and CompletableFuture
  • Java Locks, Deadlock and tryLock
  • Java Concurrent Collections and Virtual Threads
  • Why Slow Work Belongs off the Request Thread: 202 Accepted and a Job Id in Spring Boot
  • Virtual Threads in Spring Boot: One Property, and the Connection Pool Trap
  • @Async, Named TaskExecutors and Structured Concurrency in Spring Boot
  • Java Files and Directories with NIO.2: Path and Files
  • Reading and Writing Text Files in Java
  • Java Structured Data: Reading and Writing JSON and CSV
  1. Blog
  2. /
  3. Java
  4. /
  5. Basics

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.

Chaand Sheikh

·27 November 2026·Updated 28 November 2026Basics

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

Bars for the seven days: 45, 0, 30, 60, 0, 90, 20 minutes. The best marker starts on Day 1, moves to Day 4 when 60 beats 45, moves to Day 6 when 90 beats 60, and stays on Day 6 because 20 doesn't beat 90. 4503060 09020 Day 1Day 2Day 3Day 4 Day 5Day 6Day 7 startmovesbest = 5
best holds an index and moves only when the current day beats the day it points at.

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

IdeaWhere it shows up
else-if ladderthe badge: first true test wins
for + running total245 minutes
continue / breakskip rest days, stop at the first 60+ day
void vs returning methodprintDay vs totalMinutes and badge
index trackerbest 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.

Keep going in Java · 19 of 79

Previous
Read User Input in Java with Scanner (and the Leftover Enter Trap)
Next post
Debugging Java in IntelliJ: Stack Traces, Breakpoints and a Better AI Prompt
Community · Free to join
Single logo

StudyEasy AI Community

Hosted onSkool

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

Join free on Skool

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.

On this page

  • Program 1: award the badge
  • Program 2: walk the week
  • Program 3: put it in methods
  • Finding the best day
  • The weekly report
  • Recap
Community · Free to join
Single logo

StudyEasy AI Community

Hosted onSkool

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

Join free on Skool

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)

Single logo

Empowering Education, One Course at a Time

About
About usContact us
Legal
Terms of UsePrivacy Policy

© 2026 StudyEasy Organization. All rights reserved.