Exercise 2 – Using overloaded methods in Java

 

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…

Advertisements

Eclipse IDE Workbench, Editing, Ant, Help Tips & Tricks

 

Eclipse IDE Workbench, Editing, Ant,  Help Tips & Tricks:

 

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…

Spring Framework Introduction

 

Chapter 2: Introduction to Spring

 

Spring Framework:

The Spring framework is a comprehensive layered Java/Java EE application platform. It was created to address the complexity of enterprise application development. The Spring Framework takes best practices and design patterns that have been proven over the years, codifies these patterns as first class objects that can be integrated into applications. Spring makes it possible to use plain-vanilla Java Beans (POJOs) to achieve things that were previously only possible with EJBs.

 

Spring includes:

  • An IOC lightweight container
  • AOP functionality
  • Abstraction layer for JDBC / transaction management                    
  • ORM integration layer
  • Web integration layer
  • A flexible MVC web application framework 

 

Spring is not only an application framework, it also serves as a platform for several open source projects that are based on the core Spring Framework project such as Spring IDE, Spring Security, Spring Web Flow, Spring Web Services, Spring Rich Client,  Spring Batch, Spring Modules, Spring Dynamic Modules, Spring Integration, Spring LDAP, Spring JavaConfig, Spring .NET, Spring BeanDoc.

 

Spring’s features:

Framework:

Spring makes it possible to develop and maintain complex applications using simple components. In Spring, application objects are configured declaratively typically in an XML file. Spring also provides much infrastructure functionality (transaction management, integration, etc.), leaving only the development of application logic to you.

 

Lightweight:

Spring is lightweight in terms of both size and overhead. Spring Framework can be distributed in a single JAR file that weighs just over 2.5 MB and the processing overhead required by Spring is negligible. Spring is non intrusive in a way that the objects in a Spring-enabled application often have no dependencies on Spring-specific classes.

 

Container:

Spring provides a container which contains and manages the lifecycle and configuration of application objects. In Spring, you can declare how each of application objects should be created, how they should be configured, and how they should be associated with each other.

 

Dependency Injection:

Spring promotes loose coupling through dependency injection (DI). When DI is applied, objects are passively given their dependencies instead of looking for dependencies themselves. You can think of DI as JNDI in reverse — instead of an object looking up for dependencies from a container, the container gives the dependencies to the object at its instantiation without waiting to be asked.

 

Aspect-oriented:

Spring comes with rich support for aspect-oriented programming (AOP) that enables separating application business logic from system services (such as auditing, transaction management, logging etc). Application objects do what they’re supposed to do i.e., perform business logic — and nothing more. They are not responsible for or even be aware of other system concerns, such as logging or transactional support.

 

Spring Framework Architecture: Read more…

Spring Framework Terminologies

 

Chapter 1: Terminologies

 

Design Patterns:

A design pattern is a conceptual general reusable solution to a commonly occurring problem in software design. A design pattern can’t be transformed directly into code. It is a template for how to solve a problem that can be used in many different situations. Design patterns such as Factory, Builder, Decorator, and Service Locator (to name a few) have widespread recognition and acceptance within the software industry. Design patterns are best practices given a name that describe what the pattern does, where the pattern is best applied and the problems that the pattern addresses etc.

 

Framework:

Framework is a step ahead of design patterns. It is basically a conceptually organized structure based on best practices and design patterns to address complex issues. A software framework is an abstraction in which generic functionality can be selectively overridden, specialized or extensible by user code. Frameworks are similar to software libraries in way that they are reusable code wrapped in a well-defined API. However unlike libraries, the overall program’s flow of control is not dictated by the caller, but by the framework. This inversion of control is the distinguishing feature of software frameworks.

 

Object-Oriented Frameworks:

An object-oriented framework is a semi-finished object-oriented application laid out on OOPS concepts such as Inheritance, Polymorphism, Abstraction and Encapsulation.   Object-oriented design patterns typically show relationships and interactions between classes or objects. It encapsulates common features that can be used across the same application or even different applications. Common examples of such frameworks include Apache Struts, JSF, and Spring etc. A framework-driven approach to application development usually involves integration of multiple object-oriented frameworks and creation of specific functionalities as extensions to these frameworks.

Every framework provides its own extension points called hotspots. Hotspots are specific to a framework, usually pertaining to integration (application programming/service provider interfaces) and configuration (external metadata). For example, the EJB 2.0 specifications define hotspots for Java bean objects to be deployed in an EJB container.

 

Inversion of Control:

Inversion of control is a generic principle and not a design pattern. Rather, it is a broad concept that is implemented by several design patterns.

Inversion of control is applied during communication between a framework and custom application logic. A common feature of frameworks is to maintain control of all communication activities within an application because of which the primary objective of an application module will only be to provide functionalities that can be invoked by the framework. Compare this to a scenario without frameworks, in which there is a significant effort on how the modules can invoke and manage each other.

 

Application modules won’t have to directly access each other’s capabilities when deployed on top of a framework. The same is also true for invocations from external entities. Instead, every request must be routed through the framework. The framework, in turn, can make multiple calls across more than one module in a controlled fashion before returning a result back to the caller.

In the light of what has been said so far, inversion of control can be summed up as:

“Don’t call us (framework); we’ll call you (application)”

 

Service Locator pattern: Read more…

Using operators and decision constructs in Java

May 17, 2014 1 comment

 

Module 1: Using operators and decision constructs.

  

Objectives:

  • Identify relational and conditional operators.
  • Create if and if / else constructs.
  • Use the switch construct.

 

1. Identify relational and conditional operators:

In everyday life, we make many decisions. For instance, if its raining I will use umbrella so that I dont get wet. This is a type of branching. If one condition is true, that’s if raining,  I take my umbrella. If the condition is not true but false, then I will not take my umbrella. This type of branching decision making can be implemented in Java programming using ‘if’ statements.

 

 

Identify relational and conditional operators 

 

But before we can discuss about “if” statements, we need to know about relational operators. Because in Java to make a decisions you have to test the relationship between things.

  

Relational operators:

The following is the relational table which shows the relational operators in Java. As the name relational implies, it says how 2 things are related to each other. All the operators listed here are binary operators. What this means is, they always work on 2 things which are left and right operands. These operators will compare the 2 operands together and returns is a boolean value, a true or false value. For ex, the == operator tells us if the 2 operands are equal, if they are it returns a true value, if not it returns a false value.  

 Relational operators

 

In the above example,
int i=1;
if(i==1), will return true.

Similarly we have different operators such as not equal to, less than, less than or equal to, is greater than and greater than or equal to operators. These operators are not going to sit by themselves and we are going to use them in the “if” statements.

  

Conditional operators:

A lot of times in Java we just want to ask very simple question, such as ‘is x greater than 2’. But there are other times when we want to ask bit complicated questions. For example, in real life we might want to ask something like “if its raining and if its muddy outside, i will take my umbrella and my boots”. To do this in Java we use the conditional operators. We have AND, OR and NOT operators to implement the conditional operations as shown in the table.

 Conditional operators

 

The AND and the OR are binary operators just like the relational operators were, that means that they have left and right operands and they also return true / false. The NOT is a unary operator, it only takes 1 operand and it also returns a true / false.

For the AND operator to return true, both the left and the right operand have to evaluate to true. If either one of them are false, it will return a false. For the OR operator, if either side of the operator evaluate to true, then the operator returns a true. If both of the operands are false, then it will return false. For the NOT operator, it basically just reveres the boolean value of the single operand. That is if a NOT value is applied to a true value then it returns false, and if its applied to a false value then it returns true. We shall take a look at some examples to better understand them.

Lets start with the AND operator represented by double ampersand (&&)

ex:
int i = 2;
int j = 8;
An && operator needs a true/false value on the left hand side and a true/false value on the right hand side. So one way to do that is using relational expressions, because we know relational operators return true or false.
((i<1)&&(j>6))
So, here on the left hand side we have, “is i < 1”, and as i = 2 and hence i is not less than 1, we get false. So the whole expression now will be false. Java doesnt even have to evaluate the expression on the right hand side here because for && operator to return true, both the operands have to be true. So, the minute it finds a false value, it returns false.

true && true = true
true && false = false
false && false = false
false && true = false

 

Lets take a look at the OR example represented by (||)

ex:
int i = 2;
int j = 8;
Again, || operator needs both sides to be true / false values.
((i<1)||(j>=10))
So here, i<1 is false. At this point it cant determine if it has to return true / false for the whole expression. So, it also has to check the right hand side. So j>=10, this is false. So both sides of the OR expression is false. so it returns false.

true || true = true
true || false = true
false || false = false
false || true = true

 

Lets take a look at the NOT operator represented by (!)

ex:
int i = 2;
(!(i>3))
Here we are first asking the question, i>3. it is not. so its false. But here we are applying the NOT operator. So, NOT operator returns reverse which is true.

!true = false
!false = true

  

  

2. Creating if  and if / else constructs: Read more…

Exercise 2 – String class in Java

 

Exercise – 2 for module 2 of Beginning to Program with the Java Programming Language:

 

This post has the exercise – 2 for module 2 of Beginning to Program with the Java Programming Language. 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-1102A-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…

Ant Installation & Configuration

  

  

At the time of this post, the latest stable version available form Apache is 1.7. Older releases of Ant can be found here. Old releases are only provided as zip archives that can be extracted by jar xf archive.zip. Its highly recommend to not use old releases but upgrade to Ant’s latest release.

Ant 1.5.4 is the last release that supports JDK 1.1, Ant 1.6.0 onwards requires JDK 1.2 or later. There will be no other compatibility issues with Java versions otherwise. Current version of Ant i.e., 1.7, requires JDK version 1.2 or later, 1.5 or later is strongly recommended. Latest the version of Java is, the more Ant tasks you get. If a JDK is not present, and only JRE is present, then many tasks will not work.

  

Follow the steps below to download, install and configure Ant : Read more…

Exercise 1 – Using if and if else in Java

 

 

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

 

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

Eclipse IDE Installation & Configuration

April 4, 2014 2 comments

 

 

At the time of this post, the latest Eclipse package version available to download is Eclipse 3.5.0. Eclipse releases are named after the moons of Jupiter, and each denotes a successive release. For Ex, Galileo is 3.5 which is the latest version, previous versions are Eclipse Ganymede which is Eclipse Version 3.4.0, and Eclipse Europa which is Eclipse Version 3.3.0. Eclipse is free for downloading. All you have to do is navigate to http://www.eclipse.org/downloads , chose and download the Eclipse package suitable for your requirements from one of the download mirrors available, such as:

  • Eclipse Classic 3.5.0 (The classic Eclipse download includes the Eclipse Platform, Java Development Tools, and Plug-in Development Environment, including source and both user and programmer documentation.)
  • Eclipse IDE for Java Developers (The essential tools for any Java developer, including a Java IDE, a CVS client, XML Editor and Mylyn)
  • Eclipse IDE for Java EE Developers (If you want to develop web applications, you should download Eclipse IDE for Java EE DevelopersTools for Java developers creating Java EE and Web applications, including a Java IDE, tools for Java EE, JPA, JSF, Mylyn and others.)
  • Eclipse packages for PHP, C/C++ Developers, etc.

 

 

Since Eclipse is a Java program, you will need to have Java installed on your computer to run Eclipse [Refer to Java Installation & Configuration post for details]. Eclipse can run on a number of Java Virtual Machines. Eclipse officially recommends Java version 5 (also known as 1.5), although many Eclipse users use the newer version 6 (1.6).

Sun’s Java is available in two main distributions: the Java Runtime Engine (JRE) and the Java Development Kit (JDK). If you are using Eclipse for Java development, the JDK offers several advantages, including the availability of source code for the Java classes. If you are using Eclipse for something other than Java development, the JRE is all you need. Once you have a Java VM installed, you’re ready to work with Eclipse.

 

Download Instructions:  Read more…

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…

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…