Question: CSE205 Object Oriented Programming and Data Structures Programming Project 1 1 Project Instructions We are currently working on a system for having you submit your

 CSE205 Object Oriented Programming and Data Structures Programming Project 1 1

Project Instructions We are currently working on a system for having you

submit your programs for grading in such a way that we canautomate as much of the grading as possible (so we can return

graded projects sooner). We will let you know when that is readyand you can submit your project. In the meantime, please work on

CSE205 Object Oriented Programming and Data Structures Programming Project 1 1 Project Instructions We are currently working on a system for having you submit your programs for grading in such a way that we can automate as much of the grading as possible (so we can return graded projects sooner). We will let you know when that is ready and you can submit your project. In the meantime, please work on the project so you can complete it before the deadline the deadline is specified in the Course Schedule section of the Syllabus. 2 Learning Objectives 1. To properly use the Integer wrapper class. 2. To declare and use ArrayList class objects. 3. To write code to read from, and write to, text files. To write an exception handler for an I/O exception. 5. To write a Java class and instantiate objects of that class. 3 Background Let list be a nonempty sequence or list of non-negative random integers, each in the range [0, 32767] and let n be the length of list, e.g., here is one sample list: list = { 2, 8, 3, 2, 9, 8, 6, 3, 4, 6, 1,9 } where n = 12. List elements are numbered with an index or subscript starting at 0, so the first 2 in list is at index 0, the 8 to the right of the 2 is at index 1, the 3 to the right of the 8 is at index 2, and so on, up to the final 9 which is at index 11. 4. 3.1 Run Up Definition We define a run up in a list to be a (k + 1)-length subsequence of list (k > 1), starting at index i Osi 1. Consequently, all runs up must be subsequences of length 2 2. It may help to think of k as counting the number of spaces or holes in between each integer of the run up subsequence. Consider the run up {3 4 6}. The length of this subsequence is 3 meaning that k is 2. Notice that there are two spaces or holes in between 3 and 4 and in between 4 and 6. That is what k represents. 3.2 Run Down Definition Similarly, a run down in list is a (k+1)-length subsequence (k > 1) starting at index i (0 si 1. Consequently, all runs down must be subsequences of length 2 2. It may help to think of k as counting the number of spaces or holes in between each integer of the run down subsequence. Consider the run down {9 8 6 3). The length of this subsequence is 4 meaning that k is 3. Notice that there are three spaces or holes: in between 9 and 8; in between 8 and 6; and in between 6 and 3. That is what k represents. CSE205 Object Oriented Programming and Data Structures Programming Project 1 3.3 The Runs Up in the Example List For the example list shown above we have the following runs up: list, to list; = {2, 8} is monotonically increasing because 2 3 > 2. The subsequence length is k + 1 = 3, so k = 2. lista to listy = {9, 8, 6, 3} is mono. decreasing because 9 >8 > 6 > 3. The subsequence length is k + 1 = 4, so k = 2. list, to listo = {6, 1} is mono. decreasing because 6 > 1. The subsequence length is k +1 = 2, so k= 1. Note that we do not consider {8, 3} and {3, 2} to be runs down because both those subsequences are subsequences of the subsequence {8, 3, 2} which is a run down. 3.5 The Problem Given a list of integers of variable length, we are interested in the value of k for each run up and run down in the list. In particular we are interested in the total number of runs for each possible value of k, which we shall denote by runs, 1 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 in this program than a 1D array would be. SWDR3. ArrayList is a generic class meaning: (1) that it can store objects of 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 ArrayList 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 list = new ArrayList(); int x = 1; list.add(x); // Legal because of Java autoboxing. CSE205 Object Oriented Programming and Data Structures Programming Project 1 4. SWDR 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 exit() 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 exit(). Therefore, terminate the program this way by sending -100 to exit(). try { // Try to open input file for reading } catch (FileNotFoundException pException) { // Print friendly error message System.exit(-100); } 5. 6. SWDR 5. Similar to Item 4, you must write an exception handler that will catch the FileNotFoundException that gets thrown when the output file cannot be opened for writing. The exception handler will print the message as shown in Software Requirement and then terminate the program by sending -200 to exit(). SWDR 6. Your programming skills should be sufficiently developed that you are beyond writing the entire code for a program in the main() method or all of it in run(). You shall partition the functionality of the program into multiple methods. Remember, a method should have one function, i.e., it should do one thing and it should generally be short. If you find a method is becoming too lengthy or overly complicated because you are trying to make that method do more than one thing, then divide the method into 2, 3, 4, or more smaller methods, each of which does one thing. SWDR 7. Avoid making every variable or object an instance variable. For this project, we do not require any instance variables in the class Main so you shall not declare any instance variables in the class. Rather, all variables should be declared as local variables in methods and passed as arguments to other methods when appropriate. SWDR 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. SWDR 9. Put a comment header block at the top of each method formatted thusly: 7. 8. 9. * A brief description of what the method does. */ 10. SWDR 10. Put a comment header block at the top of each source code file not just for this project, but for every project we write, e.g., * CLASS: classname (classname.java) DESCRIPTION A description of the contents of this file. COURSE AND PROJECT INFORMATION * CSE205 Object Oriented Programming and Data Structures, semester and year * Project Number: project-number AUTHOR: your name, your-asuriteid, your-email-addr * AUTHOR: your name, your-asuriteid, your-email-addr ** Note: Include a second author line if you work with a partner ** 5.1 Software Design: Pseudocode To help you complete the program, you may follow our design in this section and implement this pseudocode if you wish. Alternatively, you may create and implement your own design, long as your program meets the software requirements discussed in $4 and the software design requirements specified in $5. Programming Project 1 CSE205 Object Oriented Programming and Data Structures Class Main Declare a static int constant RUNS_UP which is equivalent to 1 Declare a static int constant RUNS_DN which is equivalent to - 1 -- Note: In the Java implementation, all of these methods will be private methods declared inside the Main class. -- Static method main() must call run, so in essence, run( becomes the starting point of execution. Static Method main(pArgs : String[]) Returns Nothing Instantiate an object of this class and then call method run on the object End Method main -- It is imperative that runcatches and handles the FileNotFoundExceptions that may get thrown in -- readinputFile() and writeOutputFile( when the input and output files cannot be opened for reading and writing. Instance Method run Returns Nothing Declare ArrayList of Integers named list Try list - readinputFile("p1-in.txt") Catch pException : FileNotFoundException Display error message as described in SWR 4 Terminate the program with exit code of -100 End Declare and create an ArrayList of Integers named list RunsUp Count Declare and create an ArrayList of Integers named list RunsDnCount listRunsUpCount+findRuns(list, RUNS_UP) listRunsDnCount + findRuns(list, RUNS_DN) Declare ArrayList of Integers listRunsCount+mergeLists(listRunsUpCount, listRunsDn Count) Try writeOutputFile("p1-runs.txt", listRunscount) Catch pException: FileNotFoundException Display error message as described in SWR 5 Terminate the program with exit code of -200 End End Method run -- pList is the ArrayList of Integers that were read from "p1-in.txt". p Dir 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. Instance Method findRuns(pList : ArrayList of Integers,pDir : int) Returns ArrayList of Integers listRunsCount = arrayListCreate(plist.size(), 0) -- Create ArrayList of proper size and init each element to 0 Declare int variables initialized to 0:1 +0,60 the left arrow represents the assignment operator While ic plist.size- 1 Do If pDir is RUNS_UP and pList element at i is spList element at i + 1 Then -- This is a run up Increment k ElseIf pDir is RUNS_DN and pList element at i is pList element at i+ 1 Then -- This is a run down Increment k Else If k does not equal o Then Increment the element at index k of listRunsCount ko End if End If Increment1 End While If k does not equal 0 Then Increment the element at index k of listRunscount End If Return listRunscount End Method find Runs CSE205 Object Oriented Programming and Data Structures Programming Project 1 Instance Method mergeLists(plistRunsUpCount : ArrayList of Integers, pListRunsDnCount: ArrayList of Integers) Returns ArrayList of Integers listRunsCount = arrayListCreate(pListRunsUpCount.size(,0) For it 0 to pListRunsUpCount.size() - 1 Do Set element i of listRunsCount to the sum of the elements at i in pListRunsUpCount and pListRunsDnCount End For Return list Runs Count End Method mergeLists Instance Method arrayListCreate(pSize : int; pinitValue : int) 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 -- Make sure to throw the FileNotFoundException that is raised when the output file cannot be opened for writing. Instance Method writeOutputFile(pFilename: String; pListRuns : ArrayList of Integers) Returns Nothing Throws FileNotFoundException out + open pFilename for writing out.println "runs_total: ", the sum of the elements of pListRuns For kr 1 to pListRuns.size() - 1 Do out.println "runs_", k," ", the element at index k of pListRuns End For Close out End Method output -- Make sure to throw the FileNotFoundException that is raised when the input file cannot be opened for reading. Instance Method readInputFile(pFilename: String) Returns ArrayList of Integers Throws FileNotFoundException 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 End Class Main

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!