CS46A Introduction to Programming—Online Section

Spring 2014 | Cay S. Horstmann | Department of Computer Science | San Jose State University

Week 3

  1. I recorded these videos for you: Intro to Week 3 and Solution of Homework 2
  2. Work through the Udacity videos in Lesson 2 from “Implementing a test program” to the end, and Lesson 3 from the beginning to “ Figuring Things Out on a Piece of Paper”.
  3. Read Sections 2.6, 2.7, 3.1, 3.2, 3.3 in the textbook. Do the self checks.
  4. A heads-up: The order of the material is slightly different in the Udacity videos and the textbook. The textbook covers constructors in section 3.3, and the videos will only cover that the next week. For this week's programming projects, I will give you the code for the constructors.

  5. Take the quizzes on Monday and Thursday.
  6. Turn in the draft of Homework 3 on Wednesday and the final version on Sunday.
  7. Work through Lab 2.
  8. The API for the graphics classes is here. You may find this handy for the homework and labs.

About the Labs

Homework 3

You submit your homeworks in Canvas. Submit three files that you get out of code-check. Don't build your own zip files.

Ask lots of questions on Piazza.

Start early.

Download the BlueJ projects for the three assignments here.

  1. In this assignment, you will put a frame around a picture and a caption below it.

    First fill a black rectangle, then fill a white rectangle inside it. You need to ask the picture how big it is, and then use those measurements. Also, to center the caption, you need to ask it for its width. For the draft, just show the frame.

    code-check for draft | final

  2. You will complete a class that simulates a traffic light. The class has three methods:

    void next() // Advance to the next state (red -> yellow -> green -> red ...)
    int getReds() // Count how many times the light has been red in its life
    void draw() // Draw the light (final only)

    Do not store the color. Instead, keep a count, just like in the Counter class of the book.

    From that count, we can derive what we want through the division operator:

    count:     0 1 2 3 4 5 6 7 8 9 ...
    count / 3: 0 0 0 1 1 1 2 2 2 3 ...

    Note that count / 3 discards the fractional part, and that it is almost the number of times that the light has been red.

    For the final version, you also draw the traffic light. Add an instance variable

    private Ellipse circle = new Ellipse(0, 0, 50, 50);

    and a method

    public void draw()

    You haven't yet learned how to use a branch statement for computing the colors, so for now, we use Color.GRAY for all phases. We'll fix this in a later assignment.

    Of course, the circle needs to be moved to the right position. In next, move it down by 50 pixels. That works great the first two times, but the third time, the light has moved too far. Since we don't yet have an if statement, we use a trick. What is circle.getY() / 150 after the first call to next? The second? The third? How does that help you move the circle back to the top after the third move?

    code-check for draft | final

  3. In this problem, you will complete a class Sentence that collects words. A user of your class simply adds words to a sentence by calling the add method:

    Sentence sentence1 = new Sentence();

    Your class will add spaces between the words and a period at the end.

    In the draft, just add a space at the end of each word. Use the concat method: word.concat(" ") adds a space to the word. Or more precisely, it returns a string consisting of word followed by a space. Of course, then you need to concatenate that string to the text instance variable.

    In the final version, make it so that getText returns the sentence with a period and no space before the period. That's a little tricky. In order that nobody has an unfair advantage, you aren't allowed to use the if statement, which hasn't been covered yet. Ask on Piazza if you need a hint.

    code-check for draft | final