Please help with this codding assignment. I\'m using java netbeans. I can\'t fig
ID: 3856410 • Letter: P
Question
Please help with this codding assignment. I'm using java netbeans. I can't figure out how to print the contents of an array. Please leave comments explaining what you did, thank you, and on how to do input.
DOMAIN
/*This program represents a song*/
public class Song
{
private String title; //The title of the song
private String artist; //The artist who sings the song
/**
* constructor
*
* @param title The title of the song
* @param artist The artist who sings the song
*/
public 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;
}
}
DRIVER
/*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;
Song cd[] = new Song[6];
//Declare an array of songs, called cd, of size 6
for (int i = 0; i < cd.length; i++)
{
System.out.println("Enter title: " + i);
title = input.readLine();
System.out.println("Enter artist: " + i);
artist = input.readLine();
// fill the array by creating a new song with
// the title and artist and storing it in the
// appropriate position in the array
}
System.out.println("Contents of Classics:");
for (int i = 0; i < cd.length; i++)
{
//print the contents of the array to the console
System.out.println(cd.toString());
}
}
}
Explanation / Answer
Note : Could you please check the output .If you required any changes Just intimate.I will modify it.Thank You.
___________________
classics.txt (Save this file under D Drive.Then the path of the file pointing to it is D:\classics.txt )
Bit I'm Madonna
Madonna
Ice Ice Baby
Vanilla Ice
God of Every Story
Laura
A Different Corner
Michael, George
A Matter of Habit
Moddi
A Stitch in Time
Long Ryders
_________________
Song.java
/*This program represents a song*/
public class Song {
private String title; // The title of the song
private String artist; // The artist who sings the song
/**
* constructor
*
* @param title
* The title of the song
* @param artist
* The artist who sings the song
*/
public 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;
}
}
___________________
CompactDisc.java
/*This program creates a list of songs for a CD by reading from a file*/
import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
public class CompactDisc {
public static void main(String[] args) throws IOException {
//Creating an ArrayList which holds Song class objects
ArrayList < Song > s = new ArrayList < Song > ();
//Creating a Scanner class object
Scanner sc = new Scanner(new File("D:\classics.txt.txt"));
//Declaring variables
String title;
String artist;
//reading the data form the file
while (sc.hasNext()) {
title = sc.nextLine();
artist = sc.nextLine();
//Creating a Song class object and store the Song object in the ArrayList
s.add(new Song(title, artist));
}
//Displaying the Contents inside the Song class objects
System.out.println("Contents of Classics:");
for (Song songs: s) {
//print the contents of the array to the console
System.out.println(songs.toString());
}
}
}
_______________________
Output:
Contents of Classics:
" Bit I'm Madonna " by Madonna
" Ice Ice Baby " by Vanilla Ice
" God of Every Story " by Laura
" A Different Corner " by Michael, George
" A Matter of Habit " by Moddi
" A Stitch in Time " by Long Ryders
___________Thank You