HW1, Programming Part

Due July 10

50 Points

Objectives: Refresher for writing classes and methods, creating objects etc.

Overview: Students will write two classes “Song” --- which includes a title, an artist, and a duration and Playlist ---a main class which will contain an array of songs. The Song class will be a simple class to store information about a particular song with limited functionality.

1. [10 Points] Define a class "Song" in a file name Song. java which has the following data members:

. title of the song (stored as a string)

. artist of the song (stored as a string)

. duration of the song in minutes

Make all data members private.

2. [5 Points] A song object can be constructed in two ways:

. One which takes the title and duration as input.

. One which takes the title, duration and artist.

Write two constructors as defined.

3. [5 Points] Provide a method 'void print()' to print the information about the song in a nice formatted manner.

Example usage:

Song s = new Song("Cool", "Gwen Stefani", 4);

s.print();

Output:

Song "Cool" by Gwen Stefani with duration of 4 mins.

4. [5 Points] Write accessor methods for all private data members.

Example usage:

String artist = s.getArtist();

5.[5 Points] Only the artist of a song object can be changed by the user of the song object.

Create the mutator method.

Example usage:

String artist = "No Doubt";

s.setArtist(artist);

6.[10 Points] Write a class 'Playlist' stored in a file Playlist.java. Include a main method as follows:

Main will create 10 different songs and store them in an array of Songs called list. Create 5 of these songs using only title and duration and 5 including all data members. Also create a “for” loop to print all songs in the array inside the main method.

 

Students will submit the two files Song.java and Playlist.java, and use variable names and method signatures consistent with the examples above.