CS302 Practice Quiz #5



    Designate the following statements as either True or False:

  1. T A 2D array can be made up of many subarrays of different lengths.
  2. TThe java.io package contains definitions of BufferedReader objects.
  3. FIn a 2-dimensional array, the first array subscript represents the "column number" and the second array subscript represents the "row number".

    Draw the memory diagram for the following code fragment. For arrays, include the value of the length data members. For uninitialized values, place a ?. For memory which has been dealocated or changed, place a single line thru the memory.:

         int maxDim = 4;
         int table[][];
         table = new int[maxDim][];
         for(int i = 0;i < maxDim;i++)
              table[i] = new int[i+1];
         int base = 10;
         for(int k = 1;k < maxDim;k++)
              table[k][k] = base++;
    

    Write a method called removeName which takes a String as a parameter and finds any matches in the array of names. If no matches exist, return false. If there is exactly one, remove it updating currentNum and making sure that nulls do not appear in the middle of the list of names when done, then return true. If there is more than one match, use System.out to tell the user how many matches there are and ask how many to remove. Remove the number of names indicated by the user(order should not matter since these are the same, just be consistent). Don't forget to do error checking on input.

    import java.io.*;
    class Things{
         private BufferedReader stdin; //input from the console
         private String[] names;            //current array of names, partially filled
         private int currentNum;              //number of Strings in names
         public Things(BufferedReader in){
              stdin = in;
              currentNum = 0;
              names = new String[10];
         }
    
        /**
         * removeName finds matches of s in the list names. If there is more than
         * one to remove, the user choses some number of them to remove
         * swap removals and clean up list to remove string
         * @param s string to be matched
         * @return true if removed something, false if nothing matched 
         **/
         public boolean removeName(String s) throws IOException {
            boolean match = false;  //we haven't seen a match yet
            int num = 0;            //number of total matches
            //go through and count the number of matches
    	for(int i = 0;i < currentNum;i++)
                if(names[i].equalsIgnoreCase(s)) {
                   num++;
    	       match = true;
                }
    	int numRemove = 0;
            if(num > 1) {  //for more than one match, ask user
              do {
                 System.out.print("Enter the number to be removed (1-"+num+": ");
                 numRemove -= Integer.parseInt(stdin.readLine());
                 if(numRemove < 0 || numRemove => num)  //error check
                    System.out.println("Legal numbers are 1 - "+num);
              }while(numRemove < 0 || numRemove => num);
            } else  //for one match or fewer, just update numRemove
               numRemove = num;
    
    	//for each one to be removed
            for(int c = 0;c < numRemove;c++) {
    
    	   //find the match and swap with end value
    	   for(int i = 0;i < currentNum; i++)
                  if(names[i].equalsIgnoreCase(s)) {
                      names[i] = names[currentNum]; //swap
                      names[currentNum] = null;     //release memory
                      currentNum--;                 //update count
                  }
    
            }
            return match;
         }
    
    }
    
}