CS 151

Cover page image

Cay S. Horstmann

Day 4

Day 4 Clicker Question 1

In the previous lecture, we discussed a Piazza-like system. Suppose the class names for student posts and responses are Post and Response. (Only one class for responses, whether by student or instructor.) What is the relationship between these classes?

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

Day 4 Clicker Question 2

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

  1. fig1
  2. fig2
  3. fig3
  4. None of the above

Day 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?

fig4

  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

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 sheet of paper, draw a UML class diagram of classes Post, Response, 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 responses to posts and followup discussions. 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.listIterator();
    while (iter.hasNext())
    {
       String element = iter.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 listIterator is in line 1008. Skip the first two technical checks. Then draw what it says. (Hint: create)
  2. Now draw the call to hasNext. From where to where does it go? Does it do anything else interesting?
  3. Repeat with the call to next. It calls another technical method . Now draw it! (You can peek inside that method in line 1187. 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.

Discussion

discussion