Exercise Monday

Goal: Today, we'll first work through some in-class lessons, and then, we'll temporarily put aside testing and writing logic in order to give our full attention to learning Spark. Spend the rest of the day digesting the topics we’ve covered over the last few class days and earlier today. Tomorrow we'll resume creating back-end logic, too. Take advantage of this time to familiarize yourself with Spark before we begin integrating additional concepts and complexities.

Warm Up

  • What is Spark used for?
  • What is a route? When is a route executed?
  • What is the benefit of using templates?



First, expand the Handlebars concepts you learned in the weekend homework by reading through the following lessons carefully with your partner:

Adding a Spark Interface to Change Machine

Have you seen those coin slides used at cash registers that automatically return the correct amount of change?

Take a look at this project. This is the logic for a Change Machine. You can fork it and clone it down, and open it in IntelliJ, or just look at the logic on Github. Each ChangeMachine object contains a limited number of coins; 20 of each. When you provide a dollar amount, it provides you change using the smallest number of coins it can (taking into account its limited inventory). All program logic is present, and tests are included. You should be able to run the tests, and see that they all pass.

However, this program has no user interface! Use this logic to create a web application in Spark with Handlebars. The user should be met with a form on the homepage that allows them to place a dollar amount into the form (ie: 1.37). When they submit the form, they should be taken to a page detailing the coin combinations used to create change (ie: "Quarters: 5, Dimes: 1, Nickels: 0, Pennies: 2").

In your App.java routes, you'll need to retrieve their input from the form, make sure it's in the correct format to use with the existing ChangeMachine logic, call the method(s) included in the ChangeMachine class to calculate the coin combinations, then display the results a new page.

When you get everything up and running, add functionality to let the user know if the change machine doesn't have enough coins to provide change for their dollar amount.


Some Change Machine methods take a Float argument. Float stands for "floating point number", which essentially just refers to a number with a decimal point in it. Floats often include an f, like this: float testFloat = 0.25f. The lowercase float represents its primitive type, and the uppercase Float represents its corresponding wrapper class.

You can also transform a String into a Float like this:

> Float.parseFloat("10.33");
java.lang.Float res0 = 10.33

Adding Spark to Previous Projects

Now that you've successfully followed along to make multiple Spark applications, and built front-ends onto provided Java logic, revisit some of your own Java projects to add a Spark front end.

Begin by choosing a project one partner completed last week, and add a Spark front-end. Then, if you're able to finish that project with time to spare, tackle a project the other partner built last week. Continue building front-ends for the remainder of the afternoon.

Additionally, if you'd like to style your web applications, take a sneak peek at one of this week's homework lessons: Adding CSS to Spark Applications

Peer Code Review

  • Code has proper indentation and spacing
  • Variable names are descriptive
  • Tests are in place for back-end logic. All tests pass.
  • Spark setup is complete
  • Spark routes process GET requests/responses successfully