Lab 10

pdf

School

Louisiana State University *

*We aren’t endorsed by this school

Course

CIT 350

Subject

Computer Science

Date

Dec 6, 2023

Type

pdf

Pages

9

Uploaded by DrSnowBee39

Report
Lab 10, CSC 1350-03, Fall 2023 Name this project MusicLib2 Modify the code that NetBeans generates when you create your project so that it matches the code below, and update author and date information to match your name and the assignment date: import java.util.Scanner; /** * This program creates 3 parallel arrays of song titles, release years, * and artists, allows the user to search for song titles, and prints the * contents of the 3 arrays sorted by song title. * * CSC 1350-03 Lab 10 * * @author Type your name (first and last) * @since Type the date: 12/1/2023 * */ public class MusicLib2 { /** * This method validates that input from the user is an integer above a certain value. * @param LowerBound - The lower bound of valid integer values * A valid integer value must be greater than LowerBound. */ public static int InputIntAbove(String UserPrompt, int LowerBound) { // Your code for the InputIntAbove method goes here. } /** * This method validates that input from the user is a non-empty string. */ public static String InputString(String UserPrompt) { // Your code for the InputString method goes here. } /** * This method prints the contents of 3 parallel arrays: an array of strings, * an array of integers, and a second array of strings. All 3 arrays have the * same length. * @param FirstStringArray - array of strings * @param IntArray - array of integers * @param SecondStringArray - array of strings */ public static void PrintParallelArrays(String[] FirstStringArray, int[] IntArray, String[] SecondStringArray) { // Your code for the PrintParallelArrays method goes here. }
/** * This method searches an array of strings for a particular string. * @param values - array of strings * @param searchVal - string to search for */ public static int LinearSearchForString(String[] values, String searchVal) { // Your code for the LinearSearchForString method goes here. } /** * This method sorts 3 parallel arrays: an array of strings, * an array of integers, and a second array of strings. All 3 arrays * have the same length. * It does the sort on the first array of strings and carries along the * other 2 arrays in the sort. * @param FirstStringArray - array of strings * @param IntArray - array of integers * @param SecondStringArray - array of strings */ public static void SortParallelArrays(String[] FirstStringArray, int[] IntArray, String[] SecondStringArray) { // The code for the SortParallelArrays method goes here. } public static void main(String[] args) { // The code for the main method goes here. } }
The MusicLib2 Program You will write a program that has 6 methods: the InputIntAbove method, the InputString method, the PrintParallelArrays method, the LinearSearchForString method, the SortParallelArrays method, and the main method. InputIntAbove method The InputIntAbove method will use input validation to ensure that the user is entering an integer above LowerBound. Thus there are two things that the method needs to validate: 1) the input value is an integer. 2) the integer is greater than LowerBound. Do the following to implement this input validation: Declare the scanner variable Declare an int variable to store the value entered by the user and initialize it to 0 Declare a boolean variable named haveGoodInput and initialize it to false Start a while loop with the condition “!haveGoodInput” Inside the while loop, do the following: Prompt the user by printing UserPrompt Use an if statement with the condition “!in.hasNextInt()” to check if the user did not enter an integer. Inside the if statement, use a do loop with the condition “!in.hasNextInt()” that prints the message shown in the example run below and then prompts the user by printing UserPrompt. NOTE: Put “in.nextLine();” at the beginning of the body of the do loop to clear the buffer before prompting the user to enter a value. After the end of the if statement, use “in.nextInt()” to read the value the user entered. Use a second if statement to check if the value entered is greater than LowerBound. If so, redefine haveGoodInput to be true. If not, print the message shown in the example run below. End the while loop. Return the value entered by the user. NOTE: The InputIntAbove method in Lab 10 is exactly the same as the InputIntAbove method in Lab 9. If you did Lab 9 and your output displayed the same messages that were shown on the Lab 9 assignment sheet, you can copy the InputIntAbove method from Lab 9 and paste it into Lab 10. InputString method The InputString method will use input validation to ensure that the user is entering a non-empty string. Do the following to implement this input validation: Declare the scanner variable Declare a String variable named inputVal to store the string entered by the user and initialize it to an empty string ( ""). Start a while loop with the condition inputVal.equals("") Inside the while loop, do the following: Prompt the user by printing UserPrompt Use in.nextLine() to read the string that the user entered. Use an if statement with the condition inputVal.equals("") to check if the user entered an empty string. If so, print the message shown in the example run below and then print a blank line. End the while loop. Return the string entered by the user. NOTE: The InputString method in Lab 10 is exactly the same as the InputString method in Lab 9. If you did Lab 9 and your output was the same as the output that was shown on the Lab 9 assignment sheet, you can copy the InputString method from Lab 9 and paste it into Lab 10.
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
PrintParallelArrays method The PrintParallelArrays method prints the contents of 3 parallel arrays: an array of strings named FirstStringArray, an array of integers named IntArray, and a second array of strings named SecondStringArray. All 3 arrays have the same length. Do the following to implement this method: Use a for loop with a loop variable i that goes from 0 to one less than the length of FirstStringArray . Inside the for loop do the following: Print an element of FirstStringArray using the loop variable i as the index of the array. Label it as shown in the example run. Print an element of IntArray using the loop variable i as the index of the array. Label it as shown in the example run. Print an element of SecondStringArray using the loop variable i as the index of the array. Label it as shown in the example run. Print a blank line LinearSearchForString method The LinearSearchForString method will search an array of strings named values for the string searchVal and return the position of that string in the array. If that string is not found in the array, the method will return -1 for the position. Do the following to implement this method: Declare an int variable named position and initialize it to 0 Declare a boolean variable named found and initialize it to false Start a while loop with the condition !found && position < values.length Inside the while loop, do the following: Use an if statement with the condition values[position].equals(searchVal) to check if position is the index of searchVal. If so, set found to true. If not, increment position. End the while loop. After the end of the while loop, use an if statement with the condition !found to check if searchVal was not found in the array. If it was not found, set position to -1 Return position. NOTE: The LinearSearchForString method in Lab 10 is exactly the same as the LinearSearchForString method in Lab 9. If you did Lab 9 and your output was the same as the output that was shown on the Lab 9 assignment sheet, you can copy the LinearSearchForString method from Lab 9 and paste it into Lab 10.
SortParallelArrays method The code for the SortParallelArrays method is given: public static void SortParallelArrays(String[] FirstStringArray, int[] IntArray, String[] SecondStringArray) { String temp1; int temp2; String temp3; for (int i = 1; i < FirstStringArray.length; i++) { for (int j = 0; j < FirstStringArray.length - i; j++) { if (FirstStringArray[j].compareTo(FirstStringArray[j+1]) > 0) { temp1 = FirstStringArray[j]; FirstStringArray[j] = FirstStringArray[j+1]; FirstStringArray[j+1] = temp1; temp2 = IntArray[j]; IntArray[j] = IntArray[j+1]; IntArray[j+1] = temp2; temp3 = SecondStringArray[j]; SecondStringArray[j] = SecondStringArray[j+1]; SecondStringArray[j+1] = temp3; } } } }
main method The code for the main method is given: public static void main(String[] args) { int numSongs; //number of songs in the library String[] songTitle; //array of song titles String currentSong; //song title input by the user int[] releaseYear; //array of release years int currentYear; //release year input by the user String[] artist; //array of artists String currentArtist; //song artist input by the user String searchVal; //song title to search for int songPosition; //position of song in the library Scanner in = new Scanner(System.in); //input from the keyboard //input size of library numSongs = InputIntAbove("How many songs do you want to put in your library?", 1); songTitle = new String[numSongs]; releaseYear = new int[numSongs]; artist = new String[numSongs]; //input library for(int i = 0; i < numSongs; i++) { System.out.println(); currentSong = InputString("Enter Song Title:"); songTitle[i] = currentSong; currentYear = InputIntAbove("Enter the year that the song was released:", 1900); releaseYear[i] = currentYear; currentArtist = InputString("Enter the song artist:"); artist[i] = currentArtist; } //search library System.out.println(); System.out.println("Enter a song title to search for or Q to quit:"); searchVal = in.nextLine(); while (!searchVal.equals("Q")) { songPosition = LinearSearchForString(songTitle, searchVal); if (songPosition < 0) { System.out.println(searchVal + " is not a song in the library."); } else { System.out.println(searchVal + " is in the library at position " + songPosition + "."); } System.out.println(); System.out.println("Enter a song title to search for or Q to quit:"); searchVal = in.nextLine(); } //sort library SortParallelArrays(songTitle, releaseYear, artist);
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
//print library System.out.println(); System.out.println(); System.out.println("Music Library:"); System.out.println(); PrintParallelArrays(songTitle, releaseYear, artist); }
Example run How many songs do you want to put in your library? x Input value must be a whole number with a value of at least 1 How many songs do you want to put in your library? 0 Input value must be a whole number with a value of at least 1 How many songs do you want to put in your library? 5 Enter Song Title: A non-empty string must be entered Enter Song Title: Tiger Rag Enter the year that the song was released: 1917 Enter the song artist: Dixieland Jazz Band Enter Song Title: Callin' Baton Rouge Enter the year that the song was released: 1993 Enter the song artist: Garth Brooks Enter Song Title: Hey Fightin' Tigers Enter the year that the song was released: 2009 Enter the song artist: The Golden Band From Tigerland Enter Song Title: Baby Shark Enter the year that the song was released: 2016 Enter the song artist: Robin Davies Enter Song Title: Happy Enter the year that the song was released: 2013 Enter the song artist: Pharrell Williams Enter a song title to search for or Q to quit: Hoppy Hoppy is not a song in the library. Enter a song title to search for or Q to quit: Roar Roar is not a song in the library. Enter a song title to search for or Q to quit: Hey Fightin' Tigers Hey Fightin' Tigers is in the library at position 2. Enter a song title to search for or Q to quit: Q
Music Library: Song Title: Baby Shark Release Year: 2016 Artist: Robin Davies Song Title: Callin' Baton Rouge Release Year: 1993 Artist: Garth Brooks Song Title: Happy Release Year: 2013 Artist: Pharrell Williams Song Title: Hey Fightin' Tigers Release Year: 2009 Artist: The Golden Band From Tigerland Song Title: Tiger Rag Release Year: 1917 Artist: Dixieland Jazz Band Archive and Submit the Program Using windows explorer, navigate your way to the “Documents” folder. Navigate your way through the following path: NetBeansProjects | MusicLib2 | src | musiclib2. You will then see a file named MusicLib2.java. Right click on this file and select “Send to/Compressed (zipped) folder”. This will create a zip file. Rename the zip file by adding your PAWSID and an underscore to the beginning of the zip file name. Submit the zip file for grading in Moodle.
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help