- Lab
-
Libraries: If you want this lab, consider one of these libraries.
- Core Tech
Java Base Camp: Interactive Console
The base camp runs a small console kiosk where guides check hikers in before a trek. The kiosk needs to clean up messy trail codes typed at registration, print clear check-in confirmations (and route problems to an error stream so the guide's terminal logs stay readable), ask each hiker how many nights they plan to camp, and start up with the camp's name and capacity supplied as launch arguments so the same jar can run for any camp. You will build each of these pieces, then finish with a tiny standalone digest tool written in Java's newest compact source style.
Lab Info
Table of Contents
-
Challenge
Step 1: Arrive at the base camp
Java Base Camp: Interactive Console
The base camp runs guided treks out of a single console kiosk, and right now that kiosk is half-built. Guides use it to register hikers before they set out, and it has four jobs: clean up trail codes that hikers scribble on registration slips, print clear check-in confirmations without burying them in error noise, ask each hiker how many nights they're camping, and start up already knowing which camp it's running at. You'll finish each of these pieces in order, then wire up a tiny closing-digest tool using Java's newest compact source syntax. ## Where you'll work
All of your edits live under
src/main/java/lab/:TrailCode.java-- normalizes messy trail codes typed at the kiosk.ConsoleMessages.java-- builds and routes check-in and alert text.CheckIn.java-- reads a hiker's arrival note and stay length from the terminal.BaseCampConsole.java-- reads the camp's name and capacity from launch arguments.DailyDigest.java-- a standalone compact-source program for the closing digest.
Each file already compiles with
// TODOstubs marking exactly what you'll fill in. Nothing else in the project needs to change. ## How the checks workEvery task you complete is checked by running the project's test suite with
mvn --batch-mode --quiet test. Each task maps to one test method, so you can tell exactly which piece of behavior is still missing. Work through the steps in order -- later files assume the earlier ones already behave correctly. When you're ready, head into Step 2 and start with the trail codes.info> If you get stuck, you can refer to the provided solution code for each task, available in the
solutionfolder. -
Challenge
Step 2: Clean up hiker trail codes
Step 2: Clean up hiker trail codes
Every hiker checking in at the base camp hands over a trail code scrawled on a registration slip -- sometimes with spaces, sometimes with dashes, sometimes in lowercase. Before the kiosk can match that code against anything, it needs two things: a quick way to describe the raw code as the hiker wrote it, and a way to normalize it into one consistent format for comparison. You'll build both in
TrailCode.java, usingString'slength()andreplace()methods -- the same tools you'll lean on throughout this lab whenever raw input needs shaping into something the rest of the app can trust. WithdescribeCodereporting the raw code and its length, the next problem is that the same trail code can arrive in wildly different shapes:"tc 104","TC-104", and"tc104"should all be treated as one code.Stringmethods chain naturally for this -- each.replace(...)call returns a new string, so you can pipe the result straight into the next call and finish with.toUpperCase()to settle case once and for all.normalizeCodebuilds that pipeline, whiledescribeCodekeeps reporting on the untouched original -- the paper log always needs the code exactly as the hiker wrote it. -
Challenge
Step 3: Announce check-ins and alerts
Step 3: Announce check-ins and alerts
A hiker with a clean trail code still needs to hear back from the kiosk. Right now
ConsoleMessages.javahas the shape of that conversation but none of the words or wiring: it needs to assemble a confirmation message, print that confirmation where the guide can see it, and push low-supply warnings somewhere they won't get lost in the normal check-in log. Java gives you string concatenation and escape sequences to build the text, and two separate streams --System.outandSystem.err-- to keep routine confirmations apart from things that need attention. ## Building multi-line text with+and escape sequencesThe
+operator concatenates strings, and escape sequences like\n(newline) and\t(tab) let you shape plain text into something readable in a terminal without any special formatting library."a" + "\n\t" + "b"producesa, a newline, a tab, thenb.buildCheckInMessageneeds exactly this kind of layout: a header line, then the hiker's name and trail code each on their own indented line. ## Standard output: where confirmations belongBuilding a string and printing it are separate jobs --
buildCheckInMessagehands back text, and something else has to decide where that text goes.System.out.printlnwrites to standard output, the stream a terminal shows by default and the one guides will actually be watching as hikers check in.printCheckInjust needs to forward the message it's given straight to that stream. ## Standard error: where alerts belongStandard output is for the expected, routine flow -- confirmations, one after another. Low-supply warnings are a different kind of message: they need to stand out from that flow, and many log setups filter or highlight
System.errseparately fromSystem.outfor exactly this reason.printLowSupplyAlertbuilds its own short, labeled string and sends it throughSystem.err.printlninstead, so a guide scanning error logs won't have to dig through normal check-in traffic to find it. -
Challenge
Step 4: Interview hikers at the kiosk
So far the kiosk has cleaned up trail codes and printed status messages to the right stream, but it has never actually listened to a hiker. A check-in kiosk that only talks isn't a kiosk -- it needs to read what's typed at the terminal. Java's
java.util.Scannerwraps an input source (here,System.in) and gives you methods that pull typed text off it a line, a word, or a number at a time. The most basic of these isnextLine(), which blocks until the hiker presses Enter and then hands back everything they typed on that line, unmodified. You'll use it inCheckIn.javato capture the free-text note a hiker gives about their arrival. An arrival note is free text, so returning it untouched was the right call. The next thing the kiosk needs from the hiker is a number -- how many nights they plan to camp -- and numbers typed at a terminal always arrive as text too.Scannerhas no built-innextInt()-then-forget-the-line-ending trick that's safe to mix withnextLine(), so the reliable pattern is: read the line as a string,trim()it in case the hiker left a stray space, then hand it toInteger.parseInt(...)to get a realint. Skip the trim and a trailing space turns a perfectly good "3" into aNumberFormatExceptionat parse time. You now have two pieces sitting in the caller's hands: the raw arrival note and a parsedintfor nights. The kiosk's job is to turn those into one line a guide can read at a glance. String concatenation with+handles this without any extra conversion -- when Java sees aninton one side of a+with aStringon the other, it converts the number to its text form automatically, so you can build the whole summary in a single expression. -
Challenge
Step 5: Launch the camp and log the day
The kiosk has cleaned up trail codes, printed status and alerts to the right streams, and read a hiker's answer from the keyboard. The last piece it's missing is knowing which camp it's running at and how big that camp is -- neither of those should be hard-coded, because the same jar has to work for any base camp. Java programs get this kind of launch-time configuration through the
String[] argsarray that's handed tomain: whatever text follows the program's name on the command line (or in an IDE's "program arguments" field) shows up there, in order, as plain strings.args[0]is the first argument,args[1]is the second, and so on -- there is no built-in name lookup, just position. Because the array is only as long as what the launcher supplied,args.lengthtells you how many values actually arrived, and indexing past it throws anArrayIndexOutOfBoundsException. OpenBaseCampConsole.java. The launch arguments for this kiosk are always the camp's name followed by its capacity, so reading the name is just readingargs[0]. Capacity arrives the same way, but as text --args[1]is the string"40", not the number 40.Integer.parseIntconverts a numeric string into anintso it can be used in arithmetic or printed as a number rather than concatenated as a character sequence. Before reading either value, the banner should checkargs.lengthso a kiosk launched without a capacity prints a clear usage message rather than crashing. The startup banner needs both pieces of information stitched into one line the guide sees the moment the console starts. For the final piece, step outside the console entirely.DailyDigest.javais a tiny standalone program that prints one closing line at the end of the day, and it's written in Java's newest compact source form. Ordinarily every Java program needs apublic classdeclaration and amainmethod stampedpublic static void main(String[] args), even when the program never usesargsor needs more than one class. Compact source files let a file with a single top-level type skip thepublic class Name { ... }wrapper and theString[] argsparameter, and they pull injava.lang.IO's helpers automatically, soIO.println(...)is available without an import orSystem.out. The starter already gives you this shape; your job is just to fill in the one line the digest prints. -
Challenge
Step 6: Run the application
Every task is done, and each one was graded the moment you completed it. What you have not done yet is watch the whole application run at once -- which is the only place the pieces you wrote separately become a single working program.
Start the application
Open the Terminal tab and run:
mvn --batch-mode --quiet compile && java -cp target/classes lab.MainThe program runs in the terminal and prints its output there. Every behaviour it shows is code you wrote in the previous steps, so read the output against what each task asked for -- that is the whole lab, working as one program. ### Run the checks yourself
The lab ran each task's test for you as you went, but they are ordinary tests and you can run them whenever you like. The whole suite:
mvn --batch-mode --quiet testOr one task at a time:
bash runTest.sh task_2_1Each failure names the task it belongs to, so the output tells you which step to go back to rather than which line to stare at. Two things worth trying before you finish: break something on purpose and watch the matching check fail, then put it back. Knowing what a failure looks like is worth as much as knowing what a pass looks like, and it costs you nothing here.
When you are done, everything you wrote is still in the editor tabs -- the lab is yours to keep reading.
About the author
Real skill practice before real-world application
Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.
Learn by doing
Engage hands-on with the tools and technologies you’re learning. You pick the skill, we provide the credentials and environment.
Follow your guide
All labs have detailed instructions and objectives, guiding you through the learning process and ensuring you understand every step.
Turn time into mastery
On average, you retain 75% more of your learning if you take time to practice. Hands-on labs set you up for success to make those skills stick.