Go to Top

Monday, 21 March 2011

Basics of Programming

                            Now let us start with the programming part. For programming, we need to know some basic statements and commands to write some basic programs. Let us first view the structure of a basic program and then each of its parts will be defined :

class (user defined name)
{ -------------------------------à Opening Braces ---à Indicates the starting point of the class
    public static void (user defined name)(int d) -------- à “( )” indicates a method ..........(1)
     { ----------------------------à Opening braces ---à Indicates the starting point of the method
          int a,b,c; ----------------à Defining variables (Note the semicolon at the end)
          a=10; ------------------à \   
          b=20; ------------------à  | ----à Assigning values to variables
          c=0; -------------------à /
          c=a+b; -------------------à Performing mathematical calculation (Addition) ……….(2)
          System.out.print(“The sum is “+c); ---------à Print statement ……….(3)
     } ----------------------------à Closing braces -----à Indicates closing of the method
} --------------------------------à Closing braces -----à Indicates closing of the class

(1)   ------------------------------à Keywords public, static, void : The word ‘public’ is one of the access specifiers. The other specifiers are ‘private’, ‘protected’, and ‘default’. They restrict the access to the source code to certain levels. Public allows everyone to run or edit the contents of the program. Private allows for access of the variables and methods within the same class, package, and sub-classes. Protected allows for the access of the variables and the methods within the same class only, and default allows for their access in the same package. The keyword 'static', when applied to a method, makes it independent of the object of the class. Thus it can be executed without creating and using the object of the class. The keyword 'void' declares that the method has no return type i.e. it cannot return any value, after its execution, to any other method of the class. We can specify a datatype to the method due to which it will have to return a value that has the same datatype as the method. For example : we can write "public static int display( )". We can pass a parameter to a method by declaring a variable and giving its datatype. The method will then take input from the user for the variable when the method is executed. In this way, we can take inputs from a user in Java.

(2)   ------------------------------à Arithmetical calculations : The calculation simply needs to be written just after the equal to sign i.e. “=” in the Java format. For addition, a plus sign (“+”) is to be given, for subtraction, a minus sign (“-“) is to be given, for multiplication, an asterisk (“*”) is to be given, and for division, a forward slash (“/”) is to be given. First brackets can be provided to define the order in which the calculation has to be done, but the number of opening brackets must be equal to the number of closing brackets.
(3) ------------------------------à Print statement : The syntax of the print statement in Java is as follows :

Syntax : System.out.print(“Text”+variable);

To print text, the text has to be put within double quotes “” inside the brackets and to print the value of a variable, the variables has to be put inside the brackets without any quotes. To print text alongwith variables, they have to be concatenated or joined together by a plus sign. System.out.println( ) is used to print the next output in a new line instead of the same line. 

No comments:

Post a Comment