Transcribed TextTranscribed Text

2 Learning Objectives 1. Use the Integer wrapper class. 2. Declare and use ArrayList class objects. 3. Write code to read from, and write to, text files. 4. Write an exception handler for an 1/0 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 [O, 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+l)-length subsequence list;, list;+1, list;+2, ••• , list;+k, that is monotonically increasing (i.e., list;+i � listi+fI for each j = 1, 2, 3, ... , k). Similarly, a run down is a (k+l)-length subsequence list;, list;+1, list;+2, ••• , list;+k, that is monotonically decreasing (i.e., list;+f1 :5; list;+i for each j = 1, 2, 3, ... , k). For the above example list we have these runs up and runs down: Runs Up list.i through list1 = { 2, 8 }; k = 1 listi = { 3 } ; k = 0 list3 through list4 = { 2, 9 }; k = 1 listr, = { 8 } ; k = 0 lisi:t; = { 6 }; k = 0 listi through lis°t,;J = { 3, 4, 6 }; k = 2 list1o through listn = { 1, 9 } ; k = 1 Runs Down list.i = { 2 } ; k = 0 list1 through list3 = { 8, 3, 2 }; k = 2 list4 through listi = { 9, 8, 6, 3 }; k = 3 lists = { 4 } ; k = 0 lis°t,;J through list10 = { 6, 1 }; k = 1 listn = { 9 } ; k = 0 We are interested in the value of k for each run up and run down and in particular we are interested in the total umber of runs for each nonzero k, which we shall denote by runsk, 0 < k < n - l. For the example list we have: k runsk runs 1 4 { 2, 8 }, { 2, 9 }, { 1, 9 }, and { 6, 1 } 2 2 { 3, 4, 6, } and { 8, 3, 2 } 3 1 { 9, 8, 6, 3 } 4-11 0 Let runsiowl be the the sum from k = 1 to n - 1 of runsk. For the example list, runsiotal = 4 + 2 + 1 = 7. 4 Software Requirements Your program shall: 1. Open a file named p01-in.txt containing n integers, 1 � n � 1000, with each integer in [O, 32767]. There will be one or more integers per line. A sample input file: Sample p01-in.txt 2 8 3 2 9 8 6 3 4 6 1 9 2. The program shall compute runsk for k = 1, 2, 3, ... , n - 1. 3. The program shall compute runs1otal• 4. The program shall produce an output file named p01-runs.txt containing runs101a1 and runsk for k = 1, 2, 3, ... , n - 1. The file shall be formatted as shown in the example file below. Sample p01-runs.txt runs_total, 7 runs_1, 4 runs_2, 2 runs_3, 1 runs_4, 0 runs_5, 0 runs_6, 0 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. 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 run() on that object. I I Main. java public class Main { public static void main(String[] pArgs) { Main mainObject = new Main(); } private void run() { } } II 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 class. Therefore, you are not permitted to use 1D arrays. Besides, you will quickly discover that the ArrayList class is more convenient to use than 1D arrays. 3. ArrayList is a generic class meaning: (1) that it can store objects of any class; 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.lnteger wrapper class: ArrayList<Integer> list = new ArrayList<>(): int X = 1; list.add(x); II Legal because of Java autoboxing. 4. 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 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 exit() 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: try { II Try to open input file for reading } catch (FileNotFoundException pExcept) { II Print friendly error message System.exit(-1); } 5. 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. 6. A void 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 arguments to other methods when appropriate. 7. Format your code neatly. 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. Put a comment header block at the top of each method formatted thusly: I** * J brief description of what the method does. *I 9. Put a comment header block at the top of each source code file-not just for this project, but for every project we write-formatted thusly: II******************************************************************************************************** II CLASS: ciassname ( II II DESCRIPTION II J description of the contents of this fiZe. II II COURSE AND PROJECT INFO II CSE2O5 Object Oriented Programming and Data Structures, semester and year II Project Number: project-n"IJ,ITlber II II AUTHOR II your-name (your-emaiZ-addr) II******************************************************************************************************** 6 Pseudocode Method Run() Returns Nothing Declare and create an ArrayList of Integers named List List+- ReadFiLe("p01-in.txt") Declare and create an ArrayList of Integers named ListRunsUpCount Declare and create an ArrayList of Integers named ListRunsDnCount ListRunsUpCount +- FindRuns(List, RUNS_UP) ListRunsUpCount +- FindRuns(List, RUNS_DN) Declare and create an ArrayList of Integers named ListRunsCount ListRunsCount +- Herge(ListRunsUpCount, ListRunsDnCount) 0utput("p01-runs. txt", ListRunsCount) End Method Run Method FindRuns(In: pList is ArrayList of Integers; int pDir is RUNS_UP or RUNS_DN) Returns ArrayList of Integers ListRunsCount +- arrayListCreate(pList.size(), 0) Declare int varaibles i +- 0, k +- 0 While i < pList.size() - 1 Do If pDir is RUNS_UP and pList element at i is :S pList element at i + 1 Then Increment k Elseif pDir is RUNS_DN and pList element at i is 2: pList element at i + 1 Then Increment k Else If k � 0 Then Increment the element at index k of ListRunsCount k +- 0 End if End If Increment i End While If k � 0 Then Increment the element at index k of ListRunsCount End If Return ListRunsCount End Method FindRuns Method Herge(In: pListRunsUpCount, In: pListRunsDnCount) Returns ArrayList of Integers ListRunsCount +- arrayListCreate(pListRunsUpCount.size(), 0) For i +- 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 ListRunsCount End Method Herge Method arrayListCreate(In: int pSize; In: 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) on List Return List End Method arrayListCreate Method Output(In: pFiLename; pListRuns ArrayList of Integers) Returns Nothing out+- open pFiLename for writing out.print("runs_total, ", the sum of pListRuns) For k+- 1 to pListRuns.size() - 1 Do out.print("runs_k, ", the element at index k of pListRuns) End For Close out End Method Output

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

// import library for file operations
// import java.util library for ArrayList object
import java.util.*;

* Main class
public class Main {

   * main() method
   * runs on execution, constructs object, and calls run() method
public static void main(String[] pArgs) {
    Main mainObject = new Main();;

   * readFile() method
   * opens file with provided String name, constructs ArrayList of int values
   * returns ArrayList of int values
public ArrayList<Integer> readFile(String file) {
    ArrayList<Integer> list = new ArrayList<Integer>();
    try(BufferedReader br = new BufferedReader(new FileReader(file))) {
      for(String line; (line = br.readLine()) != null; ) {
       String[] values = line.split("\\s+");
       for (String value: values) {
    } catch (Exception e) {
      System.out.println("Sorry, could not open '" + file + "' for reading. Stopping.");

By purchasing this solution you'll be able to access the following files:

for this solution

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Java Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Upload a file
Continue without uploading

We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats