Professional Documents
Culture Documents
-Review (XII-IP)
What is JAVA?
Chapter 3:
JAVA is an Object Oriented programming
GUI Programming- A Review language as well a platform.
By using JAVA, we can write Platform
independent application programs, which can run
Informatics Practices Revised as per on any type of OS and Hardware.
CBSE
Class XII (CBSE Board) Curriculum JAVA is designed to build Interactive, Dynamic
2015
and Secure applications on network computer
system.
Java facilitates development of Multilingual
Visit www.ip4you.blogspot.com for more…. applications because JAVA uses 2-Byte UNICODE
character set, which supports almost all
Authored By:- Rajesh Kumar Mishra, PGT (Comp.Sc.) characters in almost all languages like English,
Kendriya Vidyalaya Upper Camp, Dehradun (Uttarakhand)
e-mail : rkmalld@gmail.com
Chinese, Arbic etc.
How to use Event Handlers in NetBeans Working with Container Control- jFrame
Right Click on desired control of the Form in Design view. Every Swing Application must have at least one Top Level container
Choose desired Event from the Context Menu and its Sub (jFrame, jApplet, jDialog). A Top Level Container may have Mid-Level
Menu. Container like Content Pane (jPanel, jMenuBar, jScrollBar etc.) or
Components (jButton, jTextField etc.)
When you click on desired Event, it opens source Code editor
A Frame (jFrame) is a Top Level (Desktop) container control having
with default Handler Name, where you can type TO DO code Title Border and other Properties.
for the Handler. ActionPerformed is commonly used Listener.
Properties Value Description
you will found a code window along with prototyped method
to perform actions defined by you. You may write commands title Text Sets the title (appears on the
top) of the frame
to be executed in //TODO section.
Control Listener Event
cursor Crosshair, East Resize, Specifies the type of mouse
Name Name Name West Resize, Northwest cursor when mouse moves on
Resize, Move, Hand, the frame.
Wait, Default cursor
Resizable True /false If checked, allows resizing of
the frame
Foreground Color Sets the foreground color. toolTipText Text Sets the text for tool tip.
ToolTipText Text Sets the text for tooltip. Text Text Caption of button.
MinimumSize X, Y values Defines the minimum width and mnemonic Shortcut or Access key Assign Shortcut key (Alt +key).
height (x,y) in Twips( 1/1440 inch) enabled True/False Determines whether Active or not.
MaximumSize X, Y values Defines the maximum(x,y) size. font Font name Sets the font for the text of button.
PreferredSize X, Y values Defines the preferred (x,y) size.
Commonly used Properties & Methods of jLabel Displaying Image with jLabel
Properties Value Description Setting up Image at Design Time :-
background Color Sets the background color. It works only Add jLabel control and click on ellipse (…) of Icon
when opaque is set to True.
property in property window.
foreground Color Sets the foreground color.
In the dialogue box, select Image chooser option.
Text Text Sets the text to be displayed.
Specify the path and file name in External Image option.
Font Font name and size Defines the font and size of text.
Open source editor and go to top of the code and write-
enabled True/False Determines whether Active or not
import javax.swing.ImageIcon;
Icon Image file to be displayed Specifies the image file to be displayed.
Setting up Image at Run time:-
Methods Description Import the javax library by placing following command at
setText(String) Sets the string of text to be displayed. top of the code.
Ex. jLabel1.setText(“I am OK”); import.javax.swing.ImageIcon;
getText() Returns the text displayed by the label. Use the following command in a Event method where
Ex. String st=jLabel1.getText(); image to be displayed or changed.
setVisible(boolean) Makes the Label visible or invisible. jLabel.setIcon(new ImageIcom(“c:\\abc.png”))
Ex. jLabel1.setVisible(false);
background Color Sets the background color. setText(String) Sets the string of text to be displayed.
foreground Color Sets the foreground color. Ex. jCheckBox1.setText(“Computer”);
Text Text Sets the text to be displayed. getText() Returns the text displayed by on the check box.
Label Text/Picture Sets the text/Picture to be Ex. String st=jCheckBox1.getText();
displayed.
setEnabled() Sets the check box enables, if true is given.
Font Font name and Defines the font and size of text. Ex. jCheckBox1.setEnabled(true);
size
isEnabled( ) Returns the state whether check box is enabled.
enabled True/False Determines whether Active or not
Ex. Boolean st=jCheckBox1.isEnabled();
mnemonic Character Specifies the shortcut (access) key
setSelected() Sets the check box selected, if true is given.
selected True/false Check box will be selected, if set to Ex. jCheckBox1.setSelected(true);
true. (default is false)
isSelected( ) Returns the state whether check box is selected or not.
Button Group Button Group Adds Check Boxes in a Group
Ex. If(jCheckBox1.isSelected())
name
{ …… }
tem dependent, Radio Buttons should be kept in a Text Text Sets the text to be displayed.
ButtonGroup container control, so that only one Label Text/Picture Sets the text/Picture to be displayed.
button can be selected at a time in a group. Font Font name and size Defines the font and size of text.
Enabled True/False Determines whether Active or not
Some features of jRadioButton control’s are-
Mnemonic Character Specifies the shortcut (access) key
It can be used to get true/false or yes/no choice-
Selected True/false Button will be selected, if set to true.
typed input for the application. (default is false)
They must be kept in a Button Group container
You must add a ButtonGroup control to the frame to group the Radio
control to form a group. Buttons by using ButtonGroup property to facilitate only-one selection.
Insert(string, int) Inserts specified text at given position. Use 0 to insert at Text Text Sets the text to be displayed.
top. Font Font and size Defines the font and size of text.
Ex. jTextArea1.insert(“Amit”,1);
enabled True/False Determines whether Active or not
setLineWrap() Enables or disables line wrap feature. editable True/False Allow user to edit text, if set to true.
Ex. jTextArea1.setLineWrap(true);
echoChar Character Specifies the character to be displayed
isEnabled( ) Returns the status whether it is enabled or not. in place of typed character.
Ex. boolean b=jTextArea.isEnabled( );
Methods Description
JOptionPane allows to create pop-up window or dialog with
predefined style.
setEchoChar(char) Sets the echo character.
Ex. jPasswordField1.setEchoChar( ‘#’ ); Java provides following types of Dialogs which can be used as
per requirement.
getPassword( )* Returns the text displayed by the password field.
String pwd= new String (jPasswordField1.getPassword());
Dialog Method Description
Types
* getPassword() method returns a character array, not a string. To store
Message showMessageDialog() Used to inform user by
it in a string variable you need to use constructor of string.
Dialog displaying a message. It
Comparing Strings in JAVA includes OK button only.
In Java two strings can not be compared directly, by using == operator. Input Dialog showInputDialog() Used to get user input using
Text Field.
Two methods equals( ) and compareTo( ) are used for this purpose.
Confirm Dialog showConfirmDialog() Used to ask a user to confirm
equals() returns TRUE/FALSE but compareTo() returns 0 if both are some information with Yes/No
equal otherwise non-zero value is returned. or OK/Cancel buttons.
Ex. if (pwd.equals(“abc”)) {……} else {…..}
The smallest individual unit in a program Keywords are the reserve words that
is known as Token. It may any word, have a special meaning to the compiler.
symbols or punctuation mark etc.
Key words can’t be used as identifiers or
Following types of tokens used in Java- variable name etc.
Keywords
Commonly used key words are-
Identifiers
char, long, for, case, if, double, int,
Literals
short, void, main, while , new etc.
Punctuators (; [] etc)
Operators (+,-,/,*, =, == etc.)
An integer constant or literals must have at A real literals are fractional numbers having at least one
digit before and after decimal point with + or – sign.
least one +/- digit without decimal point. The following are valid real numbers-
Java allows three types of integer literals - 2.0, 17.5, -13.0. -0.00626
Decimal Integer Literals (Base 10) The following are invalid real numbers-
e.g. 1234, 41, +97, -17 etc. 7, 7. , +17/2 and 17,250.26 etc.
Octal Integer Literals (Base 8)
A real literals may be represented in Exponent form having
e.g.010, 014 (Octal must start with 0) Matissa and exponent with base 10 (E). Mantissa may be a
Hexadecimal Integer Literals (Base 16) proper real numbers while exponent must be integer.
e.g. 0xC, 0xab (Hex numbers must starts with 0x) The following are valid real in exponent form-
L or U suffix can used to represent long and 152E05, 1.52E07, 0.152E08, -0.12E-3, 1.5E+8
unsigned literals respectively. The following are invalid real exponent numbers-
172.E5, 1.7E, 0.17E2.3, 17,22E05, .25E-7
int
char 2 Byte Single character 0 to 65536
Boolean 1 Byte Logical Boolean values True or False
long
+ Unary plus Represents positive int a=+25; Java supports ++ and -- operator which adds
values. or subtract 1 from its operand. i.e.
- Unary minus Represents negative int a=-25; a=a+1 equivalent to ++a or a++
values. a=a-1 equivalent to --a or a--
+ Addition Adds two values int x= a+b; ++ or -- operator may used in Pre or Post
- Subtraction Subtract second int x=a-b; form.
operands from first.
* Multiplication Multiplies two values int x= a*b;
++a or --a (increase/decrease before use)
/ Division Divides first operand int x=a/b; a++ or a– (increase/decrease after use)
by second
Ex. Find value of P? (initially n=8 and p=4)
% Modulus Finds remainder after int x= a%b;
(remainder) division.
p=p* --n; 28
+ Concatenate or Adds two strings “ab”+”cd” ”abcd”
p=p* n--; 32
String addition “25”+”12” ”2512” Ex. Evaluate x=++y + 2y if y=6.
“”+5 ”5” =7+14 = 21
“”+5+”xyz” ”5xyz”
The process of converting one It is performed by the compiler, when different data
predefined type into another is called types are intermixed in an expression. It is also
called Coercian.
type conversion.
In Implicit conversion, all operands are promoted
In mixed expression, various types of (Coercion) up to the largest data type in the
constant and variables are converted expression.
into same type before evaluation. Ex. Consider the given expression, where f is float, d is double
Java facilitates two types of conversion. and I is integer data type.
Result= (f * d) - ( f + i) + ( f / i)
Implicit type conversion
Explicit type conversion double float float
double
for (int i=1; i<=10 ; i++ ) { //loop to find even nos. up to 50 Infinite loop
System.out.println (“”+i); for (int i=0; i<=50 ; i=i+2) For.. Loop may be endless (infinite) when defined condition is
always true or it is omitted.
} System.out.println (“”+i); for ( ; ; )
Empty loop
//loop to find factorial //loop to get sum of first 10 nos. for.. Loop may not contain any statement in looping body i.e.
int fact=1,num=5; int sum=0; Empty loop. If (;) placed after for.. Loop then empty loop is
for (int i=1; i<=10 ; i++ ) { created.
for (int i=1; i<=num ; i++)
sum=sum+ i; for (int i=1; i<=300 ; i++) ;
fact=fact * i; } Variable declared inside for.. Loop can’t accessed outside the loop.
System.out.println (“factorial=”+fact); System.out.println (“”+sum); Since it is out of scope.
//while loop to find factorial // do.. Loop to print A – Z letters //do.. loop fixed time
int fact=1,num=5,i=1; char ch =‘A’; int i=1;
int i=1;
while (i<=num) do { do
while ( i<=10) {
{ fact=fact * i; System.out.println (“”+i); {System.out.println (“”+i);
i=i+1;
i++; ch++; i++;
System.out.println (“”+i);
} } while (ch<=‘Z’); } while (i<=10);
} System.out.println (“factorial=”+fact);
A while loop also may be empty or infinite A do…while loop also may be empty or infinite