## CSE 131 Module 4: Input & Output

Important!

Before beginning any work, do a Team...Pull in your repository.

### Exercises

In the repository you are using for this course, look inside the exercises source folder, and you will see assignments organized by name. Before continuing on to the next portion of the asynchronous material, spend some time on the exercise. When you are finished, commit your code so that credit for the exercise can be given.

It is not so important that you get the exercise exactly right. It is important to work through it as best you can before you look at the solution or continue with other work in this module.

Before starting this exercise:
Be sure to update your repository before beginning this assignment, so that you receive any code you need.

### Computing statistics for a list of numbers

#### Instructions

• Find and open the Stats Java program in the exercises4 package of the exercises source folder.
• Write code as directed to compute the specified statistics for the numbers that you read in.
After completing this exercise:
Commit and push any files you have changed so that you receive credit for your work!

Before starting this exercise:
Be sure to update your repository before beginning this assignment, so that you receive any code you need.

### Example using graphics

#### Instructions

• Find and open the GraphicsDemo Java program in the exercises4 package of the exercises source folder.
• For each of the following, write code to generate the graphics as specified, and run your program after each addition to see that you are creating the right kinds of shapes, colors, and sizes.
• Draw a blue point anywhere you like
• Draw a larger green point someplace else
• Draw an unfilled red triangle somewhere
• Draw a filled yellow circle somewhere
• Draw a filled blue rectangle somewhere
The book does not include mention of Sedgewick's filledRectangle method. But it is there, and you can find it by pausing after typing the .. Eclipse will then show you possible completions, which will include filledRectangle.
After completing this exercise:
Commit and push any files you have changed so that you receive credit for your work!

Before starting this exercise:
Be sure to update your repository before beginning this assignment, so that you receive any code you need.

### Mouse and keyboard

#### Instructions

• Find and open the WaitPoint Java program in the exercises4 package of the exercises source folder.
• Write code that does the following:
• Waits until the mouse has been pressed
• Waits until the mouse has been released
• Draws a visible point at the spot where the mouse was released
• Waits until the user has typed a q (all other keys are ignored)
• Writes text on the graphic screen that says goodbye
Do this a step at a time, testing your code. Look at the solution video intermittently, as necessary.
After completing this exercise:
Commit and push any files you have changed so that you receive credit for your work!

Before starting this exercise:
Be sure to update your repository before beginning this assignment, so that you receive any code you need.

### Follow the mouse

In this exercise you will apply what you have learned about graphics to cause a ball to follow the mouse on a graphics screen.

#### Instructions

• Find and open the MouseFollower Java program in the exercises4 package of the exercises source folder.
• That code already contains the standard animation loop:
• StdDraw.clear() at the beginning, to wipe the graphics display clean
• Your code will then paint one frame of the animation
• StdDraw.show(10) produces what you have drawn on the screen and waits 10 milliseconds before continuing.
• By using show(..), the drawing you do is not actually shown between calls to show.
• Instead, the drawing is done in memory, off the screen, which is much faster.
• When you call show(), the work you have done off screen is put on screen all at once.
• The program also pauses for the specified time, usually 10 to 50 milliseconds. Pausing allows your computer to do something else for a while, which will make your computer feel more responsive in the other applications that are running.
• First try to get a ball on the screen to follow your mouse. This consists of reading the mouse's coordinates, and painting a ball at that spot.
• When you have that working, try to think of how you could make the ball appear to lag in its mouse-following activities.
• As a hint, think about keeping some history of where the mouse has been.
• You can then have the ball go to locations in the mouse's past.
After completing this exercise:
Commit and push any files you have changed so that you receive credit for your work!

Before starting this exercise:
Be sure to update your repository before beginning this assignment, so that you receive any code you need.

### Frequency of a pitch

As described in the videos and text, a musical pitch can be encoded as an integer. The convention for this course is that 0 corresponds to concert A, 1 corresponds to Bb just above concert A, and so on.

In this exercise you will print out the frequency associated with a pitch, and with the pitch 7 chromatic steps above that pitch.

#### Instructions

• Find and open the Pitches Java program in the exercises4 package of the exercises source folder.
• Insert code to prompt the user to enter an integer representing a pitch.
• Compute the frequency that corresponds to that pitch.
• Print the frequency of that pitch, the frequency of a pitch 7 chromatic steps higher, and the ratio of the two frequencies.
After completing this exercise:
Commit and push any files you have changed so that you receive credit for your work!

Before starting this exercise:
Be sure to update your repository before beginning this assignment, so that you receive any code you need.

### Ratios of two pitches' frequencies

Continue your work on the above exercise as follows.

#### Instructions

• Continue modifying the Pitches Java program in the exercises4 package of the exercises source folder.
• Prompt the user to enter an integer for pitch 1.
• Prompt the user to enter an integer for pitch 2.
• Compute the frequency that corresponds to those pitches
• Print the frequency of both pitches and the ratio of the two frequencies.
After completing this exercise:
Commit and push any files you have changed so that you receive credit for your work!
```

```
```

```
```

```
```

```