Question

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.

Homework Answers

Answer #1

Song.java

public class Song {
private String name, composer;
private double runTime;
  
public Song()
{
this.name = "";
this.composer = "";
this.runTime = 0;
}

public Song(String name, String composer, double runTime)
{
this.name = name;
this.composer = composer;
this.runTime = runTime;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getComposer() {
return composer;
}

public void setComposer(String composer) {
this.composer = composer;
}

public double getRunTime() {
return runTime;
}

public void setRunTime(double runTime) {
this.runTime = runTime;
}
  
@Override
public String toString()
{
return("Name: " + this.name + ", Composer: " + this.composer + ", Runtime: " + this.runTime + " sec");
}
}

SongList.java

import java.util.ArrayList;

public class SongList {
private ArrayList<Song> songs;
  
public SongList()
{
this.songs = new ArrayList<>();
}
  
public void addSong(Song song)
{
if(song != null)
this.songs.add(song);
}
  
public Song getSong(String songName)
{
Song song = null;
  
for(int i = 0; i < this.songs.size(); i++)
{
if(songs.get(i).getName().equals(songName))
{
song = songs.get(i);
break;
}
}
return song;
}
  
public void displayAllSongs()
{
for(Song song : this.songs)
{
System.out.println(song.toString());
}
System.out.println();
}
}

SongTrackerDriver.java (Main class)

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class SongTrackerDriver {
  
public static void main(String[] args)
{
// args[0] is the user input file name
String fileName = args[0].trim();
  
// counter for songs read from the file
int count = 0;
  
// a SongList object to record all the song objects
SongList songList = new SongList();
  
// Scanner object to read the file
Scanner fileReader;
  
// read all songs from the file
// and add to the songList object using the songList.add(..) method
try
{
fileReader = new Scanner(new File(fileName));
while(fileReader.hasNextLine())
{
String line = fileReader.nextLine().trim();
String[] data = line.split(",");
String songName = data[0];
String composer = data[1];
int runTime = Integer.parseInt(data[2]);
  
songList.addSong(new Song(songName, composer, runTime));
count++;
}
fileReader.close();
}catch(FileNotFoundException fnfe){
System.out.println(fileName + " couldn't be found!");
System.exit(0);
}
  
System.out.println(count + " songs read from the file!");
  
// prompt user to enter a song name to search
Scanner sc = new Scanner(System.in);
System.out.print("\nEnter a song name to search: ");
String userSongName = sc.nextLine().trim();
  
// call the songList.getSong(..) method and store the result returned into a Song variable
Song result = songList.getSong(userSongName);
  
// check whether the result is null or a song object
if(result == null)
System.out.println("No records found with the name " + userSongName);
else
System.out.println("Match found:\n" + result.toString());
}
}

************************************************************** SCREENSHOT *************************************************************

INPUT FILE (songlist.txt) : This file needs to be created in the project directory before running the program. However, data inside the file may vary but each data fields need to be separated by comma.

CONSOLE OUTPUT :

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
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...
* Python Programming * Write a class definition named Book. The Book class should have instance...
* Python Programming * Write a class definition named Book. The Book class should have instance variables for a book's title, the author's name, and the publisher's name. The class should also have the following: a) An __init__ method for the class. The method should accept an argument for each of the instance variables. b) An __str__ method that returns a string indicating the state of the object. Use the Car class file in resources/Margie's Notes/car.py as an example, but...
Write a Java class called Grades in a class file called Grades.java. 2. Grades reads from...
Write a Java class called Grades in a class file called Grades.java. 2. Grades reads from a text file containing a series of course grades (a value between 0.0 and 100.0) with one grade entry per line. However, the first line in the file is an integer value specifying how many grade entries are contained in the file. 3. The Grades class contains four static methods: a. A method called loadGrades() that opens the file, reads in the data and...
Language: JavaScript Create a public class called Inverter with a single class method named invert. invert...
Language: JavaScript Create a public class called Inverter with a single class method named invert. invert should accept a Map<String, Integer> and return a Map<Integer, String> with all of the key-value pairs swapped. You can assert that the passed map is not null. Because values are not necessarily unique in a map, normally you would need a way to determine what the right approach is when two keys map to the same value. However, the maps passed to your function...
5. Write a bash command that will display the name of every file on the Linux...
5. Write a bash command that will display the name of every file on the Linux system whose file contaent contains the string "doug.jones". The command must run in the background, must redirect standard error to /dev/null, and must redirect standard output to ~/out. 6. Write a bash command that will kill all of the even-numbered processes associated with your userid, and no other processes. 7. Write a bash command that will start the program /home/courses/140u-doug.jones/zombie_maker as a background process,...
Write a Java class called CityDistances in a class file called CityDistances.java.    2. Your methods...
Write a Java class called CityDistances in a class file called CityDistances.java.    2. Your methods will make use of two text files. a. The first text file contains the names of cities. However, the first line of the file is a number specifying how many city names are contained within the file. For example, 5 Dallas Houston Austin Nacogdoches El Paso b. The second text file contains the distances between the cities in the file described above. This file...
readCSV Define the function readCSV(filename)to so it reads the contents of the CSV file named filename...
readCSV Define the function readCSV(filename)to so it reads the contents of the CSV file named filename and returns a list of dictionaries corresponding to its contents. The CSV file will have a header file with field names. Each dictionary in the returned list of dictionaries must consist of key-value pairs where the key is a field name. The field name must be paired with the corresponding data value from the record. For example, if sample.csv contains: a,b,c 1,2,3 4,5,6 7,8,9...
In C++ Employee Class Write a class named Employee (see definition below), create an array of...
In C++ Employee Class Write a class named Employee (see definition below), create an array of Employee objects, and process the array using three functions. In main create an array of 100 Employee objects using the default constructor. The program will repeatedly execute four menu items selected by the user, in main: 1) in a function, store in the array of Employee objects the user-entered data shown below (but program to allow an unknown number of objects to be stored,...
Write a program that reads a file named input.txt and writes a file that contains the...
Write a program that reads a file named input.txt and writes a file that contains the same contents, but is named output.txt. The input file will contain more than one line when I test this. Do not use a path name when opening these files. This means the files should be located in the top level folder of the project. Do not use a copy method that is supplied by Java. Your program must read the file line by line...
Radio station KJAVA wants a class to keep track of recordings it plays. Create a class...
Radio station KJAVA wants a class to keep track of recordings it plays. Create a class named Recording that contains fields to hold methods for setting and getting a Recording’s title, artist, and playing time in seconds. Save the file as Recording.java. b. Write an application that instantiates five Recording objects and prompts the user for values for the data fields. Then prompt the user to enter which field the Recordings should be sorted by—song title, artist, or playing time....
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT