CS 145 Lab 5
Reminder
- Submit your already completed checkpoints from lab 4 in the first 20 minutes of this lab.
- 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:
- the syntax of the language
- 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:
- look at the first candidate — so far, he’s the winner
- second candidate has fewer votes, skip
- third candidate has more votes than the first — so far, she’s the winner
- fourth candidate has 0 votes, skip
- fifth candidate has fewer votes than the third, skip
- 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:
- winner so far is the first candidate
- for each other candidate
- if candidate has more votes than winner so far, this candidate is the winner so far
- 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:
- Count the number of words in a block of text.
- Calculate the average of a set of prices.
- Decompose a list of the form “name1,name2,name3,name4” and print out each name on its own line.
- Extract all the URLs (http://*) from a chunk of text.
- 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.
- Write a method that counts the number of words in a String passed in as an argument.
- Write a main method that gets a set of prices from the user and prints the average.
- 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.
- Write a main method that extracts and prints all the URLs (http://*) from a chunk of text.
- 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.