UNIT 2 Programming
Key Points:-
IDE- Integrated development Environment:
o A programming environment, where all the tools required for programming are available under one roof is called IDE.
Key Points:-
IDE- Integrated development Environment:
o A programming environment, where all the tools required for programming are available under one roof is called IDE.
RAD- Rapid Application Development:
o A programming style which aims at building programs fastly through the use of tools and wizards is called RAD.
Token:
o The smallest individual unit in a program is known as Token. Java has the following types of tokens: keyword, Identifier, literal, punctuators and operators.
Keywords:
o Keywords are words that have a specific predefined meaning in Java. They cannot be used as variable names. They are also known as reserve words. Eg. void, private, if, while etc.
Literals:
o items having fixed data values are referred to as Literals. They are also known as Constants. Various types of literals available in Java are :
• integer literals
• Floating literals
• Boolean literals
• Character literals
• String literals
• Null literals
Variable:
o Variable is a named storage location in computer memory whose contents can change during a program run.
Data Type:
o Data type helps us identify the type and the range of value that can be stored in a variable. For example a short data type variable in java can store only non-decimal values in the range -32768 to 32767. Various primitive data types available in java are byte, short, int, long, float, double, char and Boolean.
variable
Types
Variable
IS used to temporarily hold value in the memory.
All variables in the Java language
must have a data type. A variable's type determines the values that the
variable can have and the operations that can be performed on it. For example,
the declaration int count declares that count is an integer (int).
Integers can have only whole number values (both positive and negative) and you
can use the standard arithmetic operators (+, -, and so on) on integers to perform
the standard arithmetic operations (addition, subtraction, and so on). There
are two major categories of data types in the Java language: primitive types
and reference types.
Primitive types contain a single
value and include types such as integer, floating point, character, and
boolean. The following table lists, by keyword, all of the primitive data types
supported by Java, their size and format, and a brief description of each.
Type Size/Format Description
(whole
numbers)
byte 8-bit two's complement
Byte-length integer
short 16-bit two's complement
Short integer
int 32-bit two's complement
Integer
long 64-bit two's complement
Long integer
(real
numbers)
float 32-bit IEEE 754 Single-precision floating point
double 64-bit IEEE 754 Double-precision floating point
(other
types)
char 16-bit Unicode character A single character
boolean true or false A boolean value (true or false)
Operators:
o Operators are symbols or group of symbols, which represent a operation in java. Operators in java can be classified as Unary operator- operators that require only one operand like ++, -- etc; Binary operator – operator that require two operands like +, - *, > <, == etc.; ternary operator – which require three operands like?:.
Relational Operators in
Java
Greater than: >
Less than: <
Equal to: ==
Not equal to: !=
Greater than or equal to: >=
Less than or equal to:
<=
Logical Operators in Java
Logical AND: &&
Logical OR: ||
Logical NOT: !
Scope
of a Variable:
o The part of program where a variable is usable is called scope of a variable.
Block: there are two scopes : global , local scope.
o A group of statement enclosed in pair of parenthesis {} is called block or a compound statement.
o The part of program where a variable is usable is called scope of a variable.
Block: there are two scopes : global , local scope.
o A group of statement enclosed in pair of parenthesis {} is called block or a compound statement.
Postfix, prefix OPERATORS
Both the increment operator (++)
and the decrement operator(--)
come in two varieties: prefix and postfix. The prefix variety is written before
the variable name (++myAge); the
postfix variety is written after (myAge++).In a simple statement, it doesn't much matter which you use, but in a complex statement, when you are incrementing (or decrementing) a variable and then assigning the result to another variable, it matters very much. The prefix operator is evaluated before the assignment, the postfix is evaluated after.
The semantics of prefix is this: Increment the value and then fetch it. The semantics of postfix is different: Fetch the value and then increment the original.
This can be confusing at first, but if x is an integer whose value is 5 and you write
int a = ++x;
you have told the compiler to increment x
(making it 6) and then fetch
that value and assign it to a.
Thus, a is now 6 and x
is now 6.If, after doing this, you write
int b = x++;
you have now told the compiler to fetch the value in x (6)
and assign it to b, and then go
back and increment x. Thus, b is now 6,
but x is now 7. Listing below shows the use and
implications of both types.
int Total=50,Number=10;
Total=Total
+ Number++;
Total
will be 60 and Number will be 11
int Total=50,Number=10;
Total=Total
+ ++Number;
Total
will be 61 and Number will be 11
Selection statements:
If Statement:
o If statement helps to execute a block of statement based on the result of a condition. If the condition set evaluates to true on block of statement is executed otherwise another block is executed.
if (Condition)
Statement;
[else
Statement;]
‘else’ part of ‘if statement’ is optional, if the user doesn’t provide an else part and the condition evaluates to false, then nothing would happen. Complier will not produce an error in this case.
Switch Statement:
o A Switch statement is used execute a statement from a group of statement based on the result of a expression. The expression must result in either of byte, short, integer or character.
Switch(Expression)
{
case value 1:
statement(s);
break;
case value 2:
statement(s);
break;
case value 3:
statement(s);
break;
----
----
----
[default:
statement(s);
}
Note:
The default statement is executed when none of the above mention case matches with the result of the switch expression. Default is optional.
Loop/Iteration:
o loop or iterations helps to repeat a group of statements number of times under a condition. Java supports three kinds of loop: while loop, for loop, do while loop
Entry control loop / Pre-Tested loop/ Top-Tested loop:
o An entry control loop first test the terminating condition and then executes the loop body. If the condition is found true the loop body is execute other wise the loop terminates. In case if the condition is false in first time only then the loop will not get execute even once.
Exit control loop / Post-Tested loop/ Bottom-Tested loop:
o An exit control loop first executes the loop body and then test the terminating condition. If the condition is found true the loop body executed again other wise the loop terminates. In case if the condition is false in first time only then the loop will still get execute at-least once.
While loop:
o It is an entry control loop
while (condition)
{
statement(s);
}
for loop:
o is a compact entry control loop, which all the tree parts of the loop (i.e. initialization, terminating condition, and increment/decrement of the counter variable) exists in a single line.
for(initialization ; terminating condition ; increment/decrement)
It is to be noted that all the parts of the loop in the above statement are optional. In case if a programmer wants to specify more than one initialization or increment/decrement then it has to be separated by (,).
for(int i=1; i<= 10; i++)
for(i=1, j = 10; i<j; i++, j++) // more than one initialization or increment/decrement
for(i = 10, j= 20; i>= 1 && j<= 30 ; i-- , j++) // more than one condition joined using &&
for(; i<= 10; i++) //initialization missing still using ;
for(; i<= 10;) //initialization, inc./dec. missing still using ;
do while loop:
o it is a exit control loop
do
{
statement(s);
}
while (condition);
NOTE:
Break:
o break is used to terminate the current switch statement or the loop.
Continue:
o Continue statement skips the remaining part of the current loop and begins the next iteration of the loop.
Object:
o An Object is an identifiable entity, which has certain properties (attributes) and methods (functions) associated with it.
Break:
o break is used to terminate the current switch statement or the loop.
Continue:
o Continue statement skips the remaining part of the current loop and begins the next iteration of the loop.
Object:
o An Object is an identifiable entity, which has certain properties (attributes) and methods (functions) associated with it.
Class:
o A Class is a group of similar objects. It is a generalization of an object. To make a class we encapsulate (join to from bundles) all the properties and method associated with the class.
Package:
o A Package is a group of logically related classes.
Method:
o Methods are functions associated to a class or an object.
Function:
o Function is a group of statement under a name, which are executed in an order to achieve a particular task.
Parameter:
o The set of values passed as in input to a function are known as parameters to that function. For example jTextFiled1.setText(“Apple”); Here JTextField1 is an object of the class jTextFiled, setText() is a function/method of the class jTextFiled, “Apple” is the parameter passed to the function setText(). Parameters are also known as Arguments.
Content Pane:
o The area on the frame where all the GUI controls are placed is called Content pane.
Commonly available Swing Controls in Java
jFrame: A Frame is a container control, in which all the controls can be place.
jLabel: JLable allows placing un-editable text on the Frame/Panel
jTextField: JTextFiled allows placing editable text on the Frame/Pane. User can enter text in a textFiled during runtime.
jbutton: is used to initiate an action when it is clicked.
jList: is a group of values or items from which one or more selections can be made.
jComboBox: jComboBox is similar to jList but also allow to enter editable text during run time. It is a combination of jTextFiled and jList.
jPanel: Act like a frame, to group one or more controls.
jRadioButton: Allow us to choose a single item from a group of jRadioButton options.
Remember
that out of several radio buttons belonging to a group, only one can be
selected. Therefore, the next step is toassociate the two radio buttons to each
other. This is achieved by linking both the radiobuttons with a ButtonGroup.
For each group of radio buttons, we need to create a ButtonGroup instance and
add each radio button to it. It is necessary to associate all theradio buttons
in a group to one ButtonGroup. The ButtonGroup takes care of unselectingthe
previously selected button when the user selects another button in the group.
So draga Button Group component from the Swing Controls tab and drop it
anywhere on theform. This is an invisible component which is just used to
associate several radio buttons.Now to associate them to same button group,
select the first radio button and edit thebuttonGroup property of this radio
button using the Properties Window
jCheckBox: Allow us to choose one or more items from a group of jCheckBox options.
jPasswordField: Allow us to enter a text during the run time but shows an encrypted text instead of the original text
jTextArea: JTextArea is a multi-line text component to enter or edit text.
Focus: The control under execution is said to have the focus. The control having the focus obtains input form the user.
getText(): getText() method is used to obtain the text from a jTextFiled during the run time.
setText(): setText() method is used to set or change the text of a jTextFiled during run time.
->Few MORE
methods:
1.The setVisible() method - to
set the visibility of a component at run time. setVisible(true) implies that
the component is visible and setVisible(false) implies that the component is
hidden.
2. The setEditable() method - to
set the editing property of a component at run time.The setEditable(true)
implies that the contents of this component can be changed at run time and
setEditable(false) implies that the contents of this component cannot be
changed at run time.
3. The setEnabled() method - to
set the enabled property of a component at run time.The setEnabled(true)
implies that this component can trigger a reaction at run timeand setEnabled
(false) implies that this component cannot trigger a reaction at run time.



Commonly
available functions in String class in Java are:
o equal( ):Compares two string – the string that calls the function and the argument string . If both the strings are equal it return otherwise false.
o length( ) :
o capacity( ):
o toLowerCase( ):
o toUpperCase( ):
o toString( ):
o trim( ):
o concat( ):
o substring( ):
Commonly available functions in Math class in Java are:
o pow( ):
o round( ):
o sqrt( ):
o equal( ):Compares two string – the string that calls the function and the argument string . If both the strings are equal it return otherwise false.
o length( ) :
o capacity( ):
o toLowerCase( ):
o toUpperCase( ):
o toString( ):
o trim( ):
o concat( ):
o substring( ):
Commonly available functions in Math class in Java are:
o pow( ):
o round( ):
o sqrt( ):
Shorthand way:
The
expression [Amount = Amount + 3500;] can also be written as [Amount+=3500;]. In
the same way -=, *= and /= can also be used to simplify the expressions. While
deciding between using an if statement and a switch statement always remember that
although switch is easier to read but it can only be used to test for equality.
The if statement on the other hand can test for equality as well as inequality.
--------------------------------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------------------------
The tooltip text is the text that
appears when the user moves the cursor over a component, without clicking it.
The tooltip generally appears in a small "hover box" within formation
about the component being hovered over.
No comments:
Post a Comment