Java Programming Help! Task #2 Arrays of Objects Copy the files Song.java , Comp
ID: 3556988 • Letter: J
Question
Java Programming Help!
Task #2 Arrays of Objects
Copy the files Song.java , CompactDisc.java , and Classics.txt into NetBeans or other Java IDE tools. Song.java is complete and will not be edited. Classics.txt is the data file that will be used by CompactDisc.java. We have learned about data file path in Project for chapter 4.
Creating a new song with the title and artist and storing it in the appropriate position in the array.
Print the contents of the array to the console.
Compile, debug, and run. Your output should be as follows:
Those are the instructions... Now here are the codes:
Classic.txt
Ode to Joy
Bach
The Sleeping Beauty
Tchaikovsky
Lullaby
Brahms
Canon
Bach
Symphony No. 5
Beethoven
The Blue Danube Waltz
Strauss
CompactDisc.java
/**This program creates a list of songs for a CD by reading from a file*/
import java.io.*;
public class CompactDisc
{
public static void main(String [] args) throws IOException
{
FileReader file = new FileReader("Classics.txt");
BufferedReader input = new BufferedReader(file);
String title;
String artist;
//Declare an array of songs of size 6
Song [] cd = new Song [6];
for (int i = 0; i < cd.length; i++)
{
title = input.readLine();
artist = input.readLine();
// Task #2 step 2 - creating a new song with
// the title and artist, and store it into the array, like
// cd[i] = new Song(title, artist);
}
System.out.println("Contents of Classics:");
for (int i = 0; i < cd.length; i++)
{
// Task #2 step 3 - print the contents of the array to the console
// using cd[i].toString() to get the contents string
}
}
}
Songs.java
/*This program represents a song*/
public class Song
{
/**The title of the song*/
private String title;
/**The artist who sings the song*/
private String artist;
/**constructor
@param title The title of the song
@param artist The artist who sings the song
*/
public Song(String title, String artist)
{
this.title = title;
this.artist = artist;
}
/**toString method returns a description of the song
@return a String containing the name of the song and the artist
*/
public String toString()
{
return title + " by " + artist + " ";
}
}
Ok.. So I am completely lost on this one... I can't get Netbeans to do anything with this at all.. First off, I don't understand how to read the Classics.txt file in... Please help!
Explanation / Answer
import java.io.*;
public class CompactDisc
{
public static void main(String [] args) throws IOException
{
FileReader file = new FileReader("Classic.txt"); // filename is not Classics.txt
BufferedReader input = new BufferedReader(file);
String title;
String artist;
//Declare an array of songs of size 6
Song [] cd = new Song [6];
for (int i = 0; i < cd.length; i++)
{
title = input.readLine();
artist = input.readLine();
// Task #2 step 2 - creating a new song with
// the title and artist, and store it into the array, like
cd[i] = new Song(title, artist);
}
System.out.println("Contents of Classics:");
for (int i = 0; i < cd.length; i++)
{
// Task #2 step 3 - print the contents of the array to the console
// using cd[i].toString() to get the contents string
System.out.print(cd[i].toString());
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.