Friday, August 19, 2016

Data Types and Simple Console Application in JAVA

Welcome to third part of the tutorial series of JAVA. Today we are going to code a few lines in JAVA. I am going to keep this tutorial as simple as possible. This post is written based upon the request of one of my juniors in the college.

What are Data Types and Variables?

This is a common terminology which is heard when we are programming in a type-specific language or which supports data types. The variables are those which are of some data type. To give a simple example of that, we can consider water in a glass bottle. In this scenario, the bottle is a variable, the material used to prepare bottle is glass, so bottle is a type of glass material. Water is the value which is filled into the glass bottle. The same can be represented as follows,

glass bottle = water;
(<Data_Type> <Variable_name/Identifier> = <Value/literal>;)

The above mentioned is a general way of the variable declaration and assigning a value to that. Now coming to a technical example, suppose we want to add two numbers by using variables, so what we want ? We want three variables say 'a', 'b', 'sum', if in case we need to store the sum of two numbers a and b in a third variable. So the statements in java to do this will be,

int a;
int b;
int sum = a+b;


Here the variables a, b and sum are of data type integers. The various data types and information regarding those is as mentioned in the table of data types supported in JAVA.


For simplicity let us consider these data types as containers of values. There are various data types because of the presence of different representations of the data or the values. In real world scenario, a metal bottle can hold boiling water, where as the plastic bottle cannot. Thus we will just consider the data types as containers for now.

How to take input from users ?

Simple solution for it is to import the java.util.* package and to include a line as mentioned below,

Scanner s = new Scanner(System.in);

In most of the coding competitions I personally use this in order to take input from users or the console. Now we have an example to look at adding two numbers and displaying their sum by accepting those two numbers from the user.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
import java.util.*;
 
class SumOfNumbers
{
 public static void main (String[] args)
 {
  Scanner s = new Scanner(System.in);
  int a,b,sum;
  a=s.nextInt();
  b=s.nextInt();
  sum=a+b;
  System.out.println("The sum is "+sum);
 }
}

As discussed in earlier post, every input and output will be in the form of String type. So, there should be a conversion from String to respective data type. But the beauty of Scanner class is that it has pre-defined methods in order to take input in several other data types too. In this case we have used the method nextInt() in order to take the integer input. So the conversion for this is not required (But it is better to use the data type conversion by accepting string itself, as shown in the next example). Some of the method in Scanner class are as mentioned below. 


Now another program to take the input from user console about his personal information and display the same.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
import java.util.*;

class BioData
{
 public static void main (String[] args)
 {
  Scanner s = new Scanner(System.in);
  int age = Integer.parseInt(s.nextLine());
  float salary = Float.parseFloat(s.nextLine());
  String name = s.nextLine();
  
  System.out.println("Hello "+name);
  System.out.println("You are "+age+" years old.");
  System.out.println("Your current salary is "+salary);
  salary = salary + 100;
  System.out.println("Now you have earned Rs.100 extra on your salary ");
  System.out.println("Your current salary is "+salary);
 }
}

Visit http://ideone.com/OKBCaa to work out this program. Here we have converted the String type to Integer as well as Float. These two as you notice are not in lowercase, hence they are classes. They are specifically called as Wrapper Classes in JAVA.

Here is an assignment for you if you wish to solve one.

"Mr. Ramesh wanted to calculate the sum, difference, product, quotient and remainder of two numbers, which he wish to provide as input from console. You as a JAVA programmer, help Mr. Ramesh to solve this problem."

Note: If you have not installed an IDE for coding in JAVA, then visit http://ideone.com/ for online coding or have a look at the previous post.


That's it for now ! Hope this article was informative and knowledgeable. Your comments, queries and suggestions are most welcome. See you soon in the next tutorial :)

No comments:

Post a Comment