Sunday, June 1, 2014

IDEONE JAVA BASICS 101 - Getting Started

---
IDEONE Java Basics 101 – Getting Started

PREREQUISITE

This tutorial uses the online compiler IDEone (www.ideone.com ).
IDEone gives you few benefits:
1) Zero setup. You just need Internet Connection and a modern Web Browser.
2) Online storage. You can refer back to the codes at later time.
3) Public codes available. You can also see how others write codes and learn from them as well.
4) Online Learning. You can ask a companion at remote location to help edit the codes or review your codes for improvements or troubleshooting algorithms.

STEPS

1) Start with the basic template

The first part of the screen is called the Editor Window.
The second part of the screen is the Messaging Window which constitutes the:
1) Compiler Message
2) Program Input Field
3) Program Output Field
Notice also the fork button at the top left of the code editor window.
Click fork button to clone the above codes.
Now you should have got your own code window with codes similar to the above.

2) Using the IDEone Editor.

We are going to replace // your code goes here with the following codes:

                int first_number;
                System.out.println("My First Project");
Perform the following steps:

3) Observe the output

In general, Java Codes can be categorised into the followings:
a) Package Declaration - a way of identifying many source codes for various projects.
b) Import Declaration - registering java code libraries that you want to use for your codes.
c) Class - Java codes sources are physically saved and compiled as classes.
d) Method Declaration - Codes within a class can be further grouped into methods for better organization.
e) Instructions - Codes that actually instructs the computer to perform your required task.
As for a start, we are going to focus at Instructions only (point e in the list above). We will come to the other categories when we have understood the Java Basic Instructions.

4) Writing Instructions

Refer the code above (http://ideone.com/7vU4ve ).
There are two instructions here:
1) Declare an integer variable.
2) Output string to user.
We don’t see any impact of the first instruction (int first_number;) because it is an internal process.
Fork the code.
Edit line no.13  as follows.

                System.out.println(first_number);
Run.
We get Compiler Message as follows:

Error message: “variable first_number might not have been initialized
Error Location: System.out.println(first_number);
Total Errors: 1 error
Explanation: first_number has been declared but has not been given a value. It is an error to instruct computer to print a variable that does not have a value.
The compiler will not run the program because of this error. So, we need to fix it.
Edit the instruction as follows (add the highlighted codes to the existing codes) :

                int first_number;
                first_number=10;
                System.out.println(first_number);
Run.
The error has been fixed. The compiler outputs 10.
Next, we are going to combine both String and Variable values.
Edit line no. 14 as follows (refer highlighted codes) :

                System.out.println( "First number = " first_number );
Run.
The Compiler tells us that there are two errors.
The first error starts right after the string “First number =”.
Because of this error, succeeding codes were also not recognised by the compiler, thus leading to the second error.
All in all, the actual problem is only one, ie there must be an operator in between the string and the variable (JOIN OPERATOR).
Edit line no. 14 as follows (refer highlighted codes) :

                System.out.println( "First number = " + first_number);
Run.

5) Adding More Variables.

Edit the instruction as follows (add the highlighted codes to the existing codes) :

                int first_number, second_number, answer;
                first_number=10;
                second_number=20;
                answer = first_number + second_number;
                System.out.println("Addition Total = " + answer);
Run.
Lessons:
- You can declare many variables of the same daya type in a single statement.
- Number operation is similar to that of Mathematics.

REFERENCE

---

No comments:

Post a Comment