First, if you have checkpoints left over from last lab, get them inspected during the first 15 minutes of this lab. No credit will be awarded past these 15 minutes.
Don’t forget to work in pairs! Where possible, please work with someone that you did not work with last lab. The exchange of new ideas and perspectives is not an opportunity you want to rob yourself of.
In this lab you will learn about storing and processing collections of data in arrays and
ArrayLists. Arrays let us number our data, making it easy to loop through the collection or associate data with the natural numbers. However, arrays are not flexible in structure. Before we can use them, we must know how many elements we’ll need. Adding and removing elements are not builtin operations.
ArrayList is a class that wraps around an array and offers methods for adding, removing, locating, and many other operations.
Solve two of the following problems:
getItemsAndPartsthat accepts an
ArrayList<Integer>as parameters. It prompts the user to enter items and their integer parts, storing them in the parameter lists.
sumthat accepts an
ArrayList<Integer>and returns the sum of all the integers in the list. For example, if the parts list is [1, 3], the sum is 4.
toPercentagesthat accepts an
ArrayList<Integer>of parts and returns an
ArrayList<Double>of the percentages corresponding to the parts. For example, if the parts last was [1, 3], the the percentages list is [0.25, 0.75].
Random. You are given a list of names and a parallel list of counts, where
counts[i]represents the number of votes
names[i]received. You’d like to create a new list in which
counts[i]times, but with all the names mixed together in a random order. Write a method that accepts the two lists as parameters, generates a randomly sorted list with the names weighted by popular vote, and returns as winner the name at element 0 in the random list. You can randomize the list with