Depending on the speed and memory capacity of your computer, you may need to rai
ID: 3885177 • Letter: D
Question
Depending on the speed and memory capacity of your computer, you may need to raise the multiplier to see definitive results. Adjust the multiplier as needed so that runtime is not excessive.
Expand the program by adding two more comparisons: search, which searches for a value in the data and counts how many times it is found, and double, which doubles all the values in the array (i.e., if a certain element is initially 27, it will be changed to 54). Follow the style of the given code.
New code verison should have two new methods)
For search, create a random number in the data range to be used as the search value.
import java.util.Random;
import java.util.ArrayList;
import java.util.Random;
import java.text.DecimalFormat;
/*
Description
Compare ArrayList and array for time efficiency in multiple operations.
Contains stubs for search test and double test.
Author:
Date:
*/
public class ArrayListArrayExercise {
private ArrayList list;
private int[] array;
private Random rand;
private DecimalFormat formatter;
private long startTime, endTime;
private final int SIZE = 1_000_000;
private final int DATA_LIMIT = 5_000;
private final int SIZE_MULTIPLIER = 40;
// --------------------
public ArrayListArrayExercise () {
list = new ArrayList(SIZE);
array = new int[SIZE];
rand = new Random();
formatter = new DecimalFormat();
} // ArrayListArrayExercise constructor
// --------------------
// tester method: call all tests
public void tester() {
System.out.println("Testing ArrayList vs. array operation times.");
System.out.println("data structure size: " + formatter.format(SIZE) + " elements");
System.out.println("size multiplier: " + SIZE_MULTIPLIER);
System.out.println("data range: " + "0 through " + (DATA_LIMIT-1));
System.out.println(" fill test ...");
fillTest();
System.out.println(" add test ...");
addTest();
System.out.println(" search test ...");
searchTest();
System.out.println(" double test ...");
doubleTest();
} // tester
// --------------------
// fillTest method: compare times to fill an ArrayList and an array
public void fillTest() {
int i, j;
// fill ArrayList
startTime = (int) System.currentTimeMillis();
for (i = 1; i <= SIZE_MULTIPLIER; i++) {
for (j = 0; j < SIZE; j++){
list.add(rand.nextInt(DATA_LIMIT));
}
list.clear();
}
endTime = (int) System.currentTimeMillis();
System.out.println(" fill test, elapsed time, ArrayList = " + (endTime-startTime) + " milliseconds.");
// fill array
startTime = (int) System.currentTimeMillis();
for (i = 1; i <= SIZE_MULTIPLIER; i++) {
for (j = 0; j < SIZE; j++){
array[j] = rand.nextInt(DATA_LIMIT);
}
}
endTime = (int) System.currentTimeMillis();
System.out.println(" fill test, elapsed time, array = " + (endTime-startTime) + " milliseconds.");
} // fillTest
// --------------------
// addTest method: compare times to add up an ArrayList and an array
public void addTest() {
int total;
int i, j;
// (re)fill ArrayList
startTime = (int) System.currentTimeMillis();
for (i = 0; i < SIZE; i++){
list.add(rand.nextInt(DATA_LIMIT));
}
// sum ArrayList
total = 0;
startTime = (int) System.currentTimeMillis();
for (i = 1; i <= SIZE_MULTIPLIER; i++) {
for (j = 0; j < SIZE; j++){
total = total + list.get(j);
}
}
endTime = (int) System.currentTimeMillis();
System.out.println(" add test, elapsed time, ArrayList = " + (endTime-startTime) + " milliseconds.");
// sum array
total = 0;
startTime = (int) System.currentTimeMillis();
for (i = 1; i <= SIZE_MULTIPLIER; i++) {
for (j = 0; j < SIZE; j++){
total += array[j];
}
}
endTime = (int) System.currentTimeMillis();
System.out.println(" add test, elapsed time, array = " + (endTime-startTime) + " milliseconds.");
} // addTest
// --------------------
// searchTest method: compare times to fill an ArrayList and an array
public void searchTest() {
//stub
System.out.println(">> stub: in search test");
}
// --------------------
// doubleTest method: compare times to double all values in an ArrayList and an array
public void doubleTest() {
//stub
System.out.println(">> stub: in double test");
} // double test
// --------------------
public static void main (String args[]) {
ArrayListArrayExercise timeTest = new ArrayListArrayExercise();
timeTest.tester();
} // main
} // ArrayListArrayExercise
Explanation / Answer
/* I have written the complete code which includes the search and double methods */
package arrayListArrayExcercise;
import java.util.Random;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Random;
import java.text.DecimalFormat;
/*
Description
Compare ArrayList and array for time efficiency in multiple operations.
Contains stubs for search test and double test.
Author:
Date:
*/
public class ArrayListArrayExercise {
private ArrayList list;
private int[] array;
private Random rand;
private DecimalFormat formatter;
private long startTime, endTime;
private final int SIZE = 1_000_000;
private final int DATA_LIMIT = 5_000;
private final int SIZE_MULTIPLIER = 40;
// --------------------
public ArrayListArrayExercise () {
list = new ArrayList(SIZE);
array = new int[SIZE];
rand = new Random();
formatter = new DecimalFormat();
} // ArrayListArrayExercise constructor
// --------------------
// tester method: call all tests
public void tester() {
System.out.println("Testing ArrayList vs. array operation times.");
System.out.println("data structure size: " + formatter.format(SIZE) + " elements");
System.out.println("size multiplier: " + SIZE_MULTIPLIER);
System.out.println("data range: " + "0 through " + (DATA_LIMIT-1));
System.out.println(" fill test ...");
fillTest();
System.out.println(" add test ...");
addTest();
System.out.println(" search test ...");
searchTest();
System.out.println(" double test ...");
doubleTest();
} // tester
// --------------------
// fillTest method: compare times to fill an ArrayList and an array
public void fillTest() {
int i, j;
// fill ArrayList
startTime = (int) System.currentTimeMillis();
for (i = 1; i <= SIZE_MULTIPLIER; i++) {
for (j = 0; j < SIZE; j++){
list.add(rand.nextInt(DATA_LIMIT));
}
list.clear();
}
endTime = (int) System.currentTimeMillis();
System.out.println(" fill test, elapsed time, ArrayList = " + (endTime-startTime) + " milliseconds.");
// fill array
startTime = (int) System.currentTimeMillis();
for (i = 1; i <= SIZE_MULTIPLIER; i++) {
for (j = 0; j < SIZE; j++){
array[j] = rand.nextInt(DATA_LIMIT);
}
}
endTime = (int) System.currentTimeMillis();
System.out.println(" fill test, elapsed time, array = " + (endTime-startTime) + " milliseconds.");
} // fillTest
// --------------------
// addTest method: compare times to add up an ArrayList and an array
public void addTest() {
int total;
int i, j;
// (re)fill ArrayList
startTime = (int) System.currentTimeMillis();
for (i = 0; i < SIZE; i++){
list.add(rand.nextInt(DATA_LIMIT));
}
// sum ArrayList
total = 0;
startTime = (int) System.currentTimeMillis();
for (i = 1; i <= SIZE_MULTIPLIER; i++) {
for (j = 0; j < SIZE; j++){
total = total + Integer.parseInt(list.get(j).toString());
}
}
endTime = (int) System.currentTimeMillis();
System.out.println(" add test, elapsed time, ArrayList = " + (endTime-startTime) + " milliseconds.");
// sum array
total = 0;
startTime = (int) System.currentTimeMillis();
for (i = 1; i <= SIZE_MULTIPLIER; i++) {
for (j = 0; j < SIZE; j++){
total += array[j];
}
}
endTime = (int) System.currentTimeMillis();
System.out.println(" add test, elapsed time, array = " + (endTime-startTime) + " milliseconds.");
} // addTest
// --------------------
// searchTest method: compare times to fill an ArrayList and an array
public void searchTest() {
//stub
System.out.println(">> stub: in search test");
System.out.println("Enter the number you want to search in ArrayList and Array");
Scanner reader = new Scanner(System.in); // Reading from System.in
int n = reader.nextInt();
int count = 0; // stores the number of occurance
startTime = (int) System.currentTimeMillis();
// Searching in ArrayList with Multiplier
for (int i = 1; i <= SIZE_MULTIPLIER; i++) {
for (int j = 0; j < SIZE; j++){
int data = Integer.parseInt(list.get(j).toString());
if(n==data){
count++;
}
}
}
endTime = (int) System.currentTimeMillis();
System.out.println(" search test, element " + n + " is found " + count + " times and elapsed time, Array List = " + (endTime-startTime) + " milliseconds.");
// Now count the occurance in Array
count = 0;
startTime = (int) System.currentTimeMillis();
// Searching in ArrayList with Multiplier
for (int i = 1; i <= SIZE_MULTIPLIER; i++) {
for (int j = 0; j < SIZE; j++){
int data = array[j];
if(n==data){
count++;
}
}
}
endTime = (int) System.currentTimeMillis();
System.out.println(" search test, element " + n + " is found " + count + " times and elapsed time, array = " + (endTime-startTime) + " milliseconds.");
}
// --------------------
// doubleTest method: compare times to double all values in an ArrayList and an array
public void doubleTest() {
//stub
System.out.println(">> stub: in double test");
startTime = (int) System.currentTimeMillis();
// Doubling the Value with Multiplier
for (int i = 1; i <= SIZE_MULTIPLIER; i++) {
for (int j = 0; j < SIZE; j++){
int data = Integer.parseInt(list.get(j).toString());
list.set(j, 2*data);
}
}
endTime = (int) System.currentTimeMillis();
System.out.println(" double test, elapsed time, Array List = " + (endTime-startTime) + " milliseconds.");
// For Array
startTime = (int) System.currentTimeMillis();
// Doubling the Value with Multiplier
for (int i = 1; i <= SIZE_MULTIPLIER; i++) {
for (int j = 0; j < SIZE; j++){
int data = array[j];
array[j] = 2*data;
}
}
endTime = (int) System.currentTimeMillis();
System.out.println(" double test, elapsed time, array = " + (endTime-startTime) + " milliseconds.");
}
// double test
// --------------------
public static void main (String args[]) {
ArrayListArrayExercise timeTest = new ArrayListArrayExercise();
timeTest.tester();
} // main
} // ArrayListArrayExercise
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.