Help, two important questions! Question 28 of 30 (worth 5 points) Consider both
ID: 3679887 • Letter: H
Question
Help, two important questions!
Question 28 of 30 (worth 5 points)
Consider both of the following approaches to the binary search:
Approach A
A private static int search (int[] a, int searchValue) {
int left = 0
int right = a.length1
while (left <= right) { int midpoint = (left+right)/2
if (a[midpoint] == searchValue) return midpoint
else if (a[midpoint] < searchValue) left = midpoint + 1
else right = midpoint – 1 } return 1 }
Approach B
private static int search (int[] a, int target, int left, int right) {
if (left > right) return 1
else { int midpoint = (left + right) / 2
if (a[midpoint] == target) return midpoint
else if (a[midpoint] < target) return search (a, target, midpoint + 1, right)
else return search (a, target, left, midpoint – 1) } }
Which approach uses recursion? Explain why Approach B has more parameters than Approach A. Only ONE of the following:
A. Approach A uses recursion. Approach B requires the left and right parameters because they are needed in the process of iteration to reduce the search area, either by increasing the value of left or decreasing the value of right.
B. Approach B uses recursion. Approach B has left and right parameters because they are not defined locally in the method itself. Either defining these values in the parameters or locally in the method is a matter of preference and has no bearing on the outcome. These values are needed to define the search area, either by increasing the value of left or decreasing the value of right.
C. Approach B uses recursion. Approach B includes the additional left and right parameters because they are required in the process of recursion to reduce the search area, either by increasing the value of left or decreasing the value of right when a recursive call is made.
D. Both Approach A and Approach B use recursion. The additional parameters are not necessary and should be removed to make the method implementation more readable.
Question 29
Consider the following class: private class ButtonListener implements ActionListener
{ public void actionPerformed(ActionEvent evt) { String cmd = evt.getActionCommand();
if (cmd.equals("Find")) //Find player String newName = Integer.toString(player.getValue(jtfName.getText()));
jtfHighScore.setText(newName);
jbtReplace.setVisible(true);
else (cmd.equals("Replace")) { //Find player
int newScore = Integer.parseInt(jtfHighScore.getText());
player.replaceVal(jtfName.getText(), newScore);
String topScores= player.showAll();
jtxtTop.setText(topScores);
jtfName.setText(""); jtfHighScore.setText(""); }
else (cmd.equals("Add")) { //addressBook player = new addressBook();
int newScore = Integer.parseInt(jtfHighScore.getText());
player.add(jtfName.getText(), newScore); jtfName.setText(""); jtfHighScore.setText("");
String topScores= player.showAll(); jtxtTop.setText(topScores); } } Itentify two major problems with this code.
Can be one option or 2 or 3 or all 4. This question has checking boxes
A. Curly braces are missing for this if clause if (cmd.equals("Find"))
B. The following variable must be declared in the class as an instance variable and cannot be defined locally in the method because it is used by more than one method of the class. String topScores
C. The keyword if is missing from these else clauses: else (cmd.equals("Replace")) else (cmd.equals("Add"))
D. You cannot store a value from a textbox, which holds a String in an integer variable int newScore = Integer.parseInt(jtfHighScore.getText());
Explanation / Answer
Approch B uses Recursion.
C. Approach B uses recursion. Approach B includes the additional left and right parameters because they are required in the process of recursion to reduce the search area, either by increasing the value of left or decreasing the value of right when a recursive call is made.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.