Thursday, October 30, 2008

Heads up! Homework for tomorrow is to implement Review:SpanishNumbers on page 161. If you get stuck try brainstorming and writing stuff down to kick start the design process. Be prepared to show me something even if you don't get it working.

No more slacking on the homework!!!

Tuesday, October 28, 2008

There's a madness in his method

We started methods today (Ch 07 in your book). You find the PPT presentation that I was running in the handout folder now. It's called ICS3M_2_Ch07.ppt. Note that I've included a bunch of stuff in the slide notes so you should view the slide in Notes view.

On a really general level methods are a way to support modularity in computer software in general and in Object-Oriented programming (of which Java is an example) in particular. Methods are a way of grouping together code that performs a specific/singular function in a way that hides the details of how that function is implemented from the main body of the code and that promotes the reuse of that code. We'll talk about that aspect (the implementation hiding aspect) of methods tomorrow. If I fail to mention this issues please remind me, OK?

You'll need to know the details of how we actually include, code and call methods in our code.

Calling methods is easy. You've been doing it all along. Just put the method name along with any arguments in the brackets in your code and it will be executed under the appropriate circumstances.

For instance: System.Convert.toInt16("3457"); is a method call. So, for that matter, is System.out.println("Some kinda textin' goin' on");

For 10 points everybody, how do we know if a piece of code is a method call?

IT HAS BRACKETS



We also reviewed for loops.

We remember how a for loop works by



I
nteresting



C
hallenges



E
xcite



I
nnovators


where


I
- Initialize the counter

C
- Check the (exit) condition

E
- Execute the body

I
- Increment the counter


Be prepared to chant it tomorrow.

Monday, October 27, 2008

Visual J#

A student told me today that he was being prompted for a registration key when he tried to start J# at home. That indicates to me that he had downloaded Microsoft J# Visual Studio Express 2008 rather than Visual Studio Express 2005. If you're having this problem you probably need to download the 2005 version of J# which you can locate here: http://www.microsoft.com/express/2005/download/default.aspx

Tomorrow it's on to Chapter 7 - Methods. If you want to get ahead a little, read pp 157 to 161.

Tuesday, October 14, 2008

Getting Started

As I walked 'round the class today while you were working on the AccountSetup program, I noticed more than a couple of you have trouble just getting started. Once you get over that initial hump, you seem to be able to make progress. But the first step's a doozy.

So, how to get started? How, for that matter, do I get started?

It's a hard process to describe and I suspect that I don't always proceed in the same way.

The first thing I do when getting started is to pick something simple that the program asks me to do. In AccountSetup (p. 139) one of the things we're asked to do is to get a user name and later on to display it. So I'd do that first. I'd declare a variable to hold the user name and then prompt the user to enter a user name and since I'll have to display it later, I might as well as display it now.

String userName;

System.out.print("Enter a user name, please: ");
userName = System.Console.ReadLine();

System.out.println("Your user name is " + userName);

OK. That's a start. And it also relieves you of some of the mental effort of keeping track of that particular part of the puzzle.

The other thing we had to do in AccountSetup was to prompt the user for a password and keep prompting them until they entered a password with at least 8 characters. Of course, this means that we need a loop (because we're going to keep prompting them until we get a password that 8 characters or more long). It also means that you'll need another String variable to store the password in. This part of the program can be implemented with either a while or a do...while loop. And you might as well make the condition that keeps the loop running the thing that checks to see if the password is 8 characters long or not.
do
{
System.out.print("Enter a password (8 characters minimum): ");
password = System.Console.ReadLine();
} while (password.length < 8);

I'll leave the last part up to you. That's the output part. Don't forget to convert the user name and password to lower case!!

Thursday, October 9, 2008

Factor this, buddy!

Almost everbody has either done the Factorial program or is almost there. The really tricky part is seeing how using the for loop (or any loop, for that matter) allows you to incrementally solve the problem of calculating the factorial. Here's my take on it.


The factorial of a number is what we get when we multiply all the numbers between 1 and the number together. (We write it as 5! or 4! or whatever.)


5! is 5 * 4 * 3 * 2 * 1 (= 120)

4! is 4 * 3 * 2 * 1 (=24)

3! is 3 * 2 * 1 (=6)

The difficulty (conceptually speaking) is how to implement this relatively simple piece of mathematics in a loop.

Let's look at 4!

4! is 4 * 3 * 2 * 1 and is equal to 24.

Look at the diagram below. Note that going from right to left, the number we are multiplying by increases by 1. That's our loop control variable (which I was calling idx today) because it is always incrementing by a set amount.

In each loop we multiply the product of the previous loop by the current value of the counter. So in loop 3 below we multiply 2 (which was the product from the previous loop) by 3 which is the counter value in loop 3. In loop 4, we multiply that value (6) by the counter value (which is now 4) to get 24. We exit our for loop when the counter reaches the value that we got from the user.



Loop 4 * 3 * 2 * 1
2 | | |___|
| | |
3 | 3 * 2
| |_____|
| |
4 4 * 6
|______|
|
24



Tomorrow, we'll be looking more closely at Strings and some neat methods we
can use to manipulate them. Read pp 138 and 139 if you get the chance.


Tuesday, October 7, 2008

Loop Tips

Here are a couple of pieces of advice regarding how to handle loops that arose from working with some of you today. Neither of these is always true. Consider them rules of thumb to be followed unless there's a reason not to.
  1. Always increment your counters at the bottom of the loop after your loop processing has been completed. This helps you to control any effects that your counters might have on your calculations.
  2. Report the results of loop calculations ouside the loop after it exits.

Please don't forget the homework for tomorrow. It's page 149, Qs 2, 4a, 7, 9, 10. It would also be helpful to re-read pp 135-137.

Finally, we'll also finish reviewing the test so bring your papers if you want to be able to track the corrections.

Wednesday, October 1, 2008

Gettin' Loopy

Today we had our first encounter with loops in the form of the while and the do...while loop. (pp 131-134 in the text) Here are a couple of things to remember about loops:

  • the body of the loop (which can be one statement or many) keeps executing repeatedly (i.e. looping) while the loop condition remains true.
  • the loop condition is the same type of Boolean expression as you find in the if and if...else statements
  • the loop condition is evaluated (to see if it's still true or not) every time the loop iterates
  • when the loop condition turns to false the loop stops executing and the program continues with the next statements after the loop
  • an improperly formed loop can iterate infinitely. (Bad, bad, programmer.)
Tomorrow we're going to talk about the for loop which is useful for iterating when the maximum or minimum number of iterations is known ahead of time. We'll also be talking about some debugging techniques that you can use to fix problems in longer programs.

Homework for today? Why, yes, glad you asked. P149, Qs3, 5, and 6.