1 Create a ThreeSum array method. in java and add comments, please
a - Call for sizes from 1000 to max size incrementing by 1000. Show execution time and result.
b- method ( Consistency analysis of the ThreeSum method. )For a given size repeat the computation a number of time. Show all results.
c- Analysis with repeats and increasing sizes.
Answer:
Note: For the three sum method it is not specified whether sum value must be 0 or any other value and also it is not specified the which type of integer values need to be stored in the array.
So, the below program is designed in such a way that sum value and range of values to be inserted into array are left to user. So, the user will be prompted for the values, depending the user choice, the program will compute the values of analysis.
Program code screen shot:







Sample Output:


Note: If need to perform the repeat operation on same size but with different array values, the place the arrayValues = generateRandomValues(miniRange, maxRange, size);
in the for loop while performing the repeat analysis.
Program Code to Copy:
// ThreeSum.java
import java.util.Scanner;
public class ThreeSum
{
// Define the variables as static
static int miniRange, maxRange, size, sum = 0;
static long startTime, endTime, elapsedTime;
static int startRange = 1000;
static int endRange = 0;
static Scanner input = new Scanner(System.in);
static int numTimes = 0;
static int arrayValues[] = null;
// define a static method to generate random
values
// of the given range and size
public static int[] generateRandomValues(int min, int
max, int size)
{
// initialize the array
int arrayVals[] = new
int[size];
// loop to populate the
array
for (int i = 0; i < size;
i++)
{
// generate
random number and populate
arrayVals[i] =
(int) (Math.random() * max) + min;
}
// return the new array
return arrayVals;
}
// define a static method to compute the threeSum
and
// return the count of how many sum of the three
values
// have been equivalent to the given sum
public static int threeSumMethod(int array[], int
sum)
{
// store the length of the
array
int arrayLength = array.length;
// initialize the counter
int count = 0;
// loop to go through all the
elements
for (int i = 0; i < arrayLength;
i++)
{
// loop to go
through i+1 to rest of the elements
for (int j = i +
1; j < arrayLength; j++)
{
// loop to go through (i+1)+1 = j + 1
// to rest of the elements
for (int k = j + 1; k < arrayLength;
k++)
{
// condition to check the sum
of the array
// values at i, j and k are
equivalent to
// sum
if (array[i] + array[j] +
array[k] == sum)
{
//
increment the count
count++;
}
}
}
}
// return the count value
return count;
}
// define a static method to do the analysis on the
three sum
// on the given array
public static void computeAnalysis_EachSize(int[]
array, int sum)
{
// get the start time
long startTime =
System.currentTimeMillis();
// call the threeSumMethod
int value = threeSumMethod(array,
sum);
// get the end time
long endTime =
System.currentTimeMillis();
// compute the elapsed
time
long elapsedTime = endTime -
startTime;
// display the size of array,
time taken and count value
System.out.printf("%8d %15d
%18d\n", array.length, elapsedTime, +value);
}
// define a static method to compute the
repetitions analysis
public static void computeRepetitions(int arrayLength,
int sum,
int
numTimes)
{
// initialize the array
arrayValues = new
int[arrayLength];
// generate the random
values
arrayValues =
generateRandomValues(miniRange, maxRange, size);
// compute the three sum
analysis for numTimes
for (int i = 0; i < numTimes;
i++)
{
computeAnalysis_EachSize(arrayValues, sum);
}
}
// define a static method to compute the analysis
on different
// sizes of array
public static void computeAnalysis()
{
// loop the array from
startingRange 1000 to maximumRange
// by incrementing the iteration
value by 1000
for (int i = startRange; i <=
endRange; i = i + 1000)
{
// set the size
value with i
size = i;
// initialize
the array
arrayValues =
new int[size];
// generate
the random numbers
arrayValues =
generateRandomValues(miniRange, maxRange, size);
// call the
computeAnalysis_EachSize() to analyze the
// ThreeSum
method on given array size and sum value
computeAnalysis_EachSize(arrayValues, sum);
}
}
// define a static method to compute repeats and
compute the
// analysis
public static void repeatsComputeAnalysis(int
numberTimes)
{
for (int i = startRange; i <=
endRange; i = i + 1000)
{
// set the size
value with i
size = i;
// initialize
the array
arrayValues =
new int[size];
// generate
the random numbers
arrayValues =
generateRandomValues(miniRange, maxRange, size);
// call the
cmputeRepetitions()
computeRepetitions(size, sum, numberTimes);
System.out.println();
}
}
// define a static method to retrieve values from
the user
// while performing the Analysis for different array
sizes
public static void getComputeAnalysisValues()
{
// prompt and read the maximum
range of array size to be looped
System.out.print("Enter the maximum
range to execute the loop: ");
endRange = input.nextInt();
// prompt and read the maximum
and minimum range of the values
// to be store din array
System.out.println(
"\nEnter elements range to be inserted into the
array: ");
System.out.print("\tMinimum value:
");
miniRange = input.nextInt();
System.out.print("\tMaximum value:
");
maxRange = input.nextInt();
// prompt and read the sum value
to be checked in three sum method
System.out.print("Enter the sum
value: ");
sum = input.nextInt();
}
// define a static method to retrieve values from
the user
// while performing analysis of repetitions
public static void getRepeatAnalysisValues()
{
// prompt for the size of
array
System.out.print("\nEnter the size
of array: ");
size = input.nextInt();
// prompt for number of times to
be repeated
System.out.print("Number of times
to repeat the loop: ");
numTimes = input.nextInt();
// prompt and read the sum value
to be checked in three sum method
System.out.print("Enter the sum
value: ");
sum = input.nextInt();
}
// main method of execution
public static void main(String args[])
{
// steps to perform the part
a
System.out.println("\t\t-----------Part a -----------\n");
// get the information to
perform the analysis of three sum
// of different sizes
getComputeAnalysisValues();
// display the header
System.out.println("\nSize of Array
\t Time Taken \t Count Value");
// call the method
computeAnalysis()
computeAnalysis();
// steps to perform the part
b
System.out.println("\n\t\t-----------Part b -----------");
// get the information to
perform the repetitions of three sum
// on single size
getRepeatAnalysisValues();
// display the header
System.out.println("\nSize of Array
\t Time Taken \t Count Value");
// call the method
computeRepetitions()
computeRepetitions(size, sum,
numTimes);
// steps to perform the part
c
System.out.println("\n\t\t-----------Part c -----------\n");
// get the information to
perform the analysis of three sum
// of different sizes
getComputeAnalysisValues();
// prompt for number of times to
be repeated for each size
// of array
System.out.print("Number of times
to repeat the loop: ");
numTimes = input.nextInt();
// display the header
System.out.println("\nSize of Array
\t Time Taken \t Count Value");
// call the method
repeatsComputeAnalysis()
repeatsComputeAnalysis(numTimes);
}
}
1 Create a ThreeSum array method. in java and add comments, please a - Call for...
PLEASE INCLUDE COMMENTS In java Create a Java Program Add the following comments at the beginning of the file: Your name. The name of the class(es) used in the program. The core concept (found below) for this lesson. The date the program was written. Include a recursive method separate from the main method that will add together all of the even numbers between and including 1 and the value the user supplies. For instance, if the user enters 10 then...
JAVA / please add comments Write code for a method named loadDatagrams() that returns an array of type Datagram. The method will read from the filepath C:/folder/grams.dgf and the file contains an array of type Datagram ( you may assume that the class definition is visible to the method) Remember to include appropriate exception handling
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 please Create a command line program Add an array that stores 20 integers Fill the array with random integers between 1 and 1000. Display the values in the array to stdout (the screen). Include a message explaining what is being displayed. Save the values in the array to a text file. Read the values stored in the text file to stdout (the screen). Include a message explaining what is being displayed.
***Please give the java code for the below and add comments for different areas for a dummy to understand*** This java program will combine the techniques of handling arrays, decision control statements, and loops. Your program should accomplish the following: Build an array that holds characters (size 35) Load the array with random capital letters (use random generator) Your program should present a menu to the user allowing for the following “actions”. To “search” for a target letter of the...
Please do in Java with the code available for copy :) Write a computer program that prompts the user for one number, n for the number of items in the array to sort, and create and sort 1000 different arrays of this size timing the run to get an average time to sort an array of this size. Then do the following: Initiate a variable running_time to 0 Create a for loop that iterates 1000 times. In the body of...
PLEASE DO BOTH #5 AND #6. The purpose of the project is to perform a timing experiment. You are required to complete the following activities: Write a computer program that prompts the user for a number, creates an array for that number of random integers, and then usees the bubble sort to order the array. The program should print out the array prior to the call to the sorting algorithm and afterwards. You can write the program in either Java,...
The purpose of the project is to perform a timing experiment. You are required to complete the following activities: Write a computer program that prompts the user for a number, creates an array for that number of random integers, and then usees the bubble sort to order the array. The program should print out the array prior to the call to the sorting algorithm and afterwards. You can write the program in either Java, C++, C#, or whatever language you...
in java please withe comments & check output
Write a program that contains: Client Thread class that send full operation ("1+2", "1-2", "1*2", " 1/2") to CalculaterServer Server Thread class named CalculaterServer, the server create thread to receives the request and decode the operation and call the appropriate method (add: sub, mul div) which are in class named Calculator to do the operation, and then sends back the result to client. Client show the following window: After choosing operation ask...
Please include comments in java Create a command line program that can be used for computer inventory purposes Create a super class that will store the following data: CPU speed Amount of RAM Hard Disk size Operating System Allow the user to enter data using any units of measurement they desire (i.e. - 500GB or 1TB for hard disk size). Create a class that inherits from the previous class and stores information for a Windows based computer. Store the following...