Advertisement
Guest User

SearchByArtistPrefix.java

a guest
Sep 18th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.15 KB | None | 0 0
  1. package student;
  2. import java.io.*;
  3. import java.util.*;
  4.  
  5. import student.Song.CmpArtist;
  6.  
  7. /*
  8.  * SearchByArtistPrefix.java
  9.  * starting code
  10.  * Boothe 2016
  11.  */
  12. public class SearchByArtistPrefix {
  13.  
  14.     private Song[] songs;  // The constructor fetches and saves a reference to the song array here
  15.    
  16.     public SearchByArtistPrefix(SongCollection sc) {
  17.         songs = sc.getAllSongs();
  18.     }
  19.  
  20.     private ArrayList<Song> foundSongs = new ArrayList<Song>();
  21.     ArrayList<Song> tempSongs = new ArrayList<Song>();
  22.     /**
  23.      * find all songs matching artist prefix
  24.      * uses binary search
  25.      * should operate in time log n + k (# matches)
  26.      */
  27.     public Song[] search(String artistPrefix) {
  28.  
  29.         // write this method
  30.        
  31.         Song tempSong = new Song(artistPrefix, "thisIsTitle", "thisIsLyrics");
  32.         CmpArtist cmp = new CmpArtist();
  33.        
  34.         int returnValue = 0;
  35.  
  36.  
  37.  
  38.        
  39.         while(){
  40.            
  41.            
  42.             //this should return an int value either ; negative - if the element being searched for
  43.             //is not found.. or a positive int representing at what element in the array the searched element is
  44.             returnValue = Arrays.binarySearch(songs, tempSong, cmp);
  45.            
  46.            
  47.             //if the item is not found
  48.             if (returnValue < 0) {
  49.                 break;
  50.             }
  51.  
  52.            
  53.            
  54.             //add the found song to foundSongs array
  55.             foundSongs.add(songs[returnValue]);
  56.            
  57.             //remove the found song from original array, so not to re-copy
  58.             songs[returnValue] = null;
  59.         }
  60.  
  61.         //debug
  62.         System.out.println("out of forever loop");
  63.  
  64.         songs = foundSongs.toArray(new Song[songs.length]);
  65.  
  66.         //preserve ordering
  67.         Arrays.sort(songs);
  68.         return songs;
  69.     }
  70.  
  71.  
  72.     public static void main(String[] args) {
  73.  
  74.  
  75.  
  76.         if (args.length == 0) {
  77.             System.err.println("usage: prog songfile [search string]");
  78.             return;
  79.         }
  80.  
  81.         SongCollection sc = new SongCollection(args[0]);
  82.  
  83.         SearchByArtistPrefix sbap = new SearchByArtistPrefix(sc);
  84.  
  85.         if (args.length > 1){
  86.             System.out.println("searching for: "+args[1]);
  87.             Song[] byArtistResult = sbap.search(args[1]);
  88.  
  89.             // to do: show first 10 matches
  90.             for (int i = 0; i <= 10; i++) {
  91.                 System.out.println("First 10 matches " + sbap.search("Be")[i].getArtist());
  92.             }
  93.  
  94.         }
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement