Bridge Design Pattern

Bridge Design Pattern

Name: Bridge Design Pattern

Type: Structural Design Pattern

Purpose: Decouple abstraction from implementation
                (In other words using composite over inheritance)

Sample Problem and Solution:
Assume that you are required to create some Shapes with two different patterns. Some sample shapes are SolidColorCircle, GradientCircle, SolidColorSquare and GradientSquare. A possible design is provided below.
public interface Shape {
    public void draw();
}
public abstract class Circle implements Shape {
    private double radius;

    public Circle(double radius) {
        this.radius = radius;
    }

    @Override
    public void draw() {
        System.out.println("Create a circle with radius: " + radius);
    }
}
public class SolidColorCircle extends Circle {
    private String color;

    public SolidColorCircle(double radius, String color) {
        super(radius);
        this.color = color;
    }

    @Override
    public void draw() {
        super.draw();
        System.out.println("Fill with: " + color);
    }
}
public class GradientCircle extends Circle {
    private String colorX;
    private String colorY;

    public SolidColorCircle(double radius, String colorX, String colorY) {
        super(radius);
        this.colorX = colorX;
        this.colorY = colorY;
    }

    @Override
    public void draw() {
        super.draw();
        System.out.println("Fill with: " + colorX + " & " + colorY);
    }
}
Square, SolidColorSquare and GradientSquare are provided in GitHub.
Read More

Advanced Android Database

Even though this tutorial is named as Advanced Android Database, there is nothing advanced in Android database. The last Basic Android Database tutorial explains the quick and dirty way of using SQLite database in your application. This tutorial uses the same problem discussed in the last article, but to explain the recommended way of database access in Android.


The Create, Read, Update and Delete (CRUD) related methods are not explained in this article since those are already explained in the Basic Android tutorial. If you are new to Android Database, please read the Basic Android Database article first and then continue this article in order to learn the best practices.
Read More

Install Apache Tomcat on Ubuntu

Ubuntu repository does not provide the latest version of Apache Tomcat. If you are desired to install the latest version of Apache Tomcat and to add it to the Eclipse, this article is for you.


Step 1:
Download the latest version of Apache Tomcat from this link.

Step 2:
Open the Terminal (Ctrl + Alt + T) and enter the following command to change the directory.
cd /opt/

Step 3:
Enter the command given below to extract the Tomcat from  the ~/Downloads directory. If your downloaded file is in any other directory, replace the last parameter by the actual file path.
sudo tar -xvzf ~/Downloads/apache-tomcat-8.0.26.tar.gz

Step 4: (Optional)
Rename the folder name to apache-tomcat.
sudo mv apache-tomcat-8.0.26/ apache-tomcat/

Step 5:
Tomcat creates some files at the runtime inside this folder (Log files and some other configuration files). If you want to start Tomcat without root privilege, it is required to change the permission of this directory. Enter the following command to change the permission of apache-tomcat folder.
sudo chmod -R 777 apache-tomcat/

Step 6:
An environment variable has to be added to the system. Enter the following command in the terminal to open the /etc/environment in gedit.
sudo gedit /etc/environment

Step 7:
Add the following line at the end of the file. Save and close the gedit.
CATALINA_HOME="/opt/apache-tomcat"

The /etc/environment before the modification.


The /etc/environment after the modification.


Step 8:
Reload the environment variables to the current terminal using this command.
source /etc/environment

Step 9:
Enter the following command to start the Tomcat server.
$CATALINA_HOME/bin/startup.sh

Step 10:
Visit to the following URL.
http://localhost:8080/

If you get the Apache Tomcat home page, you have successfully installed the Apache Tomcat in your system.

Step 11:
To stop the Tomcat, enter the following command.
$CATALINA_HOME/bin/shutdown.sh

Integrate Tomcat with Eclipse
If you do not have the Eclipse, follow this link and install the Eclipse first.

Step 1:
Open the Eclipse.

Step 2:
Goto Windows → Preferences in the menu bar and select the Runtime Environments under the Server.


Step 3:
Click on the 'Add' button.

Step 4:
Select your Apache Tomcat version under the 'Apache' root and click 'Next'.


Step 5:
Browse and select the Tomcat installation directory.


Step 6:
Click on the 'Finish' button

Step 7:
Click on the 'OK' button to close the Preferences dialog.


Now you are ready to use Apache Tomcat from Eclipse.
Read More

Install the latest Eclipse in Ubuntu

This article shows you the way to install the latest version of Eclipse in Ubuntu. If you do not have Java in your system, follow this link and install the Java first.


Step 1:
Download the desired version of Eclipse from this link.

Step 2:
Open the Terminal (Ctrl + Alt + T) and enter the following command to change the directory.
cd /opt

Step 3:
Enter the command given below to extract the Eclipse from ~/Downloads directory. If your downloaded file is in any other directory, replace the last parameter by the actual file path.
sudo tar -xvzf ~/Downloads/eclipse-jee-mars-R-linux-gtk-x86_64.tar.gz

Step 4:
Open another Terminal (Ctrl + Alt + T) and enter the following command to create a shortcut file for eclipse.
gedit eclipse.desktop

Step 5:
In the opened gedit, copy and paste the following text.
[Desktop Entry]
Name=Eclipse
Type=Application
Exec=/opt/eclipse/eclipse
Terminal=false
Icon=/opt/eclipse/icon.xpm
Comment=Integrated Development Environment
NoDisplay=false
Categories=Development;IDE;
Name[en]=Eclipse
Name[en_US]=Eclipse

Step 6:
Save and close the gedit.

Step 7:
Enter the following command in the terminal to install the shortcut.
sudo desktop-file-install eclipse.desktop

Now search for Eclipse in the dashboard and open it.


Fix for unreadable tooltips
In Ubuntu, the default tooltip background is black, which makes it unreadable in Eclipse. To fix this problem, follow these steps.

Step 1:
Enter the following commands one by one, in a Terminal
sudo gedit /usr/share/themes/Ambiance/gtk-2.0/gtkrc
sudo gedit /usr/share/themes/Ambiance/gtk-3.0/settings.ini
sudo gedit /usr/share/themes/Ambiance/gtk-3.0/gtk-main.css

Change the values of tooltip_fg_color and tooltip_bg_color in all the files, to the following values and save the changes.
tooltip_fg_color:#000000
tooltip_bg_color:#f5f5c5

Step 2:
Restart the Eclipse.
Read More

Install Oracle JDK in Ubuntu

Oracle Java is the proprietary, reference implementation for Java. This is no longer currently available in a supported Ubuntu repository. This article shows you the way to manually install the latest Oracle Java Development Kit (Oracle JDK) in Ubuntu.


Note: This article uses JDK8_Update_$java_update_no to demonstrate the installation. In the provided commands, replace the version specific paths and file names to your downloaded version.

Step 1:
Download the latest JDK(jdk-Xuxx-linux-xXX.tar.gz) from this official link.
If you want to download to a remote server or if you simply prefer wget, use the command given in this Stackoverflow answer: Downloading JDK
Read More
Factory Design Pattern

Factory Design Pattern

Name: Factory Design Pattern

Type: Creational Design Pattern

Purpose:
  • Hide object creation logic
  • Decouple classes from the clients

Sample Problem and Solution:
Consider a problem where you are required to create a library which provides some 2D Shapes. Other developers will use your library to create some shapes with random measurements. A possible design is provided here.
package com.javahelps.shapes;

public interface Shape {
    public void draw();
}
package com.javahelps.shapes;

public class Circle implements Shape {
    private double radius;

    public Circle(double radius) {
        this.radius = radius;
    }

    @Override
    public void draw() {
        System.out.println("Circle");
    }
}
package com.javahelps.shapes;

public class Square implements Shape {
    private int width;

    public Square(int width) {
        this.width = width;
    }

    @Override
    public void draw() {
        System.out.println("Square");
    }
}
package com.javahelps.shapes;

public class Rectangle implements Shape {
    private int width;
    private int height;

    public Rectangle(int width, int height) {
        this.width = width;
        this.height = height;
    }

    @Override
    public void draw() {
        System.out.println("Rectangle");
    }
}
Read More

Android Spinner

In Android combo box is called as Spinner. This tutorial shows you, how to use Spinner in your application.

Step 1:
Create an Android project with a project name “Spinner Sample” and a package name “com.javahelps.sample.spinner”.

Step 2:
Modify the activity_main.xml as shown below. Notice that there is a UI component Spinner which is the combo box used to display some names of animals.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/txtInfo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:text="Select an animal"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <Spinner
        android:id="@+id/cmbAnimals"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/txtInfo" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/cmbAnimals">
        <ImageView
            android:id="@+id/imgAnimal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"/>
    </RelativeLayout>

</RelativeLayout>
Step 3:
The images I have used in this project can be downloaded from this link. Download the images and copy them.

Right click on the drawable folder and select “Paste”.


Read More

Custom Font in Android

This tutorial shows you, how to develop an application in your local language. Since some Android phones have problems with Unicode, you can use custom true-type fonts for your application. The same technique can be used to decorate your application using stylish English fonts as well. This tutorial develops an application to display a welcome message in Tamil. You can use your own font to display whatever you want.

Step 1:
Create an Android application “Custom Font” with a package name “com.javahelps.customfont”.

Step 2:
Add a TextView to the activity_main.xml as shown below.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/txtMessage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>

Step 3:
Right click on the “res” folder and select NewFolderAssets Folder.


Read More

Immutability Of String

Java defines some objects as immutable objects; developers cannot modify those objects once they are created. String objects and wrapper class objects are the best examples for immutable objects. This article covers the immutable objects and the reason for having immutable objects using String as an example.

Before getting into the actual content, it is required to have a basic knowledge about the equality operator (==). Usually equality operator is used to compare two primitive data types, but it has a different meaning when it is used with references. To compare the content of two objects, equals method is used and the equality operator is used to check whether two references are referring same object.
public class EqualityOperator {
    public static void main(String[] args) {
        Student stu1 = new Student(100);
        Student stu2 = new Student(100);
        Student stu3 = stu1;
        if (stu1 == stu2) { // false
            System.out.println("stu1 == stu2");
        }
        if (stu1.equals(stu2)) { // true
            System.out.println("stu1 equals stu2");
        }
        if (stu1 == stu3) { // true
            System.out.println("stu1 == stu3");
        }
    }
}
For the complete project click on this link.

In the above example stu1 == stu2 returns false, because they refer two different objects. stu1 == stu3 returns true because both references are referring the same object.

Read More

Android: Simple Calculator

This tutorial guide you to develop a basic Calculator Android application in Android Studio.

Step 1:
Create a new Android application project with an application name: “Calculator” and package name: “com.javahelps.calculator”.

Step 2:
By default Android uses a green Android robot icon. In this project, we are going to use a custom application icon. Therefore, delete the default ic_launcher icon set from the “mipmap” folder.


Step 3:
Get any PNG image file for the application icon. (It is recommended to have a minimum size 256x256 pixels). This icon is used to display in the Google Play as well as in the applications menu of Android devices.

Step 4:
Right click on the “mipmap” folder and select New → Image Asset


Step 5:
Browse and select your icon as the image file and click on Next → Finish buttons. (Make sure that the resource name is: ic_launcher)


Step 6:
Replace the content of activity_main.xml file by the following code. This code creates a TextView as the calculator number screen and some necessary buttons. TextView is used instead of EditText, in order to prevent manual user input using the default keypad of Android. In this code, some common properties of Buttons are not provided to reduce the length of this tutorial. In your code make sure that you have included these four attributes for all the Buttons.
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="30sp"
Read More

Contact Form

Name

Email *

Message *