Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help filling out the milestones at the bottom of this code: Milestone 2: Write methods to time the ArrayList and LinkedList .get() methods

I need help filling out the milestones at the bottom of this code: Milestone 2: Write methods to time the ArrayList and LinkedList .get() methods and print the table. Milestone 3: Write methods to time the ArrayList .add(0, element) and LinkedList .addFirst methods and print the table. Milestone 4: Write methods to time the .contains method for each of the three data structures (two lists and HashSet) and print the table

CODE: package list.timing;

import java.util.List; import java.util.LinkedList; import java.util.ArrayList; import java.util.HashSet;

public class Main { public static final int iters = 100; public static final int[] sizes = {10, 50, 100, 500, 1000, 10000}; // --- // Example of how to time functions that change the size of the ADT public static float timeAdd(List startlist) { long total = 0; long start, end; for (int i=0; i

public static float timeAdd(HashSet startset) { long total = 0; long start, end; for (int i=0; i startlist) { long total = 0; long start, end; start = System.nanoTime(); int idx = startlist.size() / 2; for (int i=0; i

public static void fillToSize(List list, int size) { list.clear(); for (int i=0; i

public static void fillToSize(HashSet hs, int size) { hs.clear(); for (int i=0; i

public static void printTable(String title, List lltimes, List altimes, List hstimes) { System.out.println("--- " + title + "---"); System.out.printf("%4s\t",""); for(int size: sizes) System.out.printf("%10d\t", size); System.out.println(); System.out.printf("%4s\t", "AL"); for(Float time: altimes) { System.out.printf("%10.2f\t", time.floatValue()); } System.out.println();

System.out.printf("%4s\t", "LL"); for(Float time: lltimes) { System.out.printf("%10.2f\t", time.floatValue()); } System.out.println();

System.out.printf("%4s\t", "HS"); for(Float time: hstimes) { System.out.printf("%10.2f\t", time.floatValue()); } System.out.println(); System.out.println(); }

public static void printTable(String title, List lltimes, List altimes) { System.out.println("--- " + title + "---"); System.out.printf("%4s\t",""); for(int size: sizes) System.out.printf("%10d\t", size); System.out.println(); System.out.printf("%4s\t", "AL"); for(Float time: altimes) { System.out.printf("%10.2f\t", time.floatValue()); } System.out.println();

System.out.printf("%4s\t", "LL"); for(Float time: lltimes) { System.out.printf("%10.2f\t", time.floatValue()); } System.out.println(); System.out.println(); }

/** * @param args the command line arguments */ public static void main(String[] args) { LinkedList ll = new LinkedList<>(); ArrayList al = new ArrayList<>(); HashSet hs = new HashSet<>();

ArrayList lltimes = new ArrayList<>(); // time for linked list ops ArrayList altimes = new ArrayList<>(); // time for array list ops ArrayList hstimes = new ArrayList<>(); // time for hash set ops

// This code times the .add method, and adds the result to the appropriate list of times. for (int size: sizes) { fillToSize(ll, size); lltimes.add(timeAdd(ll)); fillToSize(al, size); altimes.add(timeAdd(al)); fillToSize(hs, size); hstimes.add(timeAdd(hs)); }

// print the table for the .add method printTable("Add (.add)", lltimes, altimes, hstimes); //Make sure to clear all of the times arraylists so you don't have extra times being tacked on. lltimes.clear(); altimes.clear(); hstimes.clear();

for (int size: sizes) { fillToSize(ll, size); lltimes.add(timeSet(ll)); fillToSize(al, size); altimes.add(timeSet(al)); } printTable("Set index in middle (.set)", lltimes, altimes);

// TODO: Milestone 2: Time ArrayList and LinkedList .get() method // You will need to write timeGet for linked list and array list // remember to clear your times arrays.

//TODO: Milestone 3: Time .addFirst for LinkedList and .add(0, element) for ArrayList // You will need to write the appropriate timing methods as above

//TODO: MIlestone 4: Time .contains() for LinkedList, ArrayList, and HashSet // You will need to write the appropriate timing methods as above }

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data And Databases

Authors: Jeff Mapua

1st Edition

1978502257, 978-1978502253

More Books

Students also viewed these Databases questions

Question

Have roles been defined and assigned?

Answered: 1 week ago