MCX1/2                                                                                                               Mr.Stanley Teitel, Principal
Mr. Gary Jaye                                                                                                       Mr. Danny Jaye, Math Chair

                            1993 Question 1
from APCS A Exam: Part II (adapted for Java)

This problem involves manipulating an unordered ArrayList of N Integer objects that does 
not contain duplicate elements. 

(a) Write a method posOfMax that returns the index of the Integer with the maximum value 
    in the ArrayList. 

                                      +----+----+----+----+----+        
    For example, if ArrayList A is:   | 61 | 34 | -8 | 99 | 73 |          
                                      +----+----+----+----+----+   
                                      
    then the expression posOfMax(A) evaluates to 3. 
    Complete function posOfMax using the header given below. 

(b)Write method posOf2nd that returns the index of the second largest element in its parameter. 
   In writing posOf2nd, you may call the function posOfMax of part (a); assume that posOfMax 
   works as specified, regardless of what you wrote in part (a). 


(c) Write a method zeroBetween that, given an ArrayList of Integers, replaces every element 
    positioned between the largest and second largest elements with zero. For example: 
     
               ArrayList Prior To Call                          ArrayList After Call
          +----+----+----+----+----+----+----+         +----+----+----+----+----+----+----+
          | 61 | 83 | 18 | 49 | 34 | 99 | 57 |         | 61 | 83 |  0 |  0 |  0 | 99 | 57 |
          +----+----+----+----+----+----+----+         +----+----+----+----+----+----+----+

          +----+----+----+----+----+----+----+         +----+----+----+----+----+----+----+
          | 61 | 99 | 18 | 49 | 34 | 83 | 57 |         | 61 | 99 |  0 |  0 |  0 | 83 | 57 |
          +----+----+----+----+----+----+----+         +----+----+----+----+----+----+----+

          +----+----+----+----+----+----+----+         +----+----+----+----+----+----+----+
          | 61 | 99 | 98 | 49 | 34 | 73 | 57 |         | 61 | 99 | 98 | 49 | 34 | 73 | 57 |
          +----+----+----+----+----+----+----+         +----+----+----+----+----+----+----+
            
   In writing zeroBetween, you may call methods posOfMax and posOf2nd of part (a) and part (b); assume 
   that posOfMax and PosOf2nd work as specified, regardless of what you wrote in parts (a) and (b). 
//----------------------------------------------------------------------------------------------------
// A93_1.java

import java.util.*;

public class A93_1 {

    public static int posOfMax(final ArrayList A) {
    //precondition: A contains no duplicates 
        return -77;
    } //=== posOfMax
       
    
     public static int posOf2nd(ArrayList A) {
    //precondition: A contains no duplicates 
        return -77;
    } //=== posOf2nd
    
    
     public static void zeroBetween(ArrayList A) {
    //precondition: A contains no duplicates 

    } //=== zeroBetween
    
//-------------------------------------------------------------------------------------------------------
    static final String TAB = "\t";
    
    public static void main(String[] args) {
        cls();
        System.out.println("\n\t1993 APCS A Exam: Question 1\n");
        driver();        
    }//== main   
    
    static void driver() {
        Integer[] X = { new Integer(61), new Integer(34), new Integer(-8), new Integer(99), new Integer(73) };   
        ArrayList A = makeArray(X);
        ArrayList B = new ArrayList(A);
        System.out.println("Testing posOfMax:");
        System.out.print("A = ") ; print(A); System.out.println(TAB + "posOfMax = " + posOfMax(A));
        A.set(3, new Integer(33));
        System.out.print("A = ") ; print(A); System.out.println(TAB + "posOfMax = " + posOfMax(A));
        System.out.println("\nTesting posOf2nd:"); A.set(3, new Integer(99));
        System.out.print("A = ") ; print(A); System.out.println(TAB + "posOf2nd = " + posOf2nd(A));
        A.set(0, A.get(3)); A.set(3, new Integer(88));
        System.out.print("A = ") ; print(A); System.out.println(TAB + "posOf2nd = " + posOf2nd(A));
        System.out.println("\nTesting zeroBetween:");  ArrayList C = A; A = B; A.set(0, new Integer(88));
        System.out.print("BEFORE zeroBetween, A = ") ; print(A); zeroBetween(A);
        System.out.print(", AFTER, A =  ") ; print(A);  A = C;
        System.out.print("\nBEFORE zeroBetween, A = ") ; print(A); zeroBetween(A);
        System.out.print(", AFTER, A =  ") ; print(A);  A = makeArray(X);
        System.out.print("\nBEFORE zeroBetween, A = ") ; print(A); zeroBetween(A);
        System.out.print(", AFTER, A =  ") ; print(A);  
        System.out.println();
    }
    
    static ArrayList makeArray(Integer[] B) {
        ArrayList A = new ArrayList(B.length);
        for (int k = 0; k < B.length; k++) A.add(B[k]);
        return A;                
    }//=== makeArray
        
    static void print(final ArrayList A) {
        System.out.print("[");
        for (int k = 0; k < A.size(); k++) System.out.print((Integer)A.get(k) + (k < A.size()-1 ? "," + TAB: "]"));        
    }//=== print
     
    static void cls() {
        for (int k = 0; k < 55; k++) System.out.println(); 
    }
} //================================== end of A93_1 file

Free Response Questions       MCX1 home       MCX2 home