Posts Tagged ‘methods’

Developing & using methods in Java


Module 3: Developing & using methods



  • 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…

Developing & Testing a program in Java

December 28, 2013 Leave a comment



Module 3: Developing & Testing a Java Technology program.



1. Identify  4 components of class.
2. Use main method in a test class to run a java program from command line.
3. Compile & execute a java program.


1. Identify  4 components of class:

Going back to what we discussed before about classes, to construct a house you will first need a blueprint. Same thing applies for classes & objects. Similarly once you have a class blueprint, you  can have as many objects of that class as u like. These objects are called instances of a class. i.e., a shirt object is an instance of a shirt class.


27. class and its objs


Lets take a look at our first class:

Class is a blueprint that tells the computer how to make objects. This class blueprint – Shirt, tells the computer how to make Shirt objects.


28. 1st class



Structuring classes:

There are 4 major components of class that you will find more often:

  • class declaration – required
  • attribute variable declaration & initialization – optional
  • methods – optional
  • comments – optional

1. Class declaration is required, and this is all that you need to make a class valid. If all you had was this, then the class and its objects would be uninteresting serving no purpose. You will be needing some attributes and operations to jazz up the object.

2. Variables are characterstics of your objects. As we discussed in previous posts, these are characterstics /  properties and they are also data holders. They hold the data for the object.

3. In this Shirt class, only one method is defined. Methods are operations. They tell the class what to do.  Its what the class can do.  This method is printing all its information to the command window.

4. Comments are for developers to understand what the code is doing, they dont influence the execution in anyway.


Class declaration: Read more…

Analyze a problem & design a solution in Java

December 25, 2013 7 comments


Module 2 – Analyze a problem & design a solution.



Analyzing a problem & designing a solution using object oriented concepts:

1. Analyse a problem using object oriented analysis
2. Design classes from which objects will be created


1. Analyze a problem using object oriented analysis:

In this process, the first step is to understand your domain & the domain will probably be the business you are trying to solve the pblm for, or the area to create new product.

 Case study:

A company called Direct Clothing  sells shirts from thier catalog.  We have to develop a product for this company for the same catalog. So initially we have to understand few points about thier shirts

  • Each shirt has an item identity(ID) – bar code
  • One or more colors – blue, green etc
  • One or more sizes
  • Have description – type of material, style etc
  • Price
  • Direct Clothing accepts cheques & credit card payments.


11. case study



The current system:

During analysis stage, you also have to learn about the current system if there is one. Currently, say there are 2 ways in which a customer can place thier order with Direct Clothing

  • Call customer care over phone and place the order.
  • Mail or Fax an order form to place the order.


12. current system


The proposed system: Read more…