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

1. Write a method called playListBuilder that takes no parameters and returns an

ID: 3816605 • Letter: 1

Question

1. Write a method called playListBuilder that takes no parameters and returns an ArrayList of Strings (song names).
In the method body, declare an ArrayList of Strings called playList and populate it with song names (Strings) using input from the user at the command line.
The user can enter as many songs as they like. Use a sentinel to allow the user to indicate when they are finished.
Don't forget to include a prompt.(2 points)

2. Write a method called searchPlayList that takes an ArrayList of Strings as a parameter (the playList) and returns a boolean.
In the method body, prompt the user to enter a song name (the search term, a String). Then check if the song name
appears in the playlist ArrayList. If it is found, print "Song found", otherwise, print "Song not found". (2 points)

3. Write a method called printPlayList that takes an ArrayList of Strings as a parameter (the playList) and returns nothing.
In the method body, loop through the playlist and print its contents (the song names) to the command line,
each on a separate line. (2 points)

4. Call each of the methods above from the main method.
The pseudocode for the main method is as given below. (2 points)

    Get the user name from the user
    Print the welcome message with the user name
    Get the playlist from the user
    Search the playlist and report if search term is found or not
    Print the playlist

Explanation / Answer

PlayListTest.java

import java.util.ArrayList;
import java.util.Scanner;


public class PlayListTest {

  
   public static void main(String[] args) {
       String name = getUserName();
       printWelcomeMessage(name);
       ArrayList<String> list = playListBuilder();
       if(searchPlayList(list)) {
           System.out.println("Song found");
       }
       else{
           System.out.println("Song not found");
       }
       printPlaylist(list);
   }
   public static void printPlaylist(ArrayList<String> list){
       for(String s: list){
           System.out.println(s);
       }
   }
   public static ArrayList<String> playListBuilder() {
       ArrayList<String> playList = new ArrayList<String>();
       Scanner scan = new Scanner(System.in);
       System.out.println("Enter the song name (quit to exit): ");
       String name = scan.nextLine();
       while(!name.equalsIgnoreCase("quit")) {
           playList.add(name);
           System.out.println("Enter the song name (quit to exit): ");
           name = scan.nextLine();
              
       }
       return playList;

   }
   public static boolean searchPlayList(ArrayList<String> playList) {
       Scanner scan = new Scanner(System.in);
       System.out.println("Enter the song name to search: ");
       String name = scan.nextLine();
       for(String song: playList){
           if(song.equalsIgnoreCase(name)){
               return true;
           }
       }
       return false;
   }
   public static String getUserName() {
       Scanner scan = new Scanner(System.in);
       System.out.println("Enter the user name: ");
       String name = scan.nextLine();
       return name;
   }
   public static void printWelcomeMessage(String userName){
       System.out.println("Welcome "+userName+"!");
       System.out.println();
   }
}

Output:

Enter the user name:
Suresh
Welcome Suresh!

Enter the song name (quit to exit):
aaaaa
Enter the song name (quit to exit):
bbbbb
Enter the song name (quit to exit):
ccccc
Enter the song name (quit to exit):
quit
Enter the song name to search:
ccccc
Song found
aaaaa
bbbbb
ccccc