import java.util.ArrayList; import java.util.LinkedList; import java.util.List;
public class Day2_Lab1 { public static void main(String args[]) { List al = new ArrayList(), ll = new LinkedList();
myPopulateRandomly(al); myPopulateRandomly(ll); long start = System.currentTimeMillis(); int maxAl = findMax(al); long end = System.currentTimeMillis(); System.out.println("ArrayList findMax: max=" + maxAl + ", time=" + (end - start)); start = System.currentTimeMillis(); int maxLl = findMax(ll); end = System.currentTimeMillis(); System.out.println("LinkedList findMax: max=" + maxLl + ", time=" + (end - start));
} public static > T findMax(List inL) { if (inL.isEmpty()) return null; T max = inL.get(0); int listSize = inL.size(); for (int i = 1; i 0) max = temp; } return max; } public static final int MAX = 500000; public static void myPopulateRandomly(List inL) { for (int i = 0; i 1. Make sure you study Participation Activity 1.3.1 in the zyBook very carefully. You don't have to fully understand how Lists are implemented using LinkedLists and/or Arraylists, but you should try to understand as much as possible. 2. In the Lab1 class, implement the following method public static void myPopulateRandomly(List inList, int howMany) This method populates inList with howMany randomly-generated ints. Note that inList is either an ArrayList or a LinkedList . Regardless, your implementation must run as efficiently as possible. 3. Only use the List method add(int index, Integer element) in your implementation of myPopulateRandomly for adding elements to inList. 4. In the Lab1 class, implement the following method: public static void myRemoveAll(List inList) This method removes all the elements from inList. Note that inList is either an ArrayList or a LinkedList. Regardless, your implementation must run as efficiently as possible. 5. You CANNOT use the built-in List method removeAll in your implementation of myRemoveAll. 6. In your implementation of myRemoveAll, only use the List method remove(i) for removing elements from inList. 7. In the Lab1 class, implement the following method: public static int myHowManyGreaterThan(List inList, int target) This method returns the number of elements in inList that are strictly greater than target. Note that inList is either an ArrayList or a LinkedList . Regardless, your implementation must run as efficiently as possible. 8. You don't need to have a main method, but you should thoroughly test your myPopulateRandomly, myRemoveAll and myHowManyGreaterThan methods. 9. If your code doesn't compile, then you will receive an automatic 0 . 10. Make sure your methods do not crash due to a RuntimeException under any circumstances. For example, what if an empty List is passed in? Introduction: This lab will introduce you to NetBeans, the integrated development environment (IDE) that we'll be using throughout this semester. It should be available on the lab machines but it's available for download for free (just Google it). If you have trouble installing NetBeans on your own machine, just let me know and I'll do my best to help you out. For this lab, you will implement some methods that process a given List: myPopulateRandomly, myRemoveAll and myHowManyGreaterThan, which populates a given List with random numbers, removes all the elements from a given List and returns the number of numbers greater than a given target number within a given List , respectively. You will NOT know the object type of the given List . In other words, the given List may be an ArrayList or a LinkedList, but you won't know which one. You need to implement these methods so that they execute as efficiently as possible, regardless of the object type of the given list. Beyond just getting up and running with NetBeans, the main conceptual focus of this lab is to get you to think about how the underlying data structure can impact performance. Getting started: 1. Open up NetBeans. 2. Go to File and select New Project. 3. In the New Project window: (a) Select Java in Categories. (b) Select Java Application in Projects. See Figure 1. (c) Click Next. 4. In the New Java Application window: (a) Name your project Lab1. (b) Choose your Project Location carefully, so that you'll remember where it's located. (c) Uncheck Create Main Class. See Figure 2. (d) Click Finish. 5. Create a new Java class: Figure 1: New Project. New Java Application Steps Name and Location 1. Choose Project 2. Name and Location Project Name: Lab1 Project Location: /Users/summerss/Google Drive Project Folder: IUsers/summerss/Google Drive Use Dedicated Folder for Storing Libraries Libraries Folder: Different users and projects ca same compilation libraries (see details). Create Main Class Figure 2: New Java Applicatio: (a) In the Projects tab, expand Source Packages. (b) Right-click on the default package. (c) Select New Java Class. See Figure 3. Figure 3: Create a new Java class. 6. In the New Java Class window: (a) Name your class Lab1. See Figure 4. (b) Click Finish. 7. Your screen should look roughly like what is shown in Figure 5. I will show you how to use several features in NetBeans throughout the semester, so make sure you attend lectures and labs. Follow the instructions in the Requirements section to see exactly what you have to do for this lab. 1. Choose file Type 2. Name and Location Project: Lab1 Location: Package: Created File: i/summerss/Google Drive. Fall2019/271/Labs/I1/Lab1/Lab1/src/Lab1.jara L.Waming: It is highly recommended that you do not place Java classes in the default package. Figure 4: New Java Class Window. Figure 5: Your screen should look roughly like this after completing steps 1 through 6 in the Getting started