Step 0 - Understanding your objective
This lab will introduce you to programming with arrays. The program for this lab has four methods besides main(), each has an array as a parameter and determines something about that array, or maipulates the array in some way.
After completing this lab, you should be familiar with the following:
Declaring and initializing an array given a specific Java data type. Using a for loop to iterate through an array and access its elements.
What You’ll Learn
- Declaring and initializing arrays
- Accessing array elements
- More method practice
Step 1 - completing removeThe()
This method searches through an array and only adds Strings that do not contain the word “the” in them to a new array.
Write method signature
This is a public static method that returns a String array and is called removeThe. It has one parameter that is a String array.
Create method variables
You need to create two variables. The first variable is a String Array of size five that will be returned at the end of the method. The secound variable is an int that represents the index of the array you just created.
Removing The
After you have declare your variables, you want to write a for loop iterate over the array from the parameter. Inside the for loop you want to check to see if the String at the current index in the array does not contain the word “the”. If they do not add them to the new array at the index of your variable you created. After that increment your index variable. At the end of your method return the array you have finished initializing.
Test your method removeThe()
Once you have finished writing your method, go down to the main method and uncomment the println that is associated with removeThe(). Your output should look like this:
New Array: [none, quokka, fish, tree, snake]
Integer array after swap:
Step 2 - Completing threes()
This method returns true or false depending on if there are two threes next to each other in an array of integers.
Write Method Signature
This method is a public static method that returns a boolean. It is called threes and has one parameter that is an array of ints.
Looking for threes
First create a loop to loop over the array from the parameter. Inside the loop check to see if the element at the current value is equal to 3 and if i is not equal to the length of the array. Then inside that if statement check to see if the element after the current element is equal to 3 and if that is true return true. At the end of the method return false.
Test your method threes()
Once you have finished writing your method, go down to the main method and uncomment the println that is associated with threes(). Your output should look like this:
New Array: [none, quokka, fish, tree, snake]
false
true
Integer array after swap:
Step 3 - Completing findMin()
This method determines the location (i.e. index) of the minimum integer in an array of integers. Then it swaps the minimum element with the first element of the array.
Write Method Signature
This method is a public static method that returns an int. It is called findMin and takes in one parameter that is an array of ints.
Create method variables
Declare two integers to represent the following: the value of the minimum element of the array and the minimum element’s index/position in the array. Assign the first integer to the value of Integer.MAX_VALUE so that we make sure that the min element will get updated by a value in the array. The second integer will be assigned to negative one because we haven’t found a min element yet so we give it an impossible position for now (it will get updated as soon as we find the first min element).
Search for the minimum
Write a for loop to determine the position and the value of the minimum element in the array. You will need to include a conditional statement in your for loop to determine if we found a smaller value than our current min value we just created. Inside that conditional, update the minimum value and minimum value’s position appropriately. Return the minimum position.
Hint: this method is similar to the one you wrote in lab 16.
Test your method findMin()
Before you move on to the next method, uncomment the println that says “Minimum element index…” and run the program to check if the output matches the one below:
New Array: [none, quokka, fish, tree, snake]
false
true
Minimum element index: 6
Integer array after swap:
Step 4 - swapMin()
This method swaps the minimum value in the array with the value at first position. This method does not return anything since arrays are passed by reference.
Write the method declaration
First, write the method declaration. This is a public static method that returns nothing. It is called swapMin and has one parameter, an int array.
Create method variables
Create two integer variables, one to represent the position of the minimum element in the array and the other a temp/placeholder to store a value of an element from the array. Assign the first integer to the method call of findMin() with arr as the argument and the second integer to the value from the array at position 0.
Find minimum
Write a conditional statement to determine if we didn’t find a min from findMin() (aka if the position equals -1). If so, just return since we don’t need to swap. Otherwise, swap the value at the min position with the value at position 0.
Swap elements
To swap use the temp variable you created in step 2 to store the value of the element at position 0. Then, reassign the value of the element at position 0 the value of the element at the minimum element’s position. Finally, reassign the value of the element at the position of the minimum element to the temp variable.
Test your method swapMin()
You have completed writing the method at this step but now you must go to the second TODO section in main() and write a for loop to print out each of the elements from iArray to see if it swapped properly. When you print in the for loop, the format should be the values of the elements separated by a space.
Running a completed ArrayLab.java without any modification to array elements in main() should result in the following printed in the zyBooks console:
New Array: [none, quokka, fish, tree, snake]
false
true
Minimum element index: 6
Array swapped with minimum element: -11 4 -7 8 10 12 1