Efficiency of Array vs. ArrayList-- Use JAVA to implement the operations
Using the list operations fill, increment, and search, investigate whether arrays or ArrayLists are faster, or whether they are about the same for int and float values. This will also test times to generate both int and float random numbers as well as the time cost of automatic expansion of an ArrayList. Remember: int and float are used in simple arrays, Integer and Float are used in the ArrayList. All results will be written to a single file named P3Output.txt (Hint – open the file for append)
The program has one class. Main calls methods for each of the operations (fill and increment ). The methods each do their own timing and output.
Input / output
This program has no input. Output goes to the file, you may also wish to display it on the screen for convenience. The numeric values must be formatted and the times must be in the same scale.
Output format as below:
int array – wall clock : xxx.xxxx seconds
int ArrayList – wall clock : xxx.xxxx seconds
int array – CPU time : xxx.xxxx seconds
int ArrayList – CPU time : xxx.xxxx seconds
float array – wall clock : xxx.xxxx seconds
float ArrayList – wall clock : xxx.xxxx seconds
float array – CPU time : xxx.xxxx seconds
float ArrayList – CPU time : xxx.xxxx seconds
Increment elements in the list:
int array – wall clock : xxx.xxxx seconds
int ArrayList – wall clock : xxx.xxxx seconds
int array – CPU time : xxx.xxxx seconds
int ArrayList – CPU time : xxx.xxxx seconds
float array – wall clock : xxx.xxxx seconds
float ArrayList – wall clock : xxx.xxxx seconds
float array – CPU time : xxx.xxxx seconds
float ArrayList – CPU time : xxx.xxxx seconds
import java.util.*;
import java.nio.file.*;
import java.io.*;
import java.text.*;
import java.util.concurrent.TimeUnit;
public class P3Program
{
Random randomGenerator;
DecimalFormat formatter = new
DecimalFormat("000.000000000");
String fileName;
String outputString;
public static final int COLLECTION_SIZE =
100000;
public static final int RANDOM_MAX =
10000000;
public static final String LS =
System.lineSeparator();
static int trials;
ArrayList<Float> floatList;
ArrayList<Integer> intList;
int intArray[];
float floatArray[];
double startCPUTime;
double stopCPUTime;
double startWallClockTime;
double stopWallClockTime;
/**
* Instantiate all the data structures
we'll be testing.
*/
public P3Program(){
intArray = new
int[COLLECTION_SIZE];
floatArray = new
float[COLLECTION_SIZE];
floatList = new
ArrayList(COLLECTION_SIZE);
intList = new
ArrayList(COLLECTION_SIZE);
randomGenerator = new
Random();
fileName =
"P3Output.txt";
trials = 0;
}
/**
* Fills both the ArrayList and the array
with values.
*/
private void fill(){
System.out.println("++++++++FILL+++++++");
startWallClockTime =
System.currentTimeMillis();
startCPUTime =
System.nanoTime();
for(int i = 0; i <
intArray.length; i++){
intArray[i] = randomGenerator.nextInt(RANDOM_MAX);
}
stopWallClockTime =
System.currentTimeMillis();
stopCPUTime =
System.nanoTime();
double intArrayWallTime
= stopWallClockTime - startWallClockTime;
double intArrayCPUTime =
stopCPUTime - startCPUTime;
startWallClockTime =
System.currentTimeMillis();
startCPUTime =
System.nanoTime();
for(int i = 0; i <
floatArray.length; i++){
floatArray[i] = randomGenerator.nextFloat();
}
stopWallClockTime =
System.currentTimeMillis();
stopCPUTime =
System.nanoTime();
double
floatArrayWallTime = stopWallClockTime - startWallClockTime;
double floatArrayCPUTime
= stopCPUTime - startCPUTime;
startWallClockTime =
System.currentTimeMillis();
startCPUTime =
System.nanoTime();
for(int i = 0; i <
COLLECTION_SIZE; i++){
intList.add(i, randomGenerator.nextInt(RANDOM_MAX));
}
stopWallClockTime =
System.currentTimeMillis();
stopCPUTime =
System.nanoTime();
double intListWallTime =
stopWallClockTime - startWallClockTime;
double intListCPUTime =
stopCPUTime - startCPUTime;
startWallClockTime =
System.currentTimeMillis();
startCPUTime =
System.nanoTime();
for(int i = 0; i <
COLLECTION_SIZE; i++){
floatList.add(i, randomGenerator.nextFloat());
}
stopWallClockTime =
System.currentTimeMillis();
stopCPUTime =
System.nanoTime();
double floatListWallTime
= stopWallClockTime - startWallClockTime;
double floatListCPUTime
= stopCPUTime - startCPUTime;
outputString = "Run #
" + trials + LS + "Fill the list: Number of Elements: " +
COLLECTION_SIZE +
LS + " int array - wall
clock : " +
formatter.format(intArrayWallTime/1000) +
LS + " int ArrayList - wall
clock : " +
formatter.format(intListWallTime/1000) +
LS + LS + " int array - CPU
time
: " + formatter.format(intArrayCPUTime/1000000000) +
LS + " int ArrayList - CPU
time : " +
formatter.format(intListCPUTime/1000000000) +
LS + LS + " float array - wall
clock : " +
formatter.format(floatArrayWallTime/1000) +
LS + " float ArrayList - wall clock :
" + formatter.format(floatListWallTime/1000) +
LS + LS + " float array - CPU
time : " +
formatter.format(floatArrayCPUTime/1000000000) +
LS + " float ArrayList - CPU
time : " +
formatter.format(floatListCPUTime/1000000000) + LS + LS;
System.out.println(outputString);
writeReport(outputString);
}
/**
* Increments every element in each data
structure by 1.
*/
private void increment(){
System.out.println("++++++INCREMENT+++++++");
startWallClockTime =
System.currentTimeMillis();
startCPUTime =
System.nanoTime();
for(int i = 0; i <
intArray.length; i++){
intArray[i]++;
}
stopWallClockTime =
System.currentTimeMillis();
stopCPUTime =
System.nanoTime();
double intArrayWallTime
= stopWallClockTime - startWallClockTime;
double intArrayCPUTime =
stopCPUTime - startCPUTime;
startWallClockTime =
System.currentTimeMillis();
startCPUTime =
System.nanoTime();
for(int i = 0; i <
floatArray.length; i++){
floatArray[i]++;
}
stopWallClockTime =
System.currentTimeMillis();
stopCPUTime =
System.nanoTime();
double
floatArrayWallTime = stopWallClockTime - startWallClockTime;
double floatArrayCPUTime
= stopCPUTime - startCPUTime;
startWallClockTime =
System.currentTimeMillis();
startCPUTime =
System.nanoTime();
for(int i = 0; i <
COLLECTION_SIZE; i++){
int incremented = intList.get(i) + 1;
intList.set(i, incremented);
}
stopWallClockTime =
System.currentTimeMillis();
stopCPUTime =
System.nanoTime();
double intListWallTime =
stopWallClockTime - startWallClockTime;
double intListCPUTime =
stopCPUTime - startCPUTime;
startWallClockTime =
System.currentTimeMillis();
startCPUTime =
System.nanoTime();
for(int i = 0; i <
COLLECTION_SIZE; i++){
float incremented = floatList.get(i) + 1;
floatList.set(i, incremented);
}
stopWallClockTime =
System.currentTimeMillis();
stopCPUTime =
System.nanoTime();
double floatListWallTime
= stopWallClockTime - startWallClockTime;
double floatListCPUTime
= stopCPUTime - startCPUTime;
outputString =
"Increment the elements in the list: " +
LS + " int array - wall
clock : " +
formatter.format(intArrayWallTime/1000) +
LS + " int ArrayList - wall
clock : " +
formatter.format(intListWallTime/1000) +
LS + LS + " int array - CPU
time
: " + formatter.format(intArrayCPUTime/1000000000) +
LS + " int ArrayList - CPU
time : " +
formatter.format(intListCPUTime/1000000000) +
LS + LS + " float array - wall
clock : " +
formatter.format(floatArrayWallTime/1000) +
LS + " float ArrayList - wall clock :
" + formatter.format(floatListWallTime/1000) +
LS + LS + " float array - CPU
time : " +
formatter.format(floatArrayCPUTime/1000000000) +
LS + " float ArrayList - CPU
time : " +
formatter.format(floatListCPUTime/1000000000) + LS + LS;
System.out.println(outputString);
writeReport(outputString);
intList.clear();
floatList.clear();
}
/**
* Writes the report with the data from the
trial
*/
private void writeReport(String
reportString){
try{
BufferedWriter writer = new BufferedWriter(new FileWriter(new
File(fileName), true));
writer.write(reportString);
writer.close();
}catch(Exception
ex){
System.out.println("try a different file name");
}
}
/**
* The main method that calls all the
methods from the class.
*/
public static void main(String args[]){
P3Program arrayTest =
new P3Program();
while(trials <=
3){
trials++;
arrayTest.fill();
arrayTest.increment();
}
}
}
![P3Program - [CAUsers SwapninP3Program] - P3Program.java - Intelli IDEA 2017.2.5 Eile Edit View Navigate Code Analyze Rcfactor](http://img.homeworklib.com/questions/c4f8c2f0-c1be-11ea-a6c9-d1b5f2d20e2c.png?x-oss-process=image/resize,w_560)
Efficiency of Array vs. ArrayList-- Use JAVA to implement the operations Using the list operations fill,...
PART 1 Modify the class ArrayList given in Exercise 1 by using expandable arrays. That is, if the list is full when an item is being added to this list, the elements will be moved to a larger array. The new array should have twice the size of the original array. Using the new class ArrayList, write a program to store 1,000 random numbers, each in the interval [0, 500]. The initial size of the array in the class should...
Implement the Stack Class with an ArrayList instead of an array, including the following functions: -empty -push -peek -pop -overrided toString() function which returns all of the stack's contents Things to note: -You no longer need a size. -You no longer need to define a constant DEFAULT_CAPACITY. since ArrayLists grow dynamically. -Whenever possible, use ArrayList functions instead of the [ ] (index operator) to implement your stack functions Then write a driver program to do palindrome check. A string is...
In Java(using BlueJ) Purpose Purpose is to practice using file input and output, and array list of objects. Also, this lab specification tells you only what to do, you now have more responsibility to design how to do it. Problem description You are given a text file called 'Students.txt' that contains information on many students. Your program reads the file, creating many Student objects, all of which will be stored into an array list of Student objects, in the Students...
I should use the array and loop to create a java program
according to the instruction, but I have no idea how to do
it.
Introduction This lab assignment continues to give you practice using loops, particularly loops with variable termination conditions, and it also provides you an opportunity to use one-dimensional arrays. Recall that an array is used to store a collection of data. The data can be values of Java primitive data types or else objects (for instance,...
Write a java program: Create a method fillRandom() that accepts an array of int as input and populates it with random numbers in the range -999 to 1000 Explicitly store zero in index [0] and 900 in index [1]. (0 and 900 will be used as search keys) Create a method DisplayLastInts() that accepts an array of int as input and displays the last hundred elements to the screen in rows of 10 elements. Format the output so the 10...
In Java, Implement a class MyArray as defined below, to store an array of integers (int). Many of its methods will be implemented using the principle of recursion. Users can create an object by default, in which case, the array should contain enough space to store 10 integer values. Obviously, the user can specify the size of the array s/he requires. Users may choose the third way of creating an object of type MyArray by making a copy of another...
Using C++, sort an array of 10,000 elements using the quick sort algorithm as follows: a. Sort the array using pivot as the middle element of the array. b. Sort the array using pivot as the median of the first, last, and middle elements of the array. c. Sort the array using pivot as the middle element of the array. However, when the size of any sublist reduces to less than 20, sort thesublis t using an insertion sort. d....
Implement and compare sorting algorithms. The task is to sort a list of integers using 5 sorting algorithms: selection sort insertion sort merge sort heap sort quicksort Your program should include 5 separate sorting methods, though it is fine for them to call some common methods (like "swap") if needed. Each sorting method should also count the number of comparison operations and assignment operations on the array elements during the sorting process. In the main program, two types of array...
IN JAVA please Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. Your code will be tested for runtime. Code which does not output a result in logarithmic time (making roughly log(2) N comparisons) will fail the tests. A sample main function is provided so that you may test your code on sample inputs. For testing purposes, the...
Must use JOPTIONPANE. Using arrays and methods create a JAVA program. Create a java program that holds an array for the integer values 1-10. Pass the array to a method that will calculate the values to the power of 2 of each element in the array. Then return the list of calculated values back to the main method and print the values