CSE143X Sample Programs handout #16
Program WordCount.java
----------------------
// This program prompts the user for the name of a file and then counts the
// occurrences of words in the file (ignoring case). It then reports the
// frequencies using a cutoff supplied by the user that limits the output
// to just those words with a certain minimum frequency.
import java.util.*;
import java.io.*;
public class WordCount {
public static void main(String[] args) throws FileNotFoundException {
// open the file
Scanner console = new Scanner(System.in);
System.out.print("What is the name of the text file? ");
String fileName = console.nextLine();
Scanner input = new Scanner(new File(fileName));
// wordCounts occurrences
SortedMap wordCounts = new TreeMap();
while (input.hasNext()) {
String next = input.next().toLowerCase();
if (!wordCounts.containsKey(next)) {
wordCounts.put(next, 1);
} else {
wordCounts.put(next, wordCounts.get(next) + 1);
}
}
// get cutoff and report frequencies
System.out.println("Total words = " + wordCounts.size());
System.out.print("Minimum number of occurrences for printing? ");
int min = console.nextInt();
for (String word : wordCounts.keySet()) {
int count = wordCounts.get(word);
if (count >= min)
System.out.println(count + "\t" + word);
}
}
}
A Sample Log of Execution
-------------------------
What is the name of the text file? moby.txt
Total words = 30368
Minimum number of occurrences for printing? 500
4571 a
1354 all
587 an
6182 and
563 are
1701 as
1289 at
973 be
1691 but
1133 by
1522 for
1067 from
754 had
741 have
1686 he
552 him
2459 his
1746 i
3992 in
512 into
1555 is
1754 it
562 like
578 my
1073 not
506 now
6408 of
933 on
775 one
675 or
882 so
599 some
2729 that
14092 the
602 their
506 there
627 they
1239 this
4448 to
551 upon
1567 was
644 were
500 whale
552 when
547 which
1672 with
774 you
Input file friends.dot
--------------------------------------
graph {
Ashley -- Christopher
Ashley -- Emily
Ashley -- Joshua
Bart -- Lisa
Bart -- Matthew
Christopher -- Andrew
Emily -- Joshua
Jacob -- Christopher
Jessica -- Ashley
JorEl -- Zod
KalEl -- JorEl
Kyle -- Lex
Kyle -- Zod
Lisa -- Marge
Matthew -- Lisa
Michael -- Christopher
Michael -- Joshua
Michael -- Jessica
Samantha -- Matthew
Samantha -- Tyler
Sarah -- Andrew
Sarah -- Christopher
Sarah -- Emily
Tyler -- Kyle
Stuart -- Jacob
}
Program file Friends.java
-------------------------
// This program reads a Graphviz graph file of friendships and allows the user
// to find the distance between two people in the graph.
import java.io.*;
import java.util.*;
public class Friends {
public static void main(String[] args) throws FileNotFoundException {
System.out.println("Welcome to the cse143 friend finder.");
Scanner input = new Scanner(new File("friends.dot"));
Map> friends = readFile(input);
Scanner console = new Scanner(System.in);
System.out.print("starting name? ");
String name1 = console.next();
if (!friends.containsKey(name1)) {
System.out.println("That person is not in the data file.");
} else {
System.out.print("target name? ");
String name2 = console.next();
showMatches(friends, name1, name2);
}
}
// pre : input is open and contains a legal Graphviz file of friendship
// connections where each friend is listed as a single token
// post: returns a map that contains for each person the set of friends for
// that person
public static Map> readFile(Scanner input) {
Map> friends = new TreeMap>();
while (input.hasNextLine()) {
String line = input.nextLine();
if (line.contains("--")) {
Scanner lineData = new Scanner(line);
String name1 = lineData.next();
lineData.next(); // this skips the "--" token
String name2 = lineData.next();
addTo(friends, name1, name2);
addTo(friends, name2, name1);
}
}
return friends;
}
// post: computes the distance between two people, printing the various
// lists of friends with their distance from name1
public static void showMatches(Map> friends,
String name1, String name2) {
Set oldFriends = new TreeSet();
Set newFriends = new TreeSet();
newFriends.add(name1);
int distance = 0;
System.out.println();
System.out.println("Starting with " + name1);
while (!newFriends.contains(name2) && !newFriends.isEmpty()) {
distance++;
oldFriends.addAll(newFriends);
Set newNewFriends = new TreeSet();
for (String friend : newFriends) {
newNewFriends.addAll(friends.get(friend));
}
newNewFriends.removeAll(oldFriends);
newFriends = newNewFriends;
System.out.println(" " + distance + " away: " + newFriends);
}
if (newFriends.contains(name2)) {
System.out.println("found at a distance of " + distance);
} else {
System.out.println("not found");
}
}
// post: records a friendship for name1. If name1 is not in the map, a new
// set is added to the map
public static void addTo(Map> friends, String name1,
String name2) {
if (!friends.containsKey(name1)) {
friends.put(name1, new TreeSet());
}
friends.get(name1).add(name2);
}
}
Sample Execution of Friends
---------------------------
starting name? Stuart
target name? Ashley
Starting with Stuart
1 away: [Jacob]
2 away: [Christopher]
3 away: [Andrew, Ashley, Michael, Sarah]
found at a distance of 3
Welcome to the cse143 friend finder.
starting name? Stuart
target name? Bart
Starting with Stuart
1 away: [Jacob]
2 away: [Christopher]
3 away: [Andrew, Ashley, Michael, Sarah]
4 away: [Emily, Jessica, Joshua]
5 away: []
not found