Copyright © Cay S. Horstmann 2012
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License.
In this part, you will write a program that reads a Java source file and makes an index of all identifiers and the line numbers, like this: (below is the output of your program)
Dict 6 Exception 8 File 1 10 Integer 12 22 Scanner 2 10 17 String 8 12 16 21 28 System 30 32 33 TreeMap 3 12 TreeSet 4 12 22 23 add 24 args 8 10 get 22 31 hasNext 19 hasNextLine 13 identifier 21 22 25 in 10 13 16 in2 17 18 19 21 index 12 22 25 28 31 ...
Scanner in = new Scanner(new File(args[0]));
If we now keep calling in.next()
, we get a sequence of words. Try it out:
while (in.hasNext()) { System.out.println(in.next()); }
Complete a program Dict.java
and run
java Dict Dict.java
(i.e. reading your own program).
Yes, that's on the command line. Open a terminal window. Navigate to the right directory, run javac Dict.java
and then the command above.
What output do you get?
[ \t\n\r]+
What does this regular expression match?
in.useDelimiter("...");
before the while
loop and run your program again. Now what output do you get?
Here, of course, ... should be your regular expression, not three periods.
int lineNumber = 0; while (in.hasNextLine()) { lineNumber++; String line = in.nextLine(); }
Now extract the identifiers from each line, by using a second scanner
Scanner in2 = new Scanner(line); while (in2.hasNext()) ...
Print each identifier and the line number in which occurs.
What output do you get?
What map? A hash map or a tree map? What sets? Hash sets or tree sets? If either is ok, say so.
index
of type ???Map<???, ???Set<???>>
, filling in the ???. Do this before the outer while
loop. What is your code????Set<???> lines = new ???Set<>(); lines.add(lineNumber); index.put(identifier, lines)
At the end of the program, add
System.out.println(index);
Run the program again. What output do you get?
???Set<???> lines = index.get(identifier)
instead of making a new set each time. (Keep the other two lines of code.) This way, you add lineNumber
to the existing set of line numbers for the identifier.
Run the program. What happens?
index.get(key)
and print it.
Run the program. What's the output?
index.get(key)
and print each of them, with spaces in between.
Run the program. What's the output? (Your output should look similar to that before step 1.)
class
and if
. Make a set of all Java reserved words
???<???> reserved = new ???<>(Arrays.asList("abstract", "continue", "for", "new", "switch", "assert", "default", "goto", "package", "synchronized", "boolean", "do", "if", "private", "this", "break", "double", "implements", "protected", "throw", "byte", "else", "import", "public", "throws", "case", "enum", "instanceof", "return", "transient", "catch", "extends", "int", "short", "try", "char", "final", "interface", "static", "void", "class", "finally", "long", "strictfp", "volatile", "const", "float", "native", "super", "while"));Be sure not to print any of them. (Or even better, not to add any of them into the map.)
In the Java API, there is a class File
that can denote either files or directories. When you have an object file
of class File
, call
file.isDirectory()
to find out whether it's actually a directoryfile.list()
to get an array of the files and directories it contains if it is in fact a directorySuppose you are asked to print all files that occur in a directory or one of its descendants. Write pseudocode for a recursive method
printAllFiles(File dir)
printAllFiles(File dir) stk = a stack push dir on stk while stk is not empty pop stack into d for each f in d.list() if f is a file, print it otherwise push it on stk
No recursion!
Assume the following directory structure:
In which order are the files printed? (Work it out by hand.)
public class Dirs { public static void main(String[] args) throws IOException { File dir = new File(args[0]); printAllFiles(dir); } ... }
What is the code of your class?
cd path/to/lab10 mkdir numberquiz cd numberquiz jar xvf /path/to/downloads/numberquiz.zip cd .. javac Dirs.java java Dirs numberquiz
(where path/to/lab10 is the directory containing Dirs.java.)
What output do you get?Your Java program is Dirs, and your input is numberquiz. You should not get any error when running these above commands.
Solve the puzzle AB + C = BA by hand. What is a solution? How did you get it? (Remember that the letters are all different.)
Given a puzzle, we'll pick the first letter, replace it with all the numbers that aren't already present, and throw each of these simpler puzzles in a queue. For example, given 2BCD+BCDE=DA01, we'll add the following to a queue:
23CD+3CDE=DA01
24CD+4CDE=DA01
25CD+5CDE=DA01
26CD+6CDE=DA01
27CD+7CDE=DA01
28CD+8CDE=DA01
29CD+9CDE=DA01
Then we take the next one from the queue and repeat.
Eventually, we'll be left over with puzzles that have no letters left, such as
2345 + 3456 = 5801
which is a solution (so we print it)
or
2435 + 4356 = 5701
which is not (so we discard it).
In other words, the queue holds the partially finished work.
Try this for the puzzle AB + C = BA. What goes into the queue?
public class PuzzleSolver { public static void main(String[] args) { Puzzle puzzle = new Puzzle(args[0], args[1], args[2]); Queue<???> q = new LinkedList<>(); q.add(???); while () { } } }
What is your code?
java PuzzleSolver AB C BA
java PuzzleSolver 2BCD BCDE DA01
java PuzzleSolver SEND MORE MONEY
The trick is always to put unfinished work on a stack or queue. (It doesn't matter which—the only difference is the order in which the work is broken down.) In this example, you get up to four pieces of unfinished work when you reach an intersection.
Let's use a stack here.
What work items do you push on the stack? (Hint: clone)
main
method of MazeSolver
to have the form
public class MazeSolver { public static void main(String[] args) { Maze maze = new Maze(...); Robot fred = new Robot(maze, 5, 8); Stack<???> stk = new Stack<>(); stk.push(???) while (...) { ??? = stk.pop(); // At exit? Print getVisited() for (int i = 0; i < 4; i++) { turnRight(); if (...can move...) { // Clone the robot // Make the clone move stk.push(???) } } } } }
What is your code?