<<

CS170 Section 5 HW #4, part 2 of 2 Due Friday, April 10 at 2:59 p.m. (NOTE TIME) Write and submit one Java program, MusicChart.java, as described on the next page.

The assignment should be submitted on the Math/CS system (from any lab computer or by accessing the computers remotely) using the following command to submit: /home/cs170005/turnin MusicChart.java hw4_2 Late turn-ins will be accepted until Sunday, April 12 at 11:59 pm (you must submit before midnight) for a 10% penalty. If you are submitting late, use the following command: /home/cs170005/turnin MusicChart.java hw4_2_late Style Guidelines: • Only one statement or code structure per line. (The class declaration can’t be on the same line as the main method declaration, for example.) • No lines (including comments) over 80 characters, except for single-website citations. • Write a block comment after the honor statement describing the program’s purpose. • Indent blocks of code in loops, methods, switch statements, and classes. See any class code for examples of this. Each level starting with a { or that could start with a { should be indented. • Use descriptive variable names. No one-letter or one-symbol variable names, except in the initial action of a for loop. • Each method must have a block comment preceding it that describes the method’s purpose, parameters, and any return values. • Every method, including the main method, longer than 7 lines must have inline comments describing what is going on in the code. Breaking these guidelines will result in a grade penalty.

Honor Code: You must abide by the honor code when completing this homework. Make sure to include the statement of collaboration/honor code (found at http://mathcs.emory.edu/~ccgarve/cs170) acknowledging that you understand and have abided by the honor code. Make sure to cite web- sites you use, and include your name/email address.

Working with a partner: If you work in a pair, you must both sign the collaboration statement and list your emails. If you start working with one partner, you cannot switch partners once you’ve started writ- ing code. If you decide to stop working with your initial partner, you must document your initial collaboration. Failure to do so violates the honor code. However, in that case it’s okay if your code is similar to your initial partner’s provided you have documented it, despite turning in an individual assignment. If you work with a partner, only one of you should submit, and each method should include a comment indicating who worked on it (in any way). If you both submit, the last submission will be used.

1 If you’re working with a partner, go back and actually read the very last paragraph on the first page.

2 [60 pts] MusicChart.java

Write a program, MusicChart.java, that provides functionality for a music top 100-style chart.

You may work in a pair for this assignment; see the first page for details.

Starting Code: The correct solution for part 1 of this assignment has been added to your cs170 directory in the new directory hw4_2. I suggest using it as a starting point for this part of the homework, as it is a correct implementation that follows the style guidelines (except that the program purpose is omitted, as you will need to add it).

Program Specifications: This program builds off of part 1; you should start with that part of the assignment’s code.

For this part of the assignment,

1. Make the methods from part 1 private.

2. Declare a private instance (non-static) String array variable (I’d suggest a name like chart, which is what I’ll refer to it as), and a private instance integer array (I’d suggest a name like movement; it’s going to represent changes in chart position). 3. Write a no-arg constructor for the MusicChart class that initializes your chart using the pre-existing startingChart method. Also have the constructor initialize movement to be the same size, and full of zeros. 4. Write a constructor that takes in two parameters: a String array, and an integer array. Assign chart and movement to those parameters; use the assignment operator so they’re intertwined.

5. Write a public instance method called getChart. It should take no parameters and return a String array. This will be an accessor for the private instance variable chart. It should return an array with the same contents as chart, but a separate copy. 6. Write a public instance method called getChanges. It should take no parameters and return an integer array. This will be an accessor for the private instance variable movement. It should return an array with the same contents as movement, but a separate copy. 7. Write an instance method printChart, which takes no parameters, returns nothing, and prints out a numbered list of the chart to the terminal. It should also indicate movement in the chart, if any. Its output should look something like (chart contents will vary):

1. by Featuring Bruno Mars 2. Sugar by Maroon 5 3. by 4. Do by 5. ( Of Grey) by The Weeknd (up 2 from last week)

3 [+20 points] Bonus: Write a public instance method, moveSong. This should be very much like the last question on the midterm, but a bit fancier, and written on a computer instead of a sheet of paper.

It should take two paramters: an integer index, indicating which song on the chart is being moved, and an integer offset, which indicates how it should be moved. The offset can be positive, zero, or negative. Positive indicates a song should move up in the chart. Zero indicates the song shouldn’t move. Negative indicates the song should be moved down.

You can assume that both the index and (index - offset) will be within the valid indices of the array. That is: we won’t test with inputs that intentionally put items off the end of your chart.

You should move the elements in chart according to the parameters. Be careful not to just swap the element at index with the element at (index - offset), though. The behavior should instead be like the following:

Assuming your chart starts as

Uptown Funk by Mark Ronson Featuring Bruno Mars Sugar by Maroon 5 Thinking Out Loud by Ed Sheeran Love Me Like You Do by Ellie Goulding FourFiveSeconds by Rihanna & Kanye West & Paul McCartney then moving the element at index 4 (“FourFiveSeconds...”) with an offset of 3 should result in the ordering

Uptown Funk by Mark Ronson Featuring Bruno Mars FourFiveSeconds by Rihanna & Kanye West & Paul McCartney Sugar by Maroon 5 Thinking Out Loud by Ed Sheeran Love Me Like You Do by Ellie Goulding

Further, you should update the movement data to reflect the new movement. Whatever song moves should have its movement data updated with the offset passed in, and it should maintain relative position identical to the song (this means it should move just like the song). So if the offsets in the above example started as

[0, -1, 2, -3, 1] then they should end as

[0, 3, -1, 2, -3].

This has the element at index 4 (which is originally 1) shifted to index 1 and updated to have value 3. All of the elements it passed get bumped over.

4