*****I need the four classes Reverse Line, Tester, TooManyLinesException, Except
ID: 3773369 • Letter: #
Question
*****I need the four classes Reverse Line, Tester, TooManyLinesException, Exception Concpets**********
Write a program that replaces each line of a file with its reverse. For example, if you run the program with HelloWorld.java file (which is a text file):
Then the contents of HelloWorld.java are changed to
dlroWolleH sslac cilbup
{
)sgra ][gnirtS(niam diov citats cilbup
{
Wodniw elosnoc eht ni gniteerg a yalpsid //
;)”!dlroW ,olleh”(nltnirp.tuo.metsyS
}
}
Your programs should still consist of a tester and the ReverseLine and TooManyLinesException classes.
The tester should get the file name from the user and open it for reading.
The file should not have more than 10 lines and if it does, the method in ReverseLine class should throw a TooManyLinesException, which is a new exception and you will write the class for it.
Use of exception concepts (throwing, throws, catch, try, finally) is absolutely necessary.
Submit just the source (*.java) files in a ZIP file here. Do not include old files (*.java~) in the zip.
All your files must have a header with your name, class, and assignment information.
Make sure that you supply proper comments for appropriate lines of code as well as all me
Compile (10):
Run (20):
Tester (10):
ReverseLine (20):
TooManyLinesException (10):
Exception concpets (20):
Explanation / Answer
Complete Program:
// File: TooManyLinesException.java
public class TooManyLinesException extends Exception
{
public TooManyLinesException(int lines)
{
super(" at line " + lines);
System.out.print("Exception: TooManyLinesException");
}
}
// File: ReverseLine.java
import java.io.*;
public class ReverseLine
{
private String filename;
private int lines;
public ReverseLine()
{
filename = "HelloWorld.java";
lines = 0;
}
public void reverseLines()
{
String line = "";
String data = "";
try
{
BufferedReader infile = new BufferedReader(new FileReader(filename));
while((line = infile.readLine()) != null)
{
lines++;
if(lines > 10)
throw new TooManyLinesException(lines);
if(lines == 1)
data = line;
else
data = line + " " + data;
}
data = data.replace(data, new StringBuilder(data).reverse().toString());
FileOutputStream outfile = new FileOutputStream(filename);
outfile.write(data.getBytes());
infile.close();
outfile.close();
}
catch(FileNotFoundException e)
{
System.out.println(filename + " cannot be opened.");
}
catch(TooManyLinesException e)
{
System.out.println(e.getMessage());
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
// File: ReverseLineTester.java
public class ReverseLineTester
{
public static void main(String[] args)
{
ReverseLine rev = new ReverseLine();
rev.reverseLines();
}
}
File before reverse: HelloWorld.java
File after reverse: HelloWorld.java
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.