Question: Write a Java program to format an arbitrary text file to have exactly 8 0 Write a Java program to characters per line ( except

Write a Java program to format an arbitrary text file to have exactly 80 Write a Java program to
characters per line (except for the last line which may have 80 or less characters), after replacing all occurrences of tabs and two or more consecutive
spaces with a single space. Your program must use three threads running
concurrently. The first thread reads characters from the input file using the
provided class A1Reader, and replaces end-of-line characters with spaces.
The second thread removes and replaces tabs and removes consecutive
occurrences of spaces, and the third thread writes lines of 80 characters
to the output file using the provided class A1Writer. The threads must
communicate characters via circular buffers of length 20 characters using
the algorithm from question 1.The three threads should be started by the main thread of your Java
program. Your program should use cooperative multitasking, i.e., a thread
should allow others to proceed when it can do no useful work but not
otherwise, and it should terminate gracefully, i.e., all threads should
reach the end of their run methods.The three threads should be started by the main thread of your Java
program. Your program should use cooperative multitasking, i.e., a thread
should allow others to proceed when it can do no useful work but not
otherwise, and it should terminate gracefully, i.e., all threads should
reach the end of their run methods.A zip file containing a file FileConverter.java with your
main method for the program, along with all supporting source (.java) files
(apart from A1Reader and A1Writer), and a file readme.txt describing (in
a few paragraphs) the approach you have taken to coding your program.For testing purposes, it is a requirement that you use the
provided A1Reader and A1Writer classes. It is also important that you do
not modify the provided classes. Also, do not make your submitted files
dependent on being in a particular package. That is, remove any lines.You should NOT use locks, semaphores, or the synchronized or wait/notify capabilities of Java objects in this Assignment.Please do not make your submitted files dependent on being in a particular package.
This assignment can be implemented just depending on java.io package.
Please ensure any other packages are not imported.
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.lOException;
/**
DO NOT MODIFY THIS CLASS
This class is used to read single characters from a file whose name is passed
as a parameter to its constructor.
*/
public class A1Reader {
private BufferedReader br; // an object for reading characters from a byte stream
/**
Construct a file reader for reading from a specified file.
@param fileName the name of the file
*/
public A1Reader(String fileName) throws IOException {
br = new BufferedReader(new FileReader(fileName));
}
/**
Read a single character from the file.
@return the character or -1 if end-of-file is reached (-1 is also returned if
an IOException is caught)
*/
public int read(){
try {
int i = br.read();
// ignore '\r'(carriage return) which occurs before '
'(new line) on Windows operating systems
if ((char) i =='\r')
i = br.read();
return i;
} catch (IOException e){
System.err.println("Exception occurred while reading.");
return -1;
}
}
/**
Close the file reader. This should be done when reading to the file is
finished to release associated system resources.
*/
public void close(){
try {
br.close();
} catch (IOException e){
System.err.printIn("Exception occurred while closing reader.");
}
}
} import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
/**
DO NOT MODIFY THIS CLASS
This class is used to write single characters and line breaks to a file whose
name is passed as a parameter to its constructor.
*/
public class A1Writer {
PrintWriter pw; // an object for writing characters to a byte stream
/**
Construct a file writer for writing to a specified file.
@param fileName the name of the file
*/
public A1Writer(String fileName) throws IOException {
pw = new PrintWriter(new FileOutputStream(new File(fileName)));
}
/**
Write a single character to the file.
@param d the character
*/
public void write(char d){
pw.write(d);
}
/**
Write a line break to the file.
*/
public void lineBreak(){
pw.write(System.getProperty("line.separator"));
}
/**
Close the file writer. This must be done when writing to the file is
finished to ensure any buffered characters are flushed.
*/
public void close(){
pw.close();
}
}
Write a Java program to format an arbitrary text

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Finance Questions!