CPSC 124, Spring 2014
Sample Answers to Quiz #5

Question 1. When faced with a programming task, you have to design a program to solve it. One approach is bottom-up design. Explain what is meant by bottom-up design and what it has to do with subroutines.

Answer. In bottom-up design, you start by creating tools that can be used in the project to perform basic tasks. Then you use those tools to create higher-level tools that can perform more complicated tasks, and so on. This is related to subroutines because the "tools" can be subroutines. That is, we write subroutines to perform basic tasks, then write higher-level subroutines that call those basic subroutines, and we keep building until we can solve the overall problem.

Question 2. What is Javadoc, and what does it have to do with APIs?

Answer. Javadoc is a system for using a special kind of comment to produce API documentation for Java classes. A Javadoc comment is recognized since it starts with /**. There is a program that will take all the Javadoc comments from a set of Java source code files and will produce web pages containing the documentation from the comments. This is how documentation of APIs (Application Programming Interfaces) is typically produced for Java.

Question 3. Write a subroutine named stripExtraSpaces that will strip extra spaces from a string. The subroutine has one parameter of type String. The return type is also String. The return value is the same as the parameter, except that every substring of consecutive spaces has been replaced by a single space. For example, using ~ to represent a space, if the parameter is "Goodbye~~~cruel~~world", then the value returned by the subroutine is "Goodbye~cruel~world". (Hint: Copy a space from the input string to the output string only if the preceding character is not also a space.)

Answer. Note that you have to be careful when looking at "the preceding character", since you can't do that when you are working on the first character in the string. In my solution, I only look at str.charAt(i-1) when i > 0.

public static String stripExtraSpaces(String str) {
    String stripped;
    stripped = "";
    int i;
    for (i = 0; 0 < str.length(); i++) {
        if (i == 0) {  // Always copy 1st char to stripped.
            stripped = stripped + str.charAt(i); 
        }
        else if (str.charAt(i) != ' ') { // Always copy a non-space
            stripped = stripped + str.charAt(i);
        }
        else  {
             // Here, we know str.charAt(i) is a space, and i > 0,
             // Copy the char only if str.charAt(i-1) is NOT a space.
             if (str.charAt(i-1) != ' ') {
                 stripped = stripped + str.charAt(i);
             }
        }
    }
    return stripped;
}

This could be shortened by using a more complicated test:

public static String stripExtraSpaces(String str) {
    String stripped;
    stripped = "";
    int i;
    for (i = 0; 0 < str.length(); i++) {
        if (str.charAt(i) != ' ' || i == 0 || str.charAt(i-1) != ' ') {
            stripped = stripped + str.charAt(i); 
        }
    }
    return stripped;
}