1. Conditionals and Loops
Download project ZIP
| Submit to TigerFile
Use conditionals and loops to write small programs and practice debugging.
Getting Started
- Read Section 1.3 of the textbook and review the corresponding lectures and precepts.
- Download and unzip the project ZIP
, then open the
loopsproject in IntelliJ. - Refer to the Programming Assignments FAQ as needed.
Restrictions
- You may use only Java features introduced so far, including conditionals and loops. You may call methods in StdAudio and StdPicture.
- You may not use arrays, functions, or any other feature not yet introduced in COS 126.
Saying Military Time
Military time specifies the time of day in the form hhmm, where hh is the hour since midnight and mm is the minutes since the last full hour.
For example, 1200 is noon; 2359 is one minute before midnight; and 0100 is one hour after midnight.
To parse and say military time, following these steps:
- Extract the hours
hhand minutesmmfrom the four-digit timehhmm. - Say the hours
hh. Ifhhis between 1 and 9, precede it with the word “zero.” - Say the word “hundred.”
- Say the minutes
mm, unlessmmis00. - Say the word “hours.”
For example, 2359 is spoken as “twenty-three hundred fifty-nine hours” and 0100 as “zero one hundred hours.”
Write a program SayMilitaryTime.java to say a given military time using these rules. The program takes two command-line arguments:
-
Time: A 4-digit string in military time (e.g.,
1200,2359, or0100). -
Voice: The name of a voice subdirectory (e.g.,
AlexorSiri).
The subdirectories Alex, Amélie, Luca, Paulina, Samantha, Veena, and Siri contain voice recordings of people saying the numbers 0 through 59, along with the words for hundred and hours.
For example, Alex/59.wav is Alex saying the number 59 and Siri/hundred.wav is Siri saying the word hundred.
Below are some sample executions:
~/Desktop/project> java-introcs SayMilitaryTime 2359 Alex ~/Desktop/project> java-introcs SayMilitaryTime 1100 Siri ~/Desktop/project> java-introcs SayMilitaryTime 0609 Veena ~/Desktop/project> java-introcs SayMilitaryTime 0011 Paulina
Q.What happens if the argument to Integer.parseInt() has leading zeros, such as 0011?
Integer.parseInt("0011") returns the integer 11.Context: The 24-hour clock is used widely in Asia, Europe, Africa, and Latin America. In the United States, it is used in the military, aviation, navigation, computing, logistics, and hospitals.
Random Walkers
Fix the bugs in RandomWalkers.java.
It must compile with no errors and no warnings, and it must pass all autograder checks, including style checks.
You will encounter compile-time errors, runtime exceptions, and logic errors.
- The program takes two integer command-line arguments: \(n\) (the number of steps) and \(trials\) (the number of experiments).
- One trial simulates a random walk for \(n\) steps, starting from the origin:
- maintain the current location \((x, y)\), initially \((0, 0)\)
- repeat \(n\) times: move one unit north, east, south, or west, each with probability \( \tfrac14 \)
- after \(n\) steps, compute the squared distance from the origin: \(x^2 + y^2\)
- Print the mean squared distance (the average of the squared distances over all trials).
Here are sample executions of the corrected RandomWalkers.
Because the walk is random, your output will vary. However, when \(trials\) is large, your output should be close to the output below.
~/Desktop/project> java-introcs java-introcs RandomWalkers 100 10000 mean squared distance = 101.446 ~/Desktop/project> java-introcs java-introcs RandomWalkers 100 10000 mean squared distance = 99.1674 ~/Desktop/project> java-introcs java-introcs RandomWalkers 200 1000 mean squared distance = 195.75 ~/Desktop/project> java-introcs java-introcs RandomWalkers 200 1000 mean squared distance = 198.21 ~/Desktop/project> java-introcs java-introcs RandomWalkers 1600 100000 mean squared distance = 1600.13064 ~/Desktop/project> java-introcs java-introcs RandomWalkers 1600 100000 mean squared distance = 1603.72
Context: Random walks model Brownian motion and diffusion, with applications ranging from ink dispersing in water to polymer formation and neural activity.
Duotone Filter
A duotone filter maps each pixel to a blend of two RGB colors, \(color_1\) and \(color_2\). The blend weight is based on the pixel’s brightness.
To apply a duotone filter, process each pixel independently:
- Let \((r, g, b)\) be the pixel’s red, green, and blue components. Each component is an integer between \(0\) and \(255\).
- Compute the pixel brightness \( \alpha \) (a real number between 0 and 1) using the NTSC weights:
\[\alpha = \tfrac{1}{255} (0.299r + 0.587g + 0.114b) \]
- Let \((r_1, g_1, b_1)\) and \((r_2, g_2, b_2)\) be the RGB components of \(color_1\) and \(color_2\).
- Compute the new color \((r^\prime, g^\prime, b^\prime)\) by blending the two palette colors: \begin{aligned} r' & = \alpha \, r_1 \; + \; (1 - \alpha) \, r_2 \\ g' & = \alpha \, g_1 \; + \; (1 - \alpha) \, g_2 \\ b' & = \alpha \, b_1 \; + \; (1 - \alpha) \, b_2 \end{aligned} Round each component to the nearest integer.
Write a program Duotone.java that takes seven command-line arguments:
filename, r1, g1, b1, r2, g2, and b2.
It should apply the duotone filter and display the result using StdPicture.
The sample execution below applies a duotone filter to a photo of Johnson Arch using Princeton orange for \(color_1 = (245, 128, 37)\) and black for \( color_2 = (0, 0, 0)\).
~/Desktop/project> java-introcs StdPicture JohnsonArch.jpg~/Desktop/project> java-introcs Duotone JohnsonArch.jpg 245 128 37 0 0 0
![]()
Take a photo of a campus structure (building, archway, gate, etc.) and apply your duotone filter.
Submit the original photo as MyPhoto.jpg, and record the two RGB colors you used in your readme.txt.
Q.Which methods are available in StdPicture?
Q.How do I round a double to the nearest int?
(int) Math.round(x).Context: Duotone is a popular effect for photographers and digital artists. See this site for examples.
Musical Dice Game
A musical dice game (Musikalisches Würfelspiel) generates music by stitching together precomposed measures chosen at random. In this exercise, you will assemble a 32-measure Viennese waltz in the style of Mozart. The waltz has two parts: a minuet followed by a trio.
- Minuet. The minuet consists of 16 measures (numbered 0 to 15).
- For measure \(i\), roll two fair six-sided dice and let \(s\) be the sum.
- Play
minuet<i>-<s>.wav. - For example, if you roll an 8 for measure 3, play
minuet3-8.wav. - Note: sums range from 2 to 12; 7 is most likely, and 2 and 12 are least likely.
- Trio. The trio consists of 16 measures (numbered 0 to 15).
- For measure \(i\), roll one fair six-sided die.
- Play
trio<i>-<s>.wav. - For example, if you roll a 6 for measure 15, play
trio15-6.wav.
-
Instruments and files. The project folder includes several instrument subdirectories. For example,
mozart-pianocontains 272 WAV files named using the conventions above:- Minuet: 16 measures × 11 possible sums = 176 files.
- Trio: 16 measures × 6 possible die outcomes = 96 files.
- The other subdirectories (
mozart-clarinet,mozart-flute-harp, andmozart-mbira) provide the same measures for different instruments.
For example, to play
minuet3-8.wavusing a clarinet, callStdAudio.play("mozart-clarinet/minuet3-8.wav").
Write a program MusicalDiceGame.java to play a waltz according to the rules above.
Your program takes one command-line argument: the instrument subdirectory name (e.g., mozart-piano).
Here are some sample executions:
~/Desktop/project> java-introcs MusicalDiceGame mozart-piano ~/Desktop/project> java-introcs MusicalDiceGame mozart-piano ~/Desktop/project> java-introcs MusicalDiceGame mozart-clarinet ~/Desktop/project> java-introcs MusicalDiceGame mozart-flute-harp ~/Desktop/project> java-introcs MusicalDiceGame mozart-mbira
Context: Each run generates a new waltz from precomposed measures that follow a rigid harmonic structure. It sounds convincingly Mozart-like, but it rarely produces anything truly extraordinary.
Submission
Log in to TigerFile with your OIT NetID and upload the required files.
SayMilitaryTime.javaRandomWalkers.javaDuotone.javaMusicalDiceGame.javaMyPhoto.jpgreadme.txtacknowledgments.txt
Grading breakdown
| File | Points |
|---|---|
| SayMilitaryTime.java | 8 |
| RandomWalkers.java | 8 |
| Duotone.java | 9 |
| MusicalDiceGame.java | 9 |
| MyPhoto.jpg | 2 |
| readme.txt | 4 |
| Total | 40 |
This assignment was developed by Kevin Wayne. Copyright © 2022–2026.