Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

21A GUI should avoid bundling actions together, because the user may not anticip

ID: 3679321 • Letter: 2

Question

21A GUI should avoid bundling actions together, because the user may not anticipate any side effects. For example, if a user chooses to cancel a request to send a note, only the send request should be cancelled. Do not bundle another action, such as deletion of the note, with the cancel request.

True
False

22Which of the following is a top-level container?

23The JButton class provides the functionality for an ordinary push button. A button can only contain text.

True
False

24Unlike radio buttons, in which many or none can be selected at once, check boxes only have one button selected at a time.

True
False

26Consider the following class:

public class BinarySearch {
  public static boolean contains(int[] a, int b) {
    if (a.length == 0) {
      return false;
    }
    int low = 0;
    int high = a.length-1;

    while(low <= high) {
      int middle = (low+high) /2;
      if (b> a[middle]){
        low = middle +1;
      } else if (b< a[middle]){
        high = middle -1;
      } else { // The element has been found
        return true;
      }
    }
    return false;
}
}

What is the purpose of iteration in the above code.

27Consider the following search method.

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 of the following might be described as the base case(s) for this method to end the recursion?

if (a[midpoint] == target)

if (a[midpoint] < target)

if (a[midpoint] > target)

if (left > right)

28Consider both of the following approaches to the binary search:

Approach 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.

29Consider 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.

int newScore = Integer.parseInt(jtfHighScore.getText());

if (cmd.equals("Find"))

else (cmd.equals("Replace"))

else (cmd.equals("Add"))

String topScores

30You have previously defined these GUI objects needed for your JPanel.

jbtAdd

jbtFind

jbtReplace

Which code block should you use to add these objects to a new JPanel called p2 using a GridLayout with 1 row and 3 columns?

JPanel p2 = new JPanel(new GridLayout(1, 3));

A. JLabel B. JPanel C. JFrame D. JButton

Explanation / Answer

Please find the answers below:

21) FALSE

22) C) JFrame

23) FALSE; icons can be associated with a button

26) c) To search between the low and high index numbers as long as there are index numbers between low and high to search.

27) A and D

28) 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.

29) B and C

30) D

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote