Category Archives for "Java"

Nov 21

Java Tutorial 2 – Fun with fundamentals

By Farvin Gonsalves | Java

Introduction

Before I can explain the different programming concepts in Java, let’s take a look at one of the basic building block in Java - Class. I will not go in depth in this tutorial but I need to make sure that you can follow along in the next tutorial.

Assuming that you followed the last post, let’s start by creating a new package in our Helpful Java project. To create a package right click on the src folder -> New-> Package, I will call this package tutorial2.

Now let’s create a class in this package, right click on the tut​​​​orial2 package -> New -> Class  and name it Person ->Finish

Class

Eclipse will create a new class called Person. So what is a class? A class can be anything that has some attributes or properties. It is the most basic building block in Java. Anything you want to do in Java will require a class.

Fields

The attributes or properties of a class are known as member variables or fields of a class. They may be also called data of a class. For our Person class, we can have the following fields: a name, date of birth, gender, height, weight, etc. For now let’s keep it simple with just name.

Methods

The fields in the class by itself are not of much use, we need to be able to perform operations on this data. More importantly we need to access the fields. For these reasons we create methods that allow us to manipulate and perform operations on the data. Methods are also known as functions.

Eclipse allows us to create some basic methods easily by using Getters & Setters. Click Source -> Generate Getters and Setters using Fields

Check name -> Click OK

You should see 2 methods created for you: setName() and getName()

This method will set the value for the class field name. Public means that this method can be accessed from outside of this class. Void means that this method doesn’t return or send back anything to the caller. setName is the name of the method and String name is a parameter taken by this method. String is the datatype of the parameter “name” and “name” is the name of the parameter. The name of the parameter can be anything you want.

this.name = name;

N​​​​ow this statement can get a little confusing so let’s get to it. This keyword refers to the current object or instance of the class. I have explained what an object is just down below.

Let’s try changing the parameter name to simplify things. Double click on the parameter name -> right click -> Refactor -> Rename… -> Type myName and hit enter.

Now we see

public String getName()                                                         

The getName() method returns or sends back a String type variable. As we can see the setName() had void whereas getName() has String.

return name;

This is the statement used to return something back to the caller. ’Return’ is a keyword in Java.

Objects

To use this class we will have to create an object of this class. Let’s first create a Junit test and in the test we will create the object.

Delete the fail("Not yet implemented"); line and type

Person farvin = new Person();

This is the syntax for creating an object of a class. Person is the class that we are using to create the object from. Farvin is the name of the object, (Farvin is my name, feel free to use your name or any name you want). The ‘new’ keyword is used to create an object. Person() is a constructor, we will look at constructors in a future tutorial. An object is an instance of a class. Objects allow us to access the data and methods of a class. The naming convention for objects is to use camelCase - start with lowercase and capitalise the next word if it’s a multi word name, for example “numberOfDays”.

Now we can use the setName() method. Type the following on the next line.

farvin.setName(“Farvin Gonsalves”);

assertEquals("Name doesn't match", "Farvin Gonsalves", farvin.getName());

Save and let’s run this test, Right click -> Run As -> Junit Test

We have a green bar, this means that the test passed and we have verified that setName() works as it’s supposed to.

Play Time

I know this is a lot to take in but these are the building blocks of Java, if you have any questions then please comment below, also if you liked this post then feel free to share it. I’m interested in continuously improving myself so if you have any feedback let me know below. Thank you for reading.

May 11

Helpful Java: Tutorial 1 – Hello Tester!

By Farvin Gonsalves | Java

Introduction

In this Java tutorial you will learn to install Java and Eclipse, create a project, write and run a simple test.

Download and install Java

If you don’t already have Java installed, you can download it from here. You need to download the JDK and then install it.

Download and install Eclipse

Using an IDE for development makes things easier for us.I will be using Eclipse since the community supporting Eclipse is quite large. You can download the latest version of Eclipse from here. Installing Eclipse is simple, here’s a link just in case.

Create your first project

When you open Eclipse, Eclipse will ask you to select a workspace, this is where eclipse will store the project files. If you’re not happy with the default option, browse to your desired location. 

Let’s create our first project. Click on File -> New -> Java Project

Enter a project name, I have used Helpful Java and Click Finish

Now let’s examine the folder structure

We will be writing code inside the src folder but first let's create a package. Packages are used to bundle a group of related classes together. Let’s create a package called tutorial1. To create a package, right click on the src folder -> select New -> Package

Name the package tutorial1. Click on Finish.

Creating your first test

Programming in Java is done using an Object Oriented(OO) approach. All the code is written inside a class. Classes have variables and methods that allow operations. To create a test case in Java, we need to create a JUnit class. JUnit is a unit testing library that makes it easy to write unit tests.

Right click on the tutorial1 package -> New -> Junit Test Case​


Enter the Name as MyFirstTest and click on Finish. (If you see a popup about JUnit 4 not being in the build path, click OK)

​The name of any class should always start with a capital letter and conventionally CamelCase is followed. JUnit test classes should also have the word Test either in the beginning or end of the class name

Now Eclipse will create the JUnit Class with some code in it. Let us go through it.

package tutorial1;

The first line package tutorial1; means that this class belongs to the tutorial1 package.

import static org.junit.Assert.*;

import org.junit.Test;

The import statements allow us to use methods from different classes. The classes are Assert and Test.

public class MyFirstTest

public means that this class can be accessed by any other class, class is the keyword used when creating a class, MyFirstTest is the name of the class and the curly brackets {} mark the body and scope of the class

@Test

The Test annotation tells JUnit that the public void method to which it is attached can be run as a test case.

public void test()

This is the test method declaration where public means this method can be accessed by other classes, void means this method doesn’t return anything and test() is the name of the method. Method names are followed by () parentheses.

fail("Not yet implemented");

Eclipse has filled in some code for us in this test case. fail is a method that will always fail this test when run.

Run the test

Let’s run the test to see what happens. Right click anywhere in the class and select Run As and click on JUnit Test. 

You should see a red bar. The test has failed.

Writing your first test case

We want to test that a message says “Hello Tester”. So first let’s change the name of the test from test() to messageIsHelloTester(). Do notice method names start with first word in small caps and conventionally camelCase is followed.

Next delete the fail("Not yet implemented"); statement and type this instead

String message = "Hello Tester";

assertEquals("Message is Hello Tester", "Hello Tester", message);

Your code should look like this:​

Click on the save button and run the test (right click -> Run As -> JUnit Test). You should see a green bar. The test has passed.

Now let’s go through the code that we’ve written:

String message = "Hello Tester";

String is a data type which stores a string of characters, message is a variable name of type String which is used to store a string, and “Hello World” is the string that is stored in message.

assertEquals("Message is Hello Tester", "Hello Tester", message);

assertEquals() is a method that takes 3 parameters: message, expected result and actual result. This method checks if the expected result is equal to the actual result and displays the message if it fails.

Play Time

Now that we’ve gone through the concepts, let’s play around with the code.

Change the line String message = “Hello Tester”; to String message = “HelloTester”;

Save and Run the test. Did the test pass? I don’t think so.


Now try making this test pass without reverting our new change. (Hint: You need to change something in assertEquals("Message is Hello Tester", "Hello Tester", message);


Were you able to get the green bar? If you did, well done! If not, don’t worry, here’s the change:

assertEquals("Message is Hello Tester", "HelloTester", message);


This concludes tutorial 1 of the Helpful Java Series. We have gone through quite a lot in this tutorial. If you haven’t understood something or are having trouble with anything regarding this tutorial, please leave a comment. Your feedback is appreciated.