Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Program 1 (20 pts) Write a program that opens a PrintWriter writing to a file named numbers.txt. Write to this file the even numbers between

Program 1 (20 pts) Write a program that opens a PrintWriter writing to a file named numbers.txt. Write to this file the even numbers between 0 and 100000, 1 number per line. Open the created file and make sure it looks right! Program 2 (20 pts) Using a JFileChooser, prompt the user for a file to open. Using a Scanner, read one line from this file at a time, until the end, printing out each line in upper case to System.out. Program 3 (60 pts) For this last program, we are going to build a class that reads one line at a time from a file and uses a callback (interface) to process each line. Heres a possible interface for processing individual String values: public interface StringProcessor { void process(String s); } You would need to create this new interface to use it in your code. I will refer to StringProcessor throughout the following description. 0.1 The general processing class Create a new class called FileProcessor with the following instance variables and constructor: class FileProcessor { private Scanner infile; public FileProcessor(File f) throws FileNotFoundException { // initialize the Scanner instance variable here } } Instances of this class open files for reading when they are constructed. Other methods can refer to the Scanner instance variable to process the contents of the file. Write a method for this class called go that takes a StringProcessor as an explicit parameter. go should repeatedly read one line of text from the open file and pass it to the explicit parameter. (You can copy most of this logic from your Program 2). 0.2 The main method Now you have a class that is capable of doing fairly arbitrary things with a text file! Write a new Driver class with a main method that uses a JFileChooser to select a file, and construct a new FileProcessor using the selected File. We are going to write several different implementations of StringProcessor to pass to FileProcessor.go. In other words, well come back to this after youve written another new class. 0.3 Interface implementation 1 Write a new class that implements StringProcessor. This classs method should print out a String in upper case. Change your main() method to call FileProcessor.go on an instance of this new class. Run the Driver and confirm that the output is correct (that is, the contents of the file are printed out in upper case). 0.4 Interface implementation 2 Write another new class that implements StringProcessor. This class should count the total number of characters in all processed Strings. This new class will need an instance variable to contain the accumulated count, and an accessor to retrieve the count. Add this to the main method youve started writing in Driver. Try this class in your main method. 2 When this new class is used, you should print out the count after processing the file. Nothing else should be printed! Notice how the interface can be implemented by classes with arbitrary instance variables and other methods. 0.5 Interface implementation 3 Write one last class that implements StringProcessor. This one should print all Strings to a file named copy.txt. The constructor of this new class should create the appropriate PrintWriter. Try this class in your main method. Make sure to close the file you are writing after the input file has been processed (after go() returns). Closing the file will require a second method in this class, since the underlying PrintWriter should be public. Good luck! 0.6 Finish up the main method With these 3 interface implementations written, return to Driver.main and make it so that after creating the FileProcessor, the user is prompted to select one of the 3 options for processing the file.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

How Do I Use A Database Research Tools You Can Use

Authors: Laura La Bella

1st Edition

1622753763, 978-1622753765

More Books

Students also viewed these Databases questions