Spring 2014 | Cay S. Horstmann | Department of Computer Science | San Jose State University
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.
lab_partner
for that purpose.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.
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.
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() { circle.setColor(Color.GRAY); circle.fill(); }
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?
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(); sentence1.add("Mary"); sentence1.add("had"); sentence1.add("a"); sentence1.add("little"); sentence1.add("lamb");
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.