Question: Instructions Start NetBeans. Create a new project called Lab4. Create a Java main class file using the class name YourlastnameLab4 with your actual last name.
Instructions
- Start NetBeans.
- Create a new project called Lab4.
- Create a Java main class file using the class name YourlastnameLab4 with your actual last name.
- Create a Java class file for a Song class.
- Implement the Song class.
- Add the following private instance variables to the Song class:
- A String representing the title of the song.
- A String representing the artist that performed the song.
- An int representing the length of the song in seconds.
- Add the following constructors to the Song class:
- A default constructor (no arguments) that sets the title of the song to "untitled", the artist to "unknown", and the length of the song to zero seconds.
- A constructor that takes four arguments: the title, the artist, and two arguments representing the song length in minutes and seconds. Use the formula length=minutes60+seconds to convert the last two arguments to get the length of the song in seconds.
- Add accessor and mutator methods for each private instance variable in the class.
- Add a void method called display that displays the song information in the following format:
- (Artist) - Minutes:Seconds
- Add the following private instance variables to the Song class:
-
- example, the song For What Its Worth by Buffalo Springfield which is 157 seconds long would be displayed as:
- What It's Worth" (Buffalo Springfield) - 2:37
- the / and % operators to calculate the minutes and seconds.
- Use the main class to test the Song class.
- Write a unit test for each of the constructors. Use your accessor methods to determine the values of each of the private instance variables.
- Write a unit test for the display method.
- Run the main program to see if the tests were successful. The output should look something like this:
-
- default constructor title: untitled artist: unknown length: 0 four-parameter constructor title: For What It's Worth artist: Buffalo Springfield length: 157 display method "untitled" (unknown) 0:0 "For What It's Worth" (Buffalo Springfield) 2:37
- Add improvements to the Song class.
- Make the four-parameter constructor display an error and exit the program using System.exit if the seconds provided are greater than 59 or if either the seconds or minutes are less than zero.
- Make the displaySong method display a leading zero if the number of seconds is less than 10. For example, the song Blue Canadian Rockies by The Byrds is 122 seconds long and its length should be displayed as 2:02 not 2:2.
- Add more tests to the main class.
- Add a unit test that tests displaySong on a song with a length that would require a leading zero.
- Add a unit test that tests the four-parameter constructor with a seconds value that is greater than 59. Make sure this is the last test you perform since it will exit the program if it works correctly.
- Run the main program to see if the tests were successful. The output should look something like this:
-
- default constructor title: untitled artist: unknown length: 0 four-parameter constructor title: For What It's Worth artist: Buffalo Springfield length: 157 display method "Blue Canadian Rockies" (The Byrds) 2:02 four-parameter constructor with invalid seconds Error: invalid running time value
- Rewrite your main class to use private static helper methods.
- Write a private static void method called testDefaultConstructor that performs your default constructor unit test.
- Replace the code in the main method that performs this unit test with a call to testDefaultConstructor.
- Run the main program to make sure it still works.
- Write a private static void method called test4ParamConstructor that has four parameters and uses these parameters to test the four-argument constructor as you did in the main method.
- Replace the code in the main method that performs this unit test with calls to test4ParamConstructor.
- Run the main program to make sure it still works. The output should be the same as in the previous step.
- Hand in the source files, YourlastnameLab2.java and Song.java, to the D2L assignment dropbox called Lab 4. The comments in each file should contain your name, CSCI 2011 Lab 4, and a description of what the class defined in the file does.
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
