teaching machines

CS 145 Lab 5

September 30, 2011 by . Filed under cs145, fall 2011, labs.

Reminder

  1. Submit your already completed checkpoints from lab 4 in the first 20 minutes of this lab.
  2. Lab will not be held on October 12 to give you time to study and because your instructor is out of town. Submit any unfinished checkpoints from lab 5 on October 19.

Hurdles

When you’re first learning to program, you’ve got to overcome two hurdles:

  1. the syntax of the language
  2. the careful expression of a process

Many new programmers blame their difficulties on the crazy new syntax, when the larger problem is really failing to clarify the process they want to execute in their own heads. Clarification of a process is best done away from a computer and in your natural language. For the checkpoints this week, we’ll ask you to first sketch out a draft of your solutions on paper in pseudocode.

Pseudocode

Suppose your problem is this:

I’m administering an electronic election through a website. I’ve got a bunch of names of candidates, each with a number of votes. Who won?

Ultimately you’ll use a machine to determine the winner. But let’s just start writing down what we might do if we had to solve it with only our human powers:

  1. look at the first candidate — so far, he’s the winner
  2. second candidate has fewer votes, skip
  3. third candidate has more votes than the first — so far, she’s the winner
  4. fourth candidate has 0 votes, skip
  5. fifth candidate has fewer votes than the third, skip
  6. we’re out of candidates, so the third candidate wins!

We see some repetition in our solution, so we can simplify our pseudocode a bit:

  1. winner so far is the first candidate
  2. for each other candidate
    • if candidate has more votes than winner so far, this candidate is the winner so far
  3. the winner so far is the winner!

The process is now fairly clarified. Now we can go to a computer and translate our high-level process into Java.

If you want to enjoy programming and struggle less, spend some time working out your ideas in pseudocode.

Write pseudocode on paper to accomplish three of the following five tasks:

  1. Count the number of words in a block of text.
  2. Calculate the average of a set of prices.
  3. Decompose a list of the form “name1,name2,name3,name4” and print out each name on its own line.
  4. Extract all the URLs (http://*) from a chunk of text.
  5. Calculate the position of and plot n points evenly distributed along the circumference of a circle with radius 1.

Checkpoint #1: show your pseudocode to your instructor or TA.

Loops

Now, let’s rewrite those three problems in programming language terms. Four of these can be deftly solved with the aid of a Scanner and its next* methods. You may need to change where the Scanner gets its input. Up till now, we’ve just used System.in. Instead, you can pass a String as the input source. Check out Scanner’s other methods to help parse the text and set up your loop conditions.

  1. Write a method that counts the number of words in a String passed in as an argument.
  2. Write a main method that gets a set of prices from the user and prints the average.
  3. Write a method that decomposes its String argument, a list of the form “name1,name2,name3,name4,…”, printing out each name on its own line.
  4. Write a main method that extracts and prints all the URLs (http://*) from a chunk of text.
  5. Write a method that prints n points evenly distributed along the circumference of a circle with radius 1 in the form:
    (x1,y1)
    (x2,y2)
    (x3,y3)

    (xn,yn)
    You can paste the output at this website to verify your results.

Checkpoint #2: show your three solutions to your instructor or TA.