Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

**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**

  1. Start NetBeans.
  2. Create a new project called Lab4.
  3. Create a Java main class file using the class name YourlastnameLab4 with your actual last name.
  4. Create a Java class file for a Song class.
  5. Implement the Song class.
    1. 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.
    2. 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.
    3. Add accessor and mutator methods for each private instance variable in the class.
    4. Add a void method called display that displays the song information in the following format:
    1. (Artist) - Minutes:Seconds
    1. example, the song For What Its Worth by Buffalo Springfield which is 157 seconds long would be displayed as:
    2. What It's Worth" (Buffalo Springfield) - 2:37
    3. the / and % operators to calculate the minutes and seconds.
  1. Use the main class to test the Song class.
    1. Write a unit test for each of the constructors. Use your accessor methods to determine the values of each of the private instance variables.
    2. Write a unit test for the display method.
    3. Run the main program to see if the tests were successful. The output should look something like this:
    1. 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
  1. Add improvements to the Song class.
    1. 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.
    2. 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.
  2. Add more tests to the main class.
    1. Add a unit test that tests displaySong on a song with a length that would require a leading zero.
    2. 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.
    3. Run the main program to see if the tests were successful. The output should look something like this:
    1. 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
  1. Rewrite your main class to use private static helper methods.
    1. Write a private static void method called testDefaultConstructor that performs your default constructor unit test.
    2. Replace the code in the main method that performs this unit test with a call to testDefaultConstructor.
    3. Run the main program to make sure it still works.
    4. 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.
    5. Replace the code in the main method that performs this unit test with calls to test4ParamConstructor.
    6. Run the main program to make sure it still works. The output should be the same as in the previous step.
  2. 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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Refactoring Databases Evolutionary Database Design

Authors: Scott Ambler, Pramod Sadalage

1st Edition

0321774515, 978-0321774514

More Books

Students also viewed these Databases questions

Question

Discuss laws affecting collective bargaining.

Answered: 1 week ago

Question

Discuss the Hawthorne experiments in detail

Answered: 1 week ago

Question

Explain the characteristics of a good system of control

Answered: 1 week ago

Question

State the importance of control

Answered: 1 week ago

Question

What are the functions of top management?

Answered: 1 week ago