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

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

For this problem, you will write three methods that involve a class StringType used to represent strings. The
implementation of StringType is not known to you; however, StringType objects can be manipulated using
the methods specified below.

public char head()
//precondition: owner of call represents a string that has at least one character
//postcondition: head returns first character string of string that the call owner represents

public boolean isEmptyString()
//postcondition: returns true when owner represents empty string, returns false otherwise

public static void removeHead(StringType st)
// precondition: st represents the string c0c1c2...cn, n ³  0
//postcondition: st represents the string c1c2...cn.

public static void appendChar(StringType st, char x)
//precondition: st represents the string c0c1c2...cn, n ³  0.
//postcondition: st represents the string c0c1c2...cnx..

(a) Write free method moveFirstToLast that moves the first character of the (non-empty) string represented
      by its StringType parameter to the end of the string.

     For example:

    st before the call moveFirstToLast(st)                               st after the call
                        eat                                                                          ate
                        range                                                                      anger
                        a                                                                             a
                        abcd                                                                       bcda

In writing moveFirstToLast, you may only manipulate strings using using member and free methods head,
isEmptyString
, removeHead, and appendChar.

Complete method moveFirstToLast using the header given below.

public static void moveFirstToLast(StringType st)
//precondition: S represents the string c0c1c2...cn, n ³  0.
//postcondition: S represents the string c1c2...cnc0.


(b) Write method removeLeadingVowels that removes all leading vowels from the string represented by
      its StringType parameter S, leaving the rest of the string unchanged.

    For example:

    st before the call removeLeadingVowels(st)                      st after the call
                            eat                                                                  t
                            range                                                              range
                            a                                                                    the empty string
                            abcd                                                              bcd

In writing removeLeadingVowels, you may only manipulate strings using using member and free methods
head
, isEmptyString, removeHead, and appendChar. You may also include calls to the free method
isVowel
whose prototype is given below.

public static boolean isVowel(char ch)
//postcondition: returns true if ch is a vowel, false otherwise.

Complete method removeLeadingVowels using the header given below.

public static void  removeLeadingVowels(StringType st)

(c) Write a method removeAllVowels that removes all of the vowels from the string represnted by its
      StringType
parameter. The order of the nonvowel characters of S is unchanged. Assume that the
      string represented by S contains no duplicate characters.

    For example:

    st before the call removeAllVowels(st)                          st after the call
                        eat                                                                 t
                        range                                                             rng
                        a                                                                   the empty string
                        abcd                                                              bcd

In writing removeAllVowels, you may only manipulate strings using using calls to member and free methods
moveFirstToLast
, removeLeadingVowels, head, isEmptyString, removeHead, and appendChar.
Assume that methods moveFirstToLast and removeLeadingVowels work as specified, regardless of
what you wrote in parts (a) and (b). You may also include calls to isVowel. You will not receive full
credit for this part of the problem if your code includes any local variables of the StringType.

Complete method removeAllVowels using the header given below.

public static void removeAllVowels(StringType st)
//*** precondition: S represents a string that contains no duplicate characters
//============================================================================
// A93_3.java
public class A93_3 {

    public static void moveFirstToLast(StringType st) {
    //precondition & postcondition: see part A

    }//==== moveFirstToLast

    public static void removeLeadingVowels(StringType st) {
    //precondition & postcondition: see part B

    }//==== removeLeadingVowels

    public static void removeAllVowels(StringType st) {
    //precondition & postcondition: see part C

    }//==== removeAllVowels
//-------------------------------------------------------------------------------
    final static String TAB = "\t";
   
    public static void main(String[] args) {
        cls();
        System.out.println("\n\t1993 APCS A Exam: Question 3\n");
        driver();                
    } //==== main
   
   
    static void driver() {
        final String TAB4 = "\t\t\t\t";
        System.out.println("st before moveFirstToLast(st)      st after the call");       
        String[] A = { "eat", "range", "a", "abcd"};
        for (int k = 0; k < A.length; k++) {
            StringType st = new StringType(A[k]);
            moveFirstToLast(st);
            System.out.println(TAB + A[k] + TAB4 + st);         
        }
        //-------------------------------------------------------------
        System.out.println("\nst before removeLeadingVowels(st) st after the call");       
        for (int k = 0; k < A.length; k++) {
            StringType st = new StringType(A[k]);
            removeLeadingVowels(st);
            System.out.println(TAB + A[k] + TAB4 + (st.toString() != "" ? st.toString() : "the empty string"));         
        }
        //-------------------------------------------------------------
        System.out.println("\nst before removeAllVowels(st)    st after the call");       
        for (int k = 0; k < A.length; k++) {
            StringType st = new StringType(A[k]);
            removeAllVowels(st);
            System.out.println(TAB + A[k] + TAB4 + (st.toString() != "" ? st.toString() : "the empty string"));         
        }
        System.out.println();                       
    }//==== driver


    static boolean isVowel(char ch) {
        ch = (char)(ch | 32);
        return ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u';                   
    }//==== isVowel
   
    static void cls() {
        for (int k = 0; k < 55; k++) System.out.println();
    }      
   
    static void removeHead(StringType str) {
        str.removeHd();
    }//==== removeHead
   
    static void appendChar(StringType str, char x) {
        str.appendCh(x);
    }//==== appendChar
   
}//==== A93_3

class StringType {
    char head() {
        if ( isEmptyString() ) {
            System.err.println("Error: StringType method head called with empty string");
            return '#';
        }
        return myString.charAt(0);
    }//=== head
   
    boolean isEmptyString() {
        return myString.length() == 0;       
    }//==== isEmptyString
   
   
    void removeHd() { myString.deleteCharAt(0); }
    void appendCh(char ch) { myString.append(ch); }
 
    public String toString() {
        return myString.toString();      
    }
   
    StringType() { myString = new StringBuffer(); }
    StringType(String st) { myString = new StringBuffer(st); }
    private StringBuffer myString;   
}
//=================================================== end of A93_3 file


Free Response Questions       MCX1 home       MCX2 home