Learning java for selenium – part II

Learning java for selenium

In this tutorial, we will extend the previous tutorial on a different approach for learning java for selenium. This is specially relevant for the manual testing professionals, who wish to move to selenium automation testing. This learning approach will help them in picking up java programming with less pain and difficulty.

This has been our experience with most of the professionals, who come to our Academy for learning Selenium. At Techcanvass, We are constantly working to simplify the learning process for Java, so that manual testers can pick up java skills easily.

In the first part of this java tutorial, we learnt the first step towards learning java.

Part I : Best way to learn java

The labyrinth way

In the first part of this tutorial, we discussed about the labyrinth approach to learn java. This approach has been the preferred approach to teach foreign languages across the world. This is actually a goal-driven approach and works best for a quick and focused learning.

We will develop a simple interest calculator in this post. The input will be dynamic and will be accepted from the user.

The Goal of the program

The first step is to understand the goal of the program and work backwards. So what’s the goal of the program?

Goal: Calculate Simple interest based on user inputs

The next step is to write down the steps to achieve the goal. This is absolutely important step.

  • Step 1: Create java program structure
  • Step 2: Accept user input
  • Step 3: Calculate Simple Interest
  • Step 4: Print Simple Interest

Step 1: Create java program structure

First of all, let’s start by referring to our basic java program structure. As we discussed in the first part of this tutorial, here is the program structure:


As explained, some parts of java program is standard. So let’s create the template by filling up specific information. Let’s say, the name of the program is Our program will look as shown below:

java-program-structureWe have used Javatutorials as package name. I have added the steps in the program structure only to keep the focus. This completes the first step.

Step 2: Accept user input

At this stage, we have no clue as to how this can be done, so the best way to approach it, is to use Google. Conversely, you can go through the documentation and understand the ways, but that’s difficult and will take much more time.

Using Google

A simple google search on “accepting user input in java” fetches number of results. Out of millions of search results, you can pick up anyone in the top 5 and that should be fine. In our case, I chose the second one and that led me to the page, which showed the following result:


It clearly shows how to accept user input. In our simple interest calculator, what do we need to accept?

  • Principal amount
  • rate of interest
  • Duration in years

For simplicity, we will keep all of them to be non-decimal. This is known as Integer in java. So for our program, the code would be as follows:

Scanner in = new Scanner(;
System.out.println("Enter the principal amount:");
p = in.nextInt();
System.out.println("Enter the rate of interest:");
r = in.nextInt();

System.out.println("Enter the duration in years:");
t = in.nextInt();

So, if we add these in our program, our program now looks as shown below:


Understanding Code

Let’s try and understand the pieces of the program.

Scanner in = new Scanner(;

This line of code is a standard way of accepting input in Java. You can see a RED CROSS on the left side of the code. This is an error message displayed by Eclipse.

Java needs to understand the reference to the scanner term. Java has encapsulated all the functions in the libraries. In this case, if you click on the RED CROSS, you can see the suggestion. It suggests to add “import java.util.scanner”.

We can add the following line in the top and that will resolve the error.

package Javatutorials;

import java.util.Scanner;

The other piece of code is about accepting the user input

System.out.println("Enter the principal amount:");
p = in.nextInt();

We have already discussed about “System.out.println()”. The next line is the code for accepting an integer (a non-decimal number). in.nextInt() will allow the user to type in the value. Int in nextInt() indicates the type of input value.

The user input value should be stored in some variable so that it can used later. “p=” will store the user input value of principal amount in variable p. Since the user input is integer, the variable storing it should also be integer. So, we will make a small correction:

int p = in.nextInt();

Step 3: Calculate Simple Interest

This is relatively simple. We need to use the simple interest formula to calculate the interest. The formula can be written as follows:

Simple Interest = Principal X Interest X Duration / 100

Here is the code for calculating simple interest.

int si = p * r * t / 100;

Step 4: Print Simple Interest

We can print the calculated value of simple interest as shown below:

System.out.println(“The simple interest is : ” + si);

Complete Java Program

Here is the complete Java program:

package Javatutorials;

import java.util.Scanner;

public class calculateSI {

public static void main(String[] args) {

//Get user inputs

Scanner in = new Scanner(;

System.out.println(“Enter the principal amount:”);
int p = in.nextInt();

System.out.println(“Enter the rate of interest:”);
int r = in.nextInt();

System.out.println(“Enter the duration in years:”);
int t = in.nextInt();

////Calculate Simple Interest

int si = p * r * t / 100;

////Print Simple Interest

System.out.println(“The simple interest is : ” + si);



Running the program

The program will ask for user inputs when you run the program as shown below. I have run this program with the following values:

Principal = 10,000

Rate of interest = 8

Duration = 5 years

The execution of the program:


The simple interest calculator is ready. However, this program has one issue. If we run this program with the following values:

Principal = 17867

Rate of interest = 7

Duration = 7 years

The actual value of simple interest should be 8,754.83. But if you use our simple interest calculator, the program will print 8,754.

This error happens because the variable “si” is an integer. It can’t store the decimal values and hence “.83” got truncated. We need to use “Double” data type instead of “Int” and that will work.


In this tutorial, I have created a simple interest calculator and used a two-pronged approach to do so.:

  • In the first step, I wrote the steps involved in creating the simple calculator
  • Then, I used google and other resources to fill in the code for each of the step

In the third and concluding part, I will discuss another important aspect of learning java.

About Techcanvass

Techcanvass is a software training organization and offers certification courses in Business Analysis and automation testing. Our courses in selenium are:

Selenium Certification (CP-SAT) Training

Selenium Certification Course






Selenium Training with Java

Selenium Training Course