SlideShare a Scribd company logo
1 of 6
Download to read offline
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

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

We will be making 4 classes Main - for testing the code Hi.pdf
 We will be making 4 classes Main - for testing the code Hi.pdf We will be making 4 classes Main - for testing the code Hi.pdf
We will be making 4 classes Main - for testing the code Hi.pdfanithareadymade
 
Write a program in java that asks a user for a file name and prints .pdf
Write a program in java that asks a user for a file name and prints .pdfWrite a program in java that asks a user for a file name and prints .pdf
Write a program in java that asks a user for a file name and prints .pdfatulkapoor33
 
Write a program in Java that reads a set of doubles from a file, sto.pdf
Write a program in Java that reads a set of doubles from a file, sto.pdfWrite a program in Java that reads a set of doubles from a file, sto.pdf
Write a program in Java that reads a set of doubles from a file, sto.pdfarihantpatna
 
Create a text file named lnfo.txt. Enter the following informatio.pdf
Create a text file named lnfo.txt. Enter the following informatio.pdfCreate a text file named lnfo.txt. Enter the following informatio.pdf
Create a text file named lnfo.txt. Enter the following informatio.pdfshahidqamar17
 
import java.io.BufferedReader;import java.io.BufferedWriter;.docx
import java.io.BufferedReader;import java.io.BufferedWriter;.docximport java.io.BufferedReader;import java.io.BufferedWriter;.docx
import java.io.BufferedReader;import java.io.BufferedWriter;.docxwilcockiris
 
This is a java lab assignment. I have added the first part java re.pdf
This is a java lab assignment. I have added the first part java re.pdfThis is a java lab assignment. I have added the first part java re.pdf
This is a java lab assignment. I have added the first part java re.pdffeetshoemart
 
Write a java program that allows the user to input the name of a fil.docx
 Write a java program that allows the user to input  the name of a fil.docx Write a java program that allows the user to input  the name of a fil.docx
Write a java program that allows the user to input the name of a fil.docxajoy21
 
Write a java program that allows the user to input the name of a file.docx
Write a java program that allows the user to input  the name of a file.docxWrite a java program that allows the user to input  the name of a file.docx
Write a java program that allows the user to input the name of a file.docxnoreendchesterton753
 
Recursively Searching Files and DirectoriesSummaryBuild a class .pdf
Recursively Searching Files and DirectoriesSummaryBuild a class .pdfRecursively Searching Files and DirectoriesSummaryBuild a class .pdf
Recursively Searching Files and DirectoriesSummaryBuild a class .pdfmallik3000
 
Input output files in java
Input output files in javaInput output files in java
Input output files in javaKavitha713564
 
Please I am trying to get this code to output in -txt file- I need you.pdf
Please I am trying to get this code to output in -txt file- I need you.pdfPlease I am trying to get this code to output in -txt file- I need you.pdf
Please I am trying to get this code to output in -txt file- I need you.pdfasenterprisestyagi
 
Lecture 4 - Object Interaction and Collections
Lecture 4 - Object Interaction and CollectionsLecture 4 - Object Interaction and Collections
Lecture 4 - Object Interaction and CollectionsSyed Afaq Shah MACS CP
 
Tutorial on developing a Solr search component plugin
Tutorial on developing a Solr search component pluginTutorial on developing a Solr search component plugin
Tutorial on developing a Solr search component pluginsearchbox-com
 
Java Input Output and File Handling
Java Input Output and File HandlingJava Input Output and File Handling
Java Input Output and File HandlingSunil OS
 
Write a program that will count the number of characters- words- and l.docx
Write a program that will count the number of characters- words- and l.docxWrite a program that will count the number of characters- words- and l.docx
Write a program that will count the number of characters- words- and l.docxlez31palka
 
Chapter 12 - File Input and Output
Chapter 12 - File Input and OutputChapter 12 - File Input and Output
Chapter 12 - File Input and OutputEduardo Bergavera
 

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

We will be making 4 classes Main - for testing the code Hi.pdf
 We will be making 4 classes Main - for testing the code Hi.pdf We will be making 4 classes Main - for testing the code Hi.pdf
We will be making 4 classes Main - for testing the code Hi.pdf
 
Write a program in java that asks a user for a file name and prints .pdf
Write a program in java that asks a user for a file name and prints .pdfWrite a program in java that asks a user for a file name and prints .pdf
Write a program in java that asks a user for a file name and prints .pdf
 
Write a program in Java that reads a set of doubles from a file, sto.pdf
Write a program in Java that reads a set of doubles from a file, sto.pdfWrite a program in Java that reads a set of doubles from a file, sto.pdf
Write a program in Java that reads a set of doubles from a file, sto.pdf
 
Create a text file named lnfo.txt. Enter the following informatio.pdf
Create a text file named lnfo.txt. Enter the following informatio.pdfCreate a text file named lnfo.txt. Enter the following informatio.pdf
Create a text file named lnfo.txt. Enter the following informatio.pdf
 
import java.io.BufferedReader;import java.io.BufferedWriter;.docx
import java.io.BufferedReader;import java.io.BufferedWriter;.docximport java.io.BufferedReader;import java.io.BufferedWriter;.docx
import java.io.BufferedReader;import java.io.BufferedWriter;.docx
 
Files nts
Files ntsFiles nts
Files nts
 
Basic input-output-v.1.1
Basic input-output-v.1.1Basic input-output-v.1.1
Basic input-output-v.1.1
 
This is a java lab assignment. I have added the first part java re.pdf
This is a java lab assignment. I have added the first part java re.pdfThis is a java lab assignment. I have added the first part java re.pdf
This is a java lab assignment. I have added the first part java re.pdf
 
Write a java program that allows the user to input the name of a fil.docx
 Write a java program that allows the user to input  the name of a fil.docx Write a java program that allows the user to input  the name of a fil.docx
Write a java program that allows the user to input the name of a fil.docx
 
Write a java program that allows the user to input the name of a file.docx
Write a java program that allows the user to input  the name of a file.docxWrite a java program that allows the user to input  the name of a file.docx
Write a java program that allows the user to input the name of a file.docx
 
Recursively Searching Files and DirectoriesSummaryBuild a class .pdf
Recursively Searching Files and DirectoriesSummaryBuild a class .pdfRecursively Searching Files and DirectoriesSummaryBuild a class .pdf
Recursively Searching Files and DirectoriesSummaryBuild a class .pdf
 
Input output files in java
Input output files in javaInput output files in java
Input output files in java
 
Please I am trying to get this code to output in -txt file- I need you.pdf
Please I am trying to get this code to output in -txt file- I need you.pdfPlease I am trying to get this code to output in -txt file- I need you.pdf
Please I am trying to get this code to output in -txt file- I need you.pdf
 
Lecture 4 - Object Interaction and Collections
Lecture 4 - Object Interaction and CollectionsLecture 4 - Object Interaction and Collections
Lecture 4 - Object Interaction and Collections
 
Tutorial on developing a Solr search component plugin
Tutorial on developing a Solr search component pluginTutorial on developing a Solr search component plugin
Tutorial on developing a Solr search component plugin
 
H file handling
H file handlingH file handling
H file handling
 
working with files
working with filesworking with files
working with files
 
Java Input Output and File Handling
Java Input Output and File HandlingJava Input Output and File Handling
Java Input Output and File Handling
 
Write a program that will count the number of characters- words- and l.docx
Write a program that will count the number of characters- words- and l.docxWrite a program that will count the number of characters- words- and l.docx
Write a program that will count the number of characters- words- and l.docx
 
Chapter 12 - File Input and Output
Chapter 12 - File Input and OutputChapter 12 - File Input and Output
Chapter 12 - File Input and Output
 

More from secunderbadtirumalgi

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

More from secunderbadtirumalgi (20)

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

Recently uploaded

Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 

Recently uploaded (20)

Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 

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