Java Basics
Java Basics
Object Oriented
Programming (Java)
1
CHAPTER 1. OBJECT ORIENTED PROGRAMMING (JAVA) 2
Loads Code The class loader loads all classes needed for the execution of
a program.
Verifies Code The byte code verifier tests format of code fragment and
checks code fragments for illegal code, which is code that forges point-
CHAPTER 1. OBJECT ORIENTED PROGRAMMING (JAVA) 3
Line 1 declares the user-defined class named Welcome. Every Java pro-
gram consists at least one public class. The Welcome.java file creates
a Welcome.class file when the source file is being complied.
Line 2 declares the main method, where the program starts to execute.
The following describes each element of Line 2:
1 p u b l i c c l a s s MyDate{
2 p r i v a t e i n t day ;
3 p r i v a t e i n t month ;
4 pr ivat e i n t year ;
5
6 p u b l i c MyDate ( ) {
7 day = 1 ;
8 month = 6 ;
9 year = 1979;
10 }
11
12 p u b l i c v o i d showDate ( ) {
13 System . out . p r i n t l n ( ” Day : ” + day ) ;
14 System . out . p r i n t l n ( ” Month : ” + month ) ;
15 System . out . p r i n t l n ( ” Year : ” + y e a r ) ;
16 }
17
18 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
19 MyDate d a t e = new MyDate ( ) ;
20 d a t e . showDate ( ) ;
21 }
22 }
1. package < topP ackageN ame > .[< subP ackageN ame >];
1.9.2 Semicolons
In the Java source code a statement is one or more lines of code terminated
with a semicolons (;).
1 public c l a s s TestSemicolon {
2 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
3 i n t a =10 , b=20 , c ;
4 c = a+b ;
5 System . out . p r i n t l n ( c ) ;
6 }
7 }
1.10 Blocks
A block as a group of statements that are collected together called a com-
pound statement. It is bound by opening and closing braces { }. A class
definition is contained in a block. A block statement can be nested withing
another block. In Java source code, block statements are executed first,
then constructor statements are executed, and then method statements are
executed.
1 p u b l i c c l a s s B lockT est {
2 public String info ;
3
CHAPTER 1. OBJECT ORIENTED PROGRAMMING (JAVA) 9
4 p u b l i c B lockT est ( ) {
5 i n f o = ” C o n s t r u c t o r : Executed i n 2nd s t e p ” ;
6 System . out . p r i n t l n ( i n f o ) ;
7 }
8
9 {
10 i n f o = ” Block : Executed i n 1 s t s t e p ” ;
11 System . out . p r i n t l n ( i n f o ) ;
12 }
13
14 p u b l i c v o i d show ( ) {
15 i n f o = ”Method : Executed i n 3 rd s t e p ” ;
16 System . out . p r i n t l n ( i n f o ) ;
17 }
18
19 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
20 BlockT est bt = new Blo ckTes t ( ) ;
21 bt . show ( ) ;
22 }
23 }
6 t h i s . num2 = num2 ;
7 }
8
9 p u b l i c v o i d add ( ) {
10 r e s u l t = num1 + num2 ;
11 System . out . p r i n t l n ( ” R e s u l t i s : ” + r e s u l t ) ;
12 }
13
14 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
15 AddNumbers addnum = new AddNumbers ( 1 0 , 2 0 ) ;
16 addnum . add ( ) ;
17 }
18 }
3. Integral: byte (8 bits), short (16 bits), int (32 bits), and long (64 bits)
CHAPTER 1. OBJECT ORIENTED PROGRAMMING (JAVA) 11
Class or reference data used to create objects which are two types:
1. Textual: String
3. Relational operator
4. Logical operator
5. Bitwise operator
7. instanceof operator
CHAPTER 1. OBJECT ORIENTED PROGRAMMING (JAVA) 12
1.17 Casting
Casting means assigning a value of one type to a variable of another type.
1 p u b l i c c l a s s CastTest {
2 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
3 l o n g b i g V a l u e = 99L ;
4 i n t s m a l l V a l u e = ( i n t ) b i g V a l u e ; // C a s t i n g
5 System . out . p r i n t l n ( s m a l l V a l u e ) ;
6 smallValue = 50;
7 b i g V a l u e = s m a l l V a l u e ; // Auto C a s t i n g
8 System . out . p r i n t l n ( b i g V a l u e ) ;
9 }
10 }
Chapter 2
13
CHAPTER 2. FLOW CONTROLS, AND ARRAYS 14
[ break ; ]
c a s e <c o n s t a n t >: <s t a t e m e n t o r b l o c k >∗
[ break ; ]
d e f a u l t : <s t a t e m e n t o r b l o c k >∗
}
1 p u b l i c c l a s s SwitchTest {
2 p u b l i c v o i d mySwitch ( i n t x ) {
3 switch ( x ){
4 c a s e 1 : System . out . p r i n t l n ( ”RED” ) ;
5 break ;
6 c a s e 2 : System . out . p r i n t l n ( ”GREEN” ) ;
7 break ;
8 c a s e 3 : System . out . p r i n t l n ( ”BLUE” ) ;
9 break ;
10 d e f a u l t : System . out . p r i n t l n ( ”WHITE” ) ;
11 }
12 }
13
14 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
15 S w i t c h T e s t s t = new S w i t c h T e s t ( ) ;
16 s t . mySwitch ( 2 ) ;
17 }
18 }
continue; Use the continue to jump at the end of the loop body, and then
return to the loop control.
1 p u b l i c c l a s s Pyramid {
2 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
3 i n t n=9;
4 f o r ( i n t i =1; i<=n ; i ++){
5 f o r ( i n t j=n−i ; j >=1; j −−)
6 System . out . p r i n t ( ” ” ) ;
7 f o r ( i n t k=1; k<=i ; k++)
8 System . out . p r i n t ( k+” ” ) ;
9 f o r ( i n t p=i −1; p>=1; p−−)
10 System . out . p r i n t ( p+” ” ) ;
11 System . out . p r i n t l n ( ) ;
12 }
13 }
14 }
1 p u b l i c c l a s s Loop do {
2 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
3 i n t i =1;
4 do{
5 System . out . p r i n t l n ( i ) ;
6 i ++;
7 } w h i l e ( i <=50);
8 }
9 }
2.3 Array
An array is a group of variables of the same data type referable by a common
name. The data type can be either a primitive data type or an class or
reference type.
1 p u b l i c c l a s s ArrayTest {
2 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
3 i n t [ ] myArray ; // d e c l a r i n g an a r r a y
4 myArray = new i n t [ 5 ] ; // c r e a t i n g a r r a y
5 // i n t [ ] myArray = new i n t [ 5 ] ;
6 f o r ( i n t i =0; i <5; i ++)
7 myArray [ i ]= 100+ i ; // i n i t i a l i z i n g a r r a y
8
9 f o r ( i n t i =0; i <=4; i ++)
10 System . out . p r i n t l n ( myArray [ i ] ) ;
11 }
12 }
1 c l a s s Bird {
2 public void f l y (){
3 System . out . p r i n t l n ( ” Bird can f l y ” ) ;
4 }
5 }
6
CHAPTER 2. FLOW CONTROLS, AND ARRAYS 18
7 public c l a s s ClassArray {
8 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
9 Bird [ ] myArray = new Bird [ 3 ] ;
10 f o r ( i n t i =0; i <3; i ++)
11 myArray [ i ] = new Bird ( ) ;
12 f o r ( i n t i =0; i <3; i ++)
13 myArray [ i ] . f l y ( ) ;
14 }
15 }
Write a Java program to create a class named ArrayExample, in this class
declare an array named myArray of Car type. Now initialize the myAarray
and call the display method for each index of myArray in the main method.
In the Car class there are two class variables: carName and carModel, con-
structor (initialized the variables), and display method ( display the value
of carName and carModel).
1 c l a s s Car{
2 p u b l i c S t r i n g carName ;
3 p u b l i c S t r i n g carModel ;
4
5 p u b l i c Car ( S t r i n g carName , S t r i n g carModel ) {
6 t h i s . carName = carName ;
7 t h i s . carModel = carModel ;
8 }
9
10 public void display (){
11 System . out . p r i n t l n ( ”Name : ”+carName+”, and Model : ”+carModel ) ;
12 }
13 } // end o f Car c l a s s ;
14
15 p u b l i c c l a s s ArrayExample {
16 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
17 Car [ ] myArray = new Car [ 3 ] ;
18 myArray [ 0 ] = new Car ( ”BMW” , ”Road Track 5 0 9 ” ) ;
19 myArray [ 1 ] = new Car ( ” Toyota ” , ”X c o r o l a 2 0 1 2 ” ) ;
20 myArray [ 2 ] = new Car ( ” Honda ” , ”M 2 0 1 1 ” ) ;
21
22 f o r ( i n t i =0; i <3; i ++)
23 {
24 myArray [ i ] . d i s p l a y ( ) ;
25 } // end o f f o r l o o p ;
26 } // end o f t h e main ( ) ;
27 } // end o f t h e c l a s s ;
CHAPTER 2. FLOW CONTROLS, AND ARRAYS 19
1 p u b l i c c l a s s Magic {
2 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
3 i n t [ ] [ ] r a t = new i n t [ 3 ] [ ] ;
4 r a t [ 0 ] = new i n t [ 2 ] ;
5 r a t [ 1 ] = new i n t [ 5 ] ;
6 r a t [ 2 ] = new i n t [ 3 ] ;
7
8 f o r ( i n t row =0; row<=2; row++){
9 s w i t c h ( row ) {
10 c a s e 0 : f o r ( i n t j =0; j <=1; j ++)
11 r a t [ row ] [ j ] = 5+ j ;
12 break ;
13 c a s e 1 : f o r ( i n t j =0; j <=4; j ++)
14 r a t [ row ] [ j ] = 50+ j ;
15 break ;
CHAPTER 2. FLOW CONTROLS, AND ARRAYS 20
1 p u b l i c c l a s s Matrix Mult {
2
3 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
4 i n t [ ] [ ] a = new i n t [ 3 ] [ 3 ] ;
5 i n t [ ] [ ] b = new i n t [ 3 ] [ 3 ] ;
6 i n t [ ] [ ] c = new i n t [ 3 ] [ 3 ] ;
7
8 f o r ( i n t i =0; i <=2; i ++)
9 f o r ( i n t j =0; j <=2; j ++)
10 {
11 a [ i ] [ j ] = 10+ i+j ;
12 b [ i ] [ j ] = 20+ i+j ;
13 }
14 System . out . p r i n t l n ( ” Matrix A : ” ) ;
15 f o r ( i n t i =0; i <=2; i ++)
16 {
17 f o r ( i n t j =0; j <=2; j ++)
18 System . out . p r i n t ( ” \ t”+a [ i ] [ j ] ) ;
CHAPTER 2. FLOW CONTROLS, AND ARRAYS 21
19 System . out . p r i n t ( ” \ n ” ) ;
20 }
21 System . out . p r i n t l n ( ” Matrix B : ” ) ;
22 f o r ( i n t i =0; i <=2; i ++)
23 {
24 f o r ( i n t j =0; j <=2; j ++)
25 System . out . p r i n t ( ” \ t”+b [ i ] [ j ] ) ;
26 System . out . p r i n t ( ” \ n ” ) ;
27 }
28
29 // Matrix M u l t i p l i c a t i o n
30 f o r ( i n t i =0; i <=2; i ++)
31 f o r ( i n t j =0; j <=2; j ++)
32 c [ i ] [ j ]=( a [ i ] [ 0 ] ∗ b [ 0 ] [ j ] )
+(a [ i ] [ 1 ] ∗ b [ 1 ] [ j ] ) + ( a [ i ] [ 2 ] ∗ b [ 2 ] [ j ] ) ;
33
34 System . out . p r i n t l n ( ” Matrix C : ” ) ;
35 f o r ( i n t i =0; i <=2; i ++)
36 {
37 f o r ( i n t j =0; j <=2; j ++)
38 System . out . p r i n t ( ” \ t”+c [ i ] [ j ] ) ;
39 System . out . p r i n t ( ” \ n ” ) ;
40 }
41 }
42 }
11
12 p u b l i c v o i d arrayCopy ( ) {
13 int [ ] x = {1 ,2 ,3 ,4 ,5 ,6};
14 int [ ] y = {11 ,12 ,13 ,14 ,15 ,16 ,17 ,18 ,19 ,20};
15 System . a r r a y c o p y ( x , 0 , y , 0 , x . l e n g t h ) ;
16 }
17 }
Object Oriented
Programming
23
CHAPTER 3. OBJECT ORIENTED PROGRAMMING 24
10 i n t pInt = 420;
11 I n t e g e r wInt = p I n t ; // a u t o b o x i n g
12 i n t p2 = wInt ; // autounboxing
13 }
14 }
1. Encapsulation
2. Inheritance
3. Polymorphism
3.3.1 Encapsulation
Encapsulation is the technique of hiding some members of a class from other
classes but provides a public interface to access that members.
1 c l a s s MyNumber{
2 p r i v a t e i n t number ;
3
4 p u b l i c v o i d setNumber ( i n t number ) {
5 t h i s . number = number ;
6 }
7
8 p u b l i c i n t getNumber ( ) {
9 r e t u r n number ;
10 }
CHAPTER 3. OBJECT ORIENTED PROGRAMMING 25
11 }
12
13 p u b l i c c l a s s EncapTest {
14 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
15 MyNumber my = new MyNumber ( ) ;
16 my . setNumber ( 4 5 ) ;
17 System . out . p r i n t l n (my . getNumber ( ) ) ;
18 }
19 }
At line 2, attribute number is a private member of MyNumber class,
so this attribute will not be accessible form other classes. But from the
EncapTest class we are accessing the number variable of MyNumber class
using set and get methods of MyNumber class.
3.3.2 Inheritance
Inheritance is the process of sub-classing that we can create a child-class
from a parent-class. Java programming language permits single inheritance,
because in Java a class can extend one other class only. A child-class can
inherited all of the members from the parent-class, but it does not inherit
the constructor.
1 c l a s s Cat{
2 public int x = 10;
3 public int y = 20;
4
5 p u b l i c v o i d show ( ) {
6 System . out . p r i n t l n ( x+” ”+y ) ;
7 }
8 }
9
10 p u b l i c c l a s s Rat e x t e n d s Cat{
11 public int z = 30;
12
13 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
14 Rat r = new Rat ( ) ;
15 r . show ( ) ;
16 System . out . p r i n t l n ( r . x+” ”+r . y+” ”+r . z ) ;
17 }
18 }
In the above code, Rat class extends Cat class, so Rat class become child
class of Cat class.
CHAPTER 3. OBJECT ORIENTED PROGRAMMING 26
Overriding Methods
If a method is defined in a sub-class so that the name, return type, and
argument list must exactly those of a method in the parent class, then the
new method is said to override the old one. The overriding method can not
be less accessible than the method it overrides.
1 c l a s s A{
2 p u b l i c v o i d show ( ) {
3 System . out . p r i n t l n ( ” Bird can f l y ” ) ;
4 }
5 }
6
7 p u b l i c c l a s s B e x t e n d s A{
8 p u b l i c v o i d show ( ) {
9 System . out . p r i n t l n ( ” Bird f l y i n t h e sky ” ) ;
10 }
11
12 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
13 B b = new B ( ) ;
14 b . show ( ) ;
15 }
16 }
In line 8 declare the method show(), which override the parent class
show() method of line 2.
14 super . showDetails ( ) ;
15 System . out . p r i n t l n ( ” ”+department ) ;
16 }
17
18 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
19 Manager m = new Manager ( ) ;
20 m. s h o w D e t a i l s ( ) ;
21 }
22 }
30 m. s h o w D e t a i l s ( ) ;
31 }
32 }
Overloading Methods
We can declare several methods of same name in a class, which is known
as methods overloading. For overloading methods argument lists must be
different and return type can be different. In the following code, there are
four methods of same name with different argument, which is an example
of overloading methods.
1 p u b l i c c l a s s ABC{
2 int a , b , c ;
3
4 public void setValue (){
5 a =2;
6 b=4;
7 c =6;
8 }
9
10 public void setValue ( i n t a ){
11 t h i s . a=a ;
12 b=4;
13 c =6;
14 }
15
16 public void setValue ( i n t a , i n t b){
17 t h i s . a=a ;
18 t h i s . b=b ;
19 c =6;
20 }
21
22 public i n t setValue ( i n t a , i n t b , i n t c ){
23 t h i s . a=a ;
24 t h i s . b=b ;
25 t h i s . c=c ;
26 i n t z = a+b+c ;
27 return z ;
28 }
29 }
CHAPTER 3. OBJECT ORIENTED PROGRAMMING 29
Overloading Constructors
We can declare several constructors with different arguments in a class,
which is overloading constructors.
1 public c l a s s Light {
2 int a , b , c ;
3
4 public Light (){
5 a =2;
6 b=4;
7 c =6;
8 }
9
10 public Light ( i n t a ){
11 t h i s . a=a ;
12 b=4;
13 c =6;
14 }
15
16 public Light ( i n t a , i n t b){
17 t h i s . a=a ;
18 t h i s . b=b ;
19 c =6;
20 }
21
22 public Light ( i n t a , i n t b , i n t c ){
23 t h i s . a=a ;
24 t h i s . b=b ;
25 t h i s . c=c ;
26 }
27 }
3.4 Polymorphism
Polymorphism is the technique of creating object of parent-class through
the constructor of child-class. Using polymorphism we can call or execute
the child-class overriding method by the parent-class object.
1 c l a s s Man{
2 public void f l y (){
3 System . out . p r i n t l n ( ”Man can not f l y ” ) ;
4 }
5 }
6
7 c l a s s SuperMan e x t e n d s Man{
8 public void f l y (){
9 System . out . p r i n t l n ( ” Superman can f l y ” ) ;
10 }
11 }
12
13 p u b l i c c l a s s TestMan{
14 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
15 Man m = new SuperMan ( ) ; // polymorphism
16 m. f l y ( ) ;
17 }
18 }
method but we do not know this x object is from which class. Then we use
instanceof operator to know object x is from which class by the conditions.
1 c l a s s Car{
2 p u b l i c v o i d abc ( ) {
3 System . out . p r i n t l n ( ” Car ” ) ;
4 }
5 }
6
7 c l a s s Bus{
8 p u b l i c v o i d xyz ( ) {
9 System . out . p r i n t l n ( ” Bus ” ) ;
10 }
11 }
12
13 public class Testinstanceof {
14 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
15 Car c = new Car ( ) ;
16 Bus b = new Bus ( ) ;
17 T e s t i n s t a n c e o f t = new T e s t i n s t a n c e o f ( ) ;
18 t . test (c );
19 t . test (b ) ;
20 }
21
22 p u b l i c v o i d t e s t ( Object o b j ) {
23 i f ( o b j i n s t a n c e o f Car ) {
24 System . out . p r i n t l n ( ” Object i s o f Car c l a s s ” ) ;
25 } e l s e i f ( o b j i n s t a n c e o f Bus ) {
26 System . out . p r i n t l n ( ” Object i s o f Bus c l a s s ” ) ;
27 } else {
28 System . out . p r i n t l n ( ” Object i s not o f Car/Bus c l a s s ” ) ;
29 }
30 }
31 }
equals method. A simple implementation could use a bit wise XOR on the
hash codes of the elements tested for equality.
1 p u b l i c c l a s s TestEquals {
2 public int x ;
3 public int y ;
4
5 p u b l i c TestEquals ( i n t x , i n t y ){
6 t h i s . x=x ;
7 t h i s . y=y ;
8 }
9
10 p u b l i c b o o l e a n e q u a l s ( Object o b j ) {
11 boolean r e s u l t = f a l s e ;
12 i f ( ( o b j != n u l l )&&( o b j i n s t a n c e o f T e s t E q u a l s ) ) {
13 TestEquals t = ( TestEquals ) obj ;
14 i f ( ( x==t . x)&&(y==t . y ) ) {
15 r e s u l t = true ;
16 }
17 }
18 return r e s u l t ;
19 }
20
21 p u b l i c i n t hashCode ( ) {
22 r e t u r n ( xˆy ) ;
23 }
24
25 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
26 T e s t E q u a l s t 1 = new T e s t E q u a l s ( 5 , 1 0 ) ;
27 T e s t E q u a l s t 2 = new T e s t E q u a l s ( 5 , 1 0 ) ;
28 i f ( t 1==t 2 ) {
29 System . out . p r i n t l n ( ” t 1 i s i d e n t i c a l t o t 2 ” ) ;
30 } else {
31 System . out . p r i n t l n ( ” t 1 i s not i d e n t i c a l t o t 2 ” ) ;
32 }
33 i f ( t1 . equals ( t2 )){
34 System . out . p r i n t l n ( ” t 1 i s e q u a l t o t 2 ” ) ;
35 } else {
36 System . out . p r i n t l n ( ” t 1 i s not e q u a l t o t 2 ” ) ;
37 }
38 System . out . p r i n t l n ( ” S e t t 2=t 1 ” ) ;
39 t 2=t 1 ;
40 i f ( t 1==t 2 ) {
41 System . out . p r i n t l n ( ” t 1 i s i d e n t i c a l t o t 2 ” ) ;
CHAPTER 3. OBJECT ORIENTED PROGRAMMING 35
42 } else {
43 System . out . p r i n t l n ( ” t 1 i s not i d e n t i c a l t o t 2 ” ) ;
44 }
45 }
46 }
8 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
9 f o r ( i n t i =0; i <3; i ++){
10 System . out . p r i n t l n ( ” Count i s : ”+ T e s t S t a t i c . count ) ;
11 T e s t S t a t i c . incrementCount ( ) ;
12 }
13 }
14 }
10 }
11
12 p u b l i c c l a s s Car e x t e n d s V e h i c l e {
13 p u b l i c v o i d goFast ( ) {
14 System . out . p r i n t l n ( ” Car can go f a s t ” ) ;
15 }
16
17 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
18 // Vehcle v = new V e h i c l e ( ) ; // c o m p i l e r e r r o r
19 Car c = new Car ( ) ;
20 c . show ( ) ;
21 c . goFast ( ) ;
22 }
23 }
18 t . bounce ( ) ;
19 t . setBounce ( 1 5 ) ;
20 }
21 }
35 System . out . p r i n t l n ( ” T i g e r ” ) ;
36 }
37
38 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
39 T i g e r t = new T i g e r ( ) ;
40 t . name ( ) ;
41 t . jump ( ) ;
42 t . run ( ) ;
43 t . fly ();
44 }
45 }
3.15 Exceptions
Exceptions are a mechanism used by many programming languages to de-
scribe what to do when errors happens. There are two types of exceptions in
Java programming, known as checked and unchecked exceptions. Checked
exceptions are those that the programmer can easily handle this type of ex-
ceptions like: file not found, and network failure etc. Unchecked exceptions
are arises from the conditions that are difficult for programmers to handle.
Unchecked exceptions are called runtime exceptions. In Java, Exception
class is the base class that represents checked and unchecked exceptions
and RuntimeException class is the base class that is used for the unchecked
exceptions.
1 p u b l i c c l a s s TestExceptionA {
2 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
3 i n t sum = 0 ;
4 f o r ( i n t i =0; i <a r g s . l e n g t h ; i ++){
5 sum += I n t e g e r . p a r s e I n t ( a r g s [ i ] ) ;
6 }
7 System . out . p r i n t l n ( ”Sum : ” + sum ) ;
8 }
9 }
This program works if all of the command-line arguments are integers.
Compile : j a v a c TestExceptionA . j a v a
Run : j a v a TestExceptionA 2 4 6 8
Output : 20
But this program fails if any of the arguments are not integers;
Run : j a v a TestExceptionA 2 4 s i x 8
Output : Runtime E x c e p t i o n
CHAPTER 3. OBJECT ORIENTED PROGRAMMING 40
The E x c e p t i o n D e c l a r e Rules
In Java , we can d e c l a r e e x c e p t i o n s by f o l l o w i n g :
1 . try −catch −f i n a l l y
2 . v o i d methodA ( ) throws IOException {}
3 . v o i d methodB ( ) throws IOException , OtherException {}
1 public c l a s s DeclareException {
2 p u b l i c v o i d methodA ( ) {
3 try {
4 } catch ( Exception e ){
5 System . out . p r i n t l n ( ” I f e r r o r i n t r y then c a t c h e x e c u t e ” ) ;
6 }finally{
7 System . out . p r i n t l n ( ” f i n a l l y must e x e c u t e s ” ) ;
8 }
9 }
10 p u b l i c v o i d methodB ( ) throws S e c u r i t y E x c e p t i o n {
}
CHAPTER 3. OBJECT ORIENTED PROGRAMMING 42
11 p u b l i c v o i d methodC ( ) throws S e c u r i t y E x c e p t i o n , E x c e p t i o n {
}
12 }
43
CHAPTER 4. TEXT-BASED AND GUI-BASED APPLICATIONS 44
4.3 Scanner
The scanner class provides formated input functionality. It is a part of the
java.util package.
1 import j a v a . i o . ∗ ;
2 import j a v a . u t i l . Scanner ;
3
4 p u b l i c c l a s s ScannerTest {
5 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
6 Scanner s = new Scanner ( System . i n ) ;
7 S t r i n g s t r = s . next ( ) ;
8 System . out . p r i n t l n ( s t r ) ;
9 i n t num = s . n e x t I n t ( ) ;
10 System . out . p r i n t l n (num ) ;
11 s . close ();
CHAPTER 4. TEXT-BASED AND GUI-BASED APPLICATIONS 45
12 }
13 }
1 import j a v a . i o . ∗ ;
2
3 public c l a s s WriteFile {
4 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
5 F i l e f i l e = new F i l e ( ” / home/ f a r i d /MyJavaBook ” , ”MyText . t x t ” ) ;
6 try {
7 InputStreamReader i s r = new InputStreamReader ( System . i n ) ;
8 B u f f e r e d R e a d e r i n = new B u f f e r e d R e a d e r ( i s r ) ;
9 P r i n t W r i t e r out = new P r i n t W r i t e r ( new F i l e W r i t e r ( f i l e ) ) ;
10 System . out . p r i n t l n ( ” Write S t r i n g : ” ) ;
11 String s t r = in . readLine ( ) ;
12 out . p r i n t l n ( s t r ) ;
13 in . close ( ) ;
14 out . c l o s e ( ) ;
15 } c a t c h ( IOException e ) {
16 e . printStackTrace ( ) ;
17 }
18 }
19 }
4.4.2 Read String from a File
1 import j a v a . i o . ∗ ;
2
3 p u b l i c c l a s s ReadFile {
4 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
5 F i l e f i l e = new F i l e ( ” / home/ f a r i d /MyJavaBook ” , ”MyText . t x t ” ) ;
6 try {
7 B u f f e r e d R e a d e r i n = new B u f f e r e d R e a d e r ( new F i l e R e a d e r ( f i l e ) ) ;
8 String s t r = in . readLine ( ) ;
9 w h i l e ( s t r != n u l l ) {
CHAPTER 4. TEXT-BASED AND GUI-BASED APPLICATIONS 46
20 p . setBackground ( C o l o r . g r e e n ) ;
21 f . add ( p ) ;
22 f . s e t V i s i b l e ( true ) ;
23 }
24
25 p u b l i c v o i d windowClosing ( WindowEvent e ) {
26 System . e x i t ( 0 ) ;
27 }
28
29 public void windowOpened ( WindowEvent e ) { }
30 public void w i n d o w I c o n i f i e d ( WindowEvent e ) { }
31 public void w i n d o w D e i c o n i f i e d ( WindowEvent e ) { }
32 public void windowClosed ( WindowEvent e ) { }
33 public void windowActivated ( WindowEvent e ) { }
34 public void windowDeactivated ( WindowEvent e ) { }
35
36 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
37 FrameWithPanel f p = new FrameWithPanel ( ) ;
38 f p . launchFrame ( ) ;
39 }
40 }
1 import j a v a . awt . ∗ ;
2 import j a v a . awt . e v e n t . ∗ ;
3
4 p u b l i c c l a s s TestMenuBar implements WindowListener , A c t i o n L i s t e n e r {
5 p r i v a t e Frame f ;
6 p r i v a t e MenuBar mb;
7 p r i v a t e Menu m1, m2, m3 ;
8 p r i v a t e MenuItem mi1 , mi2 , mi3 , mi4 ;
9
10 p u b l i c TestMenuBar ( ) {
11 f = new Frame ( ” MenuBar Example ” ) ;
12 mb = new MenuBar ( ) ;
13 m1 = new Menu( ” F i l e ” ) ;
14 m2 = new Menu( ” E di t ” ) ;
15 m3 = new Menu( ” Help ” ) ;
16 mi1 = new MenuItem ( ”New ” ) ;
17 mi2 = new MenuItem ( ” Save ” ) ;
18 mi3 = new MenuItem ( ” Load ” ) ;
19 mi4 = new MenuItem ( ” Quit ” ) ;
20 }
21
22 p u b l i c v o i d launchFrame ( ) {
CHAPTER 4. TEXT-BASED AND GUI-BASED APPLICATIONS 48
21 mi4 . a d d A c t i o n L i s t e n e r ( t h i s ) ;
22 m1 . add ( mi1 ) ;
23 m1 . add ( mi2 ) ;
24 m1 . add ( mi3 ) ;
25 m1 . a d d S e p a r a t o r ( ) ;
26 m1 . add ( mi4 ) ;
27
28 mb. add (m1 ) ;
29 mb. add (m2 ) ;
30 mb. setHelpMenu (m3 ) ;
31 f . setMenuBar (mb ) ;
32
33 f . addWindowListener ( t h i s ) ;
34 f . setSize (400 ,400);
35 f . setBackground ( C o l o r . r e d ) ;
36 f . setLayout ( n u l l ) ;
37 f . s e t V i s i b l e ( true ) ;
38 }
39
40 p u b l i c v o i d a c t i o n P e r f o r m e d ( ActionEvent e ) {
41 System . e x i t ( 0 ) ;
42 }
43
44 p u b l i c v o i d windowClosing ( WindowEvent e ) {
45 System . e x i t ( 0 ) ;
46 }
47
48 public void windowOpened ( WindowEvent e ) { }
49 public void w i n d o w I c o n i f i e d ( WindowEvent e ) { }
50 public void w i n d o w D e i c o n i f i e d ( WindowEvent e ) { }
51 public void windowClosed ( WindowEvent e ) { }
52 public void windowActivated ( WindowEvent e ) { }
53 public void windowDeactivated ( WindowEvent e ) { }
54
55 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
56 TestMenuBar tmb = new TestMenuBar ( ) ;
57 tmb . launchFrame ( ) ;
58 }
59 }
CHAPTER 4. TEXT-BASED AND GUI-BASED APPLICATIONS 49
40 f . add ( t f ) ;
41 f . addWindowListener ( t h i s ) ;
42 f . setSize (250 ,150);
43 f . setBackground ( C o l o r . r e d ) ;
44 f . s e t V i s i b l e ( true ) ;
45 }
46
47 p u b l i c v o i d a c t i o n P e r f o r m e d ( ActionEvent e ) {
48 String str ;
49 i f ( e . getActionCommand()==” y e s button ” ) {
50 s t r = ”You p r e s s YES button ” ;
51 t f . setText ( s t r ) ;
52 }
53 i f ( e . getActionCommand()==”no button ” ) {
54 s t r = ”You p r e s s NO button ” ;
55 t f . setText ( s t r ) ;
56 }
57 i f ( e . getActionCommand()==” c l e a r button ” ) {
58 str = ” ”;
59 t f . setText ( s t r ) ;
60 }
61 }
62
63 p u b l i c v o i d windowClosing ( WindowEvent e ) {
64 System . e x i t ( 0 ) ;
65 }
66
67 public void windowOpened ( WindowEvent e ) { }
68 public void w i n d o w I c o n i f i e d ( WindowEvent e ) { }
69 public void w i n d o w D e i c o n i f i e d ( WindowEvent e ) { }
70 public void windowClosed ( WindowEvent e ) { }
71 public void windowActivated ( WindowEvent e ) { }
72 public void windowDeactivated ( WindowEvent e ) { }
73
74 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
75 EventHandle eh = new EventHandle ( ) ;
76 eh . launchFrame ( ) ;
77 }
78 }
1 import j a v a . awt . ∗ ;
CHAPTER 4. TEXT-BASED AND GUI-BASED APPLICATIONS 51
2 import j a v a . awt . e v e n t . ∗ ;
3
4 p u b l i c c l a s s MouseExample implements WindowListener , MouseMotionListen
5 p r i v a t e Frame f ;
6 private TextField t f ;
7
8 p u b l i c MouseExample ( ) {
9 f = new Frame ( ” Mouse Example ” ) ;
10 t f = new T e x t F i e l d ( 3 0 ) ;
11 }
12
13 p u b l i c v o i d launchFrame ( ) {
14 L ab el l a b e l = new La be l ( ” C l i c k and drag t h e mouse ” ) ;
15 f . add ( l a b e l , BorderLayout .NORTH) ;
16 f . add ( t f , BorderLayout .SOUTH) ;
17 f . addMouseMotionListener ( t h i s ) ;
18 f . addMouseListener ( t h i s ) ;
19 f . addWindowListener ( t h i s ) ;
20 f . setSize (300 ,200);
21 f . s e t V i s i b l e ( true ) ;
22 }
23
24 p u b l i c v o i d mouseDragged ( MouseEvent e ) {
25 S t r i n g s = ”Mouse dragged : X= ”+e . getX ()+” Y=”+e . getY ( ) ;
26 t f . setText ( s ) ;
27 }
28
29 p u b l i c v o i d mouseEntered ( MouseEvent e ) {
30 S t r i n g s = ”The mouse e n t e r e d ” ;
31 t f . setText ( s ) ;
32 }
33
34 p u b l i c v o i d mouseExited ( MouseEvent e ) {
35 S t r i n g s = ”The mouse has l e f t t h e b u i l d i n g ” ;
36 t f . setText ( s ) ;
37 }
38
39 p u b l i c v o i d mousePressed ( MouseEvent e ) { }
40 p u b l i c v o i d mouseReleased ( MouseEvent e ) { }
41 p u b l i c v o i d mouseMoved ( MouseEvent e ) { }
42 p u b l i c v o i d mouseClicked ( MouseEvent e ) { }
43
44 p u b l i c v o i d windowClosing ( WindowEvent e ) {
45 System . e x i t ( 0 ) ;
CHAPTER 4. TEXT-BASED AND GUI-BASED APPLICATIONS 52
Table 4.1: Table Color class static constants and RGB values.
Color Constant Color RGB value
public final static Color orange Orange 255, 200, 0
public final static Color pink Pink 255, 175, 175
public final static Color cyan Cyan 0, 255, 255
public final static Color magenta Magenta 255, 0, 255
public final static Color yellow Yellow 255, 255, 0
public final static Color black Black 0, 0, 0
public final static Color white White 255, 255, 255
public final static Color gray Gray 128, 128, 128
public final static Color lightGray Light Gray 192, 192, 192
public final static Color darkGray Dark Gray 64, 64, 64
public final static Color red Red 255, 0, 0
public final static Color green Green 0, 255, 0
public final static Color blue Blue 0, 0, 255
46 }
47
48 public void windowOpened ( WindowEvent e ) { }
49 public void w i n d o w I c o n i f i e d ( WindowEvent e ) { }
50 public void w i n d o w D e i c o n i f i e d ( WindowEvent e ) { }
51 public void windowClosed ( WindowEvent e ) { }
52 public void windowActivated ( WindowEvent e ) { }
53 public void windowDeactivated ( WindowEvent e ) { }
54
55 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
56 MouseExample me = new MouseExample ( ) ;
57 me . launchFrame ( ) ;
58 }
59 }
3
4 p u b l i c c l a s s T e s t C o l o r s implements WindowListener , A c t i o n L i s t e n e r {
5 p r i v a t e Frame f ;
6 p r i v a t e Button b ;
7
8 public TestColors (){
9 f = new Frame ( ” Frame T i t l e ” ) ;
10 b = new Button ( ” Change C o l o r ” ) ;
11 b . setActionCommand ( ” button p r e s s ” ) ;
12 }
13
14 p u b l i c v o i d launchFrame ( ) {
15 b . addActionListener ( t h i s ) ;
16 b . setForeground ( Color . red ) ;
17 b . setBackground ( C o l o r . y e l l o w ) ;
18 f . add ( b ) ;
19 f . addWindowListener ( t h i s ) ;
20 f . setSize (300 ,300);
21 f . setBackground ( C o l o r . g r e e n ) ;
22 f . s e t L a y o u t ( new FlowLayout ( ) ) ;
23 f . s e t V i s i b l e ( true ) ;
24 }
25
26 p u b l i c v o i d a c t i o n P e r f o r m e d ( ActionEvent e ) {
27 int x , y , z ;
28 i f ( e . getActionCommand()==” button p r e s s ” ) {
29 x = ( i n t ) ( Math . random ( ) ∗ 1 0 0 ) ;
30 y = ( i n t ) ( Math . random ( ) ∗ 1 0 0 ) ;
31 z = ( i n t ) ( Math . random ( ) ∗ 1 0 0 ) ;
32 C o l o r c = new C o l o r ( x , y , z ) ;
33 f . setBackground ( c ) ;
34 }
35 }
36
37 p u b l i c v o i d windowClosing ( WindowEvent e ) {
38 System . e x i t ( 0 ) ;
39 }
40
41 p u b l i c v o i d windowOpened ( WindowEvent e ) { }
42 p u b l i c v o i d w i n d o w I c o n i f i e d ( WindowEvent e ) { }
43 p u b l i c v o i d w i n d o w D e i c o n i f i e d ( WindowEvent e ) { }
44 p u b l i c v o i d windowClosed ( WindowEvent e ) { }
45 p u b l i c v o i d windowActivated ( WindowEvent e ) { }
46 p u b l i c v o i d windowDeactivated ( WindowEvent e ) { }
CHAPTER 4. TEXT-BASED AND GUI-BASED APPLICATIONS 54
47
48 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
49 T e s t C o l o r s t c = new T e s t C o l o r s ( ) ;
50 t c . launchFrame ( ) ;
51 }
52 }
5. GridBagLayout -
14
15 p u b l i c v o i d launchFrame ( ) {
16 f . s e t L a y o u t ( new FlowLayout ( ) ) ;
17 // f . s e t L a y o u t ( new FlowLayout ( FlowLayout . LEFT ) ) ;
18 // f . s e t L a y o u t ( new FlowLayout ( FlowLayout . RIGHT ) ) ;
19 // f . s e t L a y o u t ( new FlowLayout ( FlowLayout .CENTER) ) ;
20 // f . s e t L a y o u t ( new FlowLayout ( FlowLayout . RIGHT, 2 0 , 3 0 ) ) ;
21 f . add ( b1 ) ;
22 f . add ( b2 ) ;
23 f . add ( b3 ) ;
24 f . addWindowListener ( t h i s ) ;
25 f . setSize (250 ,150);
26 f . setBackground ( C o l o r . r e d ) ;
27 f . s e t V i s i b l e ( true ) ;
28 }
29
30 p u b l i c v o i d windowClosing ( WindowEvent e ) {
31 System . e x i t ( 0 ) ;
32 }
33
34 public void windowOpened ( WindowEvent e ) { }
35 public void w i n d o w I c o n i f i e d ( WindowEvent e ) { }
36 public void w i n d o w D e i c o n i f i e d ( WindowEvent e ) { }
37 public void windowClosed ( WindowEvent e ) { }
38 public void windowActivated ( WindowEvent e ) { }
39 public void windowDeactivated ( WindowEvent e ) { }
40
41 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
42 FlowExample f e = new FlowExample ( ) ;
43 f e . launchFrame ( ) ;
44 }
45 }
The BorderLayout manager contains five distinct areas: NORTH, SOUTH,
EAST, WEST, and CENTER, indicated by BorderLayout.NORTH, and so
on.
1 import j a v a . awt . ∗ ;
2 import j a v a . awt . e v e n t . ∗ ;
3
4 p u b l i c c l a s s BorderExample implements WindowListener {
5 p r i v a t e Frame f ;
6 p r i v a t e Button b1 , b2 , b3 , b4 , b5 ;
7
8 p u b l i c BorderExample ( ) {
CHAPTER 4. TEXT-BASED AND GUI-BASED APPLICATIONS 56
5 p r i v a t e Frame f ;
6 p r i v a t e Button b1 , b2 , b3 , b4 , b5 , b6 ;
7
8 p u b l i c GridExample ( ) {
9 f = new Frame ( ” FlowLayout ” ) ;
10 b1 = new Button ( ” Button 1 ” ) ;
11 b2 = new Button ( ” Button 2 ” ) ;
12 b3 = new Button ( ” Button 3 ” ) ;
13 b4 = new Button ( ” Button 4 ” ) ;
14 b5 = new Button ( ” Button 5 ” ) ;
15 b6 = new Button ( ” Button 6 ” ) ;
16 }
17
18 p u b l i c v o i d launchFrame ( ) {
19 f . s e t L a y o u t ( new GridLayout ( 3 , 2 ) ) ;
20 f . add ( b1 ) ;
21 f . add ( b2 ) ;
22 f . add ( b3 ) ;
23 f . add ( b4 ) ;
24 f . add ( b5 ) ;
25 f . add ( b6 ) ;
26 f . addWindowListener ( t h i s ) ;
27 f . setSize (300 ,300);
28 f . setBackground ( C o l o r . r e d ) ;
29 f . s e t V i s i b l e ( true ) ;
30 }
31
32 p u b l i c v o i d windowClosing ( WindowEvent e ) {
33 System . e x i t ( 0 ) ;
34 }
35
36 public void windowOpened ( WindowEvent e ) { }
37 public void w i n d o w I c o n i f i e d ( WindowEvent e ) { }
38 public void w i n d o w D e i c o n i f i e d ( WindowEvent e ) { }
39 public void windowClosed ( WindowEvent e ) { }
40 public void windowActivated ( WindowEvent e ) { }
41 public void windowDeactivated ( WindowEvent e ) { }
42
43 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
44 GridExample ge = new GridExample ( ) ;
45 ge . launchFrame ( ) ;
46 }
47 }
CHAPTER 4. TEXT-BASED AND GUI-BASED APPLICATIONS 58
30 }
31 }
32 );
33 }
34 }
34
35 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
36 TestLineRectOval t l r o = new TestLineRectOval ( ) ;
37 t l r o . addWindowListener (
38 new WindowAdapter ( ) {
39 p u b l i c v o i d windowClosing ( WindowEvent e ) {
40 System . e x i t ( 0 ) ;
41 }
42 }
43 );
44 }
45 }
Code 4-16 provides an password example using Jlabel, JtextField, Jpass-
wordField, and Jbutton.
1 import j a v a . awt . ∗ ;
2 import j a v a . awt . e v e n t . ∗ ;
3 import j a v a x . swing . ∗ ;
4
5 p u b l i c c l a s s Password e x t e n d s JFrame{
6 p r i v a t e s t a t i c f i n a l l o n g s e r i a l V e r s i o n U I D =0;
7
8 private JLabel l 1 , l 2 ;
9 private JTextField t1 ;
10 private J P a s s w o r d F i e l d pw ;
11 private JButton b1 , b2 ;
12
13 p u b l i c Password ( ) {
14 s u p e r ( ” Password Example ” ) ;
15 C o n t a i n e r c = getContentPane ( ) ;
16 c . s e t L a y o u t ( new FlowLayout ( ) ) ;
17
18 l 1 = new JLabel ( ” Enter User Name ”);
19 c . add ( l 1 ) ;
20
21 t 1 = new J T e x t F i e l d ( 1 5 ) ;
22 c . add ( t 1 ) ;
23
24 l 2 = new JLabel ( ” Enter Password ”);
25 c . add ( l 2 ) ;
26
27 pw = new J P a s s w o r d F i e l d ( 1 0 ) ;
28 c . add (pw ) ;
29
CHAPTER 4. TEXT-BASED AND GUI-BASED APPLICATIONS 61
1 import j a v a . awt . ∗ ;
2 import j a v a . awt . e v e n t . ∗ ;
3 import j a v a x . swing . ∗ ;
4
5 p u b l i c c l a s s TestJComt e x t e n d s JFrame{
6 p r i v a t e s t a t i c f i n a l l o n g s e r i a l V e r s i o n U I D =0;
7
8 private JCheckBox bold , i t a l i c ;
9 private JRadioButton male , f e m a l e ;
10 private ButtonGroup radioGroup ;
11 private JComboBox cBox ;
12 private S t r i n g [ ] s t r 1 = {” s p r i n g ” , ”summer ” , ” f a l l ” } ;
13 private JList colorList ;
14 private S t r i n g [ ] s t r 2 = {”Red ” , ” Green ” , ” Blue ” , ” Black ” , ” White ” , ”
15 ” Orange ” , ” Pink ” , ”Magenta ” , ”Sky ” , ”Cya
16 p r i v a t e JTextArea t a 1 ;
17
18 p u b l i c TestJComt ( ) {
19 s u p e r ( ” Test JComponents ” ) ;
20 C o n t a i n e r c = getContentPane ( ) ;
21 c . s e t L a y o u t ( new FlowLayout ( ) ) ;
22
23 b o l d = new JCheckBox ( ” Bold ” ) ;
24 c . add ( b o l d ) ;
25 i t a l i c = new JCheckBox ( ” I t a l i c ” ) ;
26 c . add ( i t a l i c ) ;
27
28 male = new JRadioButton ( ” Male ” ) ;
29 c . add ( male ) ;
30 f e m a l e = new JRadioButton ( ” Female ” ) ;
31 c . add ( f e m a l e ) ;
32 radioGroup = new ButtonGroup ( ) ;
33 radioGroup . add ( male ) ;
34 radioGroup . add ( f e m a l e ) ;
35
36 cBox = new JComboBox ( s t r 1 ) ;
37 c . add ( cBox ) ;
38
39 c o l o r L i s t = new J L i s t ( s t r 2 ) ;
40 c o l o r L i s t . setVisibleRowCount ( 5 ) ;
41 c . add ( new J S c r o l l P a n e ( c o l o r L i s t ) ) ;
42
43 S t r i n g s = ” Java i s a o b j e c t o r i e n t e d programming l a n g u a g e ” ;
44 t a 1 = new JTextArea ( s , 1 0 , 1 5 ) ;
CHAPTER 4. TEXT-BASED AND GUI-BASED APPLICATIONS 63
45 c . add ( new J S c r o l l P a n e ( t a 1 ) ) ;
46
47 s e t S i z e (200 , 350);
48 show ( ) ;
49 }
50
51 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
52 TestJComt j c = new TestJComt ( ) ;
53 j c . addWindowListener (
54 new WindowAdapter ( ) {
55 p u b l i c v o i d windowClosing ( WindowEvent e ) {
56 System . e x i t ( 0 ) ;
57 }
58 }
59 );
60 }
61 }
1 import j a v a . awt . ∗ ;
2 import j a v a . awt . e v e n t . ∗ ;
3 import j a v a x . swing . ∗ ;
4
5 p u b l i c c l a s s OpenFile e x t e n d s JFrame{
6 p r i v a t e s t a t i c f i n a l l o n g s e r i a l V e r s i o n U I D =0;
7 p r i v a t e JButton b1 ;
8 private JFileChooser j f c ;
9
10 p u b l i c OpenFile ( ) {
11 s u p e r ( ” F i l e Opener ” ) ;
12 b1 = new JButton ( ” Open F i l e Chooser ” ) ;
13 b1 . a d d A c t i o n L i s t e n e r (
14 new A c t i o n L i s t e n e r ( ) {
15 p u b l i c v o i d a c t i o n P e r f o r m e d ( ActionEvent e ) {
16 abc ( ) ;
17 }
18 }
19 );
20 getContentPane ( ) . add ( b1 , BorderLayout .NORTH) ;
21 s e t S i z e (300 , 200);
22 show ( ) ;
23 }
24
25 p r i v a t e v o i d abc ( ) {
26 j f c = new J F i l e C h o o s e r ( ) ;
CHAPTER 4. TEXT-BASED AND GUI-BASED APPLICATIONS 64
27 j f c . showOpenDialog ( t h i s ) ;
28 }
29
30 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
31 OpenFile o f = new OpenFile ( ) ;
32 o f . addWindowListener (
33 new WindowAdapter ( ) {
34 p u b l i c v o i d windowClosing ( WindowEvent e ) {
35 System . e x i t ( 0 ) ;
36 }
37 }
38 );
39 }
40 }
Chapter 5
1. A virtual CPU.
65
CHAPTER 5. THREADS, SOCKETS, AND COLLECTIONS API 66
8 }
9
10 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
11 ThreadTest t = new ThreadTest ( ) ;
12 Thread t h r e a d 1 = new Thread ( t ) ;
13 Thread t h r e a d 2 = new Thread ( t ) ;
14 Thread t h r e a d 3 = new Thread ( t ) ;
15 thread1 . s t a r t ( ) ;
16 thread2 . s t a r t ( ) ;
17 thread3 . s t a r t ( ) ;
18 }
19 }
1 p u b l i c c l a s s MyThread e x t e n d s Thread {
2 p u b l i c v o i d run ( ) {
3 i n t i =1;
4 w h i l e ( i <=100){
5 System . out . p r i n t l n ( ” i : ”+ i ) ;
6 i ++;
7 }
8 }
9
10 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
11 Thread t h r e a d 1 = new MyThread ( ) ;
12 Thread t h r e a d 2 = new MyThread ( ) ;
13 Thread t h r e a d 3 = new MyThread ( ) ;
14 thread1 . s t a r t ( ) ;
15 thread2 . s t a r t ( ) ;
16 thread3 . s t a r t ( ) ;
17 }
18 }
The sleep() is a static method in the Thread class, it operates on the
current thread and is referred to as Thread.sleep(x); where x is the minimum
number of millisecond.
1 p u b l i c c l a s s ThreadSleep implements Runnable {
2 p u b l i c v o i d run ( ) {
3 i n t i =1;
4 w h i l e ( i <=10){
5 System . out . p r i n t l n ( ” i : ”+ i ) ;
6 i ++;
7 try {
8 Thread . s l e e p ( 3 0 0 ) ;
9 } catch ( InterruptedException e ){
CHAPTER 5. THREADS, SOCKETS, AND COLLECTIONS API 67
10 System . out . p r i n t l n ( e ) ;
11 }
12 }
13 }
14
15 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
16 ThreadSleep t s = new ThreadSleep ( ) ;
17 Thread t h r e a d 1 = new Thread ( t s ) ;
18 Thread t h r e a d 2 = new Thread ( t s ) ;
19 Thread t h r e a d 3 = new Thread ( t s ) ;
20 thread1 . s t a r t ( ) ;
21 thread2 . s t a r t ( ) ;
22 thread3 . s t a r t ( ) ;
23 }
24 }
Code : The S t a c k T e s t . j a v a a p p l i c a t i o n
1 c l a s s MyStack{
2 p r i v a t e i n t i d x =0;
3 p r i v a t e c h a r [ ] data = new c h a r [ 6 ] ;
4
5 p u b l i c s y n c h r o n i z e d v o i d push ( c h a r c ) {
6 this . notify ();
7 i f ( i d x !=5){
8 data [ i d x ]= c ;
9 i d x ++;
10 }
11 }
12
13 p u b l i c s y n c h r o n i z e d c h a r pop ( ) {
14 i f ( i d x ==0){
15 try {
16 t h i s . wait ( ) ;
17 } catch ( InterruptedException e ){
18 System . out . p r i n t l n ( e ) ;
19 }
20 }
21 idx −−;
22 r e t u r n data [ i d x ] ;
23 }
24 }
25
26 c l a s s Producer implements Runnable {
27 p r i v a t e MyStack s t a c k ;
CHAPTER 5. THREADS, SOCKETS, AND COLLECTIONS API 68
28
29 p u b l i c Producer ( MyStack s ) {
30 stack = s ;
31 }
32
33 p u b l i c v o i d run ( ) {
34 char c ;
35 f o r ( i n t i =0; i <50; i ++){
36 c = ( c h a r ) ( Math . random ()∗26+ ’A ’ ) ;
37 s t a c k . push ( c ) ;
38 System . out . p r i n t l n ( ” Producer : ”+c ) ;
39 try {
40 Thread . s l e e p ( ( i n t ) ( Math . random ( ) ∗ 3 0 0 ) ) ;
41 } catch ( InterruptedException e ){
42 System . out . p r i n t l n ( e ) ;
43 }
44 }
45 }
46 }
47
48 c l a s s Consumer implements Runnable {
49 p r i v a t e MyStack s t a c k ;
50
51 p u b l i c Consumer ( MyStack s ) {
52 stack = s ;
53 }
54
55 p u b l i c v o i d run ( ) {
56 char c ;
57 f o r ( i n t i =0; i <50; i ++){
58 c = s t a c k . pop ( ) ;
59 System . out . p r i n t l n ( ” Consumer : ”+c ) ;
60 try {
61 Thread . s l e e p ( ( i n t ) ( Math . random ( ) ∗ 3 0 0 ) ) ;
62 } catch ( InterruptedException e ){
63 System . out . p r i n t l n ( e ) ;
64 }
65 }
66 }
67 }
68
69 pub lic c l a s s StackTest {
70 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
71 MyStack s = new MyStack ( ) ;
CHAPTER 5. THREADS, SOCKETS, AND COLLECTIONS API 69
1 import j a v a . u t i l . ∗ ;
2
3 p u b l i c c l a s s SetExample {
4 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a g r s ) {
5 S e t s e t = new HashSet ( ) ;
6 s e t . add ( ” One ” ) ;
7 s e t . add ( ” 2 nd ” ) ;
8 s e t . add ( ” 3 rd ” ) ;
9 s e t . add ( new I n t e g e r ( 6 ) ) ;
10 s e t . add ( new F l o a t ( 7 . 7 F ) ) ;
11 s e t . add ( ” 2 nd ” ) ; // d u p l i c a t e a r e not added
12 System . out . p r i n t l n ( s e t ) ;
13 }
14 }
1 import j a v a . u t i l . ∗ ;
2
3 p u b l i c c l a s s ListExample {
4 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a g r s ) {
5 L i s t l i s t = new A r r a y L i s t ( ) ;
6 l i s t . add ( ” One ” ) ;
7 l i s t . add ( ” 2 nd ” ) ;
8 l i s t . add ( ” 3 rd ” ) ;
9 l i s t . add ( new I n t e g e r ( 6 ) ) ;
CHAPTER 5. THREADS, SOCKETS, AND COLLECTIONS API 70
10 l i s t . add ( new F l o a t ( 7 . 7 F ) ) ;
11 l i s t . add ( ” 2 nd ” ) ; // d u p l i c a t e i s added
12 System . out . p r i n t l n ( l i s t ) ;
13 }
14 }
1 import j a v a . n e t . ∗ ;
2 import j a v a . i o . ∗ ;
3
4 public class ClientServer {
5 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
6 try {
7 S o c k e t s 1 = new S o c k e t ( ” 1 2 7 . 0 . 0 . 1 ” , 5 4 3 2 ) ;
8 InputStream i s = s 1 . g e t I n p u t S t r e a m ( ) ;
CHAPTER 5. THREADS, SOCKETS, AND COLLECTIONS API 71