CS111 Spring 2010

Problem Set 8

Due Tuesday 13 April 2010

About this Problem Set

The purpose of this problem set is to give you experience with iteration (i.e., loops), arrays, objects, instance variables, and state. In Task 1, you will write several iterative methods in TurtleWorld. In Task 2 you will design an application that maintains information about a music library of songs.

All code for this assignment is available in the ps08_programs folder in the cs111d download directory on the cs server.

How to turn in this Problem Set

You are required to turn in both a hardcopy and a softcopy. For general guidelines on problem set submission, including how to submit a softcopy and how to check if you softcopy submission was successful, click here. Please make sure to keep a copy of your work, on your computer, in your private directory (or, to play it safe, both).

Hardcopy Submission

Your hardcopy packet should consist of:
  1. The cover page;
  2. Your modified StepWorld.java file from Tasks 1a and 1b;
  3. Your newly developed Song.java and MusicLibrary.java files from Task 2.
Staple these together, and submit them at the start of class on the due date.

Softcopy Submission

You should submit your final version of your ps08_programs folder. In particular,

Task 1: Turtle Steps

In this problem, you will use a turtle to draw sequences of steps made out of square blocks. For example:

The steps are controlled by two parameters:

Below, you are asked to implement two different strategies for drawing such steps. The skeleton code that you need to flesh out can be found in the StepMaker class within the StepWorld.java file within the folder StepWorld.

To experiment with a working version of a solution to this problem, run the applet StepWorld in a web browser. (Note: a "StepWorld" parameters window will appear at the top of your screen, but if you don't see it, it may be located behind your web browser window, so you should move your web browser window.) Your solution should produce the same drawings as this sample solution applet.

Task 1a: Row Decomposition

One way to decompose a set of steps is into rows of blocks. For instance, the sample steps above can be decomposed into five rows of blocks, where the first row consists of 5 blocks, the second row consists of 4 blocks, the third row consists of 3 blocks, the fourth row consists of two blocks, and the fifth row consists of 1 block.

In this part, you are to implement this strategy via the following three iterative methods:

public void block (int blockSize)
Use a for loop to draw a square block with the given block size. The turtle should return to its initial position and heading.

public void rowOfBlocks (int n, int blockSize)
Use a for loop to draw a row consisting of n blocks, each with size blockSize. The turtle should return to its initial position and heading.

public void stepsFor_1 (int n, int blockSize)
Use a for loop to draw a set of steps consisting of n rows of blocks. The first row should contain n blocks, the second row should contain n - 1 blocks, the third row should contain n - 2 blocks, and so on. The final row should contain 1 block. The turtle should return to its initial position and heading.

You can test out your code for this part by (1) selecting stepFor_1 in the parameter window; (2) choosing values for n and blockSize in the parameter window; and (3) selecting Run in the Turtle window. The turtle originally starts at a coordinate of (0, 0) (in the center of the screen) with a heading of 0 degrees (facing east). In order to test that stepsFor_1() properly returns the turtle to its initial position and heading, the testing code has the turtle turn left 225 degrees and move forward 100 steps after the call to stepsFor_1().

For example, the following parameter settings

should result in the following picture:

Task 1b: Rectangle Decomposition

An alternative strategy for drawing the steps is based on the following observation: a set of steps with a base of n blocks can be drawn as the superposition of n rectangles whose widths increase from 1 * blockSize to n * blockSize and whose heights decrease from n * blockSize to 1 * blockSize. For example, the 5-block base steps shown below result from superimposing the five rectangles to its right so that their lower left points coincide:

Use this idea to implement the following method:

public void stepsFor_2 (int n, int blockSize)
Draw a set of steps with a base of n blocks, each of which is a block of size blockSize, and return the turtle to its initial position and heading. The problem should be decomposed into drawing n superimposed rectangles as described above. A for loop should be used to draw the rectangles. Keep in mind that you can accomplish this with only one for loop.

You may define auxiliary methods for this problem if you wish.

You can test out your code for this part as in part a, except you should select stepsFor_2 in the parameter window. To ensure that the turtle is returned to its initial position and heading, the testing code will draw the additional line extending from the lower left corner as mentioned in the testing notes for part a.


Task 2: Maintaining a Music Library of Songs

Collaboration

On this task, you are strongly encouraged (but not required) to work with a partner as part of a two-person team. The partner may or may not be someone you have partnered with in the past. If you work on a team, your team will submit a single softcopy and hardcopy of the problem set and the same grade will be given to both team members.

All work by a team must be a true collaboration in which members actively work together on all parts of the assignment. It is not acceptable for team members to split up the problems and work on them independently. All programming should be done with both team members working at the same computer console. It is strongly recommended that both team members share the responsibility of "driving" (typing at the keyboard), swapping every so often. The only work that you may do alone is debugging code that you have written together and talking with the instructors and drop-in tutors.

Reading

File I/O (available on e-Reserve on the CS111 FirstClass Conference)

Task

In this task, you will develop a music library of songs. Songs have titles (i.e., the name of the song), artists (the name of the person or group performing the song), and genres (e.g., country, rock, R&B, etc.). A music library maintains a collection of songs.

Initially, your music library should be populated be reading in a set of songs from a file. Here is an example file containing information about six songs. Note that lines 1-3 in the file contain information about the first song's title, artist, and genre, respectively. Similarly, lines 4-6 contain information about a second song. Lines 7-9 contain information about a third song, etc. You are, of course, encouraged to update this file with your own musical preferences.

You must write two classes from scratch: a Song class and a MusicLibrary class. The Song class must implement the constructor and four instance methods specified in the Song contract. The MusicLibrary class must implement the constructor, three instance methods, and static main method specified in the MusicLibrary contract. You are responsible for determining and defining appropriate instance variables for each of the two class.

The figure below illustrates sample execution of a working MusicLibrary application.

To experiment with a working version of a solution to this problem, use Dr. Java to open the SampleLibrary.txt file in the ps08_programs/MusicLibrary/test/ folder. Then, in the Dr. Java Interactions pane, type java MusicLibrary_Solution.