Archive

Archive for March, 2014

Exercise 1 – Using arguments and return values in Java

 

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…

Advertisements

Developing & using methods in Java

 

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

 

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 switch construct in Java

  

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

This post has the exercise – 2 for module 1 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-module1-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…

SpringSource Tool Suite Installation & Configuration

March 6, 2014 1 comment

 

SpringSource Tool Suite(STS) Installation & Configuration:

 

At the time of this post, the latest version available form SpringSource is STS 2.2.1. Since its built on top of Eclipse (version 3.5 – Galileo), you will need to have Java installed on your computer. Refer to Eclipse IDE category posts for details on this part. It requires JDK 5 and higher and is supported on Windows, Mac & Linux operating systems so far. I will be posting ‘Exploring Spring Framework’ series developed using STS, so this is a starter post for it. Moreover, its worth trying such a comprehensive Spring development tool which has been made available for free recently.

As such downloading and installing STS is straight forward. All you have to do is to visit the SpringSource STS  site and follow the instructions. Yet, here is step-by-step instructions and other relative information for doing it. 

 

Follow the steps below to download & install STS:

  • Before downloading STS, make sure that your system meets these pre-requisites. As you can see, you need to have JDK 5 or higher installed in your system. [Refer to Java Installation & Configuration post for details].

Read more…

Eclipse IDE Miscellaneous & Debugging Tips & Tricks

 

Eclipse IDE Productivity Tips & Tricks – Miscellaneous & Debugging:

 

In continuation to the previous post, this one has Tips & Tricks for Views in Eclipse IDE. Refer to posts in Eclipse Tips & Tricks for others.   

 

Note: If the image displayed below is unclear, then click on the image.  Then hover over the image in the newly opened window. You will get a zoom icon, click the image with it and it will be displayed with better visibility. Read more…