Archive

Archive for the ‘WJ1103A – Using Decision and Looping Constructs with the Java Programming Language’ Category

Exercise 2 – Using overloaded methods in Java

November 19, 2009 Leave a comment

 

Exercise – 2 for module 3 of Using operators and decision constructs:

 

This post has the exercise – 2 for module 3 of Using operators and decision constructs. Let’s practice what we have learnt. Before practicing the exercise, follow the instructions below which will give you a step-by-step instruction:

 

 

 

 

You can download the source code (WJ-1103A-module3-exercise2.zip) for this exercise from the Box widget in the sidebar. Follow the video tutorial below if you ever get stuck while executing the programs. Read more…

Exercise 1 – Using arguments and return values in Java

November 19, 2009 Leave a comment

 

Exercise – 1 for module 3 of Using operators and decision constructs:

 

This post has the exercise – 1 for module 3 of Using operators and decision constructs . Let’s practice what we have learnt. Before practicing the exercise, follow the instructions below which will give you a step-by-step instruction:

 

 

 

 

You can download the source code (WJ-1103A-module3-exercise1.zip) for this exercise from the Box widget in the sidebar. Follow the video tutorial below if you ever get stuck while executing the programs. Read more…

Developing & using methods in Java

November 19, 2009 Leave a comment

 

Module 3: Developing & using methods

 

Objectives:

  • Describe the advantages of methods and define worker and calling methods.
  • Declare and invoke a method.
  • Compare object and static methods.
  • Use overloaded methods.

 

 

1. Describe the advantages of methods and define worker and calling methods:

Creating and invoking methods:

Let’s begin our discussion of methods by  looking into the syntax required to declare a method in a class.

 Creating and invoking methods 

 

[modifiers] -> The modifiers are optional keywords that modify the behavior of a method. Some of these modifiers are the keywords, public, private, protected and static. We will look into all of them in the coming posts.

return_type  -> First thing that all the methods should have is the return type. Every method can return utmost one value. If the method does not return anything, then the method must specify the keyword void. If it does return a value, the value can either be primitive or a object reference. For instance, if the method returns an integer, then the return type should be specified as int, if the method returns an object such as String, then the return value should be specified as String.

method_identifier -> This is the name that you assign to a method such as displayShirtInformation.

Then we have the paranthesis and inside of it we have arguments. The paranthesis are always required whether we have arguments or not.

[arguments] -> The arguments are optional. This will be a list of variables, whose values will be passed into the method as input. The method can then use those values to do something with them, such as print them to the console etc.

Then we have the left and right curly brace, enclosing the method_code_block. These are always required.

method_code_block -> These are lines of Java code, that you will want to be executed when the method is called.

 

Basic form of a method:

Let’s take a look at an example of method declaration. Here we have a class called Shirt inside of which we have a method called displayShirtInformation() that’s been declared. Notice the optional modifier public specified indicating that this method can be invoked from anywhere, the return type void specifying that the method doesn’t return anything, then the name of the method displayShirtInformation followed by a pair of paranthesis which is empty i.e., specifying that the method doesn’t take any arguments. Then we have the left curly brace which encloses the code block of Java code that we want to be executed whenever this method is called. And then we have a terminating right curly brace specifying the end of method body.

 

Basic form of a method

 

 

To the right, we have a class called ShirtTest. Inside of it we have a method called main method. Notice the optional modifier public specified indicating that this method can be invoked from anywhere. We also have static. You can have valid multiple combination of modifiers in the method declaration. We will cover it all in detail in the coming posts. Then we have the return type void indicating that this method doesn’t return anything. Then we have the method identifier as main. As we know from before, this is the entry point into a Java program.

Then we have the paranthesis enclosing one argument. We have an array of Strings called args that will be passed to this method. We will talk about arrays in posts to come. Then we have our left curly brace beginning our method body, then we have our lines of Java code that are to be executed whenever this method is called. And then we end the method body by closing the braces.

Notice that on line 9, the code is invoking displayShirtInformation() method in the Shirt class. So in this case the main method is called the calling method or the caller method whereas the displayShirtInformation() method is called as the worker method. When one method calls another, the calling method is called the caller method, and the called method is called the worker method.

Also, notice how we have called the displayShirtInformation() method. We have used the Shirt object reference called myShirt and the dot notation (.) and the name of the method. When one class wants to call a method in another class, it needs an object reference of that class to invoke a method in that class.

 

Guidelines for invoking methods: Read more…

Exercise 3 – Using do while loop in Java

November 18, 2009 Leave a comment

 

Exercise – 3 for module 2 of Using operators and decision constructs:

 

This post has the exercise – 3 for module 2 of Using operators and decision constructs . Let’s practice what we have learnt. Before practicing the exercise, follow the instructions below which will give you a step-by-step instruction:

 

 

 

 

You can download the source code (WJ-1103A-module2-exercise3.zip) for this exercise from the Box widget in the sidebar. Follow the video tutorial below if you ever get stuck while executing the programs. Read more…

Exercise 2 – Using for loop in Java

November 18, 2009 Leave a comment

 

Exercise – 2 for module 2 of Using operators and decision constructs:

 

This post has the exercise – 2 for module 2 of Using operators and decision constructs . Let’s practice what we have learnt. Before practicing the exercise, follow the instructions below which will give you a step-by-step instruction:

 

 

You can download the source code (WJ-1103A-module2-exercise2.zip) for this exercise from the Box widget in the sidebar. Follow the video tutorial below if you ever get stuck while executing the programs. Read more…

Exercise 1 – Using while loop in Java

November 18, 2009 Leave a comment

 

Exercise – 1 for module 2 of Using operators and decision constructs:

 

This post has the exercise – 1 for module 2 of Using operators and decision constructs. Let’s practice what we have learnt. Before practicing the exercise, follow the instructions below which will give you a step-by-step instruction:

 

 

You can download the source code (WJ-1103A-module2-exercise1.zip) for this exercise from the Box widget in the sidebar. Follow the video tutorial below if you ever get stuck while executing the programs. Read more…

Using loop constructs in Java

November 18, 2009 Leave a comment

 

Module 2: Using loop constructs:

 

Objectives:

  • Create while loops.
  • Develop for loops.
  • Create do / while loops.

 

 

1. Create while loops:

The while loop is used to execute a set of code for multiple times as long as a condition is true. Once the condition becomes false, the code block is no longer executed and the control exits the while loop and continues with the execution of rest of the program.

 

 Create while loops

We can see the syntax for the while loop in the syntax figure above. We begin with the keyword while, and then we put a boolean expression inside a set of paranthesis. If this boolean expression is true, we execute the code block between the left and the right curly braces. If the boolean expression is false, then we do not execute the code block and we leave the while loop. This code block will be executed as many number of times as long as the boolean expression continues to be true.

This is illustrated in a flow chart to the right. The boolean expression is tested for true / false. If true, then we execute the code block, and you can also see that we return back to again test the boolean expression after executing the code block, and again the expression will be tested for true / false. As long as the boolean expression continues to be true, the flow will be the same. Once the expression evaluates to false, the code block will no longer be executed and the control leaves the while loop.

 

While loop example:

Let’s take a look at an example of while loop. As you can see from the figure below, we have a while loop that is checking if the variable i is less than 5. If it is, we will enter the code block, and we will print out “Hello Thomas” to the command prompt / console. And then we are incrementing the i variable by 1. We will then again go back to the while loop condition and check if the i variable is still less than 5. As long as it is, we will continue to print to the console and continue to increment i and continue back to the while loop condition. Once its not, we will leave the while loop and continue with the rest of the program. Let’s see this for 2 different values of i.

 

For i = 0: 

 While loop example

 

When i = 0, the while condition is true for the first time, and we enter the loop and print “Hello Thomas” to the console, increment i by 1 because of which it becomes 1, and again go back to the while loop condition and check it again. Here i is 1 and still less than 5 so the whole loop is repeated for 5 times until i < 5 and “Hello Thomas” is printed 5 times to the console. Once i is 5, i < 5 will be false so it will exit the while loop and continue with the execution of the rest of the program.

 

For i = 5:

 While loop example 1

 

 

When i = 5, we check the while loop condition first, which is i < 5, as we have i as 5, we get this condition to be false. Hence we wont enter the code block, hence not printing anything to the console and continue with the execution of rest of the program.

 

Nested while loops: Read more…