1
e02
CS56 M16
Name:
(as it would appear on official course roster)
Umail address: @umail.ucsb.edu

EXAM: e02: Final Exam

ready? date points
true Thu 07/28 09:30AM

You may not collaborate on this exam with anyone. If you need to use the restroom, you must leave your cell phone with the exam proctor before leaving the room.

  • Write your name at the top of this page AND EVERY ODD NUMBERED PAGE.
  • Double check that you turned in ALL pages; look for "End of Exam" on the last page.
  • This exam is closed book, closed notes, closed mouth, cell phone off.
  • You are permitted one sheet of paper (max size 8.5x11") on which to write notes.
  • This sheet will be collected with the exam, and might not be returned.
  • Please write your name on your notes sheet.

  1. (10 pts) A common misconception about Java threads is that when you invoke the .start() method on them, they begin running immediately. Explain what happens instead.

  2. (10 pts) What is special or different about instances of inner classes (versus instances of objects that are not inner classes?)

    Clarification: The question is asking about “non-static inner classes”.
    If you don’t know what that means, that’s fine: we didn’t really talk about static vs. non-static inner classes.
    Just answer the question from the standpoint of “inner classes” as discussed in the HFJ textbook, and in lecture.
    If you know what “static inner classes” are, then answer the question about “non-static” inner classes.

  3. (10 pts) From a “big picture” standpoint, what is the role of design patterns in object oriented software development?

  4. (10 pts) This problem and several that follow will make to the following handouts. Please refer to them while answering these problems.

    • p. 1 of Handout A which has several reminders about Comparable, Comparator, etc.
    • p. 1 of Handout B which has incomplete code for a Student class.

    Consider the following class that uses Student, called StudentSort.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    // StudentSort.java
    import java.util.ArrayList;
    
    class StudentSort {
        
        public static void main(String [] args) {
    	ArrayList<Student> roster = new ArrayList<Student>();
    	roster.add(new Student(6234567,"Bob",3.25,"CMPSC"));
    	roster.add(new Student(7152353,"Charlie",3.96,"CMPSC"));
    	roster.add(new Student(2352353,"Alice",3.97,"CMPEN"));
    	roster.add(new Student(2888888,"Danielle",4.00,"CMPSC"));
    	roster.add(new Student(1152353,"Ed",2.90,"CMPEN"));
    
    	java.util.Collections.sort(roster);
    	
    	for (Student s: roster) {
    	    System.out.println(s);
    	}	
        }
    }
    

    Suppose we want line 14 of this code, java.util.Collections.sort(roster); to sort in lexicographic order by name (Alice, Bob, Charlie, Danielle, Ed). In order for this code to work properly, and indeed for the Student.java class to even compile, at least one extra method must be added.

    Please write the additional method that would be added Student.java in the space below.
    Please try to avoid writing too close to the end of the page (it doesn’t scan well.)

  5. (10 pts) Please continue to refer to:

    Assume that you’ve added the necessary method from the previous problem so that Student.java now compiles, and line 14 will sort the ArrayList<Student> in the correct order.

    Below, you see some two examples of output. The output on the left is what we got, but the output on the right is what we want.

    What We Got What We Want
    Student@7852e922
    Student@4e25154f
    Student@70dea4e
    Student@5c647e05
    Student@33909752
    
    Student(2352353,"Alice",3.97,CMPEN)
    Student(6234567,"Bob",3.25,CMPSC)
    Student(7152353,"Charlie",3.96,CMPSC)
    Student(2888888,"Danielle",4.00,CMPSC)
    Student(1152353,"Ed",2.90,CMPEN)
    

    Below please write the additional code that would need to be added to Student.java to make this change.
    As always, please avoid writing too close to the edge of the page.

  6. (10 pts) Please continue to refer to:

    Consider the following Java code that also makes use of the Student class. As you can see, in order for this code to compile, four additional methods would have to be added to the Student class, namely the ones invoked on lines 16,17,18 and 19.

    On p. 2 of Handout A, you will see the output of various invocations of this program (assuming the necessary change are made to the code, including the methods you added Student.java in questions 4 and 5, and the implementations of these four additional methods.

    For this question, we focus ONLY on the method sortByName invoked on line 16.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    // StudentMain.java
    import java.util.ArrayList;
    
    class StudentMain {
        
        public static void main(String [] args) {
    	ArrayList<Student> roster = new ArrayList<Student>();
    	roster.add(new Student(6234567,"Bob",3.25,"CMPSC"));
    	roster.add(new Student(7152353,"Charlie",3.96,"CMPSC"));
    	roster.add(new Student(2352353,"Alice",3.97,"CMPEN"));
    	roster.add(new Student(2888888,"Danielle",4.00,"CMPSC"));
    	roster.add(new Student(1152353,"Ed",2.90,"CMPEN"));
    	
    	if (args.length > 0 ) {
    	    switch (args[0]) {
    	    case "name":      Student.sortByName(roster); break;
    	    case "majorName": Student.sortByMajorThenName(roster); break;
    	    case "gpaDesc":   Student.sortByGPADesc(roster); break;
    	    case "perm":      Student.sortByPerm(roster); break;
    	    default: break;
    	    }
    	}
    	
    	for (Student s: roster) {
    	    System.out.println(s);
    	}	
        }
    }
    

    In the space below, please write an implementation of sortByName that sorts the roster by name as indicated on p. 2 of Handout A.

    For full credit: please write the body of this method as a single line of code.
    Specifically: use a line of code that takes advantage of the fact that Student implements Comparable<Student>.

    For half-credit, you may use any technique that sorts the array correctly.

  7. (10 pts) Please continue to refer to:

    • The StudentSort.java file on p. 2 of the exam.
    • p. 1 and p. 2 of Handout A
    • p. 1 of Handout B

    Now, please write the code for the sortByMajorThenName method invoked on line 17 of StudentMain.java.

    For Full Credit the body of your method should use the sort method of java.util.ArrayList, passing in an object that implements Comparator<ArrayList> that is

    • EITHER an instance of an anonymous class
    • OR an instance of an inner class of Student.

    If you choose the inner class technique, include the definition of the inner class in your answer.

    For half credit: use any technique that will sort the array correctly.

  8. (10 pts) Please continue to refer to:

    • The StudentSort.java file on p. 2 of the exam.
    • p. 1 and p. 2 of Handout A
    • p. 1 of Handout B

    Now, please write the code for the sortByPerm method invoked on line 19 of StudentMain.java.

    For Full Credit you MUST use a lambda expression.

    That is, the body of your method should use the sort method of java.util.ArrayList, passing in an object that implements Comparator<ArrayList> in the form of a lambda expression.

    As a reminder, the general syntax of a Java lambda expression is as follows:

    parameters syntax
    x (x) -> return_value
    x,y (x,y) -> return_value
    etc…  

    For half credit: use any technique that will sort the array correctly (e.g. inner class, anonymous class). If you use an inner class, be sure to include the definition of that inner class in your answer.

  9. (10 pts) Please continue to refer to:

    • The StudentSort.java file on p. 2 of the exam.
    • p. 1 and p. 2 of Handout A
    • p. 1 of Handout B

    Now, finally please write the code for the sortByGPADesc method invoked on line 18 of StudentMain.java.

    Take note of two things: * The fact that we want the order to be descending, not ascending * The fact that we are using double not int.
    * The java.lang.Math.signum function described on p. 1 of Handout A.

    For Full Credit the body of your method should use the sort method of java.util.ArrayList, passing in an object that implements Comparator<ArrayList>.

    However, that object can be created using any reasonable technique, at your discretion, i.e. any of the following:

    • anonymous class
    • inner class
    • lambda expression
  10. (4 pts) Briefly, at a "big picture" level, what was the task you were given to do in lab05, the parsing assignment? If you aren't sure what I'm asking for in this question, perhaps this will help. You were given a working piece of code that did one thing, and you were asked to transform it into a working piece of code that did the same thing, plus something extra. What was the thing it did before your changes, and what was the extra part you added?
  11. Again, at the "big picture" level, briefly describe each of the following, with specific examples—specific enough that there is no doubt in the grader's mind that you know what you are talking about, but not a lengthy essay.
    1. (2 pts) tokens
    2. (2 pts) productions in a grammar, for example, each of the lines in the box at right.

      expression ::= additive-expression
      additive-expression ::= multiplicative-expression ( ( '+' | '-' ) multiplicative-expression ) *
      multiplicative-expression ::= primary ( ( '*' | '/' ) primary ) *
      primary ::= '(' expression ')' | INTEGER | '-' primary
      
    3. (2 pts) abstract syntax trees (ASTs)
End of Exam