Skip to content

Loop Assignments#

Extended Deadline

While these assignments are usually to be completed within a week, I'm extending this deadline because in parallel you'll be working on your Lunar Lander assignments.

Assignment: Try The Loops

Create this once with a while and once with a for loop. Have equally spaced lines horizontally and vertically (kind of like graph paper). Pick any color you want, settle on any spacing that looks good. The important part is using the loop. Save to your assignments repository!

Assignment Example of Graph Paper, white background with thin blue lines horizontally and vertically

Assignment: Create a Chessboard

Create this Chessboard. This can be done in under 20 lines of code. If your solution is slightly longer, don't worry.

Hint

Take a look at the nested loops once again.

Hint

Maybe the modulo operator helps you while giving every other field its own color!

A Standard 2D chess board

Assignment: Write a Small Change calculator

Write a program that outputs which coins to give someone for a given amount of money. The output does not need to be fancy, simple output with println() is sufficient. Use a loop to arrive at the answer.

Example: You owe Bernhard 1.47 Euro. Bernhard receives

  • 1€ coin
  • 20ct coin
  • 20ct coin
  • 5ct coin
  • 2ct coin

Bonus points if you shorten the output to remove the multiples like this:

  • 1 × 1€ coin
  • 2 × 20ct coin
  • 1 × 5ct coin
  • 1 × 2ct coin
Hint 1

In cases like these, it is usually easier to work with cents. Use 147 cents rather than 1.47 Euros.

Hint 2

You will need some kind of variable that you check against all the coins available. Can you pay 147 cents with a 200 cents (2€) coin? No. Can you pay 147 cents with a 100 cents coin? You bet! But you will have 47 cents left after that. And so on.

Bonus Assignment: Odd Fairy Tale Economy

Write a program (more realistically: copy your previous program and start from there) that formats small change for a fairy tale currency. This currency has

  • Diamonds (worth 3 Gold Nuggets and a Silver Coin)
  • Gold Nuggets (worth 2 Silver Coins and 142 Scraps1)
  • Silver Coins (worth 3 Hardwood Blocks)
  • Hardwood Blocks (worth 102 Scraps)
  • Scraps (worth … 1 Scrap)

What's an efficient way to transport currency with a combined worth of 29841 Scraps? How about Scraps?


  1. Don't blame me, I didn't2 make the rules! 

  2. Ok, to be honest, I totally made these rules.