1
e03
CS56 f17
DO NOT WRITE IN THIS AREA! Name: Seat:
(as it would appear on official course roster)
Umail address: @umail.ucsb.edu

EXAM: e03: Final

ready? date points
true Tue 01/09 08:00AM

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. For this question, you need the additional handout A with code for these files: Beverage.java, Edible.java, Food.java, FreeCandy.java and Product.java. These are classes used by a grocery store known as “Trader Bobs”.

    Some of these methods will compile and run, while others will not.

    Indicate, for each method, where it compiles or not, and if it does compile, the output when invoked. in context of the classes on handout A and assuming the methods appear inside this class:

    public class TraderBobs {
     // methods appear here
    }
    
    Will it compile?Output when called (only if it compiles)
    Yes
    No
    1. (4 pts)
        public static void TB01 () {
          Beverage m = new Beverage(99,"Coke",150,12.0);
          System.out.println("m: " + m.getCalories());
        }
      
    2. (4 pts)

        public static void TB02 () {
          Beverage n = new Edible(199,"Gummi Bears",520,5);
          System.out.println("n: " + n.getPrice());
        }
      
  2. Continued from previous problem…

    Some of these methods will compile and run, while others will not.

    Indicate, for each method, where it compiles or not, and if it does compile, the output when invoked. in context of the classes on handout A and assuming the methods appear inside this class:

    public class TraderBobs {
     // methods appear here
    }
    
    1. (4 pts)

        public static void TB03 () {
          Edible o = new Beverage(89,"Diet Coke",0,12.0);
          System.out.println("o: " + o.getFluidOunces());
        }
      
    2. (4 pts)

        public static void TB04 () {
           Edible p = new Food(249,"Kind Bar",200,1.4);
           System.out.println("p: " + p.getCalories());	
        }
      
    3. (4 pts)

        public static void TB05 () {
           Edible q = new Edible(149,"Snickers",245,1.56);
           System.out.println("q: " + q.getCalories());
        }
      
    4. (4 pts)

        public static void TB06 () {
             Food r = new Food(99,"Almonds",100,0.63);
             System.out.println("r: " + r.getName());
        }
      
    5. (4 pts)

        public static void TB07 () {
           FreeCandy s = new FreeCandy(25);
           System.out.println("s: " + s.getName());
        }
      
  3. Continued from previous problem…

    Some of these methods will compile and run, while others will not.

    Indicate, for each method, where it compiles or not, and if it does compile, the output when invoked. in context of the classes on handout A and assuming the methods appear inside this class:

    public class TraderBobs {
     // methods appear here
    }
    
    1. (4 pts)

        public static void TB08 () {
           Edible t = new FreeCandy(30);
           System.out.println("t: " + t.getPrice());
        }
      
    2. (4 pts)

        public static void TB09 () {
           Product u = new Product(299,"Ziploc Bags");
           System.out.println("u: " + u.getName());
        }
      
    3. (4 pts)

        public static void TB10 () {
           Product v = new FreeCandy(30);
           System.out.println("v: " + v.getCalories());     
        }
      
  4. (20 pts) Fully explain the difference between == and .equals. Your answer should touch on, at least, these points:

    • primitive types
    • object references
    • the .equals implementation inheritated from java.lang.Object
    • the relationship between overriding .equals() and overriding .hashCode()
  5. (10 pts) Lambda Expressions are a relatively new feature added to Java.

    Would you say the benefit of lambda functions is more a matter of peformance (as in, the program runs faster or uses less memory, if it was written with lambda functions), or is it more a matter of developer productivity (developer teams write, understand, and maintain code more easily?)

    Or do benefits apply in both of these dimensions?

    Support your answer in a way that makes it clear you understand what lambda functions are, and what their benefits are.

  6. (20 pts) Assume that there is a class available in the classpath of your JVM called edu.ucsb.Student class with the following methods:

    • public int getPerm()
    • public double getGPA()
    • public static ArrayList<Student> initFile(String filename) throws Exception

    As a reminder, a main method must exist in the context of a class, e.g.

    public class Answer {
        public static void main(String[] args) {
            ...
        }
    }
    

     

    Write the full source for a class Answer that will:

    • invoke initFile on the first command line argument (i.e. args[0])
    • sort the returned ArrayList by GPA, NOT using a sort written by hand, but rather, one of the sorting methods provided by the Java library (see p. 2 of Handout A and Handout B for clues.
    • write out, on System.out a line for each student with their gpa and perm number, separated by a comma (,). The lines should be in ascending order by GPA (lowest to highest). Where there is a “tie”, the lines may be in an arbitrary order.

    Things to watch for:

    • Leave room to write the import statements that you would need, and write those.
    • Pay attention to the fact that you need to do something about the checked exception thrown by the initFile method. Do something reasonable. What that means is for you to figure out.
    • There is more than one way of writing the code. Reasonable solutions will get full credit.

    There is more space on the next page if you need it.

     

     

    Extra space for the coding
    problem in case you need it.

  7. (10 pts) One of the consequences of certain design decisions in Object Oriented Software is “tight coupling”, or “loose coupling”. Good design patterns are said to promote one of these and avoid the other.

    In the context of an Object Oriented Design:

    • What does “tight” vs. “loose” coupling mean?
    • Which is preferred?
    • Why?

    Hint: If it helps, you might make reference to the TokenFactory and ASTFactory interfaces in lab08. If that throws you off, then just ignore it.

End of Exam