Introduction
In this lab you will learn more about how arrays work through manipulating them in several different ways. There are five methods you need to complete, they do not have anything to do with each other but they should help increase your understanding of arrays. For more information about arrays click here or refer to the zyBooks reading.
Step 1 - Writing maxValue
This method loops through an array of ints and returns the largest number from the provided array.
To begin you must write the method signature. This method is a public static method that returns an int. It is called maxValue and takes in one parameter that is an array of ints.
Inside the method you need to declare an int variable that will contain the max value to return at the end of your method.
Next you must loop through the array from the parameter. Inside your for loop compare the value at current index in the array to the you variable declared before your for loop and if the value at the current index in the array is greater than the variable you created than reassign your variable to be equal to the value at the current index in the array.
Testing maxValue
Copy and paste the following code into the main method to see if your method is returning the largest value in the array.
//Testing maxValue
int[] iArr1 = {7, 39, 29, 78, 23, 9};
int[] iArr2 = {239, 394, 293, 930, 494};
int[] iArr3 = {1, 3, 89, 30, 40};
System.out.println(maxValue(iArr1)); //should return 78
System.out.println(maxValue(iArr2)); //should return 930
System.out.println(maxValue(iArr3)); //should return 89
Step 2 - Writing average
In this method you are going to calculate the average of the values from an array of doubles.
You must first write the method signature. The method is a public static method that returns a double. It is called average and takes in a parameter that is an array of doubles.
Inside the method you must first create a variable of type double that will contain the sum of all the numbers in the array.
Next loop through the array from the parameter using a for loop. Inside the for loop add all the numbers from the array to the variable you created.
Outside of the for loop, divide the variable that contains the sum of all the numbers from the array and divide it by the length of the array. Then return the answer from that calculation.
Testing Average
Copy and paste the following code into the main method to see if your method is returning the average of the elements in the array.
//Testing average
double[] dArr1 = {1, 2, 3, 4, 5, 6, 7, 8, 9};
double[] dArr2 = {29, 36, 32, 45, 59};
double[] dArr3 = {78, 290, 38, 2, 294, 33};
System.out.println(average(dArr1)); //should return 5.0
System.out.println(average(dArr2)); //should return 40.2
System.out.println(average(dArr3)); //should return 122.5
Step 3 - Writing doubleValues
This method takes an array of doubles and multiplies every odd index in the array by 2.
You must first write the method signature. It is public static method that returns an array of doubles. The method is called doubleValues and takes in one parameter that is an array of doubles.
Inside the method you must use a for loop to loop through the array from the parameter. Inside the for loop you must check to see if the index is is not divisible by 2 and if so multiply the value at that index by 2 and store it back into the array. Outside of the for loop return the array.
Testing doubleValues
Copy and paste the following code into the main method to see if all the values at every odd index is doubled. Use the arrays declared above from testing average.
//Testing doubleValues
System.out.println(Arrays.toString(doubleValues(dArr1))); //should return [1.0, 4.0, 3.0, 8.0, 5.0, 12.0, 7.0, 16.0, 9.0]
System.out.println(Arrays.toString(doubleValues(dArr2))); //should return [29.0, 72.0, 32.0, 90.0, 59.0]
System.out.println(Arrays.toString(doubleValues(dArr3))); //should return [78.0, 580.0, 38.0, 4.0, 294.0, 66.0]
Step 4 - Writing reverseString
This method takes an array of Strings and reverses each String in the array then adds them to a new array with that contains the all the reversed Strings from the original array.
You must first write the method signature. It is public static method that returns an array of Strings. It is called reverseString and has one parameter that is an array of Strings.
Inside the method you must first create an empty array of Strings with the same size as the the array from the parameter. Then you must declare a String that will contain the reversed version of each String from the String array from the parameter.
Next loop through the String array from the parameter. Inside that loop create another for loop that will loop through each String in the array in reverse. For the inner for loop you will start at the end of the String in the array and go until the index reaches 0 decrementing by 1 every time.
Inside the innermost for loop concatenate each character from the String onto the String you created at the beginning of your method.
Outside of the innermost for loop but inside the outermost for loop assign the newly created String to the String array you created at the current index in the outermost for loop. Than reset your String that contains the reversed String to an empty String.
At the end of the method return the Array that contains all the reversed Strings.
Testing reverseString
Copy and paste the following code into the main method to make sure your method returns all the Strings in reverse.
//Testing reverseString
String[] sArr1 = {"This", "is", "an", "array"};
String[] sArr2 = {"After", "all", "this", "time", "always"};
String[] sArr3 = {"quokkas", "are", "the", "best", "animal"};
System.out.println(Arrays.toString(reverseString(sArr1))); //should return [sihT, si, na, yarra]
System.out.println(Arrays.toString(reverseString(sArr2))); //should return [retfA, lla, siht, emit, syawla]
System.out.println(Arrays.toString(reverseString(sArr3))); //should return [sakkouq, era, eht, tseb, lamina]
Step 5 - Writing sort
In this method you will take an array of ints and sort the numbers from smallest to largest.
First you must write the method signature. It is a public static method that returns an array of ints. The method is called sort and takes in one parameter is an array of ints.
Inside the method you must create two int variables one represents the index of the smallest number and the other variable is your loop variable.
After that create a while loop that will continue to run while your loop variable is less than the length of the array from the parameter. Inside the while loop assign the value that represents the index of the smallest number to your loop variable.
Then create a for loop that loops over the array from the parameter. Inside the for loop check to see if the element at the current index in the array is less than the element at the index of your variable. If so set your variable equal to the index.
Outside of the for loop, create an int variable and assign it to the value of the array at the index of the smallest element. Then assign the value in the array at the index of your loop variable to at the index of the smallest element in the array. Then assign the int variable you created to the array at the index of the loop variable. Finally increment your loop variable.
Lastly return the array.
Testing sort
Copy and paste the following code into the main method to make sure your method sorts the provided array from smallest to largest. Use the arrays you copied above for testing maxValue.
//Testing sort
System.out.println(Arrays.toString(sort(iArr1))); //should return [7, 9, 23, 29, 39, 78]
System.out.println(Arrays.toString(sort(iArr2))); //should return [239, 293, 394, 494, 930]
System.out.println(Arrays.toString(sort(iArr3))); //should return [1, 3, 30, 40, 89]