Spring 2014 | Cay S. Horstmann | Department of Computer Science | San Jose State University
Read Sections 2.1 - 2.5 and 2.8 in the textbook. There is a lot of important terminology in this chapter. Do the self checks. You may want to first watch a few videos, then read up in the book what you just read, and then go back to watch more videos. Or plough through all the videos and then read the book to make sense of what you just watched. Find your own style.
When we ran this course last summer, a big complaint from students was that the textbook was not required, and that they had to tediously rewatch the videos whenever they were stuck. That's not so effective. Videos are great for the first time, and a textbook is better for reference. Think about how to make optimum use of both.
A heads-up: The Rectangle
class in the book is slightly different from the Rectangle
class in the Udacity course. We'll use the Udacity rectangle in exercises. It works a lot better in BlueJ.
lab_partner
for that purpose.You submit your homeworks in Canvas. Homeworks are due Wednesdays and Sundays at 6 pm. (There is a short grace period in case you have connectivity problems, so that you can get to the nearest internet café and upload your work.)
From now on, all your assignments will be programming problems. You will check them with a system called code-check, which is exactly the same system that is used inside Udacity for checking your programs. Work out your solution in BlueJ. When you think you are done, paste it to the code-check link and see if it passes. Once you are happy with the outcome, save the signed zip of the report and upload it to Canvas. You will upload three such files for the draft, and three for the final version.
Download the BlueJ projects for the three assignments here.
In this assignment, you will practice using the replace
method of the String
class. You will be given a variable sentence
, initialized with a sentence such as
String sentence = "Mary had a little lamb";
When your program is executed, a different sentence will be substituted so that you can't just print the expected answer for this sentence.
Your task is to replace all words “a” with “the”, so that the result in this case is “Mary had the little lamb”. That’s all for the draft, but there is a small catch. You can't just replace "a"
with "the"
. Try it out to see why not... Instead, you need to take the spaces before and after the words into account. (You can assume that the word “a” does not appear at the beginning or the end of the sentence.)
In the final version, you replace all words “a” with “the” and all “the” with “a”. For example, you should turn the sentence “I saw the cat and a lamb” into “I saw a cat and the lamb”
In this problem, you will be given a variable declaration of a rectangle, such as
Rectangle box = new Rectangle(5, 10, 20, 30);
Again, as your program is run, the rectangle may be replaced with another so you can't just hard-code the expected answers. Your job is to
Use this format for your output:
Area: 600 Area after growing: 1500 Difference: 900
For the draft, just compute and print the area. Don't grow the rectangle yet, and don't print any difference.
In this problem, you will start with the rectangle
Rectangle box = new Rectangle(50, 50, 200, 100);
You will move it to the right by 200 pixels, then down by 100 pixels, then to the left by 200 pixels. After the constructor and after each move, you will call
Canvas.snapshot();
This call makes a snapshot of what is currently on the screen, so that one can see what is happening.
For the draft, just call the constructor, draw the rectangle, and call Canvas.snapshot()
. Don't move the rectangle yet.
On the Udacity site, you will find a set of problem sets. These are not used in this class. Feel free to do them for practice and exam prep, but for course credit, do the assignments on these pages.