Question

Do the TODOs in SongFileAccessor.java. It inherits from FileAccessor class. TODO 1: Implement the processLine method....

Do the TODOs in SongFileAccessor.java. It inherits from FileAccessor class.

TODO 1: Implement the processLine method. When the text file is processed, each line of text will be passed to processLine . Each line contains 4 fields: title, album, artist, and play time. The album field is optional. Each field is separated by a comma.

TODO 2: Implement the songToCSVString method. This method takes a Song object as a parameter and returns a String which is the csv representation of its data. Use the Song class get methods to get the data from the Song object and compose a csv string. See the examples above.

TODO 3: Implement the getSongsAsCSV . This method takes a SongList object as a parameter. It assembles a CSV-formatted String for all of the songs in the SongList and returns that String. Call the songToCSVString method on each Song to get the csv String for that Song. Append each Song’s csv String to a result String that will be returned.

SongFileAccessor.java:

1 import java.io.*;
2 import java.util.ArrayList;
3
4 /**
5 * This class extends FileAccessor to process the song data in a text file.
6 * The songs are stored in the file in comma-delimited(CSV) format. Each
7 * line in the file reporesents one song. Each line in the file is processed
8 * into a new Song object, which is placed on the SongList member variable "songs".
9 * Another method, writeSongsToFile, takes a SongList, converts its songs to a single
10 * CSV formatted String and writes it to the text file.
11 **/
12 public class SongFileAccessor extends FileAccessor{
13
14 SongList songs;
15 /* After the call to the superclass constructor, initialize the member variable "songs"
16 to a new SongList.
17 */
18 public SongFileAccessor(String f) throws IOException{
19 super(f);
20 songs = new SongList();
21 }
22
23 /* This method implements the abstract method processLine in the FileAccessor class.
24 It uses the String class method split to parse the line into individual Strings.
25 The split method is passed "," as a parameter since the comma is the delimeter.
26
27 Each line of the file has this format:
28 title,album,artist,playTime
29 Where title, artist and playTime are required, and album is optional. An example of
30 a line with no album:
31 title,,artist,playTime
32
33 You may assume that the title, artist and playTime fields will always be present. After
34 the line has been tokenized, the array returned by split contains the data needed to create a
35 new Song object which is then added to the SongList object songs.
36 You have to check the length of the second token to determine if the album field is present and based
37 on that length which Song constructor to call.
38 */
39 public void processLine(String line){
40 // TODO 1: Implement this method.
41
42
43
44 }
45
46 /* Formats the data in a Song object into a String in CSV format.
47 * See the text files and instructions for examples of this format.
48 */
49 public String songToCSVString(Song song){
50 // TODO 2: Implement this method.
51 return null;
52 }
53
54 /* This method returns a String of all songs in the song list
55 in CSV format. Each song must be put into CSV format, and
56 a line break inserted between songs. Calls songToCSVString.
57 */
58 public String getSongsAsCSV(SongList songList){
59 // TODO 3: Implement this method.
60
61
62
63 return null;
64 }
65
66 /* Returns the SongList member variable "songs".
67 */
68 public SongList getSongList(){
69 return songs;
70 }
71
72 /* This method writes the data in a SongList object to the text file.
73 The songs on the list must first be converted into a String in CSV
74 format. Then this method calls the writeToFile method, passing it the
75 CSV String and the fileName.
76 */
77 public void writeSongsToFile(SongList songs) throws IOException{
78 writeToFile(getSongsAsCSV(songs), fileName);
79 }
80
81 // This main can be run for testing SongFileAccessor.
82 public static void main(String[] args)throws IOException{
83 // need a file to pass in, but can ignore it and enter your own inputs.
84 String dataFile = "songfiles/test0songs.txt";
85 SongFileAccessor sfa = new SongFileAccessor(dataFile);
86 Song song1 = new Song("Kashmir", "Physical Graffiti", "Led Zepplin", 8.37);
87 // Test songToCSVString
88 System.out.println("****** Test songToCSVString ******");
89 String songCSVstr = sfa.songToCSVString(song1);
90 System.out.println("songToCSVString output: "+ songCSVstr);
91 System.out.println("correct output: Kashmir,Physical Graffiti,Led Zepplin,8.37");
92 System.out.println(" ");
93 // Test getSongsAsCSV
94 System.out.println("****** Test getSongsAsCSV ******");
95 SongList songList = new SongList();
96 songList.addSong(new Song("What About Us", "Beautiful Trauma", "P!nk", 4.31));
97 String songsCSVstr = sfa.getSongsAsCSV(songList);
98 System.out.println("songToCSVString output: "+ songsCSVstr);
99 System.out.println("correct output: What About Us,Beautiful Trauma,P!nk,4.31");
100 System.out.println("-note: should see a space between the above because a line break is added in getSongsAsCSV.");
101 System.out.println(" ");
102 // Test processLine
103 System.out.println("****** Test processLine ******");
104 String csvLine = "Like It Is,The Blue,Yusef Lateef,7.36";
105 sfa.processLine(csvLine);
106 SongList songs = sfa.getSongList();
107 ArrayList<Song> songArrayList = songs.getSongList();
108 Song testSong = songArrayList.get(0);
109 System.out.println("title test: "+"Like It Is"+ " - "+testSong.getTitle());
110 System.out.println("album test: "+"The Blue"+ " - "+testSong.getAlbum());
111 System.out.println("artist test: "+"Yusef Lateef"+ " - "+testSong.getArtist());
112 System.out.println("playtime test: 7.36"+ " - "+testSong.getPlayTime());
113 }
114 }

FileAccessor.java:

1 import java.util.Scanner;
2 import java.io.*;
3
4 /**
5 * This abstract class contains the functionality of reading lines from a text file
6 * and writing a String to a text file. Its abstract method, processLine, is intended to
7 * be implemented by subclasses so they can process each line in their specialized manner.
8 * This class utilizes a Scanner member variable to read from a text file. The file name is
9 * a protected member variable and is available directly to its subclasses. A PrintWriter is used
10 * in the writeToFile method to write a String to the file.
11 */
12 public abstract class FileAccessor{
13 protected String fileName;
14 Scanner scan;
15
16 /* Constructor that initializes the Scanner and opens the file.
17 * An IOException is thrown if the file cannot be opened or is not found.
18 */
19 public FileAccessor(String fName) throws IOException{
20 fileName = fName;
21 scan = new Scanner(new FileReader(fileName));
22 }
23
24 /* Assumes the Scanner instance contains the lines of text from the file.
25 * The lines are read from the scanner, and processLine is called in each line.
26 */
27 public void processFile() {
28 while(scan.hasNext()){
29 processLine(scan.nextLine());
30 }
31 scan.close();
32 }
33
34 /* This method must be implemented by any subclass. This allows the
35 * subclass to handle the line of text in its own specialized manner.
36 */
37 public abstract void processLine(String line);
38
39 /* This method utilizes a PrintWriter to write a String to a text file.
40 * An IOException is thrown if the file cannot be opened or found.
41 */
42 public void writeToFile(String data, String fileName) throws IOException{
43 PrintWriter pw = new PrintWriter(fileName);
44 pw.print(data);
45 pw.close();
46 }
47
48 }

Homework Answers

Answer #1

SongFileAccessor.java After Completing TODO's:

package song;

import java.io.*;
import java.util.ArrayList;


/**
 * This class extends FileAccessor to process the song data in a text file. The
 * songs are stored in the file in comma-delimited(CSV) format. Each line in the
 * file reporesents one song. Each line in the file is processed into a new Song
 * object, which is placed on the SongList member variable "songs". Another
 * method, writeSongsToFile, takes a SongList, converts its songs to a single
 * CSV formatted String and writes it to the text file.
 **/
        public class SongFileAccessor extends FileAccessor {

        SongList songs;

        /*
         * After the call to the superclass constructor, initialize the member variable
         * "songs" to a new SongList.
         */
        public SongFileAccessor(String f) throws IOException {
                super(f);
                songs = new SongList();
        }

        /*
         * This method implements the abstract method processLine in the FileAccessor
         * class. It uses the String class method split to parse the line into
         * individual Strings. The split method is passed "," as a parameter since the
         * comma is the delimeter.
         * 
         * Each line of the file has this format: title,album,artist,playTime Where
         * title, artist and playTime are required, and album is optional. An example of
         * a line with no album: title,,artist,playTime
         * 
         * You may assume that the title, artist and playTime fields will always be
         * present. After the line has been tokenized, the array returned by split
         * contains the data needed to create a new Song object which is then added to
         * the SongList object songs. You have to check the length of the second token
         * to determine if the album field is present and based on that length which
         * Song constructor to call.
         */
        public void processLine(String line) {
                // TODO 1: Implement this method.
                //split to parse the line into individual Strings
                String[]fields=line.split(",");
                String title=fields[0];//title
                String album=fields[1];//album
                String artist=fields[2];//artist
                double playTime=Double.parseDouble(fields[3]);//duration
                Song s;
                //checking whether album is there or  not
                if(album.length()==0)//if second field length is zero(no album)
                {
                        //create song object by passing title,artist,playTime
                        s=new Song(title,artist,playTime);
                }
                else
                {
                        //create song object by passing title,album,artist,playTime
                        s=new Song(title,album,artist,playTime);
                }
                //add song to list
                songs.addSong(s);
        }

        /*
         * Formats the data in a Song object into a String in CSV format. See the text
         * files and instructions for examples of this format.
         */
        public String songToCSVString(Song song) {
                // TODO 2: Implement this method.
                String s="";
                s=s+song.getTitle()+","+song.getAlbum()+","+song.getArtist()+","+song.getPlayTime();
                return s;
        }

        /*
         * This method returns a String of all songs in the song list in CSV format.
         * Each song must be put into CSV format, and a line break inserted between
         * songs. Calls songToCSVString.
         */
        public String getSongsAsCSV(SongList songList) {
                
                String s="";
                
                ArrayList<Song>list=songList.getSongList();
                for(Song song:list)
                {
                        s+=songToCSVString(song)+"\n";
                }
                return s;
        }

        /*
         * Returns the SongList member variable "songs".
         */
        public SongList getSongList() {
                return songs;
        }

        /*
         * This method writes the data in a SongList object to the text file. The songs
         * on the list must first be converted into a String in CSV format. Then this
         * method calls the writeToFile method, passing it the CSV String and the
         * fileName.
         */
        public void writeSongsToFile(SongList songs) throws IOException {
                writeToFile(getSongsAsCSV(songs), fileName);
        }

        // This main can be run for testing SongFileAccessor.
        public static void main(String[] args) throws IOException {
                // need a file to pass in, but can ignore it and enter your own inputs.
                String dataFile = "songfiles/test0songs.txt";
                SongFileAccessor sfa = new SongFileAccessor(dataFile);
                Song song1 = new Song("Kashmir", "Physical Graffiti", "Led Zepplin", 8.37);
                // Test songToCSVString
                System.out.println("****** Test songToCSVString ******");
                String songCSVstr = sfa.songToCSVString(song1);
                System.out.println("songToCSVString output: " + songCSVstr);
                System.out.println("correct output: Kashmir,Physical Graffiti,Led Zepplin,8.37");
                System.out.println(" ");
                // Test getSongsAsCSV
                System.out.println("****** Test getSongsAsCSV ******");
                SongList songList = new SongList();
                songList.addSong(new Song("What About Us", "Beautiful Trauma", "P!nk", 4.31));
                String songsCSVstr = sfa.getSongsAsCSV(songList);
                System.out.println("songToCSVString output: " + songsCSVstr);
                System.out.println("correct output: What About Us,Beautiful Trauma,P!nk,4.31");
                System.out
                                .println("-note: should see a space between the above because a line break is added in getSongsAsCSV.");
                System.out.println(" ");
                // Test processLine
                System.out.println("****** Test processLine ******");
                String csvLine = "Like It Is,The Blue,Yusef Lateef,7.36";
                sfa.processLine(csvLine);
                SongList songs = sfa.getSongList();
                ArrayList<Song> songArrayList = songs.getSongList();
                Song testSong = songArrayList.get(0);
                System.out.println("title test: " + "Like It Is" + " - " + testSong.getTitle());
                System.out.println("album test: " + "The Blue" + " - " + testSong.getAlbum());
                System.out.println("artist test: " + "Yusef Lateef" + " - " + testSong.getArtist());
                System.out.println("playtime test: 7.36" + " - " + testSong.getPlayTime());
        }
}

Useful Image:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
TODO 1: Constructor. It assigns the songList member variable to a new instance of an ArrayList...
TODO 1: Constructor. It assigns the songList member variable to a new instance of an ArrayList that stores Song objects.. TODO 2: Implement the isEmpty method. Takes no parameters. Returns true if there are no songs on the list, false otherwise. TODO 3: Implement the addSong method. This method takes a Song object as a parameter and does not return a value. It adds the song to the songList. TODO 4: Implement the getSongListAsString method. This method takes no parameters....
Complete the // TODO sections in the EasyRental class. Create a method that implements an iterative...
Complete the // TODO sections in the EasyRental class. Create a method that implements an iterative sort that sorts the vehicle rentals by color in ascending order (smallest to largest) Create a method to binary search for a vehicle based on a color, that should return the index where the vehicle was found or -1 You are comparing Strings in an object not integers. Ex. If the input is: brown red white blue black -1 the output is: Enter the...
Can someone please edit my code so that it satisfies the assignments' requirements? I pasted the...
Can someone please edit my code so that it satisfies the assignments' requirements? I pasted the codes below. Requirement: Goals for This Project:  Using class to model Abstract Data Type  OOP-Data Encapsulation You are asked to write an app to keep track of a relatively small music library. The app should load song information from a data file once the app is started. It should allow user to view, add, remove, and search for songs. The app should...
THIS IS FOR JAVA I have to write a method for a game of Hangman. The...
THIS IS FOR JAVA I have to write a method for a game of Hangman. The word the user is trying to guess is made up of hashtags like so " ###### " If the user guesses a letter correctly then that letter is revealed on the hashtags like so "##e##e##" If the user guesses incorrectly then it increments an int variable named count " ++count; " String guessWord(String guess,String word, String pound) In this method, you compare the character(letter)...
Songlist.java is a class that keeps track of songs in file named songfile. Write a function:...
Songlist.java is a class that keeps track of songs in file named songfile. Write a function: public Song getSong(String Songname) which will return the song associated with the name of the song passed if it is present in the library or null if its not? The filename is passed in as command line argument.
i am trying to wrire a word and change it to capital letters using client-server. im...
i am trying to wrire a word and change it to capital letters using client-server. im not able to write any words when i run the file in netbeans or command promot. is it something with my code? /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package assignment3retake; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter;...
ex3 Write a method public static boolean isPalindrome(String input) that uses one or more stacks to...
ex3 Write a method public static boolean isPalindrome(String input) that uses one or more stacks to determine if a given string is a palindrome. [A palindrome is a string that reads the same forwards and backwards, for example ‘racecar’, ‘civic’]. Make sure that your method works correctly for special cases, if any. What is the big-O complexity of your method in terms of the list size n. Supplementary Exercise for Programming (Coding) [Stacks] Download and unpack (unzip) the file Stacks.rar....
please write the code in java so it can run on jGRASP import java.util.Scanner; 2 import...
please write the code in java so it can run on jGRASP import java.util.Scanner; 2 import java.io.*; //This imports input and output (io) classes that we use 3 //to read and write to files. The * is the wildcard that will 4 //make all of the io classes available if I need them 5 //It saves me from having to import each io class separately. 6 /** 7 This program reads numbers from a file, calculates the 8 mean (average)...
Instruction This task is about using the Java Collections Framework to accomplish some basic textprocessing tasks....
Instruction This task is about using the Java Collections Framework to accomplish some basic textprocessing tasks. These questions involve choosing the right abstraction (Collection, Set, List, Queue, Deque, SortedSet, Map, or SortedMap) to efficiently accomplish the task at hand. The best way to do these is to read the question and then think about what type of Collection is best to use to solve it. There are only a few lines of code you need to write to solve each...
Given main() complete the Stack class by writing the methods push() and pop(). The stack uses...
Given main() complete the Stack class by writing the methods push() and pop(). The stack uses an array of size 5 to store elements. The command Push followed by a positive number pushes the number onto the stack. The command Pop pops the top element from the stack. Entering -1 exits the program. Ex. If the input is Push 1 Push 2 Push 3 Push 4 Push 5 Pop -1 the output is Stack contents (top to bottom): 1 Stack...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT