Question

Now this: Suppose that you have been assigned to take over a project from another programmer who has just been dismissed for
0 0
Add a comment Improve this question Transcribed image text
Answer #1

The bugs have been pointed out inline in the given code snippet:

    private int[] insertValue(int value, int[] array) {
       int[] result = new int[array.length + 1];
       /*
       * Bug #1: result.length will be 1 larger than array.length
       * hence, array[i] will throw indexOutOfBound exception
       * when it tries to access the element array[result.length]
       * The for-loop should run array.length times.
       */
       for (int i = 0 ; i < result.length; i++) {
           result[i] = array[i];
       }
      
       int pos = 0;
       /*
       * Here we are trying to find the position for the new value
       * Bug #2: if value > array[i], we need to keep the value
       * right after the position i, hence its position should be
       * i+1 and not i as written in line #26
       */
       for (int i = 0; i < array.length; i++) {
           if (value > array[i]) {
               pos = i;
               /*
               * Bug #3: break is not required here, because
               * it will cause the loop to exit at the very first
               * finding of value being greater than the element.
               * We need to keep traversing till we find the
               * immediate smaller number.
               */
               break;
           }
       }
       /*
       * Bug #4: Loop can't start with the result.length index.
       * as arrays indexing start with 0, hence i should start
       * with result.length - 1.
       *
       * Bug #5: If pos = 0, and i = 0 at some iteration,
       * the condition i>=pos will be true, and the loop will
       * execute making result[i-1] throw indexOutOfBounds
       * exception
       */
       for (int i = result.length; i >= pos; i--) {
           result[i] = result[i - 1];
       }

       result[pos] = value;
      
       return result;
   }
   

private int[] insertValue(int value, int[] array) { int[] result = new int[array.length + 1]; Bug #1: result.length will be 1

Add a comment
Know the answer?
Add Answer to:
Now this: Suppose that you have been assigned to take over a project from another programmer...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Hey, I was wondering if there’s another way to make these methods not reliable to imports...

    Hey, I was wondering if there’s another way to make these methods not reliable to imports such as “import java.util.Arrays” and “import java.util.Hash” I am still new in coding and would like to know if there’s another way to do it! Here is the code! (Thank you in advance and I will really appreciate it :) ) /** This exercise involves implementing several methods. Stubs for each method with documentation are given here. It is your task to fill out...

  • I have the following classes SearchMain.java import java.security.SecureRandom; import java.util.Arrays; import java.util.HashSet; import java.util.Set; /** *...

    I have the following classes SearchMain.java import java.security.SecureRandom; import java.util.Arrays; import java.util.HashSet; import java.util.Set; /** * * DO NOT CHANGE THIS CODE * */ public class SearchMain {    public static void main(String[] args) {        ArraySearcher arraySearch = new ArraySearchImp();        SecureRandom secureRandom = new SecureRandom();        int[] sortedArray = generateRandomSortedIntArray(10);        System.out.println(Arrays.toString(sortedArray));        Set<Integer> intSet = new HashSet<Integer>();               for(int i = 0; i < 5; i++) {       ...

  • IN JAVA please Given a sorted array and a target value, return the index if the...

    IN JAVA please Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. Your code will be tested for runtime. Code which does not output a result in logarithmic time (making roughly log(2) N comparisons) will fail the tests. A sample main function is provided so that you may test your code on sample inputs. For testing purposes, the...

  • Need help with this Java project implementing an interpolation search, bolded the missing parts: /**   ...

    Need help with this Java project implementing an interpolation search, bolded the missing parts: /**    A class that implements an interpolation search and    a binary search of a sorted array of integers.    @author Frank M. Carrano    @author Timothy M. Henry    @version 4.0 */ public class SearchComparer {    private int[] a;    private int interpolationSearchCounter;    private int binarySearchCounter;    private static final int CAPACITY = 50;    public SearchComparer(int[] array, int n)    {...

  • Need help with this Java. I need help with the "to do" sections. Theres two parts...

    Need help with this Java. I need help with the "to do" sections. Theres two parts to this and I added the photos with the entire question Lab14 Part 1: 1) change the XXX to a number in the list, and YYY to a number // not in the list 2.code a call to linearSearch with the item number (XXX) // that is in the list; store the return in the variable result 3. change both XXX numbers to the...

  • Create a CodeBlocks project "HW 9" Write the code to ask the user to enter the...

    Create a CodeBlocks project "HW 9" Write the code to ask the user to enter the size of an array. Then create an integer array of that exact size. Ask the user to enter a maximum value and then write a loop to fill the array with random numbers with value in the range of 1 to the maximum value. For example, if the maximum value is 100, random numbers must have value 1 to 100 inclusive. Input size of...

  • // ArrayIns.java // demonstrates insertion sort 11--- class ArrayIns private long[] a; private int nElems; //...

    // ArrayIns.java // demonstrates insertion sort 11--- class ArrayIns private long[] a; private int nElems; // ref to array a // number of data items public ArrayIns(int max) // constructor a = new long[max]; nElems - © // create the array // no items yet --- public void insert(long value) // put element into array a[nElems] = value; nElems++; // insert it // increment size public void display() // displays array contents for(int j=0; j<ntlems; 1++) 1/ for each element,...

  • Project Description: In this project, you will combine the work you’ve done in previous assignments to...

    Project Description: In this project, you will combine the work you’ve done in previous assignments to create a separate chaining hash table. Overview of Separate Chaining Hash Tables The purpose of a hash table is to store and retrieve an unordered set of items. A separate chaining hash table is an array of linked lists. The hash function for this project is the modulus operator item%tablesize. This is similar to the simple array hash table from lab 5. However, the...

  • Hi, So I have a finished class for the most part aside of the toFile method...

    Hi, So I have a finished class for the most part aside of the toFile method that takes a file absolute path +file name and writes to that file. I'd like to write everything that is in my run method also the toFile method. (They are the last two methods in the class). When I write to the file this is what I get. Instead of the desired That I get to my counsel. I am having trouble writing my...

  • Summary You will write an application to build a tree structure called Trie for a dictionary...

    Summary You will write an application to build a tree structure called Trie for a dictionary of English words, and use the Trie to generate completion lists for string searches. Trie Structure A Trie is a general tree, in that each node can have any number of children. It is used to store a dictionary (list) of words that can be searched on, in a manner that allows for efficient generation of completion lists. The word list is originally stored...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT