Now the question is, how can i do what is described right below? ------------ So
ID: 3531408 • Letter: N
Question
Now the question is, how can i do what is described right below?
------------
Song[] cd = new Song[6]
cd[i] = new Song(title, artist);
These statements cannot identify the symbol SOng because they have not been inherited
import java.[package name].Song;
This includes the class song in the last code
Now the classes should be inserted in a package and each class should be connected as per the inheritence.
---------------------------
//This program creates a list of songs for a CD by reading from a file
import java.io.*;
import java.util.Scanner;
public class CompactDisc
{
public static void main(String[] args) throws IOExceptio
{
File file = new File("Classics.txt");
Scanner input = new Scanner(file);
String title;
String artist;
//Declare an array of songs, called cd, of size 6
for (int i = 0; i < cd.length; i++)
{
title = input.nextLine();
artist = input.nextLine();
//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
}
}
}
_________________________________________________________
public class Song
{
private String title;
private String artist;
public Song(String title, String artist)
{
this.title = title;
this.artist = artist;
}
public String toString()
{
return title + " by " + artist + " ";
}
}
Explanation / Answer
//both files using default package
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Song.java
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public class Song
{
private String title;
private String artist;
public Song(String title, String artist)
{
this.title = title;
this.artist = 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.Scanner;
public class CompactDisc
{
public static void main(String[] args) throws IOException
{
File file = new File("Classics.txt");
Scanner input = new Scanner(file);
String title;
String artist;
//Declare an array of songs, called cd, of size 6
Song[] cd=new Song[6];
for (int i = 0; i < cd.length; i++)
{
title = input.nextLine();
artist = input.nextLine();
cd[i]=new Song(title,artist);
//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.print(cd[i].toString());
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.