COSC 117 Array Lists 1 Introduction For each of the following create a new project with an appropriate name and then write a program that solves the given problem. Remember to use Shift+Ctrl+F to...

Arraylist exercises


COSC 117 Array Lists 1 Introduction For each of the following create a new project with an appropriate name and then write a program that solves the given problem. Remember to use Shift+Ctrl+F to format the program, or Shift+Command+F on the Mac and include the the standard comments of at the top which include the author, date, and a program description. Put a line or two of comments before major blocks of code on what that section of code is doing. Also before each decision structure a line or two of comments on what that decision structure is doing. The same goes for your loops, a line or two comment before each loop explaining what that loop is accomplishing. Each method should have a several line comment on what the method does, the meaning of any input parameters, and the intended return if any. For each program, you will be submitting the java code file through MyClasses, as you did before. I also want either a Microsoft Word docx file, LibreOffice Writer odt, or a text file (which you can create with NotePad++) which contains the output of at least three runs of each program on different data inputs. You do not need to include an algorithm with these programs but you should think the program through before you start coding. I would suggest flow-charting the main sections of the program layout, this will save you a lot of time in rewrites. 2 Exercises 1. Write a program that will simulate the rolling of a single die, use a method to do this. The program will roll the die until it encounters a run of ten 6’s in a row. Each roll is to be stored in an ArrayList of integers, that is, one ArrayList will store the entire simulation. Once the run of ten 6’s is encountered the simulation stops. The program will then go through the ArrayList and find the runs of 6’s that are of length 1, length 2, length 3, and so on, up to length 10. Be careful not to over-count a run. That is, 3 6 6 6 6 2 is a run of four 6’s, not two runs of three, three runs of two, or four runs of one. A run of the program is below. Number of rolls needed = 91375101 Runs of length 1 = 10578617 Runs of length 2 = 1759344 Runs of length 3 = 294191 Runs of length 4 = 49061 Runs of length 5 = 8099 Runs of length 6 = 1388 Runs of length 7 = 230 Runs of length 8 = 35 Runs of length 9 = 8 Runs of length 10 = 1 2. Create a program that does the following. (a) The main will create an array of integers. (b) The main will call the method PopulateArrayList(ArrayList A) which will ask the user for each entry of the array one by one and insert the input values into the ArrayList. This will end when the user inputs a negative number. The negative number is not to be inserted into the ArrayList. (c) The main will then print the ArrayList. (d) The main should then call a method 1 COSC 117 Array Lists ManipulateArrayList(ArrayList A) which does the following to each entry in the array. If the entry is even then the entry is replaced by half its value, if the entry is evenly divisible by 3 then the entry is replaced with one third its value, if the entry is neither divisible by 2 or 3 then 5 is added to the entry. (e) The main should then print out the contents of the ArrayList. (f) The main should then call the manipulate and print the ArrayList two more times. A run of the program is below. Input entry 1: 4 Input entry 2: 3 Input entry 3: 6 Input entry 4: 7 Input entry 5: 23 Input entry 6: 54 Input entry 7: 12 Input entry 8: -1 [4, 3, 6, 7, 23, 54, 12] [2, 1, 3, 12, 28, 27, 6] [1, 6, 1, 6, 14, 9, 3] [6, 3, 6, 3, 7, 3, 1] 3. Recall the Nifty sequence that takes a number and if it is even it will divide it by 2 and if it is odd it will multiply by 3 and add 1. As we discussed before the sequence always seems to hit one at some point and then we stop the sequence. This exercise will take an input number from the user and instead of just printing the sequence out it will store the sequence in an ArrayList. Create a program that does the following. (a) In the main, the program will ask the user for the first number. (b) The main then creates an ArrayList of integers and loads n into the first position and then calls, public static void Populate(ArrayList A) which loads the sequence into the ArrayList. (c) Have the main print out the ArrayList. (d) Have the program call two more methods that you will write. One of these is to count the number of even numbers in the list and the other is to count the odd numbers in the list. The headers for these will look like, public static int CountEvens(ArrayList A) public static int CountOdds(ArrayList A) Have the main print these values out. A run of the program is below. Input n: 12 [12, 6, 3, 10, 5, 16, 8, 4, 2, 1] Number of even numbers in the list: 7 Number of odd numbers in the list: 3 4. Create a program that does the following. (a) In the main, the program will ask the user for a number, n, a double in this case that is greater than 0. (b) Create an ArrayList of doubles and load n into the first position. 2 COSC 117 Array Lists (c) In a loop, take the last number in the ArrayList, say we call it v, calculate, w = 1 2 ( v + n v ) and put this new number w on the back of the ArrayList. This process should continue until the absolute value of the difference between the last two entries in the ArrayList is less than 0.000000001. (d) Print out the ArrayList with each entry on its own line. A few runs of the program are below. Input n: 13 13.0 7.0 4.428571428571429 3.6820276497695854 3.6063454894655185 3.6055513629176015 3.6055512754639905 3.6055512754639896 Input n: 2 2.0 1.5 1.4166666666666665 1.4142156862745097 1.4142135623746899 1.414213562373095 Input n: 1000 1000.0 500.5 251.249000999001 127.61455816345908 67.72532736082603 41.24542607499115 32.745269344488634 31.642015868650788 31.622782450701045 31.622776601684336 31.622776601683793 3
Nov 16, 2021
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here