CSE142—Computer Programming I
Sample Movie Programs
This handout has 3 similar programs that process imdb.txt, which has lines like:
8
169931 8.8 One Flew Over the Cuckoo's Nest (1975)
9 85454 8.8 12 Angry Men (1957)
10 230925 8.8 Star Wars: Episode V - The
Empire Strikes Back (1980)
11 140486 8.8
Each version prompts for a phrase, finds all lines containing that phrase and presents information about those lines. Version 1 produces text output, but is bad style because it uses “method chaining.” Version 2 has the same external behavior as Version 1 but is good style. Version 3 produces graphical output where bars show each movie’s popularity. Version 3 is closest to Homework 6, where you also process a file to produce a bar chart.
The third version does not produce a DrawingPanel unless there is at least one match. It produces output like the following:
Search word: part
4 matches.
Version
1: Bad Style Due to Method Chaining
//
Displays IMDB's Top 250 movies that match a search
string.
import java.io.*; // for File
import java.util.*; // for Scanner
public class MoviesChaining {
public static String
MOVIE_FILE = "imdb.txt";
public static void
main(String[] args) throws FileNotFoundException
{
getWord();
}
// Asks the user for their search word and returns it.
public static void getWord()
throws FileNotFoundException {
System.out.print("Search word: ");
Scanner console = new Scanner(System.in);
String searchWord
= console.next();
searchWord
= searchWord.toLowerCase();
System.out.println();
Scanner input = new Scanner(new
File(MOVIE_FILE));
search(input, searchWord);
}
//Breaks
apart each line, looking for lines that match the search word.
public static void
search(Scanner input, String searchWord) {
int
matches = 0;
while (input.hasNextLine()) {
String line = input.nextLine();
String lineLC = line.toLowerCase(); // case-insensitive match
if (lineLC.indexOf(searchWord) >=
0) {
matches++;
if(matches == 1) {
System.out.println("#\tRating\tVotes\tTitle");
}
display(line);
}
}
System.out.println(matches + " matches.");
}
// Displays the line in the proper format on
the screen.
public static void
display(String line) {
Scanner lineScan
= new Scanner(line);
int
rank = lineScan.nextInt();
int
votes = lineScan.nextInt();
double rating = lineScan.nextDouble();
String title = "";
while (lineScan.hasNext()) {
title += lineScan.next() + " "; // the rest of the line
}
System.out.println(rank + "\t" + rating + "\t" + votes +
"\t"
+ title);
}
}
Version 2: Good style with text output
public class MoviesTextOutput
{
public static String MOVIE_FILE = "imdb.txt";
public static void main(String[] args)
throws FileNotFoundException {
String searchWord = getWord();
Scanner
input = new Scanner(new File(MOVIE_FILE));
String
line = search(input, searchWord);
int
matches = 0;
if (line.length() > 0) {
System.out.println("#\tRating\tVotes\tTitle");
while (line.length() > 0) {
matches++;
display(line);
line = search(input, searchWord);
}
}
System.out.println(matches + " matches.");
}
// Asks the user for their search word and returns it.
public static String getWord() {
System.out.print("Search
word: ");
Scanner
console = new Scanner(System.in);
String searchWord = console.next();
searchWord = searchWord.toLowerCase();
System.out.println();
return searchWord;
}
// Breaks
apart each line, looking for lines that match the search word.
public static String search(Scanner input, String searchWord) {
while (input.hasNextLine()) {
String
line = input.nextLine();
String lineLC = line.toLowerCase(); //
case-insensitive match
if (lineLC.indexOf(searchWord) >= 0) {
return line;
}
}
return "";
// not found
}
// Displays
the line in the proper format on the screen.
public static void display(String line) {
Scanner lineScan = new Scanner(line);
int rank = lineScan.nextInt();
int votes = lineScan.nextInt();
double rating = lineScan.nextDouble();
String
title = "";
while (lineScan.hasNext()) {
title += lineScan.next() + "
"; // the rest of the line
}
System.out.println(rank + "\t" + rating + "\t" + votes +
"\t" + title);
}
}
Version 3: Graphical output (import statements elided to
fit on a page)
public class MoviesGraphical
{
public static String MOVIE_FILE = "imdb.txt";
public static void main(String[] args)
throws FileNotFoundException {
String searchWord = getWord();
Scanner
input = new Scanner(new File(MOVIE_FILE));
String line = search(input,
searchWord);
int matches = 0;
if (line.length() > 0) {
Graphics
g = createWindow();
while (line.length() > 0) {
matches++;
display(g, line, matches);
line = search(input, searchWord);
}
}
System.out.println(matches + " matches.");
}
// Asks the user for their search word and returns it.
public static String getWord() {
System.out.print("Search
word: ");
Scanner
console = new Scanner(System.in);
String searchWord = console.next();
searchWord = searchWord.toLowerCase();
System.out.println();
return searchWord;
}
//Breaks
apart each line, looking for lines that match
public static String search(Scanner input, String searchWord) {
while (input.hasNextLine()) {
String
line = input.nextLine();
String lineLC = line.toLowerCase(); //
case-insensitive match
if (lineLC.indexOf(searchWord) >= 0) {
return line;
}
}
return "";
// not found
}
// Displays
the line in the proper format on the screen.
public static void display(Graphics g, String line, int matches) {
Scanner lineScan = new Scanner(line);
int rank = lineScan.nextInt();
int votes = lineScan.nextInt();
double rating = lineScan.nextDouble();
String
title = "";
while (lineScan.hasNext()) {
title += lineScan.next() + "
"; // the rest of the line
}
drawBar(g,
matches, title, rank, rating);
}
// Creates a drawing panel and draws all fixed graphics.
public static Graphics createWindow()
{
DrawingPanel panel = new DrawingPanel(600, 500);
Graphics
g = panel.getGraphics();
for (int i
= 0; i <= 10; i++)
{ // draw tick marks
int
x = i * 50;
g.drawLine(x, 20, x, 30);
g.drawString(i + ".0", x, 20);
}
return g;
}
// Draws
one red bar representing a movie's votes and ranking.
public static void drawBar(Graphics
g, int matches, String title, int
rank, double rating) {
int y = 70 + 100 * (matches - 1);
int w = (int)
(rating * 50);
int h = 50;
g.setColor(Color.BLUE); //
draw the blue bar for that movie
g.fillRect(0, y, w, h);
g.setColor(Color.BLACK);
g.drawString("#"
+ rank + ": " + title, 0, y);
}
}