CS46A Introduction to Programming—Online Section

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

Week 2

  1. I recorded two videos to explain how you will do the labs, why you should expect a deep plunge this week, how to use BlueJ and code-check, and what to do when you get stuck. Watch them here and here
  2. Work through the Udacity videos in Lesson 2 until the segment entitled “Copying Numbers”.
  3. 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.

  4. Quizzes are do every Monday and Thursday. Be sure to do Quiz 1 right away. It tests your understanding of Chapter 1.
  5. Homework 2 is due in two phases. A draft is do on Wednesday. In the draft, you will submit a partial solution. You may well find that you have no clue what to do, and that's a wake-up call. Start asking questions on Piazza. If you start asking questions on Wednesday and Thursday, you'll figure it out by Sunday. If you only start on Saturday, maybe not. In an online course, you will make yourself do what's right.
  6. This week, you need to work through Lab 1 and submit your lab report to Canvas.

How To Do the Labs

Homework 2

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.

  1. 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”

    code-check for draft | final

  2. 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.

    code-check for draft | final

  3. 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


    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.

    code-check for draft | final

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.