PROGRAMME CODING- JAVA

Programming is the process of translating the algorithm and flowchart to a source code in

the syntax of a programming language, so that, it can be executed by the computer and a useful output can be obtained. Without an algorithm (steps to do a task) there can be no programme.

A programme is a set of instructions that instructs the computer to perform a task and results in an output. To get an expected output, instructions need to follow the rules of the programming language, which the programmer is using. These rules are called syntax.

A programmer, writes the programme in high level language. A high level language is any programming language that enables development of a programme in a much more user friendly programming context. It is independent of the computer’s hardware architecture. It focuses more, on the programming logic rather than the underlying hardware components such as memory addressing and register utilisation.

The translation of the programme from high level to low level language is done by Compilers and Interpreters, which themselves are the programmes written in low level languages.

BASIC, C / C++ and Java are popular examples of high level languages.

The interpreter reads a programme line and converts each line to machine language and executes it.

A compiler on the other hand, translates all the instructions of high level source code written by a programmer in a high level programming language into low level language (binary codes) in machine language.

ERRORS OF A PROGRAMME

1.   Syntax Errors - When the grammatical rules of a programming language are violated,  an error that occurs is called a syntax error. For example,  in Java, every statement must end with a “;”. If this “; is omitted, it will be considered as a syntax error.

2.   Run Time Errors -  The errors that occur during the execution (while the programme is being run)  of a programme are called  Run Time errors.  For example, if there is an instruction, which attempts to divide an integer by a zero, it results in a Run Time error “Division by zero” message.

3.   Logical ErrorsWhen there is no syntax or run time error, the programme executes, but provides an unexpected output which could be due to an incorrect logic, the programme is said to have logical error. For example, a programme which should convert a temperature in Celsius to Fahrenheit gives an incorrect answer, could have suffered a logical error due to wrong formula.  

TOKENS OF A PROGRAMMING LANGUAGE

Tokens are the smallest building block of a programme. They are the fundamental units, using which a programme is built. Some of the tokens in a programming language are :

(i)                Keywords – int, float, char, etc. 

(ii)          Identifiers - Names given to different parts of a programmes. They are named  by programmers while following certain rules :

(a) Starts with an alphabet or underscore.

(b)  First character must be followed by alphabets, digits or underscore.

(c) Space and special characters are nor allowed.

(d)  Keywords cannot be the names of the identifiers.

(iii)           Literals(Variables) – They are constant values. The different tupes of literals are:

(a) Integer literals, e.g. 90,

(b)  Decimal literals, e.g. 89.23 or 89.23f,

(c) Character literals, e.g. ‘a’,

(d)  Boolean literals, e.g. true and false,

(e)String literals, e.g. “aditi”.

iv)            Punctuator – They are also called separators. For example, comma (,) and semicolon (;).

(v)              Operators – Symbols applied on operands and yield a specific result.


TYPES OF OPERATORS

 1.     Assignment Operators

Equal to sign (=) is an assignment operator which is used to assign a value to a variable. Variable stores a value based on the data type of the variable. E.G., int x=10;

Here, variable x is declared as an integer variable. It can store an integer number (i.e., 10)


2.   Arithmatic Operators


3. Relational Operators – 



4.   Logical Operator


 Variables

 

A variable is a memory location that stores data/value. The type of data that it stores depends on the data type used to declare a variable. The value of the variable can change in a program. Variables are named by programmers following the rules for naming identifiers.

 

Data Types

A data type represents a keyword of the programming language that allocates sufficient storage space (memory for the data)

 


A program is given below to compute the area of a rectangle with length =50 cm and breadth = 15 cm. The following is to be written in the “BlueJ” application which uses the programming language of Java.

 

// compute the area of a rectangle//

class prg

{

  void display()

{

     int length =50, breadth = 15;

     int area = length * breadth;

     System.out.println (“Area of given rectangle =”+area);

}//end of method

}//end of class

 

To see the output of the program, follow the given steps :

1.     After typing the program,click on ‘compile’.

2.     Close the window and right click on the file named prg;

3.     Choose ‘create object’ and a window will open up naming the program as prg 1.Click on OK.

4.     A red box will appear at the bottom of the window. Click and open it.

5.     The output of the program will be shown as :

“Area of given rectangle =750”

 Decision making statement

 The decision making statements form the selection construct of the program. The statements of the program are executed based on whether the condition is true or false. The flow of control is selected based on the result of the condition. E.g., if – else, if – else – if, switch.

 General syntax of if – else :

 if (text expression)

{

   // Block containing any valid Java statements if the text expression is evaluated to true.//

 }

   else

{

  // Block containing any valid Java statements if the text expression is evaluated to false.//

 }

 Given below is a program following the above syntax:

 // to input a number and check whether it is positive or negative.//

 class prgm

 {

   void display (int n)

 { if (n>0)

 {

  System –out.println (n+”is a negative number”);

}

 else if (n<0)

{

System.out.println(n + “is a negative number”);

}

else

{

   System.out.println(n + “ is a zero”);

}

}//end of display

}//end of class

The way to view the output will remain the same except for a slight change, i.e. once the red box with the name of the class appears at the bottom of the window,we will have to:

 (i)                Right click on it and choose ‘ void display ()”.

 

(ii)             A box will open where we can input any numerical for the variable. This is to be done at times when the value of the variable is not mentioned in the code itself.

 (iii)           Once the value has been inserted, click on OK and click on the red box to view the output.

E.g.> In the above  program (prgm) if n=5, then the output will be shown as “5 is a positive number.”

Post a Comment

If you have any doubt, pl. let me know

[blogger]

MKRdezign

Contact Form

Name

Email *

Message *

Powered by Blogger.
Javascript DisablePlease Enable Javascript To See All Widget