Question
**PLEASE GO THROUGH ALL STEPS, THANK YOU** Start NetBeans. Create a new project called Lab4. Create a Java main class file using the class name
**PLEASE GO THROUGH ALL STEPS, THANK YOU**
- 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.
HERE IS WHAT I HAVE SO FAR
main class:
public static void main(String[] args) { //Create song with default constructor. Song song = new Song(); System.out.println("title: " + song.getTitle() + " " + "artist: " + song.getArtist() + " " + "length: "+ song.getLength() + " "); //Create song with parameterized constructor. Song song2=new Song("For What It's Worth","Buffalo Springfield",2,37); System.out.println("title: " + song2.getTitle() + " " + "artist: " + song2.getArtist() + " " + "length: " + song.getLength() + " "); song.display(); song2.display(); Song song3=new Song("Blue Canadian Rockies", "The Byrds", 2 , 2); song3.display(); Song song4=new Song("Blue Canadian Rockies", "The Byrds", 2 , 60); song4.display(); }
}
-----------------------------------------------------
song class:
public class Song {
//Instance variables. private String title; private String artist; private int length; public Song() { super(); this.title = "untitled"; this.artist = "unknown"; this.length = 0; } /** * @param title * @param artist * @param length */ public Song(String title, String artist,int minutes, int secs) { super(); this.title = title; this.artist = artist; if(secs<0 || secs>59) { System.out.println("Error: invalid running time value"); System.exit(1); } this.length = (minutes*+60)+secs; } /** * @return the title */ public String getTitle() { return title; } /** * @param title the title to set */ public void setTitle(String title) { this.title = title; } /** * @return the artist */ public String getArtist() { return artist; } /** * @param artist the artist to set */ public void setArtist(String artist) { this.artist = artist; } /** * @return the length */ public int getLength() { return length; } /** * @param length the length to set */ public void setLength(int length) { this.length = length; } public void display() { String secs=(length%60)<10?"0"+(length%60):String.valueOf(length%60); System.out.println(title+" ("+artist+") - "+(length/60)+":"+secs); } }
Problem so far:
I'm getting a build failure, not sure where to go from here and I don't have step 9. My output is also giving me length: 0 for the song For What It's Worth
Please double check the steps and what I already have, thank you!
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started