Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

still not running correctly after getting some help... import java.util.Scanner; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; public class Main { public

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

still not running correctly after getting some help...

import java.util.Scanner;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.PrintWriter;

import java.util.ArrayList;

public class Main {

public static void main(String[] args) {

/ew object

Main mainObj = new Main();

mainObj.run();

}//main method

public void run() {

//constants

final int RUNS_UP = 0, RUNS_DOWN = 1;

//lists

ArrayList list = new ArrayList();

ArrayList listRunsUpCount = findRuns(list,RUNS_UP);

ArrayList listRunsDnCount = findRuns(list,RUNS_DOWN);

ArrayList listRunsCount = merge(listRunsUpCount,listRunsDnCount);

try {

//method to create ArrayList with read

readInputFile("p01-in.txt");

}catch(FileNotFoundException pExcept){

System.out.println("Sorry, could not open 'p01-in.txt' for reading. Stopping.");

System.exit(-1);

}//try

try {

writeOutputFile("p01-runs.txt",listRunsCount);

}catch(FileNotFoundException pExcept){

System.out.println("Sorry, could not open 'p01-runs.txt' for writing. Stopping.");

System.exit(-1);

}//try

}//run()

//findRuns()

public ArrayList findRuns(ArrayList pList, int pDir){

//constants

final int RUNS_UP = 0, RUNS_DOWN = 1;

//lists

ArrayList list = new ArrayList();

ArrayList listRunsUpCount = findRuns(list,RUNS_UP);

ArrayList listRunsDnCount = findRuns(list,RUNS_DOWN);

ArrayList listRunsCount = merge(listRunsUpCount,listRunsDnCount);

listRunsCount = arrayListCreate(pList.size(),0);

int i = 0, k = 0;

while(i

if((pDir==RUNS_UP)&&(pList.get(i)

k++;

}

else if((pDir==RUNS_DOWN)&&(pList.get(i)>=pList.get(i+1))){

k++;

}

else{

if(k!=0){

listRunsCount.set(k,listRunsCount.get(k)+1);

k = 0;

}

}

i++;

}

if(k!=0){

listRunsCount.set(k,listRunsCount.get(k)+1);

}

return listRunsCount;

}//findRuns()

//merge()

public ArrayList merge(ArrayList pListRunsUpCount,ArrayList pListRunsDnCount){

//constants

final int RUNS_UP = 0, RUNS_DOWN = 1;

//lists

ArrayList list = new ArrayList();

ArrayList listRunsUpCount = findRuns(list,RUNS_UP);

ArrayList listRunsDnCount = findRuns(list,RUNS_DOWN);

ArrayList listRunsCount = merge(listRunsUpCount,listRunsDnCount);

listRunsCount = arrayListCreate(pListRunsUpCount.size(), 0);

for(int i = 0;i

listRunsCount.set(i,pListRunsUpCount.get(i)+pListRunsDnCount.get(i));

}

return listRunsCount;

} //merge()

public ArrayList arrayListCreate(int pSize, int pInitValue){

ArrayList list = new ArrayList();

for (int i = 0; i

list.add(pInitValue);

}

return list;

}//arrayListCreate()

//readInputFile()

public ArrayList readInputFile(String pFileName) throws FileNotFoundException

{

ArrayList list = new ArrayList();

File in = new File(pFileName);

System.out.println(in.getAbsoluteFile());

Scanner input = new Scanner(in);

int num;

while(input.hasNext())

{

num = input.nextInt();

list.add(num);

}//while

//close

input.close();

return list;

}//readInputFile()

//writeOutPutFile

public void writeOutputFile(String pFilename, ArrayList pListRuns) throws FileNotFoundException {

//constants

final int RUNS_UP = 0, RUNS_DOWN = 1;

//lists

ArrayList list = new ArrayList();

ArrayList listRunsUpCount = findRuns(list,RUNS_UP);

ArrayList listRunsDnCount = findRuns(list,RUNS_DOWN);

ArrayList listRunsCount = merge(listRunsUpCount,listRunsDnCount);

/eeds work

File out = new File(pFilename);

PrintWriter pw = new PrintWriter(out);

System.out.println("runs_total "+pListRuns);

for(int k = 1; k

System.out.println("runs_"+k+" "+pListRuns.get(k));

}

//close

pw.close();

}//writeOutPutFile()

}//Main

CSE205 Object Oriented Programming and Data Structures Programming Project 1 25 pts 1 Submission Instructions Create a folder named asuriteid-p01 where asuriteid is your ASURITE user id (for example, if your ASURITE user id is jsmith6 then your folder would be named jsmith6-p01) and copy all of your .java source code files to this folder. Do not copy the .class files or any other files. Next, compress the asuriteid-p1 folder creating a zip archive file named asuriteid-p01 zip (e.g., jsmith6-p01.zip) If you work with a partner, then it important that both authors include their information in the header comment block at the top of each source code file. See 5 Item 10 for the format of these comments. If you work by yourself, then omit the AUTHOR2 line. Also, if you work with a partner, put both of your asuriteid's in the folder name you create and the zip archive. For example, if Biff and Buffy work together, then either Biff or Buffy will create an empty folder named biff buffy-p01, copy their source code files to that folder, and then compress that folder to form a zip archive named biff-buffy- p01.zip. Only one partner should submit the file to BB for grading, eg., Buffy. Because there are two names in the zip archive and also because there are two names in the header comment block, then the grader will know that Buffy worked with a partner and each of you wil be assigned the same score Upload asuriteid-p01.zip or asuriteid1-asuriteid2-p01.zip to the Blackboard Project 1 submission link by the project dead line. Be sure that you click the Submit button on that page to submit the file. If you press the Save button, it wl not upload your file and we will not be able to access it for grading. We do not accept emailed files after the deadline, so this would cause you to earn a score of 0 on the project. I believe that after you submit the file to BB, that you can view or download the submitted file. I strongly encourage you to review the submitted file to ensure that you submitted the correct file using the correct BB submission link. These are easily avoidable mistakes that can lead to a score of 0 Please see the Course Schedule section in the Syllabus for the deadline. Consult the Syllabus for the late and academic integrity pol cies. 2 Learning Objectives 1. Use the Integer wrapper class 2. Declare and use ArrayListEclass objects. 3. Write code to read from, and write to, text files. 4. Write an exception handler for an I/O exception. 5. Write Java classes and instantiate objects of those classes 3 Background Let list be a nonempty sequence of nonnegative random integers, each in the range [0, 32767 and let n be the length of list, e.g. list 2, 8, 3, 2, 9, 8, 6, 3, 4, 6, 1, 9 where n -12. List elements are numbered starting at 0. We define a run up to be a (k1)-length subsequence starting at index i: list, last,:, listi-2, , listi-k, that is monotonically increasing (i.e., listi: j- list, jl for each J = 1, 2, 3, , k Similarly, a run down is a (k+1)-length subsequence starting at index i: list, list, list 2, ..., list- that is monotoni cally decreasing (i.e., list, S list for each j-1, 2, 3, ) For the above example list we have these runs up and runs down: Runs Up listo through lis 2, 81, 2-length subseq list 3 ; k 0, 1-length subseq list through list 2,9; k- 1, 2-length subseq lists 8 ; k 0, 1-length subseq liste 6 k0,1-length subseq list, through lis { 3, 4, 6 }; k = 2, 3-1en subseq listi) through listi,-(1.9 }; k-1, 2-len subseq 1 The runs up and runs down test is used in statistics. When I wrote my master's thesis on random number generation algorithms, I had to write these tests to determine the indepence between successive random numbers generated by my algorithms CSE205 Object Oriented Programming and Data Structures Runs Down list 2 ; k- 0,1-length subseq list through lists 8, 3, 2 k 2, 3-length subseq Programming Project 1:: 25 pts lists through list 9, 8, 6, 3 ; k 3, 4-length subseq lists = { 4 }: k= 0, i-length subseq list, through listo 6, 1 ; k 1, 2-length subseo lish! = { 9 }; k-0, l-length subseq We are interested in the value of k for each run up and run down and in particular we are interested in the total number of runs for each nonzero k, which we shall denote by runs, 0 listnew ArrayListo int x 1; list.add(x); // Legal because of Java autoboxing 1. You must write an exception handler that will catch the FileNotFoundException that gets thrown when the input file does not exist (make sure to test this). The exception handler will print the friendly error message as shown in Software Requirement 5 and immediately terminate the Java program. To immediately terminate a Java program we call a static method named erit) which is in the java.lang.System class. The erit) method expects an int argument For this project, it does not matter what int argument we send to erit). Therefore, terminate the program this way by sending -1 to erit) tr // Try to open input file for reading catch (FileNotFoundException pExcept) // Print friendly error message System.exit(-1); CSE205 Object Oriented Programming and Data Structures Programming Project 1:: 25 pts 5. Similar to Item 4, you must write an exception handler that will catch the FileNotFoundErception that gets thrown when the output file cannot be opened for writing. The exception handler will print the message as shown in Software Requirement 6 and then terminate the program 6 Your programming skills should be sufficiently developed that you are beyond writing the entire code for a progranm in one method. Divide the program into multiple methods. Remember, a method should have one purpose. i.e., it should do one thing. If you find a method is becoming complicated because you are trying to make that method do more than one thing, then divided the method into 2, 3, 4, or more distinct methods, each of which does one thing 7. Avoid making every variable or object an instance variable. For this project you shall not declare any instance variables in the class. That is, all variables should be declared as local variables in methods and passed as argu ments to other methods when appropriate Neatly format your code. Use proper indentation and spacing. Study the examples in the book and the examples the instructor presents in the lectures and posts on the course website 8. 9. Put a comment header block at the top of each method formatted thusly * A brief description of what the method doe:s s/ 10. Put a comment header block at the top of each source code file not just for this project, write formatted thusly (or you may use /** but for every project we */ comment style if you wish) / CLASS: classname (classname.java) // DESCRIPTION / A description of the contents of this file // COURSE AND PROJECT NFO // CSE205 Object Oriented Programming and Data Structures, semester and year // Project Number: project-number AUTHOR1: your-name, your-asuriteid, your-email-addr / AUTHOR2: your-name, your-asuriteid, your-email-addr 5.1 Software Design: Pseudocode To help you complete the program, I recommend you implement this pseudocode. -Note: In the Java implementation, maln() would call run(), so in essence, run() becomes the starting point --of execution Method run) Returns Nothing Declare ArrayList of Integers list re dInputFile("p01-in. txt") -Reads the integers from the input file Declare and create an ArrayList of Integers named listRunsUp Count Declare and create an ArrayList of Integers named lis tRunsDn Count listRunsUp Count findRuns (list, RUNS-UP) -RUNS-UP and RUNS-DN are named constants, it does not matter what listhunsDnCount fndRuns (list, RUNS-DN) -- value you assign to them as long as the values are different Declare ArrayList of Integers listRunsCount mergeLists(listAunsUp Count, listRunsDnCount) uriteDutputFile("p01-runs.txt", listRunsCount) End Method run plist is the ArrayList of Integers that vere read from "p01-in.txt". pDir is an int and is either RUNS UP or RUNS DN -which specifies in this method whether we are counting the number of runs up or runs down Method findRuns (pList ArrayList of Integers, pDir int) Returns ArrayList of Integers listRunsCount arrayL is t Create(plist. size(), 0) -- size is the same as plist and each element is init 'd to 0 Declare int variables i 0, k 0 -- the left arrow represents the assignment operator CSE205 Object Oriented Programming and Data Structures Programming Project 1 : 25 pts Whileplist.size()-1 Do If pDir is RUNS-UP and pList element at t is plist element at i + 1 Then ElseIf pDir is RUNS DN and plist element at i is 2 pList element at i + 1 Then Else Increment k Increment k If k does not equal 0 Then Increment the element at index Kof listRunsC0nt End if End If Increment i End While If k does not equal 0 Then Increment the element at index k of listRunsCount End If Return listRunsCount End Method findRuns Method merge (plistAunsUpCount is ArrayList of Integers, plis tRunsDmCount is ArrayList of Integers Returns ArrayList of Integers ListRunsCount + arrayListcreate(pListRunsUpCount. size), 0) For i0 to pListRunsUpCount.size Do Set element 1 of listRns Count to the sum of the elements at 1 in PListRuns upCount and pListRunsDnCount End For Return listRunsCount End Method merge Method arTayListCreate(int pSize; int pInitValue) Returns ArrayList of Integers Declare and create an ArrayList of Integers named list Write a for loop that iterates pSize times and each time call add(pInitValue) to list Return list End Method arrayListCreate Method uritelutputFile(String pFilename; pListRuns is ArrayList of Integers) Returns Nothing -Make sure to handle the FileNotFoundException that is raised when the output file cannot be opened for writing -Remember the exception design principle: throw early, catch and handle late. This method should throw the FNFE out open pFilename for writing out.println("runs total, ", the sum of pListRuns) For k 1 to plistRuns. size() - 1 Do out.print ln("runs_k, ", the element at index k of pListRuns) End For Close out End Method output Method readInputFile(String pFilename) Returns ArrayList of Integers -Make sure to handle the FileNotFoundEaception that is raised when the input file cannot be opened for reading -Remember the exception design principle; throw early, catch and handle late. This method should throw the FNFE in + open pFilename for reading Declare and create an ArrayList of Integers named list While there is more data to be read from in Do Read the next integer and add it to list End While Close in Return list End Method readInputFile CSE205 Object Oriented Programming and Data Structures Programming Project 1 25 pts 1 Submission Instructions Create a folder named asuriteid-p01 where asuriteid is your ASURITE user id (for example, if your ASURITE user id is jsmith6 then your folder would be named jsmith6-p01) and copy all of your .java source code files to this folder. Do not copy the .class files or any other files. Next, compress the asuriteid-p1 folder creating a zip archive file named asuriteid-p01 zip (e.g., jsmith6-p01.zip) If you work with a partner, then it important that both authors include their information in the header comment block at the top of each source code file. See 5 Item 10 for the format of these comments. If you work by yourself, then omit the AUTHOR2 line. Also, if you work with a partner, put both of your asuriteid's in the folder name you create and the zip archive. For example, if Biff and Buffy work together, then either Biff or Buffy will create an empty folder named biff buffy-p01, copy their source code files to that folder, and then compress that folder to form a zip archive named biff-buffy- p01.zip. Only one partner should submit the file to BB for grading, eg., Buffy. Because there are two names in the zip archive and also because there are two names in the header comment block, then the grader will know that Buffy worked with a partner and each of you wil be assigned the same score Upload asuriteid-p01.zip or asuriteid1-asuriteid2-p01.zip to the Blackboard Project 1 submission link by the project dead line. Be sure that you click the Submit button on that page to submit the file. If you press the Save button, it wl not upload your file and we will not be able to access it for grading. We do not accept emailed files after the deadline, so this would cause you to earn a score of 0 on the project. I believe that after you submit the file to BB, that you can view or download the submitted file. I strongly encourage you to review the submitted file to ensure that you submitted the correct file using the correct BB submission link. These are easily avoidable mistakes that can lead to a score of 0 Please see the Course Schedule section in the Syllabus for the deadline. Consult the Syllabus for the late and academic integrity pol cies. 2 Learning Objectives 1. Use the Integer wrapper class 2. Declare and use ArrayListEclass objects. 3. Write code to read from, and write to, text files. 4. Write an exception handler for an I/O exception. 5. Write Java classes and instantiate objects of those classes 3 Background Let list be a nonempty sequence of nonnegative random integers, each in the range [0, 32767 and let n be the length of list, e.g. list 2, 8, 3, 2, 9, 8, 6, 3, 4, 6, 1, 9 where n -12. List elements are numbered starting at 0. We define a run up to be a (k1)-length subsequence starting at index i: list, last,:, listi-2, , listi-k, that is monotonically increasing (i.e., listi: j- list, jl for each J = 1, 2, 3, , k Similarly, a run down is a (k+1)-length subsequence starting at index i: list, list, list 2, ..., list- that is monotoni cally decreasing (i.e., list, S list for each j-1, 2, 3, ) For the above example list we have these runs up and runs down: Runs Up listo through lis 2, 81, 2-length subseq list 3 ; k 0, 1-length subseq list through list 2,9; k- 1, 2-length subseq lists 8 ; k 0, 1-length subseq liste 6 k0,1-length subseq list, through lis { 3, 4, 6 }; k = 2, 3-1en subseq listi) through listi,-(1.9 }; k-1, 2-len subseq 1 The runs up and runs down test is used in statistics. When I wrote my master's thesis on random number generation algorithms, I had to write these tests to determine the indepence between successive random numbers generated by my algorithms CSE205 Object Oriented Programming and Data Structures Runs Down list 2 ; k- 0,1-length subseq list through lists 8, 3, 2 k 2, 3-length subseq Programming Project 1:: 25 pts lists through list 9, 8, 6, 3 ; k 3, 4-length subseq lists = { 4 }: k= 0, i-length subseq list, through listo 6, 1 ; k 1, 2-length subseo lish! = { 9 }; k-0, l-length subseq We are interested in the value of k for each run up and run down and in particular we are interested in the total number of runs for each nonzero k, which we shall denote by runs, 0 listnew ArrayListo int x 1; list.add(x); // Legal because of Java autoboxing 1. You must write an exception handler that will catch the FileNotFoundException that gets thrown when the input file does not exist (make sure to test this). The exception handler will print the friendly error message as shown in Software Requirement 5 and immediately terminate the Java program. To immediately terminate a Java program we call a static method named erit) which is in the java.lang.System class. The erit) method expects an int argument For this project, it does not matter what int argument we send to erit). Therefore, terminate the program this way by sending -1 to erit) tr // Try to open input file for reading catch (FileNotFoundException pExcept) // Print friendly error message System.exit(-1); CSE205 Object Oriented Programming and Data Structures Programming Project 1:: 25 pts 5. Similar to Item 4, you must write an exception handler that will catch the FileNotFoundErception that gets thrown when the output file cannot be opened for writing. The exception handler will print the message as shown in Software Requirement 6 and then terminate the program 6 Your programming skills should be sufficiently developed that you are beyond writing the entire code for a progranm in one method. Divide the program into multiple methods. Remember, a method should have one purpose. i.e., it should do one thing. If you find a method is becoming complicated because you are trying to make that method do more than one thing, then divided the method into 2, 3, 4, or more distinct methods, each of which does one thing 7. Avoid making every variable or object an instance variable. For this project you shall not declare any instance variables in the class. That is, all variables should be declared as local variables in methods and passed as argu ments to other methods when appropriate Neatly format your code. Use proper indentation and spacing. Study the examples in the book and the examples the instructor presents in the lectures and posts on the course website 8. 9. Put a comment header block at the top of each method formatted thusly * A brief description of what the method doe:s s/ 10. Put a comment header block at the top of each source code file not just for this project, write formatted thusly (or you may use /** but for every project we */ comment style if you wish) / CLASS: classname (classname.java) // DESCRIPTION / A description of the contents of this file // COURSE AND PROJECT NFO // CSE205 Object Oriented Programming and Data Structures, semester and year // Project Number: project-number AUTHOR1: your-name, your-asuriteid, your-email-addr / AUTHOR2: your-name, your-asuriteid, your-email-addr 5.1 Software Design: Pseudocode To help you complete the program, I recommend you implement this pseudocode. -Note: In the Java implementation, maln() would call run(), so in essence, run() becomes the starting point --of execution Method run) Returns Nothing Declare ArrayList of Integers list re dInputFile("p01-in. txt") -Reads the integers from the input file Declare and create an ArrayList of Integers named listRunsUp Count Declare and create an ArrayList of Integers named lis tRunsDn Count listRunsUp Count findRuns (list, RUNS-UP) -RUNS-UP and RUNS-DN are named constants, it does not matter what listhunsDnCount fndRuns (list, RUNS-DN) -- value you assign to them as long as the values are different Declare ArrayList of Integers listRunsCount mergeLists(listAunsUp Count, listRunsDnCount) uriteDutputFile("p01-runs.txt", listRunsCount) End Method run plist is the ArrayList of Integers that vere read from "p01-in.txt". pDir is an int and is either RUNS UP or RUNS DN -which specifies in this method whether we are counting the number of runs up or runs down Method findRuns (pList ArrayList of Integers, pDir int) Returns ArrayList of Integers listRunsCount arrayL is t Create(plist. size(), 0) -- size is the same as plist and each element is init 'd to 0 Declare int variables i 0, k 0 -- the left arrow represents the assignment operator CSE205 Object Oriented Programming and Data Structures Programming Project 1 : 25 pts Whileplist.size()-1 Do If pDir is RUNS-UP and pList element at t is plist element at i + 1 Then ElseIf pDir is RUNS DN and plist element at i is 2 pList element at i + 1 Then Else Increment k Increment k If k does not equal 0 Then Increment the element at index Kof listRunsC0nt End if End If Increment i End While If k does not equal 0 Then Increment the element at index k of listRunsCount End If Return listRunsCount End Method findRuns Method merge (plistAunsUpCount is ArrayList of Integers, plis tRunsDmCount is ArrayList of Integers Returns ArrayList of Integers ListRunsCount + arrayListcreate(pListRunsUpCount. size), 0) For i0 to pListRunsUpCount.size Do Set element 1 of listRns Count to the sum of the elements at 1 in PListRuns upCount and pListRunsDnCount End For Return listRunsCount End Method merge Method arTayListCreate(int pSize; int pInitValue) Returns ArrayList of Integers Declare and create an ArrayList of Integers named list Write a for loop that iterates pSize times and each time call add(pInitValue) to list Return list End Method arrayListCreate Method uritelutputFile(String pFilename; pListRuns is ArrayList of Integers) Returns Nothing -Make sure to handle the FileNotFoundException that is raised when the output file cannot be opened for writing -Remember the exception design principle: throw early, catch and handle late. This method should throw the FNFE out open pFilename for writing out.println("runs total, ", the sum of pListRuns) For k 1 to plistRuns. size() - 1 Do out.print ln("runs_k, ", the element at index k of pListRuns) End For Close out End Method output Method readInputFile(String pFilename) Returns ArrayList of Integers -Make sure to handle the FileNotFoundEaception that is raised when the input file cannot be opened for reading -Remember the exception design principle; throw early, catch and handle late. This method should throw the FNFE in + open pFilename for reading Declare and create an ArrayList of Integers named list While there is more data to be read from in Do Read the next integer and add it to list End While Close in Return list End Method readInputFile

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

Beginning Microsoft SQL Server 2012 Programming

Authors: Paul Atkinson, Robert Vieira

1st Edition

1118102282, 9781118102282

More Books

Students also viewed these Databases questions