2. Arrays and Input/Output
Download project ZIP
| Submit to TigerFile
Goals
- Write programs that use arrays.
- Read from standard input using
StdIn. - Print to standard output using
StdOut. - Draw graphics using
StdDraw. - Produce sound using
StdAudio. - Redirect standard input to read from a file.
Getting Started
- Read Sections 1.4 and 1.5 of the textbook and review the corresponding lectures and precepts.
- Download and unzip the project ZIP
, then open the
arraysproject in IntelliJ. - Review the formatted printing examples.
- Take the Getting Help in COS 126 Quiz.
Restrictions
- You may use only Java features introduced so far, including arrys.
- You may call methods in
StdIn,StdOut,StdAudio, andStdDraw. - You may not use functions, or any other feature not yet introduced in COS 126.
Dice and the Gaussian Distribution
Write a program DiceHistogram.java that takes two command-line arguments: \(d\), the number of fair six-sided dice, and \(n\), the number of times to roll them. Assume that \(0 \leq d \leq 100\) and \(n \geq 0\). Each roll consists of rolling all \(d\) dice at once, summing the results, and storing the total for that roll. Use an integer array to tabulate the number of times each possible total (between \(d\) and \(6 \times d\)) occurs across the \(n\) rolls. Then print a text histogram of the results.
Each histogram value should be right-justified in a field width of three characters; followed by a colon; followed by a single space; followed by a sequence of asterisks corresponding to the number of times that value occurred as a sum.
Here are a few sample executions. Because the dice are random, your output will vary. However, when \(n\) is large, it should be close to the results below.
~/Desktop/project> java-introcs DiceHistogram 2 500 2: ************** 3: *************************** 4: ********************************* 5: ************************************************************* 6: ********************************************************************** 7: ****************************************************************************** 8: *********************************************************************** 9: ********************************************************** 10: ********************************************** 11: **************************** 12: ************** ~/Desktop/project> java-introcs DiceHistogram 10 1000 10: 11: 12: 13: 14: 15: 16: 17: 18: * 19: **** 20: 21: *** 22: ****** 23: ******** 24: **************** 25: ************* 26: ********** 27: ********************************* 28: **************************************** 29: ********************************* 30: *************************************************** 31: ***************************************************************** 32: ******************************************************** 33: ************************************************************************************** 34: *********************************************************** 35: ********************************************************************* 36: *********************************************************************************** 37: ************************************************************** 38: ***************************************************************** 39: *************************************** 40: ***************************************************** 41: ************************************ 42: **************************** 43: ************************ 44: ************************ 45: ********* 46: *********** 47: ******* 48: *** 49: ** 50: 51: 52: * 53: 54: 55: 56: 57: 58: 59: 60:
Context: The central limit theorem, a key result in probability and statistics, asserts that the shape of the resulting histogram tends to the ubiquitous bell curve (Gaussian distribution) if the number of dice and rolls is large.
Q.How do I print an integer right-justified in a field width of three characters?
Q.I have the magic number 6 sprinkled through DiceHistogram.java. Is there a better way?
SIDES) and use this symbolic name.Echo Filter
Consider what happens if you superpose a sound file with itself, but shifted over a specified number of samples. You’ll hear the same sound twice, the second time slightly delayed. If the delay is relatively short (say, 15,000 samples or about 1/3 of a second) and you decrease the loudness of the second copy (by multiplying the samples by a factor of, say, 0.5), it will result in an echo effect, much like you might hear in a tunnel or large empty room.
Recall from lecture that a sound wave is a mechanical wave and is subject to the principle of superposition. This means that to combine two (or more) sound waves, you simply add the corresponding samples together.
Write a program EchoFilter.java that takes three command-line arguments (a string filename, an integer delay, and a floating-point decay factor) and applies an echo filter to the given sound file. Note that the length of the resulting sound wave will be equal to the original length plus the delay. Play the resulting sound wave. For more hints, see the FAQ below.
Here are some sample executions:
~/Desktop/project> java-introcs EchoFilter IHaveADream.wav 0 0.0 ~/Desktop/project> java-introcs EchoFilter IHaveADream.wav 15000 0.5 ~/Desktop/project> java-introcs EchoFilter PearlHarbor.wav 15000 0.5 ~/Desktop/project> java-introcs EchoFilter TheHillWeClimb.wav 10000 0.5 ~/Desktop/project> java-introcs EchoFilter Crow.wav 10000 0.5 ~/Desktop/project> java-introcs EchoFilter HelloWorld.wav 5000 1.0 ~/Desktop/project> java-introcs EchoFilter RowYourBoat.wav 117600 1.0
Context: An echo filter is popular voice effect that you can apply on social media platforms such as TikTok.
Q.Is the integer delay equal to the number of samples we are shifting over?
Q.How do I test my program?
double[] original = { 1.0, 0.0, 0.5, 0.0, -1.0, -0.5, 1.0, 0.25 };
with
decay = 0.5 and delay = 3. While it would not make sense to play an echo filter applied to this example, you can print the results after applying your echo filter to make sure your calculations are correct. Hint: use a for loop to print the array. You can also try modifying this example with other values for the original array, decay and delay. Testing and debugging with small examples will help you understand your approach.Q.Can I assume that delay is a non-negative integer?
Q.Can I assume that decay is between 0 and 1?
Q.Is it possible for the delay to be longer than the number of samples in the original array?
Q.How many times should StdAudio.play() be called?
StdAudio.play(double[]).Q.The echo filter repeats the sound source only once. How could I get a repeating, decaying echo?
World Maps
Write a program WorldMap.java that reads boundary information of a country (or other geographic entity) from standard input (using StdIn) and plots the results to standard drawing (using StdDraw). A country consists of a set of regions (e.g., states, provinces, or other administrative divisions), each of which is described by a polygon. A polygon will be represented by two floating-point arrays, one containing the \(x\)-coordinates and the other containing the \(y\)-coordinates of the vertices of the polygon.
Input format. The first line contains two integers: width and height. The remaining part of the input is divided into regions.
-
The first entry in each region is the name of the region. For simplicity, names will not contain spaces.
-
The next entry is an integer specifying the number of vertices in the polygon describing the region.
-
Finally, the region contains the \(x\)- and \(y\)-coordinates of the vertices of the polygon.
For simplicity, if a region requires more than one polygon to describe its boundary, we treat it as multiple regions, with one polygon per region.
Output format. Draw the polygons to standard drawing, using the following guidelines:
-
Call
StdDraw.setCanvasSize()to set the size of the canvas to be width-by-height pixels. -
Call
StdDraw.setXscale()andStdDraw.setYscale()so that \(x\)-coordinates of the canvas range from 0 to width and the \(y\)-coordinates range from 0 to height. -
Call
StdDraw.polygon()to draw each polygon. This method takes two arrays as arguments—the array of \(x\)-coordinates and the array of \(y\)-coordinates. It draws the polygon whose sequence of vertices is defined by those \(x\)- and \(y\)-coordinates.
Data files. The project folder includes a number of data files:
Here are some sample executions:
~/Desktop/project> java-introcs WorldMap < shapes.txt~/Desktop/project> java-introcs WorldMap < usa.txt
~/Desktop/project> java-introcs WorldMap < iceland.txt
~/Desktop/project> java-introcs WorldMap < world.txt
![]()
Q.Can I draw the boundary of a region as a sequence of line segments instead of a polygon?
StdDraw.polygon(), as specified.Q.My program runs slowly for inputs with a large number of polygons. Any way to speed it up?
By default, as soon as you call a drawing method such as StdDraw.polygon(), the result appears on screen. However, updating the screen is a
relatively expensive operation (in terms of CPU time). If there are thousands of geometric objects to draw, this can slow down your program
considerably. To avoid this slowdown, use the double buffering feature of standard draw:
- Call
StdDraw.enableDoubleBuffering()once before any drawing code to enable double buffering. Now, all drawing will be done on an offscreen canvas. - Call the drawing commands such as
StdDraw.polygon(). Nothing will appear on screen at this time. - Call
StdDraw.show(). This will transfer the offscreen canvas to the onscreen canvas and you will see the drawing. You can think of double buffering as collecting all of the lines, points, shapes, and text that you tell it to draw, and then drawing them all simultaneously, upon request.
Q.My favorite country is not supplied as one of the data files. Why not?
Q.The displayed map distorts the areas of regions. How can I fix this?
Submission
Submit DiceHistogram.java, EchoFilter.java, WorldMap.java, readme.txt and acknowledgments.txt files to TigerFile
.
Also, complete the quiz on getting help in COS 126 on Canvas
Grading breakdown
| Files | Points |
|---|---|
| DiceHistogram.java | 12 |
| EchoFilter.java | 12 |
| WorldMap.java | 12 |
| readme.txt | 2 |
| Quiz | 2 |
| Total | 40 |
This assignment was developed by Kevin Wayne. Copyright © 2022–2026.