CS 151

Cover page image

Cay S. Horstmann

Lecture 4

Lecture 4 Clicker Question 1

In the previous lecture, we discussed a Piazza-like system with classes Post and Response. What is the relationship between these classes?

  1. Dependency
  2. Aggregation
  3. Inheritance
  4. None of the above

Lecture 4 Clicker Question 2

Which of the following diagrams correctly describes the relationship between the given classes?

  1. None of the above

Lecture 4 Clicker Question 3

Consider this code snippet:

Scanner in = new Scanner(new FileReader("input.txt"));
while (in.hasNextLine())
{
   String line = in.nextLine();
   System.out.println(line);
}

What is wrong about this sequence diagram?

  1. The scanner doesn't create the file reader.
  2. The scanner doesn't call read on the file reader.
  3. The call to println should originate with the scanner.
  4. All of the above.

Lab

The Lab Report

  1. Everyone: Make a directory to hold your lab work and put an empty file in it.
    cd ~/cs151
    mkdir lab4
    You will later put UML diagram files inside.

A Class Diagram

  1. On a whiteboard, draw a UML class diagram of classes Question, Answer, User and FollowupDiscussion. Just draw relationship arrows. Pay attention to the multiplicities. Take a photo when you are done.
  2. Now add attributes that you can see from looking at Piazza. For example, a question and answer can be “good”. What about those “Updated 5 days ago by Joanne Lee” messages? Any other attributes?
  3. What do you notice when you look at Question and Answer. Are they exactly identical? Do they have something in common? Can you come up with class from which both inherit?

A Sequence Diagram

  1. Consider this code for iterating over a map:
    ArrayList<String> list = ...
    Iterator<String>> iter = list.iterator();
    while (iterator.hasNext())
    {
       String element = iterator.next();
       System.out.println(element);
    }
    
    Draw a sequence diagram with an unnamed object representing this code, and objects list and iter. Now the fun begins. Look at the implementation of ArrayList. The definitition of iterator is in line 818. Draw what it says. (Hint: create)
  2. Now draw the call to next. From where to where does it go? When you look at the Itr.hasNext method in line 830, you see it doesn't do anything else.
  3. Repeat with the call to next—line 834. It calls another method. Draw it! (You can peek inside that method in line 884. It doesn't call other methods.)
  4. What type do you have for iter? Is it the correct type? Fix it if not.
  5. Take a photo of your diagram.
  6. Don't erase the whiteboard until the end of class.

Discussion