Exam 2 Review

Collections Framework

-ArrayList

-HashMap

Internet/SMTP/Java Mail Framework

-smtp and how mail is sent

Inheritance/Interfaces

-vocabulary: protected, super, extends, final

-casting

-overriding and partial overriding

Other

-data conversion/casting - primitive types (e.g., double to int, char to int)

-method overloading

Recursion

-base case

-rules of recursion

-advantages/disadvantages of recursion

Sample Questions:

1. The following code declares and instantiates a HashMap data structure that maps names (Strings) to grade lists (ArrayLists of Doubles). Write one or more lines of code to add a new record to the HashMap as described in the second comment. The record will map the name "Stu Dent" to a list containing three grades [95.0, 88.5, 92.0].

//data structure to map a student's name to a list of that student's grades

HashMap<String, ArrayList<Double>> gradeMap = new HashMap<String, ArrayList<Double>>();

//add a new record to the hashmap that maps from a student

//"Stu Dent" to a list of grades 95.0, 88.5, and 92.0

2. Write a method printGrades that will take as input the gradeMap declared above and a String representing a name. Your method will print all of the grades for the student with the given name if the HashMap contains a record for the given name. The header for the method will look as follows:

/**

* Method to print all grades for a given student. If the gradeMap

* does not contain a record for the student identified by name

* then the method will print nothing.

* name - name of the student whose grades should be printed

* gradeMap - data structure containing student records

*/

public void printGrades(String name, HashMap<String, ArrayList<Double>> gradeMap);

3. Explain the term partial overriding.

4. SMTP is the protocol mail servers use to communicate with one another. Once your mail client (e.g., Outlook) has delivered an outgoing message to your outgoing mail server, your mail server will use SMTP to deliver the message to the mail server of the recipient. The recipient, however, does not use SMTP to retrieve the message. There are three primary protocols that a recipient may use to retrieve mail from a mail server. Name at least two of them.

5. The following program attempts to print all permutations of a given string, but it contains a logic error. Explain the error in the program and describe what would happen if the program were run as it is.

public class Permutations {

public static void permute(String result, String remaining) {

if(remaining.length() == 0) {

System.out.println(result);

} else {

for(int i = 0; i < remaining.length(); i++) {

permute(result+remaining.charAt(i), remaining);

}

}

}

public static void main(String[] args) {

permute("", "desk");

}

}

6. Consider the exponent method defined by the header below. The method will recursively calculate base to the exp power. If the input were (2, 3) the result would be 8 or 23. Write the base case for the exponent method. Your base case should have a condition that identifies the base case and should indicate what should happen when the base case is hit. Your answer will look similar to the first three lines of the permute method above. Hint: 23 is 2*22.

public int exponent(int base, int exp);