SlideShare a Scribd company logo
Change the code in Writer.java only to get it working. Must contain methods: logReverse() ,
logMax(), logDuplicates(),
This lab is going to focus on File Output, which you will find is somewhat similar to console
output.
FileMain.java
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class FileMain {
public static void main(String[] args) throws IOException{
Scanner scnr = new Scanner(System.in);
System.out.print("Please enter the name of the file you would like to read: ");
String fileName = scnr.next();
Reader reader = new Reader();
ArrayList fileContents = reader.getFileContents(fileName);
System.out.println("Please enter a name for your new file: ");
String newFileName = scnr.next();
Writer fileOut = new Writer(newFileName);
fileOut.logReverse(fileContents);
fileOut.logMax(fileContents);
fileOut.logDuplicates(fileContents);
fileOut.closeWriter();
scnr.close();
}
}
Filetester.java
import java.io.IOException;
import java.util.ArrayList;
public class FileTester {
public static boolean testLogReverse(ArrayList contents) throws IOException {
Writer writer = new Writer("logReverseTest.txt");
writer.logReverse(contents);
writer.closeWriter();
ArrayList expected = new ArrayList();
expected.add("Reversed file contents: ");
expected.add("58");
expected.add("12");
expected.add("19");
expected.add("42");
expected.add("12");
expected.add("End of file.");
Reader testReader = new Reader();
ArrayList result = testReader.getFileContents("logReverseTest.txt");
if(expected.equals(result)) return true;
else return false;
}
public static boolean testLogMax(ArrayList contents) throws IOException {
Writer writer = new Writer("logMaxTest.txt");
writer.logMax(contents);
writer.closeWriter();
ArrayList expected = new ArrayList();
expected.add("The largest number in the file is: 58");
expected.add("End of file.");
Reader testReader = new Reader();
ArrayList result = testReader.getFileContents("logMaxTest.txt");
if(expected.equals(result)) return true;
else return false;
}
public static boolean testLogDuplicates(ArrayList contents) throws IOException {
Writer writer = new Writer("logDuplicatesTest.txt");
writer.logDuplicates(contents);
writer.closeWriter();
ArrayList expected = new ArrayList();
expected.add("Duplicates found: true");
expected.add("End of file.");
Reader testReader = new Reader();
ArrayList result = testReader.getFileContents("logDuplicatesTest.txt");
if(expected.equals(result)) return true;
else return false;
}
public static void main(String[] args) throws IOException {
Reader reader = new Reader();
ArrayList fileContents = reader.getFileContents("nums.txt");
System.out.println("logReverse test passed? " + testLogReverse(fileContents));
System.out.println("logMax test passed? " + testLogMax(fileContents));
System.out.println("logDuplicates test passed? " + testLogDuplicates(fileContents));
}
}
Reader.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class Reader {
private Scanner getFileScanner(String fileName) {
try {
File file = new File(fileName);
Scanner scnr = new Scanner(file);
return scnr;
} catch(FileNotFoundException e) {
System.err.println(""" + fileName + "" not found, please ensure that the file trying to be
read is in the folder just above the src directory.");;
System.err.println("Program now exiting!");
System.exit(-1);
}
return null;
}
public ArrayList getFileContents(String fileName) {
Scanner fileScnr = getFileScanner(fileName);
ArrayList fileContents = new ArrayList();
while(fileScnr.hasNextLine()) {
fileContents.add(fileScnr.nextLine());
}
return fileContents;
}
}
Writer.java
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
public class Writer {
PrintWriter outputFile;
public Writer(String fileName) throws IOException{
//TODO Student
}
public void closeWriter() {
outputFile.print("End of file.");
outputFile.close();
}
/** Student Self-Explanation
*
*/
public void logReverse(ArrayList fileContents) {
outputFile.println("Reversed file contents: ");
//TODO Student
}
/** Student Self-Explanation
*
*/
public void logMax(ArrayList fileContents) {
//TODO Student
outputFile.print("The largest number in the file is: ");
//You will want to print your max number on this line.
}
/** Student Self-Explanation
*
*/
public void logDuplicates(ArrayList fileContents) {
outputFile.print("Duplicates found: ");
//TODO Student
}
}
Java doc for writer.java:
Method Details
closeWriter
public void closeWriter()
This method appends an end message to the PrintWriter object and then closes the PrintWriter to
ensure no memory leaks occur.
logReverse
public void logReverse(ArrayList fileContents)
This method should work through the ArrayList provided backwards, and print each element to
our file backwards, using the PrintWriter initialized in the constructor. It should not matter
whether the contents of the file are numbers, Strings, or sentences. The purpose of this method is
to reverse the contents of the fileContents ArrayList and then print them to our new file. For
example:
The new file would contain:
Parameters:
fileContents - ArrayList containing all lines of a file.
logMax
public void logMax(ArrayList fileContents)
This method works through each element in the provided ArrayList and determines the largest
number that is contained within said ArrayList. Whichever number is determined to be the
largest will be logged, using the PrintWriter initialized in the constructor. You may have noticed
that the ArrayList is populated by String objects, so using a Wrapper class to convert from String
to int will come in use here. If the provided file contained:
The new file would contain:
Parameters:
fileContents - ArrayList containing all lines of a file, all elements will be whole, positive
numbers when this method is tested.
logDuplicates
public void logDuplicates(ArrayList fileContents)
This method works through each element contained in the provided ArrayList and determines if
duplicate elements exist within the ArrayList. If duplicate elements are found, print to file,
"true", or "false" if they are not. There are multiple solutions to this problem, but one of the most
common approaches involves a nested loop. If the provided file contained:
The new file would contain:
Parameters:
fileContents - ArrayList

More Related Content

PDF
Hi, I need help with a java programming project. specifically practi.pdf
DOCX
import java.io.BufferedReader;import java.io.BufferedWriter;.docx
DOCX
Article link httpiveybusinessjournal.compublicationmanaging-.docx
PDF
Write a program in Java that reads a set of doubles from a file, sto.pdf
PPT
Oop lecture9 11
PDF
Need help with this java code. fill in lines where needed. Also, not.pdf
PDF
Oot practical
PDF
For the following I have finished most of this question but am havin.pdf
Hi, I need help with a java programming project. specifically practi.pdf
import java.io.BufferedReader;import java.io.BufferedWriter;.docx
Article link httpiveybusinessjournal.compublicationmanaging-.docx
Write a program in Java that reads a set of doubles from a file, sto.pdf
Oop lecture9 11
Need help with this java code. fill in lines where needed. Also, not.pdf
Oot practical
For the following I have finished most of this question but am havin.pdf

Similar to Change the code in Writer.java only to get it working. Must contain .pdf (20)

PDF
Using Array Approach, Linked List approach, and Delete Byte Approach.pdf
PDF
Objectives In this lab you will review passing arrays to methods and.pdf
PDF
in.txt ( Suppose if we store the text file in DDrive then the path .pdf
PDF
PLEASE MAKE SURE THE PROGRAM IS ASKING FOR INPUT FROM USER TO ADD OR.pdf
DOC
Java final lab
PDF
Java Program using RecursionWrite a program that reads in a file .pdf
PPT
File Input and Output in Java Programing language
DOCX
project2.classpathproject2.project project2 .docx
DOCX
Java programs - bubble sort, iterator, linked list, hash set, reverse string,...
DOCX
Hey i have attached the required file for my assignment.and addi
PDF
OrderTest.javapublic class OrderTest {       Get an arra.pdf
PDF
I am stuck on parts E and FExercise 1      NumberListTester.java.pdf
PDF
Java practical N Scheme Diploma in Computer Engineering
DOCX
Please complete all the code as per instructions in Java programming.docx
PPTX
Java Foundations: Lists, ArrayList<T>
PDF
JAVA PRACTICE QUESTIONS v1.4.pdf
PDF
ReversePoem.java ---------------------------------- public cl.pdf
PDF
Frequency .java Word frequency counter package frequ.pdf
PDF
Write the code above and the ones below in netbeans IDE 8.13. (Eli.pdf
DOCX
Discussion Board 2William Denison27 NOV 2014A Java class t.docx
Using Array Approach, Linked List approach, and Delete Byte Approach.pdf
Objectives In this lab you will review passing arrays to methods and.pdf
in.txt ( Suppose if we store the text file in DDrive then the path .pdf
PLEASE MAKE SURE THE PROGRAM IS ASKING FOR INPUT FROM USER TO ADD OR.pdf
Java final lab
Java Program using RecursionWrite a program that reads in a file .pdf
File Input and Output in Java Programing language
project2.classpathproject2.project project2 .docx
Java programs - bubble sort, iterator, linked list, hash set, reverse string,...
Hey i have attached the required file for my assignment.and addi
OrderTest.javapublic class OrderTest {       Get an arra.pdf
I am stuck on parts E and FExercise 1      NumberListTester.java.pdf
Java practical N Scheme Diploma in Computer Engineering
Please complete all the code as per instructions in Java programming.docx
Java Foundations: Lists, ArrayList<T>
JAVA PRACTICE QUESTIONS v1.4.pdf
ReversePoem.java ---------------------------------- public cl.pdf
Frequency .java Word frequency counter package frequ.pdf
Write the code above and the ones below in netbeans IDE 8.13. (Eli.pdf
Discussion Board 2William Denison27 NOV 2014A Java class t.docx

More from secunderbadtirumalgi (20)

PDF
Code using Java Programming and use JavaFX. Show your code and outpu.pdf
PDF
Como parte de su programa Global Health Partnerships (2008-2011), Pf.pdf
PDF
Como parte de la investigaci�n de su tesis, genera una biblioteca de.pdf
PDF
Como administrador de la colecci�n de espec�menes en un museo de his.pdf
PDF
Como alcalde de Tropical Island, se enfrenta al doble mandato de pre.pdf
PDF
Commentators on the US economy feel the US economy fell into a reces.pdf
PDF
Combe Corporation has two divisions Alpha and Beta. Data from the m.pdf
PDF
Coloque los eventos para explicar c�mo un bien p�blico llega a ser d.pdf
PDF
College students often make up a substantial portion of the populati.pdf
PDF
Code using Java Programming. Show your code and output.Create a pe.pdf
PDF
code in html with div styles 9. Town of 0z Info - Microsoft Interne.pdf
PDF
CODE FOR echo_client.c A simple echo client using TCP #inc.pdf
PDF
Coastal Louisiana has been experiencing habitat fragmentation and ha.pdf
PDF
Cloud Solutions had the following accounts and balances as of Decemb.pdf
PDF
Climate scientists claim that CO2 has risen recently to levels that .pdf
PDF
Climate change is one of the defining challenges of our time, but to.pdf
PDF
Classify each of the following items as excludable, nonexcludable, r.pdf
PDF
Chris is a young moonshine producer in the Tennessee region of Appal.pdf
PDF
choose the right answer onlyQuestion 9 What are the four facto.pdf
PDF
Choose ONE of the scenarios below and write a problem-solving report.pdf
Code using Java Programming and use JavaFX. Show your code and outpu.pdf
Como parte de su programa Global Health Partnerships (2008-2011), Pf.pdf
Como parte de la investigaci�n de su tesis, genera una biblioteca de.pdf
Como administrador de la colecci�n de espec�menes en un museo de his.pdf
Como alcalde de Tropical Island, se enfrenta al doble mandato de pre.pdf
Commentators on the US economy feel the US economy fell into a reces.pdf
Combe Corporation has two divisions Alpha and Beta. Data from the m.pdf
Coloque los eventos para explicar c�mo un bien p�blico llega a ser d.pdf
College students often make up a substantial portion of the populati.pdf
Code using Java Programming. Show your code and output.Create a pe.pdf
code in html with div styles 9. Town of 0z Info - Microsoft Interne.pdf
CODE FOR echo_client.c A simple echo client using TCP #inc.pdf
Coastal Louisiana has been experiencing habitat fragmentation and ha.pdf
Cloud Solutions had the following accounts and balances as of Decemb.pdf
Climate scientists claim that CO2 has risen recently to levels that .pdf
Climate change is one of the defining challenges of our time, but to.pdf
Classify each of the following items as excludable, nonexcludable, r.pdf
Chris is a young moonshine producer in the Tennessee region of Appal.pdf
choose the right answer onlyQuestion 9 What are the four facto.pdf
Choose ONE of the scenarios below and write a problem-solving report.pdf

Recently uploaded (20)

PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
GDM (1) (1).pptx small presentation for students
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
Basic Mud Logging Guide for educational purpose
PPTX
PPH.pptx obstetrics and gynecology in nursing
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
Sports Quiz easy sports quiz sports quiz
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
Final Presentation General Medicine 03-08-2024.pptx
Microbial disease of the cardiovascular and lymphatic systems
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
O7-L3 Supply Chain Operations - ICLT Program
Anesthesia in Laparoscopic Surgery in India
GDM (1) (1).pptx small presentation for students
STATICS OF THE RIGID BODIES Hibbelers.pdf
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Supply Chain Operations Speaking Notes -ICLT Program
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
2.FourierTransform-ShortQuestionswithAnswers.pdf
102 student loan defaulters named and shamed – Is someone you know on the list?
Basic Mud Logging Guide for educational purpose
PPH.pptx obstetrics and gynecology in nursing
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Sports Quiz easy sports quiz sports quiz
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Microbial diseases, their pathogenesis and prophylaxis
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Abdominal Access Techniques with Prof. Dr. R K Mishra
Final Presentation General Medicine 03-08-2024.pptx

Change the code in Writer.java only to get it working. Must contain .pdf

  • 1. Change the code in Writer.java only to get it working. Must contain methods: logReverse() , logMax(), logDuplicates(), This lab is going to focus on File Output, which you will find is somewhat similar to console output. FileMain.java import java.io.IOException; import java.util.ArrayList; import java.util.Scanner; public class FileMain { public static void main(String[] args) throws IOException{ Scanner scnr = new Scanner(System.in); System.out.print("Please enter the name of the file you would like to read: "); String fileName = scnr.next(); Reader reader = new Reader(); ArrayList fileContents = reader.getFileContents(fileName); System.out.println("Please enter a name for your new file: "); String newFileName = scnr.next(); Writer fileOut = new Writer(newFileName); fileOut.logReverse(fileContents); fileOut.logMax(fileContents); fileOut.logDuplicates(fileContents); fileOut.closeWriter(); scnr.close(); } } Filetester.java import java.io.IOException; import java.util.ArrayList; public class FileTester { public static boolean testLogReverse(ArrayList contents) throws IOException { Writer writer = new Writer("logReverseTest.txt"); writer.logReverse(contents); writer.closeWriter(); ArrayList expected = new ArrayList();
  • 2. expected.add("Reversed file contents: "); expected.add("58"); expected.add("12"); expected.add("19"); expected.add("42"); expected.add("12"); expected.add("End of file."); Reader testReader = new Reader(); ArrayList result = testReader.getFileContents("logReverseTest.txt"); if(expected.equals(result)) return true; else return false; } public static boolean testLogMax(ArrayList contents) throws IOException { Writer writer = new Writer("logMaxTest.txt"); writer.logMax(contents); writer.closeWriter(); ArrayList expected = new ArrayList(); expected.add("The largest number in the file is: 58"); expected.add("End of file."); Reader testReader = new Reader(); ArrayList result = testReader.getFileContents("logMaxTest.txt"); if(expected.equals(result)) return true; else return false; } public static boolean testLogDuplicates(ArrayList contents) throws IOException { Writer writer = new Writer("logDuplicatesTest.txt"); writer.logDuplicates(contents); writer.closeWriter(); ArrayList expected = new ArrayList(); expected.add("Duplicates found: true"); expected.add("End of file."); Reader testReader = new Reader(); ArrayList result = testReader.getFileContents("logDuplicatesTest.txt");
  • 3. if(expected.equals(result)) return true; else return false; } public static void main(String[] args) throws IOException { Reader reader = new Reader(); ArrayList fileContents = reader.getFileContents("nums.txt"); System.out.println("logReverse test passed? " + testLogReverse(fileContents)); System.out.println("logMax test passed? " + testLogMax(fileContents)); System.out.println("logDuplicates test passed? " + testLogDuplicates(fileContents)); } } Reader.java import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Scanner; public class Reader { private Scanner getFileScanner(String fileName) { try { File file = new File(fileName); Scanner scnr = new Scanner(file); return scnr; } catch(FileNotFoundException e) { System.err.println(""" + fileName + "" not found, please ensure that the file trying to be read is in the folder just above the src directory.");; System.err.println("Program now exiting!"); System.exit(-1); } return null; } public ArrayList getFileContents(String fileName) { Scanner fileScnr = getFileScanner(fileName); ArrayList fileContents = new ArrayList();
  • 4. while(fileScnr.hasNextLine()) { fileContents.add(fileScnr.nextLine()); } return fileContents; } } Writer.java import java.io.FileOutputStream; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; public class Writer { PrintWriter outputFile; public Writer(String fileName) throws IOException{ //TODO Student } public void closeWriter() { outputFile.print("End of file."); outputFile.close(); } /** Student Self-Explanation * */ public void logReverse(ArrayList fileContents) { outputFile.println("Reversed file contents: "); //TODO Student } /** Student Self-Explanation * */ public void logMax(ArrayList fileContents) { //TODO Student outputFile.print("The largest number in the file is: "); //You will want to print your max number on this line. }
  • 5. /** Student Self-Explanation * */ public void logDuplicates(ArrayList fileContents) { outputFile.print("Duplicates found: "); //TODO Student } } Java doc for writer.java: Method Details closeWriter public void closeWriter() This method appends an end message to the PrintWriter object and then closes the PrintWriter to ensure no memory leaks occur. logReverse public void logReverse(ArrayList fileContents) This method should work through the ArrayList provided backwards, and print each element to our file backwards, using the PrintWriter initialized in the constructor. It should not matter whether the contents of the file are numbers, Strings, or sentences. The purpose of this method is to reverse the contents of the fileContents ArrayList and then print them to our new file. For example: The new file would contain: Parameters: fileContents - ArrayList containing all lines of a file. logMax public void logMax(ArrayList fileContents) This method works through each element in the provided ArrayList and determines the largest number that is contained within said ArrayList. Whichever number is determined to be the largest will be logged, using the PrintWriter initialized in the constructor. You may have noticed that the ArrayList is populated by String objects, so using a Wrapper class to convert from String to int will come in use here. If the provided file contained: The new file would contain: Parameters: fileContents - ArrayList containing all lines of a file, all elements will be whole, positive numbers when this method is tested.
  • 6. logDuplicates public void logDuplicates(ArrayList fileContents) This method works through each element contained in the provided ArrayList and determines if duplicate elements exist within the ArrayList. If duplicate elements are found, print to file, "true", or "false" if they are not. There are multiple solutions to this problem, but one of the most common approaches involves a nested loop. If the provided file contained: The new file would contain: Parameters: fileContents - ArrayList