Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

To help you review what arrays are and how to look at them in this course, here is a summary application. Pay the most attention

To help you review what arrays are and how to look at them in this course, here is a summary application. Pay the most attention to the performance testing. You guys need to think about and test your assignments and/or code with not just a few values, but millions of values in order to see any performance differences for algorithms. This course is not a Java course, but a course that investigates data structures for programming based problem solving by learning about structures and their performance differences. Run this code and talk about what did you notice in performance differences.

public class ArraySummary_PerformanceTest{ public static void main(String args[])throws java.io.FileNotFoundException{ int me[]; me=new int[5]; int you[]={1,2,3,4,5}; System.out.println(you.length); //System.out.println(you.length()); - error int us[]=new int[5]; for(int temp:you) System.out.println(temp); for(int i=0;i highest) highest = copyArray[i]; } //Finding the Lowest Value: int lowest = copyArray[0]; for (int i = 1; i < copyArray.length; i++){ if (copyArray[i] < lowest) lowest = copyArray[i]; } //Summing Array Elements: int total = 0; // Initialize accumulator for (int i = 0; i < copyArray.length; i++) total += copyArray[i]; //Averaging Array Elements: double total2 = 0; // Initialize accumulator double average; // Will hold the average for (int i = 0; i < copyArray.length; i++) total += copyArray[i]; average = total / copyArray.length; int[] partialArray = new int[100]; int count = 0; System.out.print("Enter a number or -1 to quit: "); int number = keyboard.nextInt(); while (number != -1 && count <= 99) { partialArray[count] = number; count++; System.out.print("Enter a number or -1 to quit: "); number = keyboard.nextInt(); } java.io.PrintWriter outputFile = new java.io.PrintWriter("Values.txt"); for (int i = 0; i < copyArray.length; i++) outputFile.println(copyArray[i]); outputFile.close(); final int SIZE = 5; // Assuming we know the size. int[] numbers = new int[SIZE]; int i = 0; java.io.File file = new java.io.File ("Values.txt"); java.util.Scanner inputFile = new java.util.Scanner(file); while (inputFile.hasNext() && i < numbers.length){ numbers[i] = inputFile.nextInt(); i++; } inputFile.close(); double myArray[]=getArray(); String[] names = { "Bill", "Susan", "Steven", "Jean" }; String[] names2 = new String[4]; names2[0] = new String("Bill"); names2[1] = new String("Susan"); names2[2] = new String("Steven"); names2[3] = new String("Jean"); System.out.println(names2[0].toUpperCase()); char letter = names[3].charAt(0); for (int index = 0; index < names.length; index++) System.out.println(names[index].length()); String[] names3 = new String[4]; for (int index = 0; index < names3.length; index++) names3[index] = new String(); int result=sequentialSearch(copyArray,3); final int TESTARRAY=10000000; int testArray[]=new int[TESTARRAY]; long time=System.currentTimeMillis(); for(int index=0;index nameList = new java.util.ArrayList(); nameList.add("James"); nameList.add("Catherine"); nameList.size(); nameList.get(1); // Display the items in the ArrayList. for (String name : nameList) System.out.println(name); // The ArrayList class's toString method returns a string representing all items in the ArrayList // This statement yields : // [ James, Catherine ] System.out.println(nameList); nameList.remove(1); //To insert items at a location of choice, use the add method with two arguments: nameList.add(1, "Mary"); //To replace an existing item, use the set method: nameList.set(1, "Becky"); java.util.ArrayList list = new java.util.ArrayList(10); list.add(0, "Mary2"); list.add(0, "Mary1"); list.add(0, "Mary0"); System.out.println(); } private static void showArray(int[] array) { array[2]=100; System.out.println(array.toString()); for (int i = 0; i < array.length; i++) System.out.print(array[i] + " "); System.out.println(); } private static double[] getArray(){ double[] array = { 1.2, 2.3, 4.5, 6.7, 8.9 }; return array; } private static int sequentialSearch(int[] array, int value){ int index,element; boolean found; // Flag indicating search results index = 0; element = -1; found = false; while (!found && index < array.length) { if (array[index] == value) { found = true; // Indicate the value is found. element = index; // Save the subscript of the value. } index++; } return element; } private static void selectionSort(int[] array){ int startScan, index, minIndex, minValue; for (startScan = 0; startScan < (array.length-1); startScan++) { minIndex = startScan; minValue = array[startScan]; for(index = startScan + 1; index < array.length; index++) { if (array[index] < minValue) { minValue = array[index]; minIndex = index; } } array[minIndex] = array[startScan]; array[startScan] = minValue; } } private static int binarySearch(int[] array, int value){ int first,last,middle,position; boolean found; // Flag first = 0; last = array.length - 1; position = -1; found = false; while (!found && first <= last) { middle = (first + last) / 2; // Calculate mid point if (array[middle] == value) { found = true; position = middle; } else if (array[middle] > value) // If value is in lower half last = middle - 1; else first = middle + 1; // If value is in upper half } return position; } private static void showArray(int[][] array) { for (int row = 0; row < array.length; row++) { for (int col = 0; col < array[row].length; col++) System.out.print(array[row][col] + " "); System.out.println(); } } private static int arraySum(int[][] array){ int total = 0; // Accumulator for (int row = 0; row < array.length; row++){ for (int col = 0; col < array[row].length; col++) total += array[row][col]; } return total; } //https://docs.oracle.com/javase/1.5.0/docs/guide/language/varargs.html private static int sum(int... numbers){ int total = 0; // Accumulator for (int val : numbers) total += val; return total; } }

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_2

Step: 3

blur-text-image_3

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

Transact SQL Cookbook Help For Database Programmers

Authors: Ales Spetic, Jonathan Gennick

1st Edition

1565927567, 978-1565927568

More Books

Students also viewed these Databases questions

Question

What are the Five Phases of SDLC? Explain each briefly.

Answered: 1 week ago

Question

How can Change Control Procedures manage Project Creep?

Answered: 1 week ago