The first part of today's tutorial gives us some practice with file input and output in Java, and the rest of the tutorial mostly focuses on how to use, process, and understand Java arrays. The material for this tutorial comes from lecture module 10 (see handouts).
Recall that in Tutorial 10 we wrote a class
FrequencyList which counts how many times items of some type
are added to the list. Let's use this class to count the frequency of words
in some text file (you can get working code for FrequencyList
and the other relevant classes from the solutions from Tutorial 10 on
the main tutorials page).
Create a new class called WordCounter which
extends (i.e. is a subclass of) FrequencyList<String>.
The class should have a single constructor which takes a String
for the name of a file. The constructor should open the file, read in its
contents word by word, and add each string to the underlying frequency list.
Add a new method to your class called writeFrequencies
which takes a String for the name of a file. The method should
write each word in the frequency list to the file, with each line formatted
as
frequency <tab> wordThe words should be sorted in the file from most to least frequent.
Hints: use the mostFrequent(int k) method from the
FrequencyList class, and recall that we can write a tab character
in java as "\t".
Now use your new class to read in the entire book of Daniel from the
Old Testament Bible (here)
and write the corresponding
sorted word frequency list to a new file called "dfreq.txt".
Note the relative frequencies of the words "Daniel", "king", and "God".
The following two classes have one main method which requires
no command-line arguments and writes some output to the screen.
Without actually compiling and running these classes, determine
exactly what will be printed to the screen by each one. You will probably want
to draw the stack/heap memory and trace through the execution step by step.
public class A {
public static void foo(int[] a, int b) {
a[1] = b;
b = a[0];
a = new int[]{2,2};
System.out.println(b);
System.out.println(a[0]);
}
public static void main( String[] args ) {
int[] array = {1,2,3,4};
int x = 5;
foo(array,x);
foo(new int[]{x,x},x);
System.out.println(x);
System.out.println(array[0]);
System.out.println(array[1]);
}
}
public class B {
public static int[] foo(int[] a, int b) {
int[] c = a;
c[1] = b;
b = c[2];
a[2] = c[0];
a[0] = b;
System.out.println(a[2]);
System.out.println(b);
System.out.println(c[2]);
a = null;
return c;
}
public static void main( String[] args ) {
int[] array1 = {1,2,3,4};
int[] array2 = {10,11,12};
array2 = foo(array1,array2[1]);
array1[3] = 50;
for( int i = 0; i < array2.length; ++i )
System.out.println(array2[i]);
}
}
Write a class ClosestPair which has one method,
public static int[] find( int[] array )
This method should find the closest pair of integer entries in
array that share the same value, and return the subarray that
lies between them (inclusive). If no value is repeated in the array, the
method should return null. You can look at the file
ClosestPairTest.java for some
examples of usage and expected behavior.
Last modified on Friday, 19 August 2011, at 18:05 hours.