Created
March 14, 2016 02:24
-
-
Save connlark/ff6c561a8c0ec34e0da8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Film{ | |
static boolean compareByYear; | |
int year; | |
String title; | |
public Film(int year, String title){ | |
this.year = year; | |
this.title = title; | |
} | |
@Override | |
public String toString() { | |
return year + "\t" + title; | |
} | |
public int getYear() { | |
return year; | |
} | |
public void setYear(int year) { | |
this.year = year; | |
} | |
public String getTitle() { | |
return title; | |
} | |
public void setTitle(String title) { | |
this.title = title; | |
} | |
public static void compareByYear(){ | |
compareByYear = true; | |
} | |
public static void compareByTitle(){ | |
compareByYear = false; | |
} | |
public int compareTo(Object o){ | |
if (compareByYear) | |
return this.year - ((Film) o).getYear(); | |
return this.title.compareTo(((Film) o).getTitle()); | |
} | |
public boolean titleContainsTheWord(String word) { | |
return helper(word.toLowerCase(),title.toLowerCase()); | |
} | |
//helper for titleContainsTheWord method. | |
public boolean helper(String word, String title){ | |
if (title.indexOf(" ") == -1) return word.equals(title); | |
return word.equals(title.substring(0,title.indexOf(" "))) | |
|| helper(word,title.substring(title.indexOf(" ")+1)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.File; | |
import java.io.FileNotFoundException; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.List; | |
import java.util.Scanner; | |
/** | |
* Store Film objects in an ArrayList | |
* | |
* @author Mr. Funk | |
* @version 1.0 | |
*/ | |
public class Films | |
{ | |
private ArrayList<Film> data; | |
/** | |
* @param the location of the file containing the list of names | |
*/ | |
public Films() { | |
String[] parts; | |
data = new ArrayList<Film>(); | |
try { | |
String word; | |
Scanner scanner = new Scanner(new File("bestPicture.txt")); | |
while (scanner.hasNextLine()) { | |
word = scanner.nextLine(); | |
/** Split the line into parts which were "tab delimited"**/ | |
parts = word.split("\t"); | |
data.add(new Film(Integer.parseInt(parts[0]),parts[1])); | |
} | |
//if file not found, catch the exception thrown by the Scanner class | |
} catch (FileNotFoundException e) { | |
System.out.println("File Not Found"); | |
} | |
} | |
public ArrayList<Film> getData() | |
{ | |
return data; | |
} | |
/** | |
* precondition: none | |
* postcondition: prints out the film information for all best pictures | |
*/ | |
public void print() { | |
for (Film f:data) | |
System.out.println(f); | |
} | |
/** | |
* binary search to find title based on year | |
* @return film title (return empty string if not found) | |
*/ | |
public String searchForFilmByYear(int year) { | |
int low; int high; int middle; | |
int size = data.size(); | |
low = 0; | |
high = size - 1; | |
while(high >= low) { | |
middle = (low + high) / 2; | |
if(data.get(middle).getYear() == year) | |
return data.get(middle).getTitle(); | |
if(data.get(middle).getYear() < year) | |
low = middle + 1; | |
if(data.get(middle).getYear() > year) | |
high = middle - 1; | |
} | |
return ""; | |
} | |
/** | |
* precondition: none | |
* postcondition: none | |
* @return: the year the title won best picture. Returns -1 if the film is not found. | |
*/ | |
public int searchForFilmByTitle(String title) { | |
for (Film f:data) | |
if (f.getTitle().equals(title)) return f.getYear(); | |
return -1; | |
} | |
/** | |
* @param: word is not null | |
* @return: An ArrayList of Films whose title's contain word. | |
*/ | |
public ArrayList<Film> filmsContainingTheWord(String word) { | |
ArrayList<Film> films = new ArrayList<Film>(); | |
for (Film f:data) | |
if (f.titleContainsTheWord(word)) films.add(f); | |
return films; | |
} | |
public void mergeSort() { | |
data = mergeSortHelper(data); | |
} | |
private ArrayList<Film> mergeSortHelper(ArrayList<Film> array) { | |
if (array.size() <= 1) return array; | |
ArrayList<Film> left,right; | |
int middleRight = (int)Math.ceil(array.size()/2.0); | |
left = new ArrayList<Film>(array.subList(0,middleRight)); | |
right = new ArrayList<Film>(array.subList(middleRight,array.size())); | |
left = mergeSortHelper(left); | |
right = mergeSortHelper(right); | |
return merge(left,right); | |
} | |
private ArrayList<Film> merge(ArrayList<Film> leftSide, ArrayList<Film> rightSide) { | |
ArrayList<Film> result = new ArrayList<Film>(); | |
int size = leftSide.size() + rightSide.size(); | |
while (rightSide.size() != 0 && leftSide.size() != 0 &&result.size() != size){ | |
if (leftSide.get(0).compareTo(rightSide.get(0)) < 0){ | |
result.add(leftSide.remove(0)); | |
} | |
else result.add(rightSide.remove(0)); | |
} | |
result.addAll(rightSide); | |
result.addAll(leftSide); | |
return result; | |
} | |
/** | |
* @return a String representing all the films. The format should be year<tab>title. The films should be followed by a newline character | |
* except the last film. | |
*/ | |
public String toString() { | |
String out = ""; | |
for (Film f:data) | |
out += f.toString() + "\n"; | |
return out.substring(0,out.length()-1); | |
} | |
public void randomize(){ | |
for (int i = 0; i < data.size(); i++) { | |
data.set(i,data.get((int)(Math.random()*data.size()))); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment