Question: JAVA LANGUAGE - Skeleton for Exercise1.java is below - Comment as much as possible Exercise One: Searching, Containing and Finding Elements Download the Driver Exercise1.java.

JAVA LANGUAGE - Skeleton for Exercise1.java is below - Comment as much as possible

Exercise One: Searching, Containing and Finding Elements

Download the Driver Exercise1.java. Were going to work on a few functions here, and Ive already defined the few in question and invoked them from main. In future sections, this will be your responsibility.

Lets consider the first function, contains()

1. Define the following function in your Exercise1.java code

-a) public static boolean contains(int[] input, int target)

-b) This function takes an array and a target integer, and returns true if the integer is found in the array, false otherwise.

-c) Walk over the array looking for target; return true if found

2. Define the following function, using contains()

-a) public static int indexOf(int[] input, int target)

--i. This function takes an array as input and returns the index of the first occurrence of target in the input array, or -1 if not found

--ii. Build this indexOf() function by calling contains, which youve already made above.

3. Define the following function, using contains()

-a) public static int count(int[] input, int target)

--i. This function counts the number of occurrences of target in the input array.

---1. For example, given {1,3,4,5, 1}, the element 1 has a count of 2, whereas the element 6 has a count of 0.

-b) You should try to use contains() here in this function as well.

4. Define the following function, using count()

-a) public static void duplicates(int[]input, int[] output)

--i. This function finds all numbers that occur more than once in the input list and puts them (once) in the output list.

---1. For example, {3,3,3,4,4,5} as input produces {3,4,0,0,0,0} in output

--ii. To accomplish this, use the counts function above

---1. Use counts() to determine if, say, 3 occurs more than once in the input

----a. If it does, use counts() to determine if youve already put a 3 in the output.

Skeleton of Exercise1.java

import java.util.Arrays;

public class Exercise1 {

//Notice that in this main, each of the functions have been called for you public static void main(String[] args) { int[] data = {1,3,5,4,7,9,1,3}; int[] output = new int[data.length]; System.out.println("Does our array contain a '1':"+contains(data, 1)); //true System.out.println("Does our array contain a '0':"+contains(data, 0)); //false System.out.println("What is the index of '4'? " + indexOf(data, 4)); //3 System.out.println("The number of occurrences of '1'? " + count(data, 1)); //2 duplicates(data, output); System.out.println("After removing duplicates from data:"); System.out.println(Arrays.toString(output)); } public static boolean contains(int[] input, int target) { //todo: see lab return false; } public static int indexOf(int[] input, int target) { //todo: only find the indexOf a target if we contain() it return -1; } public static int count(int[] input, int target){ int retVal = 0; //todo: only try to count a number that we contain() return retVal; } public static void duplicates(int[] input, int[] output) { //todo: Transfer items once from the arrays:input and output. //Transfer items from input to the output array IF: // only if newArray.count(target) == 0 //ie, we haven't put this in yet // only if newArray.indexOf(target) == -1 //not found in newArray, or // only if newArray.contains(target) == false //does not exist in the new array } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!