Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Finish Coding of FIRST & SECOND class and Compile It!!!!! FileAccessor in the end, NO NEED MODIFY, ONLY FOR REFERENCE --------------------------------------FIRST CLASS---------------------------------------------- 1 import java.io.*;

Finish Coding of FIRST & SECOND class and Compile It!!!!!

FileAccessor in the end, NO NEED MODIFY, ONLY FOR REFERENCE

--------------------------------------FIRST CLASS----------------------------------------------

1 import java.io.*;

2 /**

3 * This class extends FileAccessor to process the song data in a text file.

4 * The songs are stored in the file in comma-delimited(CSV) format. Each

5 * line in the file reporesents one song. Each line in the file is processed

6 * into a new Song object, which is placed on the SongList member variable "songs".

7 * Another method, writeSongsToFile, takes a SongList, converts its songs to a single

8 * CSV formatted String and writes it to the text file.

9 **/

10 public class SongFileAccessor extends FileAccessor {

11

12 // a SongList member variable

13

14

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 }

21

22 /* This method implements the abstract method processLine in the FileAccessor class.

23 It uses the String class method split to parse the line into individual Strings.

24 The split method is passed "," as a parameter since the comma is the delimeter.

25

26 Each line of the file has this format:

27 title,album,artist,playTime

28 Where title, artist and playTime are required, and album is optional. An example of

29 a line with no album:

30 title,,artist,playTime

31

32 You may assume that the title, artist and playTime fields will always be present. After

33 the line has been tokenized, the array returned by split contains the data needed to create a

34 new Song object.

35 You have to check the length of the second token to determine if the album field is present and based

36 on that length which Song constructor to call.

37 */

38 public void processLine(String line){

39 String[] tokens = line.split(",");

40

41 }

42

43 /* Returns the SongList member variable "songs".

44 */

45 public SongList getSongList(){

46 return null;

47 }

48

49 /* This method writes the data in a SongList object to the text file.

50 The songs on the list must first be converted into a String in CSV

51 format. Then this method calls the writeToFile method, passing it the

52 CSV String and the fileName.

53 */

54 public void writeSongsToFile(SongList songs) throws IOException{

55 }

56

57 /* This method returns a String of all songs in the song list

58 in CSV format. Each song must be put into CSV format, and

59 a line break inserted between songs.

60 */

61 public String getSongsAsCSV(SongList songList){

62 return null;

63 }

64 }

-----------------------------------SECOND CLASS--------------------------------------------

1 import java.util.ArrayList;

2 /* This class encapsulates a list of songs in a user's collection and several

3 * operations that can be performed on that list. A song is represented

4 * by an instance of the Song class. Each song has the following fields:

5 * a title, an (optional) album, an artist, and a playing time.

6 */

7 public class SongList {

8 //Class member variable declaration(s):

9

10

11

12 /* Constructor that initializes the list and any other

13 * variables.

14 */

15 public SongList(){

16 }

17

18 /* Add the song passed in to the end of the list.

19 * For example, if the list contained: song1, song2,

20 * the next song added, song3, would result in this list:

21 * song1, song2, song3.

22 */

23 public void addSong(Song newSong){

24 }

25

26 /* Remove the song in the songList with the targetTitle.

27 * First, the method searches for a song in the list with a title that

28 * matches the targetTitle. If it is found, that song is removed from

29 * the list. If the targetTitle is not matched, the list remains the same and false is returned.

30 * Note that there should not be any null values between songs in the list.

31 * For example, if the list contained: song1, song2, song3,

32 * and the title of song2 was "MyTitle", this call:

33 * removeSongByTitle("MyTitle");

34 * would result in this list: song1, song3.

35 * This method returns true if the targetTitle matches the title of a song in the list,

36 * false otherwise.

37 */

38 public boolean removeSongByTitle(String targetTitle){

39 return true;

40 }

41

42 /* Return the song list as an array of Song objects.

43 * Note that the array should contain only the songs

44 * on the list in the correct order and no empty cells (null values).

45 * This method returns an array of length 0 if the song list is empty.

46 */

47 public Song[] getSongListAsArray(){

48 return null;

49 }

50

51 /* Remove all songs from the list, resulting in an empty list.

52 */

53 public void clearSongList(){

54 }

55

56 /* Returns the number of songs stored in the song list.

57 */

58 public int getSize(){

59 return 0;

60 }

61

62 /* Returns true if the song list contains no songs, false otherwise.

63 */

64 public boolean isEmpty(){

65 return true;

66 }

67

68 /* This method returns a String which consists of the String

69 * representation of each song in the list. A line break is

70 * inserted between each song String.

71 * If the song list is empty, the String "no songs" is returned.

72 */

73 public String getSongListAsAsString(){

74 return null;

75 }

76 }

image text in transcribed

-----------------------------------------FileAccessor--- NO NEED MODIFY-------------------------------------

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 }

getSongListAsString. This method takes no parameters. It returns a String, which is the concatenation of each Song on the song list's String representation (its toString method). The line break character (or String) is inserted between each song. The number of each song is prepended to the song string. The numbers start at 1, not 0. This is an example of the String returned by this method as it would look when printed: 1 XO Tour Llif3, Luv Is Rage 2, Lil Uzi Vert, 3.02 2 Man's not hot, , Big Shaq, 3.06 3 Bodak Yellow (Money Moves), , Cardi B, 3.36 5 4 What About Us, Beautiful Trauma, P!nk, 4.31 5 DNA, DAMN., Kendrick Lamar, 3.06

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Administrator Limited Edition

Authors: Martif Way

1st Edition

B0CGG89N8Z

More Books

Students also viewed these Databases questions

Question

Discuss the stages of the unionization process.

Answered: 1 week ago