Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

#3 Arrays of Objects 1. I will provide the following code files for you to compl

ID: 3709931 • Letter: #

Question

#3 Arrays of Objects

1. I will provide the following code files for you to complete Song.java (code listing 7.1), CompactDisc.java (code listing 7.2), and Classics.txt (code listing 7.3).

Song.java is complete and will not be edited. Classics.txt is the data file that will be used by CompactDisc.java, the file you will be editing.

2. In CompactDisc.java, there are comments indicating where the missing code is

to be placed. Declare an array of Songs, called cd, to be of size 6.

3. Fill the array by creating a new song with the title and artist and storing it in

the appropriate position in the array.

4. Print the contents of the array to the console.

5. Compile, debug, and run. Your output should be as follows:

Contents of Classics

Ode to Joy by Bach

The Sleeping Beauty by Tchaikovsky

Lullaby by Brahms

Canon by Bach

Symphony No. 5 by Beethoven

The Blue Danube Waltz by Strauss

//Song.java

//CompactDisc.java

//Classics.txt

Ode to Joy
Bach
The Sleeping Beauty
Tchaikovsky
Lullaby
Brahms
Canon
Bach
Symphony No. 5
Beethoven
The Blue Danube Waltz
Strauss

Explanation / Answer

import java.io.File;

import java.io.IOException;

import java.util.ArrayList;

import java.util.List;

import java.util.Scanner;

public class CompactDisc {

public static void main(String[] args) throws IOException {

File file = new File("D:\Classic.txt");

Scanner input = new Scanner(file);

String title;

String artist;

String[] cd = new String[6];

// Declare an array of songs, called cd, of size 6

int i = 0;

while (input.hasNext()) {

String line = input.nextLine();

cd[i] = line;

i++;

System.out.println(line);

}

List<String> list = new ArrayList<String>();

;

for (int j = 0; j < cd.length; j++) {

// fill the array by creating a new song with

String line1 = cd[j];

String[] artists = line1.split("by");

for (int k = 0; k < artists.length; k++) {

list.add(artists[k]);

}

// the title and artist and storing it in the

// appropriate position in the array

}

System.out.println("Contents of Classics:");

for (String string : list)

System.out.println(string);

}

}

output

Ode to Joy by Bach
The Sleeping Beauty by Tchaikovsky
Lullaby by Brahms
Canon by Bach
Symphony No. 5 by Beethoven
The Blue Danube Waltz by Strauss
Contents of Classics:
Ode to Joy
Bach
The Sleeping Beauty
Tchaikovsky
Lulla

Brahms
Canon
Bach
Symphony No. 5
Beethoven
The Blue Danube Waltz
Strauss