Lecture 11: Loops and Writing Methods


In the last lecture I went through some in-class demos of getting a guessing-game program working using loops. Today I'll embellish that a little further to finish up. Then, I'll move on to some examples using methods. To start with, remember that at the end of the last lecture we had just gotten our program to take in a number lower, and another number higher than the secret number the user was thinking of. We used that to help our while loop search a little more carefully. Now we'll use that same information to form a for loop.

Switching to a For-loop

Now that we've got upper and lower numbers for our loop to start and stop with, things can be a little more rigid. The upper and lower numbers can be used inside a for-loop statement instead of using a while loop. Note that the for loop and the while loop will do the exact same work in this case. The for loop can just provide a cleaner structure if you prefer it. The code below shows the switch to a for loop.

Changing How I Search

In an earlier lecture about algorithms, we had one puzzle about how to guess a number between 1 and 1,000,000 in only 20 yes or now questions. This fits very well into our guessing game program. I'd like to switch my guessing program to use this faster method of guessing. However, this would be a big change. So, rather than jump directly into writing code, I'm first going to put in comments detailing what I'd like to do. I'll cover this more thoroughly in class than here in the notes.

Once I've got a good idea what I'm trying to do, only now will I attempt to put those ideas into code. That code is given below:

Also, here's some links to the code files for all the examples used so far.

Code files: Loop1.java Loop2.java Loop3.java Loop4.java Loop5.java Loop6.java

Using Methods

This part of today's lecture will mostly be describing and demo-ing methods in class.

So far we've been using Java methods for every one of our programs, but we've only been using one: the main() method. A method is primarily just a block of code that executes together. A key part about it, though, is that it's supposed to take in information (parameters) and then return some information. Here's an example method.

	public static int multiplyBy2(int number){
		number = number * 2;
		return number;
	}

In the example above, my method's name was multiplyBy2. The method only took in a single parameter: a variable of type int that will be labeled with the name number. I then do a little work with that value and then return it using the return key word. This is what you can always use to return a value out of a method. However, a method stops executing whenever you return, so you only get to return once. One more part to this method: it's return type. In this case it's return type is int. We can see this because that's the return type that's indicated in the methods top line:

	public static int multiplyBy2(int number){
		number = number * 2;
		return number;
	}

So, in that top line, the 3rd word give the return type, after that is the name of the method, and next in the parantheses are any parameters that the method will take in. The last part of the method involves those curly braces: { and }. Those mark the start and end, respectively, of the block of code we want our method to execute. For the other two words, public and static for now just think of them as something you have to put on the front of your method to get it to work.

So far we're use to writing our code inside the main method. So here's a call to my multiplyBy2 method that will actually get it to run.

public class multTest {
	public static void main(String[] args) {
		System.out.println("Original number: "+2);
		int value = multiplyBy2(2);
		System.out.println("Doubled number: "+value);
	}
	
	public static int multiplyBy2(int number){
		number = number * 2;
		return number;
	}
}

Notice that my new method does not get put inside the curly braces for the main method. These are two separate blocks of code! The advantage of doing this is that now one method can call another. We've got an example of that in this code:

public class multTest {
	public static void main(String[] args) {
		System.out.println("Original number: "+2);
		int value = multiplyBy2(2);
		System.out.println("Doubled number: "+value);
	}
	
	public static int multiplyBy2(int number){
		number = number * 2;
		return number;
	}
}

All I have to do is give the method name and follow that with parantheses. Also, if the method takes any parameters, I have to give it all of those parameters (same types of parameters, same number of parameters, same order). Note that it's ok if the value I give as a parameter comes as a literal value, like the number "2" above, or if it comes from a variable as in the example below:

public class multTest {
	public static void main(String[] args) {
		System.out.println("Original number: "+2);
		int value = multiplyBy2(2);
		System.out.println("Doubled number: "+value);
		value = multiplyBy2(value);
		System.out.println("Doubled again: "+value);
	}
	
	public static int multiplyBy2(int number){
		number = number * 2;
		return number;
	}
}