Question: Create a new project in BlueJ. Create a new class using the New Class... button and name it Song. Open the Song class to edit
- Create a new project in BlueJ.
- Create a new class using the "New Class..." button and name it Song. Open the Song class to edit the source code. Select and delete all the source code so that the file is empty.
- Copy in the starting source code found here: Lab 4: Song Starting Source Code. (This link will not work for you until you have received feedback on Lab 2. Email the instructor if you think you should be able to access it but can't.)
- As usual, update the class header Javadoc comment block and be sure each method has an up-to-date comment header.
- Make sure you adhere to Java style guidelines as described in Appendix J.
- Complete the TODOs in the code, modifying and implementing methods as described. Be sure to follow the specifications as given in the TODO comments.
- Before submitting your project, delete the TODO comments. TODOs represent a task that still needs to be done, and when you complete a TODO it should be removed. There should not be any TODOs in your submitted project (unless you didn't complete the project).
- As usual, you should be testing as you go to make sure everything works!
- A unit test class is provided for you to help check your code: SongTest.java. Use it in the same manner you did for Lab 2. Remember to check the terminal window so you can visually verify the results for the tests that print to the terminal.
- When you have completed the assignment, create a jar file of your project.
Source Code: /** * A song, such as one in a music store which can be rated * * Modifications: * CT: Added validation to setLengthInSeconds * * * @author Cara Tang * @version 2020.09.06 */ public class Song { private String title; private String artist; private int lengthInSeconds; private int rating; // valid ratings are 1-4 or 0 meaning not rated /** * Create a song with the given title, artist, length, and rating * @param songTitle the song title * @param songArtist the song artist * @param songLength the length of the song in seconds * @param songRating the song's rating */ public Song(String songTitle, String songArtist, int songLength, int songRating) { title = songTitle; artist = songArtist; lengthInSeconds = songLength; // TODO: ----------------- 1 ------------------- // TODO: Replace the statement below with a call to setRating that has the same effect rating = songRating; // TODO: ----------------- 1b ------------------- // TODO: Answer this question in a comment right below (no code for this one) // TODO: (Tip: Update setRating based on TODO #3 below before answering.) // Question: Why do you think we want to replace the assignment statement with a call to setRating? // Your Answer: } /** * Create an unrated song with the given title, artist, and length * @param songTitle the song title * @param songArtist the song artist * @param songLength the length of the song in seconds */ public Song(String songTitle, String songArtist, int songLength) { title = songTitle; artist = songArtist; lengthInSeconds = songLength; rating = 0; } /** * @return the song title */ public String getTitle() { return title; } /** * @return the song artist */ public String getArtist() { return artist; } /** * @return the song length (in seconds) */ public int getLengthInSeconds() { return lengthInSeconds; } /** * @return a string giving the song length in minutes (m) and seconds (s). * Example: If lengthInSeconds is 150, the string returned is "2m 30s" */ public String getLengthAsString() { // TODO: ----------------- 2 ------------------- // TODO: Implement this method so that it behaves as specified. // TODO: Make the string exactly as specified, do not change the format. // TODO: (E.g., the m and s must be lowercase, and no space between the number and the letter) // TODO: More examples: 0m 4s 5m 10s 3m 59s 12m 0s return ""; } /** * Set the length of this song to the value given. * The length must be greater than 0. * @param newLength new length (in seconds) for this song */ public void setLengthInSeconds(int newLength) { if (newLength > 0) { lengthInSeconds = newLength; } else { System.out.println("Error: Length must be greater than 0"); } } /** * @return true if this song has not been rated, false otherwise */ public boolean isUnrated() { // TODO: ----------------- 3 ------------------- // TODO: Implement this method so that it behaves as specified. // TODO: (If a song's rating is 0, then it has not been rated.) return false; } /** * @return the song rating */ public int getRating() { return rating; } /** * Update this song's rating to the one given * @param newRating new rating for this song */ public void setRating(int newRating) { // TODO: ----------------- 4 ------------------- // TODO: Add validation: The new rating must be in the range 1 - 4 (inclusive) // TODO: If it's not, print an error message stating the valid range and don't update the rating rating = newRating; } /** * Reset the rating of this song to not rated */ public void resetRating() { rating = 0; } /** * Increase the rating of this song by 1 */ public void increaseRating() { // TODO: ----------------- 5 ------------------- // TODO: Update so that the rating is not allowed to go higher than 4. // TODO: If the rating is already at 4, calling this method has no effect. // TODO: Do not print anything in this method. rating = rating + 1; } /** * Decrease the rating of this song by 1 */ public void decreaseRating() { // TODO: ----------------- 6 ------------------- // TODO: Update so that the rating is not allowed to go lower than 1. // TODO: If the rating is already at 1, calling this method has no effect. // TODO: If the rating is 0, calling this method has no effect. // TODO: Do not print anything in this method. rating = rating - 1; } /** * Print information on this song */ public void printSongInfo() { System.out.println("---------------------------------"); System.out.println("Song Title: " + title); System.out.println("Artist: " + artist); System.out.println("Length: " + lengthInSeconds + " seconds"); System.out.println("Rating: " + rating); // TODO: ----------------- 7 ------------------- // TODO: Update the two statements above: // TODO: Call getLengthAsString to get the length to print. // TODO: For example, "Length: 2m 30s" should be printed instead of "Length: 150 seconds" // TODO: If a song is not rated yet (rating 0), print "Rating: (not rated)" instead of "Rating: 0" System.out.println("---------------------------------"); } } Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
