CS 448, Fall 2011 -- Lab Zero, Basics -- Due before class on Thursday 9/1
We will be using Java and NetBeans for programming in this class.
Since I have not had most of you in class before, I have no idea how familiar you are with them, thus, I provide this
warm-up lab. Some of it you will no doubt find entirely trivial; feel free to skip those parts. Some of it you may find
rather less familiar; do those parts.
Java Greetings in NetBeans.
Compile and run a Greetings application in the NetBeans IDE. I.e. write the one line program that will display the word greetings to the output window.
If you are unfamiliar with the NetBeans IDE you could learn about it from the online help; doing the tutorial could be very helpful.
Mechanics: File I/O
Write a GUI application with two buttons: Input and Output
- The Input button should read a text file the user chooses and display it in a TextArea
- The Output button should output the contents of the TextArea to a file (again of the user's choice).
Feel free to use the MyReader and MyWriter wrapper classes, (or util.scanner if that is more familiar).
Fix the package directive in both!
The java.io.File class
Add a button to echo all the files in a specified directory to the TextArea, separated by blank lines, with legible headers including the complete pathname for each file.
Store the text of
each file in a Pattern (yes, write a Pattern class with an ArrayList<String> inside); this will simplify Lab 1 as well as being good, modular programming.
This is easy, once you are familiar with the
Java class for dealing with files, java.io.File. Here's an example that assumes Pattern and PatternList are written (and that Pattern has toString(), and
a constructor that is passed a path):
static final boolean DEBUG=true;
PatternList patterns;
void listDirectory() {
inputData(new File("complete path to the directory you wish to read all the files from here"));
for (Pattern p : patterns) {
spew(p);
}
}
public void inputData(File directory) {
patterns = new PatternList();
if (DEBUG) System.out.println("\n " + directory + "\n");
java.io.File[] list = directory.listFiles();
for (int i = 0; i < list.length; i++) {
if (DEBUG) System.out.println("reading... " + list[i]);
patterns.add(new Pattern(list[i].getAbsolutePath()));
}
}
void spew(Pattern p) {
theTA.append(p.toString() + "\n");
}
Here is a very simple way to write PatternList:
public class PatternList extends java.util.ArrayList<Pattern>{} // PatternList is now a synonym for java.util.ArrayList<Pattern> (almost)