Consider the following method, which uses iteration (a for loop) to search for an item in...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
Consider the following method, which uses iteration (a for loop) to search for an item in an array of integers. The method returns true if the item is found in the array, and false if it is not. public static boolean search(int item, int[] arr) { for (int i = 0; i< arr.length; i++) { if (arr[i] == item) { return true; } } } return false; 1. (4 points) Rewrite this method so that it searches for an item an array that can contain any type of object. Change the types of the parameters accordingly, and make whatever changes are needed to the body of the method. 2. (6 points) Rewrite your answer to part 1 so that it uses recursion instead of iteration. You will need to add a third parameter (call it start) that keeps track of where you are in the array. More precisely, start will specify the position in the array where the search for item should begin. For example, search("hello", arr, 0) should search for "hello" in the full array (beginning at position 0), whereas search("hello", arr, 2) should search for "hello" in the subarray that begins at position 2 and goes to the end of the array. Consider the following method, which uses iteration (a for loop) to search for an item in an array of integers. The method returns true if the item is found in the array, and false if it is not. public static boolean search(int item, int[] arr) { for (int i = 0; i< arr.length; i++) { if (arr[i] == item) { return true; } } } return false; 1. (4 points) Rewrite this method so that it searches for an item an array that can contain any type of object. Change the types of the parameters accordingly, and make whatever changes are needed to the body of the method. 2. (6 points) Rewrite your answer to part 1 so that it uses recursion instead of iteration. You will need to add a third parameter (call it start) that keeps track of where you are in the array. More precisely, start will specify the position in the array where the search for item should begin. For example, search("hello", arr, 0) should search for "hello" in the full array (beginning at position 0), whereas search("hello", arr, 2) should search for "hello" in the subarray that begins at position 2 and goes to the end of the array.
Expert Answer:
Related Book For
Java How To Program Late Objects Version
ISBN: 9780136123712
8th Edition
Authors: Paul Deitel, Deitel & Associates
Posted Date:
Students also viewed these programming questions
-
can someone solve this Modern workstations typically have memory systems that incorporate two or three levels of caching. Explain why they are designed like this. [4 marks] In order to investigate...
-
This question concerns lexical grammars. (a) Tree Adjoining Grammars contain two types of elementary tree. (i) What are these trees called? [1 mark] (ii) If one were building a grammar for English...
-
Consider the generalized externality problem. Assume the damage and cost functions are given by: (a) Determine the non-regulated level of E if the polluter has the right to pollute. (b) Determine the...
-
How has social media affected the way crises are managed in our society today?
-
Vega Foods, Inc., has recently purchased a small mill that it intends to operate as one of its subsidiaries. The newly acquired mill has three products that it offers for sale'wheat cereal, pancake...
-
A fellow scientist heard that a Van de Graaff generator built 70 years ago could collect \(5.0 \mathrm{C}\) of charge on its dome, which had a radius of \(1.1 \mathrm{~m}\), and has challenged you to...
-
Using the appropriate interest table, compute the present values of the following periodic amounts due at the end of the designated periods. (a) $30,000 receivable at the end of each period for 8...
-
Kericho Sports Club has been operating for some time now. The treasurer analysed the cash book and produced the following Receipts and Payments account for the year ended 31st Dec. 2018. Receipts...
-
1. How will you characterize Tupperwares distribution strategy in relation to the theoretical models? 2. What are the advantages and disadvantages of Tupperwares distribution model? 3. How do you...
-
A critical machine part in an aircraft flap actuator may be adequately modeled as a solid cylindrical bar subjected to an axial force P of 10,000 lbs, a bending moment A{; = 1500 in-lb, and a...
-
a) Create a class Circle to store the center of a circle (an object of class Point) and radius of the circle. Similar to the Point class, add an appropriate parameterized constructor and a display()...
-
To explore why businesses haven't introduced 4IR technology to their supply chains To discover how 4IR technology can bring success to supply chains for South African businesses To identify the...
-
Submit a1 java program that use SENTINEL Create a secure java grocery store application program that prompts the user to enter as many gorcery prices ant items as they would like and when they are...
-
A zero-coupon bond is a bond that pay no interest payment. You buy the bond at a discount from the face value ($1000) and are paid the face amount when the bond matures. A zero-coupon bond that will...
-
Although your group initially favored investing 1 0 % of the company s profits in an employee bonus system, after a lively discussion of its merits you and the rest of the group agreed to allocate 2...
-
Problem 2. (0.6 points, 0.2 points for each question) (a) A company turns its inventory 2 times a month. Its months-of-supply = Its days-of-supply = Please show your analysis below: _months. days. (1...
-
Write the statement to store the contents of the txtAge control in an Integer variable named intAge.
-
Show that braces do not have to occur in matching pairs inside a string literal.
-
While exercising, you can use a heart-rate monitor to see that your heart rate stays within a safe range suggested by your trainers and doctors. According to the American Heart Association (AHA), the...
-
Write an app that continuously draws shapes of your choosing on a Canvas. Use random positions, sizes, locations, line widths, fills and alpha transparencies. The shapes should randomly move around...
-
In an air standard Otto cycle, the pressure and temperature at the start of compression stroke are 1 bar and \(30^{\circ} \mathrm{C}\), respectively. The temperature at the end of compression is...
-
What are the different bases for classification of an internal combustion engine? Explain with a neat sketch of various types of the engines.
-
The difference of total power produced and pumping power is known as: (a) Brake horse power (b) Indicated horse power (c) Net indicated horse power (d) None of the above
Study smarter with the SolutionInn App