Class 12 Informatics Practices Revision Notes Ch 3 Java Programming Fundamentals

Revision Notes for CBSE Class 12 Informatics Practices Chapter 3 -Free PDF Download

Free PDF download of Best CBSE Class 12 Informatics Practices Chapter 3 Java Programming Fundamentals Quick Revision Notes & Short Key-notes prepared by our expert Informatics Practices teachers from latest edition of CBSE(NCERT) books.

Class 12 Informatics Practices Quick Revision notes Chapter 3 Java Programming Fundamentals

Rapid application development refers to describe a method of developing software through the use of pre-programmed tools and pre-designed wizards. These tools or controls can be used in an application by simply dropping them on a designer screen to design the interface of the application. NetBeans IDE provides an environment for developing applications for Windows platform.
Integrated Development Environment (IDE)
An Integrated Development Environment (also known as integrated design environment, integrated debugging environment or interactive development environment) is a software application that provides comprehensive facilities to computer programmers for software development.
An IDE normally consists of:

1. a source code editor
2. a compiler and/or an interpreter
3. build automation tools
4. a debugger.

Sometimes a version control system and various tools are integrated to simplify the construction of a GUI. Many modern IDEs also have a class browser, an object browser and a class hierarchy diagram, for use with object-oriented software development.
NetBeans IDE
NetBeans IDE can be used to create Java applications easily. It has an efficient GUI builder.
NetBeans offers many features for application development, some are as follows:

• Drag-drop GUI creation
• Web-services
• Excellent debugging
• Wizards, code generation and management tools and many more.

Different components of NetBeans IDE are as follows:

1. Titlebar The titlebar is on the top of the window, containing the name of application.
2. Menubar Below the titlebar, there is a menubar. It contains many options, i.e. pull down menu. These options can be used to perform various tasks.
3. Toolbar Toolbar contains many small icons for performing various tasks.
4. GUI Builder It is also known as Design Area or Design Space. It is an area to place components and construct GUI application visually. There are two types of views of the GUI builder, the Design View and the Source View. In Design View, you can see the user interface of your application while in Source View, you can add/edit the code for your application. We can switch over from one view to another by simply clicking on the source and design tabs directly above
the Design Area.
5. Palette Palette contains controls or components used to create GUI applications.
6. Inspector Window This window is used to display a hierarchy of all the components or controls placed on the current form.
7. Properties Window Using this window, we can make changes in the properties of currently selected control on the form.
8. Code Editor Window It is the area where we write code for our Java applications. This window is displayed when we click on the source tab.

Components
Components are also known as Widgets. These are the basic interface elements. User interacts with JLabel, JButton, JTextField, etc. Components are placed on a container (like the JFrame).
There are two types of controls:

1. Parent or Container Controls They act as a background for other controls, e.g. Frame.When we delete a parent control, all its child controls also get deleted. When we move a parent control, all its child controls also move along with it.
2. Child Controls Controls placed inside a container control are called child controls, e.g. TextField, Label, Button, etc.

Basic Graphical Controls of Swing
Some of the commonly used controls of swing are as follows:

1. JFrame The JFrame is used to display a separate window with its own title bar.
2. JLabel The JLabel is used to display uneditable text. It means the user cannot change the information.
3. JTextField The JTextField is used to display a text box. In this text box, the user can input or edit the data. JPasswordField does not directly display its content. Infact it uses a single character (usually an ASTERISK) to represent each character that it contains, so that it is possible to see how many characters have been typed, but not what they are. As its name suggests, JPasswordField is intended to be used as a simple way to accept a users password. JPasswordField is derived from JTextField.
4. JButton The JButton displays a push button. The push button is used to generate the associated action event.
5. JCheckBox The JCheckBox is used to display a check box. The check box is used to allow the user to select multiple choices out of given choices. When the user selects a particular choice, a ‘✓’ is shown in front of it.
6. JList The JList displays a list for the application. We can select multiple elements from this list.
7. JComboBox The JComboBox provides a drop-down list of items. We can select an item from this list. Also, we can add a new item in the list. Infact, the combo box is a combination of a list and a text field.
8. JPanel The JPanel is used to organise the components in a GUI application. It is a supporting container, which cannot be displayed but can be added to another container.
9. JRadioButton The JRadioButton provides the radio buttons for the application. During the execution of a program, we can set these radio buttons either ON or OFF.

Note The area on the frame, where GUI components are placed is called content Pane.
Java Character Set
Character set refers to a set of valid characters that the language can recognise. In Java, Unicode characters are used.
Token
The token refers to the smallest unit of a program. In Java language, many tokens are used. These are keywords, identifiers, literals, punctuators and operators.
(i) Keywords The keywords are the reserved words defined in the language compiler. These reserved words are used for special purpose and cannot be used for any other purpose such as an identifier name.
Commonly used keywords are as follows:

(ii) Identifiers The identifiers are used for naming different items of the programs such as variables, characters, classes, functions, arrays, etc.
The identifiers are subject to following rules:

1. The identifier cannot be a reserved word.
3. It must start with an alphabet, dollar sign or an underscore.
4. It can contain alphabets, digits, underscore and dollar sign.
5. There is no limit on the length of the identifier.

Note Java is a case sensitive language and we can use both uppercase and lowercase characters for naming an identifier. Here, a name written in uppercase is different from the same name written in lowercase (e.g. a and A are two different identifiers).
(iii) Literals The literals or the constants are those items, whose values cannot be changed during the execution of the program. There are number of literals used in a Java program. These are integer literals, floating literals, boolean literals, character literals, string literals and null literals.
(a) Integer Literals The integer literals are the whole numbers without any fractional part. We can enter the integers as decimal numbers (base 10), octal numbers (base 8) and hexadecimal numbers (base 16).
The decimal number is entered as a number which begins with a non-zero digit (e.g. 35), for an octal number we have to enter the number that begins with a zero (e.g. 026) and a hexadecimal number must start with Ox or OX. (e.g. 0x2B).
(b) Floating Literals The floating literals or the real numbers are those numbers which have fractional part. The numbers can be written either in fractional form or in exponential form. e.g. 35.45. To write 5.3 x 103, we have to write 5.3E03 or 5.3e03.
(c) Boolean Literals The boolean type literals can have a boolean type value i.e. either true or false.
(d) Character Literals The character literal is one character enclosed in a single quotes, e.g. ‘a’.
(e) String Literals When multiple characters are entered such as a name of a person or a place. The string literals are enclosed by double quotes, e.g. “xyz”.
(f) Null Literals The null type literal has only null value. It is formed by a literal null.
(iv) Punctuators (or Separators) A punctuator is a token that has syntactic and semantic meaning to the compiler, but the exact significance depends on the context. A punctuator can also be a token that is used in the syntax of the preprocessor.
(v) Operators There are several kinds of operators. Operators are used in expression to describe operations involving one or more operands. In the expression a + b, ‘+’ is an operator involving two operands (a and b).
Data Types
The data types refer to identify the type of data and the associated operations of handling it. Broadly, the data types can be categorised into two categories as:
1. Primitive or Intrinsic Data Types The primitive data types are the basic data types of the Java language.
The primitive data type can be divided into following four categories:
(i) Numeric Integral Primitive Types
(a) byte The byte type occupies 1 byte of memory space. It can hold a signed integer in range of —128 to + 127.
(b) short The short type occupies 2 bytes of memory space. It can hold a signed integer in range of—32768 to + 32767.
(c) int The integer type occupies 4 bytes of memory space. It can hold a signed integer in range of — 2147483648 to + 2147483647.
(d) long The long type occupies 8 bytes of memory space. It can hold a signed integer in range of – 9223372036854775808 to + 9223372036854775807.
(ii) Fractional Primitive Types
(a) float The float type occupies 4 bytes of memory space. It can hold a single
precision floating number in range of 1.401298464324817E—45 to 3.402823476638528860E+38 (1.4E-45 to 3.4E+38). ‘
(b) double The double type occupies 8 bytes of memory space. It can hold a double precision floating number in range of ± 4.9 E-324 to ± 1.798E+308.
(iii) Character Primitive Type
char The char type is used to hold a single character. In Java Unicode, encoding system is used for encoding the characters. It can hold a character variable range from 0 to 65535.
(iv) Boolean Primitive Type
boolean The boolean type is used to represent a boolean value, i.e. true or false. The size of this datatype is not precisely defined.
2. Non-Primitive or Reference Data Types These data types are constructed from primitive data types. Among the reference data type, there are array, class and interface.
These data types are used to store the memory address of an object:
(i) Array It is a collection of many elements but of similar data type. It provides us facility of keeping together more than one variable in memory.
General form

data_type arrayname[];

Here, data_type determines the type of values array will store. It can store any valid data type values but all values must be of same data type, arrayname is the name of array and [ ] symbolises that this is an array.

e.g. int role[];

(ii) Class It is a way to bind data and functions associated with it together.
General form
class class_name

{
//variable declaration
//method declaration
};

(iii) Interface These are just like class. They specify what all methods they will contain but do not give method definition.
Variables
A variable refers to an item, whose value varies or changes. Infact, it is a named location in the memory, which can hold a data value of a particular data type.
To declare a variable following format is used:

data_type variablename;
e.g. int age;float salary; etc.

We can also initialise the variable at the time of declaration:

e.g. int age = 10;

Constant
To make a variable as a constant, final keyword is used. Such value cannot be changed during the execution of the program.

e.g. final float PI = 3.14;

Text Interaction Methods in Java
For text interaction in Java usually following types of methods are used:
(i) getText() Method get text from a GUI component.
The getText( ) method is used to return the text stored in the text based GUI component such as a text box. e.g. if the variable name is stuName, then to obtain text from the stuName text field. We need to type following statement:
String Name = stuName.getTextl );
(ii) setText() Method set text into a GUI component.
A setText() method is used to store or change text in a text field, e.g. to change a text in a text field named stuName we need to write: stuName.setText(“Kritika”):
(iii) parse() Method Converts String to Numbers from a GUI component.
The parse() method is used to convert a string into different numeric types. In Java, there are different types of parse methods are used.
Some of them are as follows:
(a) Byte.parseByte(String s) Used to convert a String s into a byte type value.
(b) Short.parseShort(String s) Used to convert a String s into a short type value.
(c) Integer.parseInt(String s) Used to convert a String s into an int type value.
(d) Long.parseLong(String s) Used to convert a String s into a long type value.
(e) Float.parseFloat(String s) Used to convert a String s into a float type value.
(f) Double.parseDouble(String s) Used to convert a String s into a double type value.
(iv) JOptionPane.showMessageDialog() Method display message in a dialog box.
To produce a basic dialog box, we can use this method. It will display a dialog box containing the desired information with OK button. To close the dialog box, we need to click at the OK button.
In order to use this method, we have to follow these steps:
(a) In the source editor, we need to type the following statement at the top most position

import javax.swing.JOptionPane;

(b) Then do display the desired message we need to specify the desired text as per following syntax

JOptionPane.showMessageDialog(Null,"This is a message”):

Non-GUI Output Methods
For displaying an output on console window (or terminal window) in a Java program, we can use either of two methods i.e. System.out.print() or System.out.println().
1. System.out.print( ) This method displays the text and keeps the cursor in the same line.

e.g. System.out.print(“There is an output on console window”);

2. System.out.println() This method displays the text and then moves the cursor to the next line.

e.g. System.out.println("There is an output on console window”);

Operators in Java
An operator on one or more variables and perform on the all tasks related to programming language. It consists of words or symbols. In Java language, many operators are used. We can categorise these operators as follows:
1. Arithmetic Operators In Java language, following five arithmetic operators are used:

2. Increment/Decrement Operators There are two forms of increment operator (+ +) and decrement operator (- -) as follows:
(i) Prefix Form In prefix form, the operator + + or is used before the operand. In
such form, first there will be increment or decrement in the value of the variable and then it is used. This form follows the change-then-use rule:
e.g.

int a = 5, b;
b = ++a;

Here, the value of b will be 6, also the last value of a will be 6.
(ii) Postfix Form In postfix form, the operator + + or – – is used after the operand . In the postfix form, first the value of the variable is used, then there will be increment or decrement in the value of the variable. This form follows the use-then-change rule:
e.g.

int a = 5, b;
b = a++;

Here, the value of b will be 5 and the last value of a will be 6.
3. Logical Operators The logical operators are used to make a decision on two conditions. Logical operators are mainly used to control program flow. There are three logical operators used in Java.
These are as follows:

• AND Operator (&&) This operator combines two given conditions and evaluates the result to true, if both of the given conditions are true.
• OR Operator (||) This operator combines two given conditions and evaluates the result to true, if either of the given conditions or both of the conditions are true.
• NOT Operator (!) The NOT operator is an unary operator. It negates the given condition.

4. Bitwise Operators The Bitwise operators work on bits and perform bit-by-bit operation. These operators manipulate the values of data at bit level. These operators are generally used for testing and shifting of bits.
These are as follows:

• Bitwise AND (&) This operator combines two given conditions and evaluates the result to true, if both of the conditions are true bitwise.
• Bitwise OR (|) This operator combines two given conditions and evaluates the result to true, if either of the given condition or both of the conditions are true bitwise.
• Bitwise Exclusive OR (ˆ) This operator combines two given conditions and evaluates the result to true, if one of the given conditions is true and other is false.

5. Relational Operators The relational operators are used to compare two operands and determines the relationship between them. Most of relational operators are used in “if’’statement and inside “loop” statement in order to check truthness or falseness of condition. The relational operators are binary operators and returns boolean value. The relational operators and their uses are given below:

6. Assignment Operators For assigning a value to a variable, the assignment operator is used. In Java, “=’ is the assignment operator, e.g. to assign 5 to a, we use a = 5.
Shorthand Assignment Operators
In Java language, five shorthand assignment operators are used, these are +=, – =, *=, /= and %=.
e.g.

x = x + 5 can be written as x += 5;
x = x - 5 can be written as x -= 5;
x = x * 5 can be written as x *= 5;
x = x / 5 can be written as x /= 5;
x = x % 5 can be written as x %= 5;

7. Conditional Operator It is used when one condition and two expressions are given.
Syntax

When the value of condition is true then first expression will be returned. If the condition is false, then second expression will be returned whichever value is returned is depend on the condition.
Operators can also be divided into three parts, on the basis of number of operands:

1.  Unary Operator An unary operator requires only one operand. Example of unary operators are unary + , unary -, + + and – -. These operator performs task only on a single operand.
2. Binary Operator A binary operator requires two operands. Examples of binary operators are + and %.
3. Ternary Operator A ternary operator requires three operands. Example of ternary operator is ? : (the conditional operator).

Type Conversion
When different types of constants and variables are mixed in an expression, then they are converted into the same data type, this conversion of data type is known as type conversion. The conversions in Java language are of two types namely implicit type conversion and explicit type conversion.
1. Implicit Type Conversion
The implicit type conversion is performed by the compiler itself. There is no intervention of the programmer in this regard. This is done to avoid the data loss in case when multiple types of values are used in an expression.
Java compiler converts all operands up to the type of largest operand, which is known as Type Promotion. The implicit type conversion, wherein datatypes are promoted, is known as coercion.
e.g. if one of the operand is int and another one is double, then the int is automatically converted into double. Similarly, if one of the data type is float and the other one is double, then the float type will automatically converted into double.
2. Explicit Type Conversion
Whenever a programmer wants to assign a data type of higher size to a data type of lower size, he has to do it manually. Because compiler cannot do it automatically. This type of type conversion is known as explicit type conversion.
The type casting or explicit type conversion is performed in Java as follows:
Syntax

(type) expression

e.g.

(float) (a / b);

Constructs of Java Program
In a Java program, the specified statements can be executed either sequentially, selectively or repeatedly.

1. Sequence Normally, the statements of a Java program are executed sequentially, i.e. in the same order as they are specified in a program.
2. Selection When we need to execute the program selectively, we have to use a conditional statement.
3. Iteration When there is a need for executing certain statements repeatedly, we
have to use a loop structure or iteration statement. .

1. Selection Statements
The selection statements are used to execute a statement or a group of statements depending upon a given condition.
There are following selection statements used in Java language:
(i) The if statement The if statement tests a given condition, if the given condition evaluates to true, the associated statements will be executed otherwise the control of the program will be transferred to the next statement.
The syntax of if statement is as follows:

if(condition)
statements;
e.g. if(i==5)
System.out.println(“Five”);

(ii) The if-else statement The if-else statement is an extension of the if statement. In this format, the given test condition will be evaluated, if the condition is evaluated to true one set of statements will be executed otherwise another set of statements will be executed.
The syntax of if-else statement is as follows:

if(condition)
statement 1;
else
statement 2;

(iii) Nested if statement When an if statement is inside another if or if-else statement, then it is called Nested if statement.
The general syntax of this statement is as follows:
(a)

 if(condition 1)
{
if(condition 2)
statement 1:
else
statement 2;
}
else
statement 3;

(b)

 if(condition 1)
statement 1;
else
{
if(condition 2)
statement 2;
else
statement 3;
}

(c)

 if (condition 1)
{
if(condition 2)
statement 1;
else
statement 2;
}
else
{
if(condition 3)
statement 3;
else
statement 4;
}

(iv) The if-else-if ladder form In this kind of statement, a number of logical conditions are checked for executing various statements. This is based upon the sequence of nested if and is often called the if-else-if ladder.
General form

if(condition)
statement;
else if(condition)
statement;
else if(condition)
statement;
else
statement;

(v) The switch statement The switch statement is a multiple branch selection statement. This statement tests a given condition. On the basis of the evaluation of test condition against a list of integers or character constants, the associated statements will be executed. The switch statements searches for a specified match, when a match is found it will execute the statements specified thereafter till the end of the structure or until the break statement is found. The fall of control to the following cases of matching case, is called Fall-Through. We can use an optional default with the switch statement. It will be executed, when no match is found.
The general syntax of switch is as follows:

switch(expression)
{
case value1:
statement 1;
break;
case value2:
statement 2;
break;
case value3:
statement 3;
break;
default:
statement;
}

Note The expression should be in integer, character and byte type.
2. Looping or Iteration Statements
The looping statements are used to execute a set of programs repeatedly until a given condition is found true. In Java language, four types of looping statements are used. These are for loop, while loop, do-while loop and nested loop.
(i) The for loop The for loop is used to execute a part of program repeatedly upto a fixed number of times. It is also known as Entry Controlled Loop.
The general form of the for loop is as follows:

for(initialisation; condition; increment or decrement)
statements;
e.g.
for(int i=l;i<=5;i++)
System.out.println(i);

It will display the following output:

1
2
3
4
5

(ii) The while loop It is also known as Entry Controlled Loop. The while loop is a conditional loop. It executes a part of the program repeatedly until the given condition is satisfied.
The general form of the while loop is as follows:

while(condition)
statements;
e.g. int i=l;
while i<=5)
{
System.out.println(i);
i +=2;
}

On execution of above statements, the following output will be displayed:

1
3
5

(iii) The do-while loop It is also known as Exit Controlled Loop. The do-while loop is similar to while loop, but it is an exit controlled loop. Here, the test condition is given at the end. It means the do-while loop will be executed once even if the test condition is found false at the starting of the loop.
The general syntax of the do-while loop is as follows:

do
{
statements;
}while(condition);
e.g. int i=2;
do
{
System.out.println(i);
i +=2;
} whi1e(i<=10);

It will display the following output:

2
4
6
8
10

(iv) The nested loop When one loop is specified inside another loop, it is called nested loop.
e.g.

for(int i=l;i<=5;i++)
{
for(int j=l;j<=i;j++)
System.out.print(j + “ ”);
System.out.println();
}

This code will display following output:

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

3. Jump Statements
In jump statement, the control of a program transfers unconditionally within a function.
In Java, following jump statements are used:
(i) The break statement The break statement can be used with for loop, while loop, do-while loop or switch statement. The break statement terminates the loop and moves the control to the next statement after the loop structure. The remaining statements of the loop will not be executed.
The general structures of break statement are as follows:
(a)

while(condition 1)
{
if(condition 2)
break;
:
}

(b) for(initialisation; condition 1; increment or decrement)

{
:
if(condition 2)

break;
:
}
(c)

do
{
:
if(condition 1)
break;
:
} while (condition 2);
e.g. i nt i=l;
while(i<=10)
{
if (i—5)
break;
System.out.println (i);
i++;
}

This code will display following output:

1
2
3
4

because as the value of i becomes 5, the break statement will be executed which will move the control to the ending of the loop and remaining statements in the loop will not be executed thereafter.
(ii) The continue statement The continue is a jump statement. It will cause to skip the remaining statements in the loop and moves the control to the beginning of the loop for next iteration.
The general structures of continue statement are as follows:
(a)

while(condition 1)
{
:
if(condition 2)
continue;
:
}

(b)

 for(initialisation; condition 1; increment or decrement)
{
:
if(condition 2)
continue;
}

(c)

do
{
:
if(condition 1)
continue;
:
} while(condition 2);
e.g.for(int i=l;i<=10;i++)
{
if(i>=4 && i<=8)
continue;
System.out.println(i);
}

The above code will present following output:

1
2
3
9
10

because when the value of i is 4, 5, 6, 7 and 8, the continue statement will be executed which will move the control to the beginning of the loop and skipping the remaining statements.
(iii) Return Statement This statement is used to explicitly return from a method. It causes program control to transfer back to the caller of the method. The return statement immediately terminate the method in which it is executed.
Syntax       return (Expression);