JAVA Fundamentals | Part 4 | Accepting User Input, Finding Sum of Two Numbers, Using JAVA Online Doumentation

Leave a Comment
Write Once, Run Anywhere!
"Write Once, Run Anywhere!"
Welcome everyone to the next session in our JAVA Tutorials.

In this tutorial, we would learn how to make a simple program to add two numbers by accepting the numbers from the user. Simultaneously we would learn how to use the online JAVA Documentation to refer to commonly used packages, classes and their respective methods used. Hence lets get started.

To access JAVA Online Documentation for JAVA SE 8, follow the link below:


Clicking the above link, we would get a screen as shown below. So as to be able to understand the functionality, lets divide the screen into three parts, labelled as I, II and III below.

JAVA SE 8 Documentation
JAVA SE 8 Documentation
The part I of the screen shows the list of packages JAVA contains. These packages contain respective classes which need to be included in our code so that we can use the corresponding methods that those classes contain.

The part II of the screen shows the list of classes that JAVA contains.

The part III is the general section of the screen that displays information related to your selections made on I and II.

It is highly recommended to keep reading this documentation regularly, especially for new programmers.

With the functionality of the JAVA Documentation explored, lets now get on to the addition program we were gonna make.

A basic JAVA code to add two numbers would look like this: 

Input
Input
Output
Output
Let us now understand the code.

At line 1, we have a regular comment entry which we have already studied in earlier tutorials.

At line 2, we are importing "Scanner" class from the package "java.util". The Scanner class contains methods that will enable us to read data entered by the user, which here would be numbers that the user enters that he wants to get added.
Try reading more about the package java.util or the Scanner class from the Documentation as we discussed above.
Also, if we are going to include a lot of classes from a particular package we are importing or are otherwise not sure of the correct class which we need to implement in the program, we can also give the import statement like
  • import java.util.*;
which will import all the classes of the written package.

At line 3, the public class we made by the name AddingTwoNumbers starts. The class ends its scope at line 16.

At line 5  the main method of our program starts. Note its signature.

At line 7, we make an object with the name "input" of the type "Scanner".
As you'd recall in the beginning of the program, we imported a class "Scanner". At line 7, we made an object of that class, named "input".
This object will receive input from the default input stream (keyboard).
Why? Because it will receive its input from "System" class' "in" object (System.in). System.in is the standard input stream. That's why.

At line 8, we declare three integer variables of name num1, num2 and sum. "num1" and "num2" would be the two numbers user would enter for addition while "sum" will contain the result of the addition.
Also, we could have declared those integers in the following way too:

int num1;
int num2;
int sum;

At line 9, 11 and 14 we print standard statements on the screen.You are recommended to view the functioning of print and printf functions from here:

JAVA Fundamentals | Part 3 | Learning to print text on the monitor

Notice the different format specifiers ( %s, %n, %d ) used for strings, newline characters and numbers.

Line 10 an 12 use the "nextInt()" method of the "input" object of Scanner class so as to input num1 and num2 from the user. We know that nextInt is an method because it is followed by parentheses ().
It should be noted that nextInt() is just one of the many different methods we can use from any object we create of Scanner class. To know more about the different input types we can use the Scanner class for, visit the following link:

Scanner | JAVA SE 8 Documentation\

and look for the different methods contained.
For example, nextLine() is used to input a string.

At line 13 we do a simple addition of num1 and num2, the result of which is contained in sum.

So here it was, a simple text code in JAVA that accepts two integers from the user, adds them and displays the result. Also, we learnt how to use the online JAVA Documentation for referring to the different methods, classes and packages in JAVA.With this, we conclude this lesson of ours. Stay tuned for the next one.

Take care :)



0 comments:

Post a Comment

Powered by Blogger.