wood burning stoves 2.0
The moose likes Beginning Java and the fly likes help with assignment Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "help with assignment" Watch "help with assignment" New topic

help with assignment

Stephen Norris
Ranch Hand

Joined: Oct 27, 2003
Posts: 44
Don't know where to begin. Just wondering if anyone knows what to do and could take a look at the assignment and possibly help me.

File Processing
This will be a non-GUI based assignment. You will be writing three relatively short file processing programs that replicate the functionality of the UNIX utility programs cp, cat, and grep. In all of the programs below you will be using a BufferedReader/BufferedWriter wrapped around a FileReader/FileWriter object. To open a file for reading or writing you will do the following:
BufferedReader in = new BufferedReader( new FileReader( filename ) );
BufferedWriter out = new BufferedWriter( new FileWriter( filename) );
Investigate the API docs for these classes to use the appropriate methods (most probably you'll need to use the methods readLine(), write(...), and others). You will be using command line arguments (see pages 742 - 743 for an example) in all of the programs below. The command line arguments can be accessed inside the application from the String[] args input argument of main method. The first input argument will be stored in arg[0], the second in arg[1], and so on.
(1) Write a program CopyFile that copies one file to another. The file names are specified on the command line. For example,
java CopyFile report.txt report.sav
should create another copy of the file report.txt in report.sav. Your program should print the following error message for inappropriate number of input arguments (for e.g., java CopyFile report.txt):
Usage: java CopyFile fromFile toFile
(2) Write a program CatFiles that concatenates the contents of several files into one file. For example,
java CatFiles chapter1.txt chapter2.txt chapter3.txt book.txt
makes a long file book.txt that contains the contents of the files chapter1.txt, chapter2.txt, and chapter3.txt. The target file is always the last file specified on the command line. As in (1) above, your program should print an appropriate error message of the same format when given inappropriate number of input arguments. Note that your program should be able to concatenate any number of files (and not just 3 files as shown above).
(3) Write a program Find that searches all files specified on the command line and prints out all lines containing a keyword. For example, if you call
java Find Buff report.txt address.txt Homework.java
then the program might print the foll. (i.e., all the lines that contain the word "Buff"):
report.txt: Buffet style lunch will be available at the
address.txt: Buffet, Warren|11801 Trenton Court|Dallas|TX
address.txt: Walters, Winnie|59 Timothy Circle|Buffalo|MI
Homework.java: BufferedReader in;
The keyword is always the first command line argument. Note that your program should first print the name of the file, followed by a colon, and the entire line in which the keyword appears. As before, your program should print an appropriate error message of the same format when given inappropriate number of input arguments. Note again that your program should work for any number of files (and not just 3 files as shown above).
(Note: You will need to use some methods from the String class to search each line returned by the readLine() method for the presence of the keyword.)
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
That's a long and potentially intimidating description. So start with "divide and conquer" or the new buzzword "intentional programming". Make code that says exactly what you're trying to do so you don't get lost in the bits & bytes.
Start by making your main() create an instance of the class and call a non-static method maybe called copyOneFile. Make the code restate the assignment as clearly as possible:

Of course this won't compile until you have all those methods. You might comment all out except the one you're working on next.
This technique comes from the bad old procedural days when it was called "functional decomposition." It runs a risk of producing objects that don't exude object oriented purity and goodness. I don't know if your instructor is trying to teach that stuff at the same time.
Here's one easy thing to apply: If you see duplicate code, find a single home for it. For example, each of your three assignments will open text files, read lines, write lines. Maybe you could move that common stuff into its own class so the three programs contain only the restatement of the assignment like I showed above.
Have fun! Check back with us if you get stuck!

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
I agree. Here's the link: http://aspose.com/file-tools
subject: help with assignment
It's not a secret anymore!