1. Engineering
  2. Computer Science
  3. 2 learning objectives 1 use the integer wrapper class 2...

Question: 2 learning objectives 1 use the integer wrapper class 2...

Question details

2 Learning Objectives 1. Use the Integer wrapper class. 2. Declare and use ArrayList<E> class 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 (k+1)-length subsequence starting at index i: list. list, 1, list, .2, , list, k, that is monotonically increasing (i.e.. list,.. 1st, 시 for each J-1, 2, 3, , k). Similarly, a run down is a (k+1)-length subsequence starting at index i: listi, list,-i, list,i2, ..., listuk, that is monotoni- cally decreasing (i.e., list,ti S listu for each j-1, 2, 3, ..., k). For the above example list we have these runs up and runs down: Runs Up list, through list = { 2, 8 }; k= 1, 2-length subseq list,-{ 2 }; k = 0, 1-length subseq list-{ 3 }; k= 0, 1-length subseq lists through list 2, 9 ; k-1, 2-length subseq list through list 9, 8, 6, 3 ; k 3, 4-length subseq lists- 8 k0, 1-length subseq list, { 6 }; k 0, 1-length subseq list, through list, { 3. 4. 6 }; k= 2, 3-1en subseq listi,-{ 9 }; k = 0, 1-length subseq listo through listi-11, 9 1; k-1, 2-len subseq Runs Down lish through list,-{ 8, 3, 2 }; k 2, 3-length subseq lists { 4 }; k 0, l-length subseq list through listo 6, 1 ; k-1, 2-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 runi. O < < n. For the example list we have: ん runsk runs { 2, 8 }, { 2.9 }, { i, 9 }, and { 6, 1 } 3, 4, 6, and 8, 3, 2 ) 9, 8, 6, 3 ) Note: (1+1)-length subsequence Note: (2+1)-length subsequence Note: (3+1)-length subsequence 4-11 Let runste tal be the the sum from k-1 to n-l of runs. For the example list, runs,otal-4 + 2 + 1 7 The runs up and runs down test is used in statistics. When I wrote my masters thesis on random number generation algorithms, I had to write these tests to determine the indepence between successive random numbers generated by my algorithms 1

CSE205 Object Oriented Programming and Data Structures 4 Software Requirements Your program shall 1. Open a file named p01-in.txt containing n integers, 1 Programming Project 1: 25 pts n s 1000, with each integer in 0, 32767]. There will be one or more integers per line. A sample input file: Sample p01-in.txt 2 8 3 2 9 3 4 6 1 9 2. The program shall compute runs for k = 1, 2, 3, , n-1 3. The program shall compute runstota 4. The program shall produce an output file named pol-runs. tat containing runs, td and runs: for k = 1, 2, 3, n-1 The file shall be formatted as shown in the example file below. Please make sure your output file meets the format shown below, e.g., all text is in lowercase, an underscore separates runs from the text that follows it, there is a space following each comma, and each line of text ends with a newline/endline character. During grading, we will compare your output file to our correct output file using an automated comparison program and if there is any mis- match then your program may be considered incorrect and you may lose substantial points. Sample p01-runs. tat runs total, 7 runs_1, 4 runs 2, runs 3, 1 runs 4, 0 runs 5, 0 runs_6, 0 runs.7, runs_8, 0 runs_9, 0 runs_10, 0 runs.11, 5. If the input file p01-in.txt cannot be opened for reading (because it does not exist) then display an error message on the output window and immediately terminate the program, e.g., run program... Sorry, could not open p01-in.txt for reading. Stopping 6. If the output file p01-runs.tat cannot be opened for writing (e.g., because the write access to the file is disabled) then display an error message on the output window and immediately terminate the program, e.g., run program.. Sorry, could not open p01 -runs.txt for writing. Stopping 5 Software Design Your program shall 1. Contain a class named Main. This class shall contain the main) method. The main method shall instantiate an object of the Main class and call ru on that object, see template code below

CSE205 Object Oriented Programming and Data Structures Programming Project 1:: 25 pts // Main.java public class Main f public static void main(String[] pArgs) < Main mainObject = new Main(); mainObject.run() // Or you can just write: new Main().run(); // in place of these two lines private void run // You will start writing code here to implement the software requirements 2. One of the primary objectives of this programming project is to learn to use the java.util.ArrayList< E class. Therefore, you are not permitted to use primitive 1D arrays. Besides, you will quickly discover that the ArrayList class is more convenient to use than 1D arrays. 3. ArrayList<E is a generic class meaning: (1) that it can store objects of any reference type, e.g., E could be the classes Integer or String; and (2) when an ArrayList object is declared and instantiated, we must specify the class of the objects that will be stored in the ArrayList. For this project, you need to define an ArayList that stores integers, but you cannot specify that your ArrayList stores ints because int is a primitive data type and not a class. Therefore, you will need to use the java.lang. Integer wrapper class ArrayList<Integer> list -new ArrayList<o( int x = 1; list.add(x); // Legal because of Java autoboxing 4. You must write an exception handler that will catch the FileNotFoundErception 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 cxit which is in the javalang. System class. The eait) 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() // Try to open input file for reading catch (FileNotFoundException pExcept) // Print friendly error message System.exit(-1); 5. Similar to Item 4, you must write an exception handler that will catch the File Not FoundException 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 program 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 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- 7. nts to other methods when appropriate. 8. 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.

CSE205 Object Oriented Programming and Data Structures Programming Project 1 : 25 pts Method merge(pListRunsUpCount is ArrayList of Integers, pListRunsDnCount is ArrayList of Integers) Returns ArrayList of Integers listRunsCount arrayListCreate(pListRunsUp Count.size), 0) For i +0 to pListRunsUpCount.size) - 1 Do Set element 1 of listRunsC0นnt to the sum of the elements at i in plistRunsupC01snt and pListRunsDnCount End For Return IstRนnsCount End Method merge Method arrayListCreate(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 writelutputFile(String pFilename; pListRuns is ArrayList of Integers) Returns Nothing ーMake sure to handle the FileNotF。นndException that is raised when the output file cannot be opened for writing out * open pFilename for writing out.println(runs total, , the sum of pListRuns) For K ← 1 to PL1stR ns . S2Ze() - 1 Do out.println(runs_k, , the element at index k of pListRuns) End For Close out End Method output Method readInputFile(String pFilename; pListRuns is ArrayList of Integers) Returns ArrayList of Integers 一Make sure to handle the FileNotF0นndE#ception that is raised when the input file cannot be opened for reading 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

Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution