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

Please write a program in java. Arrays of Objects Revisited with Files and the S

ID: 645556 • Letter: P

Question

Please write a program in java.

Arrays of Objects Revisited with Files and the StringTokenizer class

Problem:

1.       You are to create a Song class which has three private fields:

                     String title

                     String artist

                     String genre

                     double cost

           Create an initializer constructor for the title, the artist, the genre, and the cost

           Create a toString method which returns the title, artist, genre and the cost

2.       Create a file called MySongs.txt with the following data:

           Ode to Joy*Bach*Classical*1.99

The Sleeping Beauty*Tchaikovsky*Classical*2.99

Lullaby*Brahms*Classical*0.00

Canon*Bach*Classical*1.99

Symphony No. 5*Beethoven*Classical*0.00

The Blue Danube Waltz*Strauss*Classical*1.99

You will use the StringTokenizer class with * as the delimiter

3.     Create a class called CompactDisc.java which has the following:

private arrayList of MySong

write a method to fill the arrayList from your file

write a toString method which prints out the entire arrayList to the Screen

write a toFile method which prints out the entire arrayList to a File

write a method which accepts a genre and prints out all songs of that genre

write a method which adds up the total cost of all songs and returns that cost

4.    Create a tester class MyCollection.java which has function main to

create a CompactDisc object which calls the method to read in the file

create a MySong object which asks the user to add a Song object to the CompactObject

write out the complete collection before and after adding songs

ask the user for a genre and print out all songs of that genre

test the method to add up the total cost of all songs

Explanation / Answer

import java.io.*;
import java.util.Scanner;

class Song {
private String title;
private String artist;
private String genre;
double cost;

Song() {
title = null;
artist = null;
}

Song (String title, String artist,String genre,double cost)
{
title = title;
artist = artist;
genre=genre;
cost=cost;
}

String getTitle()
{
return title;
}
void setTitle(String val)
{
title = val;
}

String getArtist()
{
return artist;
}
void setArtist(String val)
{
artist = val;
}


String getGenre()
{
return genre;
}

void setGenre(String val)
{
genre = val;
}

double getCost()
{
return cost;
}

void setCost(double val)
{
cost = val;
}
}

public void GenreFinder(String genreIn) {
ArrayList<Song> mySong = new ArrayList<Song>();
int count;

for (count = 0; count < mySong.size(); count++) {

if (mySong.contains(genreIn)) {
System.out.println(mySong.get(count));
}
}
}

public class CompactDisc
{
ArrayList<Song> mySong = new ArrayList<Song>();
static class Song {
private String title;
private String artist;

Song() {
title = null;
artist = null;
}

Song (String title, String artist)
{
title = title;
artist = artist;
}

String getTitle() { return title; }
void setTitle(String val) { title = val; }

String getArtist() { return artist; }
void setArtist(String val) { artist = val; }
}

Class MyCollection{

public static void main(String [] args) throws IOException
{
File file = new File("MySongs.txt");
String st = new String ("MySongs.txt");
StringTokenizer tokens = new StringTokenizer(st,'*');
while (st.hasMoreTokens()) {
System.out.println(st.nextToken());
}

Scanner input = new Scanner(file);
String title;
String artist;
String genre;
double cost;

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

Song[] cd;
cd = new Song[6];

for (int i = 0; i < cd.length; i++)
{
title = input.nextLine();
artist = input.nextLine();

cd[i] = new Song(title, artist,genre,cost);
}

System.out.println("Contents of Classics:");
for (int i = 0; i < cd.length; i++)
{
System.out.println ("Song " + (i+1) + ": " + cd[i].getTitle() + " / " + cd[i].getArtist()+"/"+cd[i].getGenre()+"/"+cd[i].getCost());
}
}
}