9. 9
first.java
class first
{
public static void main (String arg[ ])
{
System.out.println(“Welcome to the world of Java”);
}
}
MS-DOS Prompt -Ÿ° õ‰¶M´ƒèœ. Ü Start--->Program--->Ms-Dos
Prompt â‹ è†ì¬÷¬òŠ ðò¡ð´ˆ¶ƒèœ.
ðì‹ðì‹ðì‹ðì‹ðì‹ 1.1
Þšõ£Á Þ™¬ôªòQ™ Windows -¡ Start Menu -M™ àœ÷ Run
è†ì¬÷¬ò AO‚ ªêŒ»ƒèœ. ܃°
ðì‹ðì‹ðì‹ðì‹ðì‹ 1.2
27. 27
{
empno=tempno;
ename=tename;
job=tjob;
salary=tsalary;
}
public void view()
{
System.out.println(“Employee Number : “ + empno);
System.out.println(“Employee Name : “ + ename);
System.out.println(“Job : “ + job);
System.out.println(“Salary : “ + salary);
}
}
class tata
{
public static void main(String arg[])
{
emp nancy = new emp();
emp john = new emp();
nancy.assign(101,”Nancy Jenifer”,”Analyst”,6000);
john.assign(102,”John Thomas”,”Programmer”,6000);
nancy.view();
john.view();
}
}
Þï¢î𢠹«ó£è¤ó£ñ¤ô¢ emp âù¢ø class åù¢Áñ¢ Tata âù¢ø class åù¢Áñ¢
â¿îð¢ð좮¼è¢è¤ù¢øù. Þï¢î Þóí¢´ class -ø¢è¢°ñ¢ Þ¬ìò¤ô¢ âï¢îªõ£¼
ªî£ìó¢¹ñ¢ Þô¢¬ô.
emp âÂñ¢ class -ô¢ å¼ èñ¢ªðù¤ò¤ô¢ «õ¬ôð¢ ð£ó¢è¢è¤ù¢ø ªî£ö¤ô£÷ó¢
è÷¤ù¢ îèõô¢è¬÷è¢ ¬èò£÷¢õîø¢°ø¤ò ¹«ó£è¤ó£ñ¤¬ù â¿î¤ò¤¼è¢è¤ù¢«ø£ñ¢.
28. 28
Üî£õ¶ Þî¤ô¢ empno, ename, job, salary âù¢ø îèõô¢è¬÷ê¢ «êèó¤è¢°ñ¢
ªð£¼ì¢´ variable -è¬÷ à¼õ£è¢è¤, Üõø¢ø¤ô¢ ñî¤ð¢¹è¬÷ ðî¤õîø¢°ñ¢, ñø¢Áñ¢
Üõø¢ø¤ô¢ Þ¼è¢è¤ù¢ø ñî¤ð¢¹è¬÷ è£í¢ðîø¢è¢°ñ£è º¬ø«ò assign ñø¢Áñ¢
view Ýè¤ò function -è¬÷ â¿î¤ò¤¼è¢è¤ù¢«ø£ñ¢.
Tata âù¢ªø£¼ class Þ¼ð¢ð¬îè¢ èõù¤»é¢è÷¢. Þ¶ Tata âù¢Âñ¢
èñ¢ªðù¤ò¤ù¢ îèõô¢è¬÷ ðó£ñó¤è¢°ñ¢ class âù¢Á èø¢ð¬ù ªêò¢¶
ªè£÷¢Àé¢è÷¢. Þï¢î Class -ô¢ î£ù¢ main function Ýù¶ Þ¼è¢è¤ù¢ø¶
âù¢ðî¬ù»ñ¢ èõù¤»é¢è÷¢. Þï¢î ¹«óè¤ó£ññ¤¬ù ï¦é¢è÷¢ Compile ªêò¢¶
Run ªêò¢»ñ¢ ªð£¿¶ Tata èñ¢ªðù¤ò¤ô¢ à÷¢÷ main function î£ù¢ Þò颰ñ¢
âù¢ð¶ àé¢èÀ袰 郎ùõ¤¼è¢°ñ¢. Þï¢î main function -ô¢ ï£ñ¢ emp âù¢Âñ¢
class õ¤¬ùê¢ ê£ó¢ï¢î Þóí¢´ object -´è¬÷ à¼õ£è¢è¤ò¤¼è¢è¤ù¢«ø£ñ¢.
Üî£õ¶,
emp nancy = new emp( ) ;
emp john = new emp( ) ;
ºîô¤ò õó¤è¬÷ °ø¤ð¢ð¤´è¤ù¢«ø£ñ¢. ï£ñ¢ Þ¼ð¢ð«î£ Tata âù¢Âñ¢ Class-
ø¢è°÷¢ Üé¢è¤¼ï¢¶ «õªø£¼ class -Ýù emp -ò¤ù¢ Object -´è¬÷ Þ颰
à¼õ£è¢è¤è¢ ªè£÷¢Àè¤ù¢«ø£ñ¢. ð¤ù¢ùó¢ nancy ñø¢Áñ¢ john Ýè¤ò Object-
´èÀ袰ø¤ò assign ñø¢Áñ¢ view function -è¬÷ð¢ ðòù¢ð´î¢î¤è¢
ªè£÷¢Àè¤ù¢«ø£ñ¢.
Þõø¢ø¤ô¤¼ï¢¶ å¼ class â¿îð¢ð좴õ¤ì¢ì£ô¢ Üîù¢ Object -´è¬÷
«õÁ âï¢î class -ù¢ à÷¢ ޼ñ¢ ðòù¢ð´î¢î¤è¢ ªè£÷¢÷ô£ñ¢ âù¢ðî¬ù
ªî÷¤õ£è Üø¤ï¢¶ ªè£÷¢Àé¢è÷¢. Þð¢ªð£¿¶, class âù¢ø£ô¢ âù¢ù âù¢Á
åó÷¾ ªî÷¤ï¢î¤¼ð¢ð¦ó¢è÷¢. Þù¤ class ù¤¬ùð¢ ðø¢ø¤ò ªî÷¤õ£ù Syntax -ä
ð£ó¢ð¢«ð£ñ¢.
Syntex:
[modifiers] class <class name> [extend <superclass>] [implements
<interfaces>]
âð¢ªð£¿¶«ñ èì¢ì¬÷è÷¤ù¢ Syntax -è¬÷ ð®è¢°ñ¢ ªð£¿¶ [ ] brackets-
è÷¤ù¢ à÷¢ Þ¼ð¢ð¬õ Option -è÷¢ ݰñ¢. Üî£õ¶ Üõø¢¬øð¢ ðòù¢ð´î¢îô£ñ¢
Üô¢ô¶ ðòù¢ð´î¢î£ñ½ñ¢ Þ¼è¢èô£ñ¢. Þ¶õ¬ó ï£ñ¢ ð£ó¢î¢î â´î¢¶è¢
è£ì¢´è÷¤ô¢ [modifiers], [extend super class] ñø¢Áñ¢ [implement interface] ºîô£ù
Þùé¢è÷¢ ðòù¢ð´î¢îð¢ðìõ¤ô¢¬ô. ܬõ åõ¢ªõ£ù¢Áñ¢ ªõõ¢«õÁ
29. 29
ðòù¢ð£´è¬÷è¢ ªè£í¢ì¬õ. âù«õ Üõø¢¬øî¢ îù¤ â´î¢¶è¢è£ì¢´è÷¢
Íôñ¢ Üø¤ï¢¶ ªè£÷¢÷ô£ñ¢.
Inheritance
Object Oriented Programming º¬øò¤ô¢ Þ¼è¢è¤ù¢ø ºè¢è¤òñ£ù
ðòù¢ð£´è÷¤ô¢ Inheritance -ñ¢ åù¢Á. Þï¢î º¬øò¤ù¢ Íôñ¢ Þóí¢´ class-
èÀè¢è¤¬ìò¤ô¢ å¼ Þ¬íð¢ð¤¬ù à¼õ£è¢°è¤ù¢«ø£ñ¢. Þîù¢ð® Þóí¢´
class -è÷¤ô¢ åù¢ø¤¬ù super class âù¾ñ¢ ñø¢ªø£ù¢ø¤¬ù subclass âù¾ñ¢
ܬöè¢è¤ù¢«ø£ñ¢.
â´î¢¶è¢è£ì¢ì£è father âù¢ªø£¼ class-ñ¢ child âù¢Âñ¢ ªðòó¤ô¢ å¼
class-ñ¢ Þ¼ð¢ðî£è ¬õ袪è£÷¢«õ£ñ¢. father class -ù¢ à÷¢ father name, age,
occupation ºîô£ù å¼ îîò¤¬ùð¢ðø¢ø¤ò îèõô¢è¬÷ê¢ «êèó¤î¢¶
¬õè¢èð¢ð좮¼è¢è¤ù¢øù. child class -à÷¢ child name, age, hobbies ºîô£ù å¼
ñèù¤¬ùð¢ðø¢ø¤ò îèõô¢è÷¢ «êèó¤î¢¶ ¬õè¢èð¢ð좮¼è¢è¤ù¢øù âù¢Á
ªè£÷¢Àé¢è÷¢. Þï¢î Þóí¢´ class -è÷¢ îù¤î¢îù¤ò£è õ¬óò¼è¢èð¢
ð좮¼ð¢ð¤ù¢ åù¢Áìù¢ åù¢Á ªî£ìó¢¹ Þ¼è¢è£¶ Üô¢ôõ£? Þð¢ªð£¿¶
Child class -ø¢è¢° å¼ Object -®¬ù à¼õ£è¢°è¤ù¢«ø£ñ¢ âù¢Á ¬õè¢
ªè£÷¢Àé¢è÷¢. Üï¢î child object -ù¢ à÷¢ ïñ袰 father -ø¤¬ùð¢ îèõô¢è÷¢
õó«õí¢´ñ¢ âù¢Á õ¤¼ñ¢ð¤ù£ô¢ Üîø¢° ï£ñ¢ inheritance º¬øò¤¬ùè¢
¬èò£÷ «õí¢´ñ¢. Þîø¢° extends âù¢Âñ¢ èì¢ì¬÷ð¢ ðòù¢ð´è¤ù¢ø¶.
Listing 2.3
class father
{
String fname;
int fage;
public void assign(String tfname, int tfage)
{
fname=tfname;
fage=tfage;
}
public void view()
{
System.out.println(“Father’s Name : “ + fname);
System.out.println(“Father’s Age : “ + fage);
30. 30
System.out.println(“”);
}
}
class child extends father
{
String cname;
int cage;
public void store(String tcname, int tcage)
{
cname=tcname;
cage=tcage;
}
public void display()
{
System.out.println(“Child’s Name : “ + cname);
System.out.println(“Child’s Age : “ + cage);
System.out.println(“”);
}
}
class school
{
public static void main(String arg[])
{
child allen = new child();
allen.assign(“Sebastian Raj”,30);
allen.store(“Allen Smith”,3);
allen.view();
allen.display();
}
}
31. 31
Þï¢î𢠹«ó£è¤ó£ñ¤ô¢ father âù¢Âñ¢ class Ýù¶ ºîô¤ô¢
â¿îð¢ð좮¼è¢è¤ù¢ø¶. Üî¬ùî¢ ªî£ìó¢ï¢¶ child âÂñ¢ class è¦ö¢è¢èí¢ìõ£Á
õ¬óò¼è¢èð¢ð좮¼ð¢ð¬îè¢ èõù¤»é¢è÷¢.
class child extends father
{
.....
}
Þ颰 extends father âù¢Á ªè£´è¢èð¢ð좮¼ð¢ð¤ù¢ Íôñ¢ child class Ýù¶
father class àìù¢ Þ¬íè¢èð¢ð좮¼è¢è¤ù¢ø¶. âù«õ Þð¢ªð£¿¶ father
class Ýù¶ child class ¬ìò superclass Ýè ªêòô¢ð´è¤ù¢ø¶. Üô¢ô¶
base Class âù¢Áñ¢ Ãøô£ñ¢.
Þîù¢ð® ï¦é¢è÷¢ âð¢ªð£¿ªîô¢ô£ñ¢ child class -ø¢° Object -´è¬÷
à¼õ£è¢°è¤ù¢ø¦ó¢è«÷£ Üð¢ªð£¿ªîô¢ô£ñ¢ super class Ýù father -ù¢ Object
åù¢Áñ¢ î£ù£è à¼õ£è¢èð¢ð좴 õ¤´ñ¢. ï£ñ¢ îù¤ò£è father class -ø¢è¢°
ªèù¢Á Object -´è¬÷ à¼õ£è¢è «õí¢®òî¤ô¢¬ô. «ñ½ñ¢ Þï¢î ¹«ó£è¤ó£ñ¤ô¢
child allen = new child( );
âù¢Á ªè£´î¢¶ allen âù¢Âñ¢ ªðòó¤ô¢ child class -ù¢ Object -¬ù à¼õ£è¢è¤
ò¤¼è¢è¤ù¢«ø£ñ¢. Þ颰
allen.store(...)
allen.display( )
ºîô£ù¬õ Child Class -¬ìò function -è÷¢ ݰñ¢.
allen.assign(...)
allen.view( )
ºîô£ù¬õ father class -ù¢ function -è÷ ݰñ¢. Þï¢î function -è÷¢ allen
âù¢Âñ¢ child class -ù¢ Íôñ¢ Þò颰è¤ù¢ø¶ âù¢ðî¬ù Üø¤õîù¢Íôñ¢
Inheritance âõ¢õ£Á Þò颰è¤ù¢ø¶ âù¢ðî¬ù Üø¤ï¢¶ ªè£÷¢÷ô£ñ¢.
°ø¤ð¢¹°ø¤ð¢¹°ø¤ð¢¹°ø¤ð¢¹°ø¤ð¢¹
Java -õ¤¬ù𢠪ð£¼î¢îõ¬óò¤ô¢ åõ¢ªõ£¼ °ø¤ð¢ð¤ì¢ì «õ¬ôò¤¬ùê¢
ªêò¢õîø¢°ñ¢ âù¢Á ðô¢«õÁ class -è÷¢ â¿î¤ ¬õè¢èð¢ð좴 Þ¼è¢è¤ù¢øù.
Üï¢î class -è¬÷ ï£ñ¢ «î¬õè¢«èø¢ø£ó¢«ð£ô Object -´è÷£è à¼õ£è¢è¤
ðòù¢ð´î¢î¤è¢ ªè£÷¢÷ «õí¢´ñ¢. Üô¢ô¶ ï£ñ£è å¼ class -¬ù à¼õ£è¢è¤
Üï¢î class -ø¢° ãø¢è¢ªèù«õ Þ¼è¢è¤ù¢ø Java class -è÷¤¬ù extend
èì¢ì¬÷ò¤¬ùð¢ ðòù¢ð´î¢î¤ Inheritance ªêò¢¶ñ¢ ðòù¢ð´î¢î¤è¢ ªè£÷¢÷ô£ñ¢.
32. 32
Constructors
å¼ class -ù¢ à÷¢ Üï¢î class -ù¢ ªðòó¤«ô«ò function -è÷¤¬ù â¿¶ñ¢
º¬ø«ò consturctor âù¢Á ܬöè¢èð¢ð´è¤ù¢ø¶.
â´î¢¶è¢è£ì¢ì£è class circle âù¢Á å¼ class Þ¼ð¢ðî£è ªè£í¢ì£ô¢ Üï¢î
class -ù¢ à÷¢ circle( ) âù¢ø ªðòó¤ô¢ å¼ function ޼袰ñ£ù£ô¢ ܶ
Constructor function âù¢Á ܬöè¢èð¢ð´è¤ù¢ø¶.
ê£î£óíñ£è ï£ñ¢ å¼ class -ù¢ à÷¢ â¿¶ñ¢ function -è¬÷ Üï¢î class-
ù¢ Object -è÷¤ù¢ õö¤ò£èî¢î£ù¢ Þò袰«õ£ñ¢. Ýù£ô¢ Constructor function
è÷¤¬ù𢠪ð£¼î¢î õ¬óò¤ô¢ ï£ñ£è Üî¬ù Þòè¢èî¬õò¤ô¢¬ô. ñ£ø£è
ܶ î£ù£è«õ Þò颰ñ¢. âõ¢õ£ªøù¤ô¢ âð¢ªð£¿ªîô¢ô£ñ¢ å¼ class - ù¢
Object à¼õ£è¢èð¢ð´è¤ù¢ø«î£, Üð¢ªð£¿ªîô¢ô£ñ¢ Üï¢î Class ù¢ à÷¢
Constructor Function -è÷¢ ޼袰ñ¢ ðì¢êî¢î¤ô¢ ܬõ î£ù£è ºîô¤ô¢
Þòé¢è¤õ¤´ñ¢.
âù«õ Þï¢î Constructor º¬øò¤ù¢ Íôñ¢ class -ù¢ Object ´è÷¢
à¼õ£°ñ¢ êñòî¢î¤ô¢ ïìè¢è «õí¢®ò intialization ºîô¤ò ïñè¢°î¢ «î¬õò£ù
«õ¬ôèÀ袰ø¤ò ¹«ó£è¤ó£ñ¢è¬÷ â¿î¤è¢ ªè£÷¢÷ô£ñ¢.
Listing 2.4
class hello
{
hello()
{
System.out.println(“Hello Constructor”);
}
public static void main(String arg[])
{
hello one = new hello();
hello two = new hello();
hello three = new hello();
}
}
Þï¢î ¹«ó£è¤ó£ñ¤ô¢ hello âù¢ø class â¿îð¢ð좮¼è¢è¤ù¢ø¶. «ñ½ñ¢ hello
âù¢ø class-ù¢ ªðòó¤«ô«ò function åù¢Áñ¢ â¿îð¢ð좮¼ð¢ð¬îè¢ èõù¤»é¢è÷¢.
33. 33
Þ¶ Þï¢î class -ù¢ constructor function ݰñ¢. ÞîÂ÷¢,
System.out.println(“Hello Constructor”);
âù¢ø õó¤ â¿îð¢ð좮¼è¢è¤ù¢ø¶. main( ) function -ô¢ one, two, three âù¢Á
Íù¢Á Object -è÷¢ new operator -ø¤¬ùð¢ ðòù¢ð´î¢î¤ à¼õ£è¢èð¢ð좮¼ð¢ð¬îè¢
èõù¤»é¢è÷¢. Þï¢î Íù¢Á Object -è÷¢ à¼õ£°ñ¢ êñòî¢î¤½ñ¢ Üï¢îï¢î object
´è¢°ø¤ò constructor function -è÷¢ î£ù£è Þòé¢è¤õ¤´ñ¢.
Listing 2.5
class circle
{
int x;
int y;
int radius;
circle()
{
x=0;
y=0;
radius=0;
}
circle(int tx,int ty,int tradius)
{
x=tx;
y=ty;
radius=tradius;
}
circle(circle obj)
{
x=obj.x;
y=obj.y;
radius=obj.radius;
}
Java-5
34. 34
public void store(int tx,int ty,int tradius)
{
x=tx;
y=ty;
radius=tradius;
}
public void display()
{
System.out.println(“X value: “ + x);
System.out.println(“Y value: “ + y);
System.out.println(“Radius: “ + radius);
System.out.println(“”);
}
}
class check
{
public static void main(String arg[])
{
circle one = new circle();
circle two = new circle(5,10,15);
circle three = new circle(two);
one.display();
two.display();
three.display();
}
}
Þï¢î ¹«ó£è¤ó£ñ¤ô¢ circle âù¢Á å¼ class â¿îð¢ð좮¼è¢è¤ù¢ø¶.
ê£î£óíñ£è å¼ circle õ¬óò «õí¢´ñ¢ âù¢ø£ô¢ ¬ñòð¢¹÷¢÷¤»ñ¢(Centre
Point) Ýóºñ¢(Radius) «î¬õ. âù«õ ï£ñ¢ x, y ñø¢Áñ¢ radius âù¢Á Íù¢Á
variable -è¬÷ class-ù¢ à÷¢ â¿î¤ò¤¼è¢è¤ù¢«ø£ñ¢. circle âù¢ø ªðòó¤ô¢ Íù¢Á
Constructor function -è÷¢ â¿îð¢ð좮¼ð¢ð¬îè¢ èõù¤»é¢è÷¢. ºîô¢ circle
constructor function -ù¤ô¢ âï¢î å¼ ñî¤ð¢¹èÀñ¢ argument -è÷£è
35. 35
ªè£´è¢èð¢ð좮¼è¢è õ¤ô¢¬ô. Þóí¢ì£õ¶ constructor function -ù¤ô¢ tx, ty,
tradius âù¢Á Íù¢Á int variable è÷¢argument -è÷£è õ£é¢èð¢ð좮¼ð¢ð¬îè¢
èõù¤»é¢è÷¢. Íù¢ø£õ¶, circle constructor function -ù¤ô¢ Obj âù¢Âñ¢ circle
class -ù¢ variable, argument Ýè ªè£´è¢èð¢ððð좮¼ð¢ð¬îè¢ èõù¤»é¢è÷¢.
Þï¢î Íù¢Á constructor function è÷¤ô¢ ãî£õ¶ åù¢Á, ï£ñ¢ Üï¢î class -ø¢è¢°
Object -è¬÷ à¼õ£è¢°ñ¢ ªð£¿¶ ªè£´è¢è¤ù¢ø ñî¤ð¢¹èÀ袰 ãø¢ø£ø¢ð¢ «ð£ô
Þò颰è¤ù¢ø¶. main ( ) function ù¤ô¢,
circle one = new circle( );
âù¢Á ªè£´è¢°ñ¢ ªð£¿¶ ï£ñ¢ ºîô¤ô¢ â¿î¤ò¤¼è¢è¤ù¢ø constructor function
Þò颰è¤ù¢ø¶. ܴ
circle two=new circle(5, 10, 15);
âù¢Á ªè£´è¢°ñ¢ªð£¿¶ Þóí¢ì£õî£è ï£ñ¢ â¿î¤ò¤¼è¢è¤ù¢ø constructor
function Ýù¶ Þò颰è¤ù¢ø¶. ãªùù¤ô¢ ï£ñ¢ Object -®¬ù à¼õ£è¢°ñ¢
ªð£¿¶ Íù¢Á int ñî¤ð¢¹è¬÷ ªè£´î¢î¤¼è¢è¤ù¢«ø£ñ¢. Üõø¢¬ø ªðø¢Áè¢
ªè£÷¢õîø¢° õêî¤ò£è Þóí¢ì£õ¶ constructor function Þ¼ð¢ðî£ô¢ Üï¢î
function Þò颰è¤ù¢ø¶.ܴ ï£ñ¢
circle three = new circle (two);
âù¢Á ªè£´è¢°ñ¢ ªð£¿¶ ï£ñ¢ â¿î¤ò¤¼è¢è¤ù¢ø Íù¢ø£õ¶ constructor function
Þò颰è¤ù¢ø¶. ãªùù¤ô¢ îø¢ªð£¿¶ three âÂñ¢ ï£ñ¢ ¹î¤ò Object -®¬ù
à¼õ£è¢°ñ¢ ªð£¿¶ ãø¢è¢ªèù«õ Þ¼è¢è¤ù¢ø Object åù¢ø¤ô¢ Þ¼è¢è¤ù¢ø
ñî¤ð¢¹è¬÷ Üð¢ð®«ò Þð¢ªð£¿¶ ï£ñ¢ à¼õ£è¢°ñ¢ Object -®ø¢è¢°÷¢ õó
«õí¢´ñ¢ âù¢ðîø¢è£è two âù¢ø Object-®¬ù parameter Ýè ÜÂð¢¹
è¤ù¢«ø£ñ¢. circle class -ù¢ object -®¬ù«ò parameter Ýè õ£é¢è¤è¢ªè£÷¢õîø¢°
õêî¤ò£è Íù¢ø£õ¶ constructor function â¿îð¢ð좴ò¤¼ð¢ðî£ô¢ ܶ
Þò颰è¤ù¢ø¶.
ªñ£î¢îî¢î¤ô¢ constructor function è÷¢ Object -´è÷¢ à¼õ£°ñ¢ êñòî¢î¤ô¢
î£ù£è Þòé¢è¤ ï£ñ¢ ªè£´î¢î¤¼è¢è¤ù¢ø ܬù «õ¬ôè¬÷»ñ¢
ªêò¢õîø¢°ñ¢ ðòù¢ð´è¤ù¢ø¶ âù¢ðî¬ù 郎ùõ¤ô¢ ªè£÷¢Àé¢è÷¢.
°ø¤ð¢¹:°ø¤ð¢¹:°ø¤ð¢¹:°ø¤ð¢¹:°ø¤ð¢¹:
å«ó ªðòó¤ù¤ô¢ åù¢Á袰ñ¢ «ñø¢ð¢ðì¢ì function-è¬÷ â¿¶«õ£«ñ
ò£ù£ô¢ Üîø¢è¢° function overloading âù¢Á ªðòó¢. Þï¢î º¬øò¤ô¢ function-
è¬÷ â¿¶ñ¢ ªð£¿¶ åõ¢ªõ£Á function -ù¤½ñ¢ ªõõ¢«õÁ data type -è÷¤ô¢
36. 36
parameter -è÷¢ ªè£´è¢èð¢ð좮¼è¢è «õí¢´ñ¢. ï£ñ¢ function -¬ù Þò袰ñ¢
ªð£¿¶ ªè£´è¢è¤ù¢ø parameter ñî¤ð¢¹èÀ袰 ãø¢ð Üï¢îï¢î function -è÷¢
Þò颰ñ.
ªêù¢ø ¹«ó£è¤ó£ñ¤ô¢ circle âù¢ø ªðòó¤ô¢ Íù¢Á function-è÷¢
â¿îð¢ð좮¼ð¢ð¶ñ¢ function overloading º¬øò¤ô¢ î£ù¢ âù¢ðî¬ù 郎ùõ¤ô¢
ªè£÷¢è.
Modifiers:
ªð£¶õ£è class -¬ù â¿¶ñ¢ ªð£¿¶ modifiers âù¢Á ܬöè¢èð¢ð´ñ¢
abstract, public, ñø¢Áñ¢ final ºîô£ù¬õ ðòù¢ð´î¢îð¢ð´è¤ù¢øù. 覫ö
ªè£´è¢èð¢ð좴÷¢÷ â´î¢¶è¢è£ì¢´è¬÷ èõù¤»é¢è÷¢
abstract class my_graphics
{
....
}
public final class String
{
....
}
abstract âù¢Á ªè£´è¢°ñ¢ ªð£¿¶ Üï¢î class-ù¢ à÷¢ â¿îð¢ð´ñ¢ function
åù¢ªøÂñ¢ abstract Ýè Þ¼è¢è «õí¢´ñ¢. abstract class åù¢Á Þ¼ð¢ðî£è¾ñ¢
Üî¬ùê¢ ê£ó¢ï¢¶ ðô subclass -è÷¢ Þ¼ð¢ðî£è¾ñ¢ ªè£÷¢Àé¢è÷¢. â´î¢¶è¢
è£ì¢ì£è my_graphics âù¢ø class Ýù¶ abstract class Ýè Þ¼è¢è¤ù¢ø¶.
Üî¬ùê¢ ê£ó¢ï¶ circle, rectangle, triangle ºîô¤ò class-è÷¢ subclass-è÷£è
Þ¼è¢è¤ù¢øù. Þ¬õ ܬùî¢î¤¼è¢°ñ¢ Draw âù¢ø ªðòó¤ô¢ function-è÷¢
Þ¼è¢è¤ù¢ø¶ âù¢Á ¬õ袪è£í¢ì£ô¢, ܬõ åõ¢ªõ£ù¢Áñ¢ ªõõ¢«õÁ
«õ¬ô ªêò¢õîø¢°ð¢ ðòù¢ð´ñô¢ôõ£? Þ¶ «ð£ù¢ø êñòé¢è÷¤ô¢ ï£ñ¢ Þõø¢¬ø
abstract º¬øò¤ô¢ â¿î «õí¢´ñ¢.
final âù¢ø modifier Ýù¶ ðòù¢ð´î¢îð¢ðì¢ì£ô¢ Üï¢î class -¬ù inheritance
ªêò¢ò Þòô£¶. Üî£õ¶ Üï¢î class Ýù¶ «õÁ âï¢î class -ø¢è¢°ñ¢ superclass
Ýè ªêòô¢ðì Þòô£¶.
public âù¢ø modifier Ýù¶ ðòù¢ð´î¢îð¢ðì¢ì£ô¢ Üï¢î class -¬ù ,
37. 37
«õÁ ðô ¹«ó£è¤ó£ñ¢è÷¤ô¢ Þ¼ï¢¶è¢ ðòù¢ð´î¢î¤è¢ ªè£÷¢÷ô£ñ¢.
â´î¢¶è¢è£ì¢ì£è ï£ñ¢ Java õ¤ô¢ ðòù¢ð´î¢¶ñ¢ String âù¢Âñ¢ class -¬ù
public class Ýè â¿î¤ Þ¼ð¢ðîù£ô¢ î£ù¢ ïñ¢ñ£ô¢ Üî¬ù ܬù
¹«ó£è¤ó£ñ¢è÷¤½ñ¢ ðòù¢ð´î¢î º®è¤ù¢ø¶.
this ñø¢Áñ¢ñø¢Áñ¢ñø¢Áñ¢ñø¢Áñ¢ñø¢Áñ¢ super
Java õ¤ô¢ this ñø¢Áñ¢ super âù Þóí¢´ ê¤øð¢¹ keyword -è÷¢ Þ¼è¢è¤ù¢øù.
å¼ class -ù¢ à÷¢ ï£ñ¢ ðô¢«õÁ variable -è÷¢ ñø¢Áñ¢ function -è¬÷ â¿î¤
ò¤¼ð¢«ð£ñ¢. Þõø¢¬ø Üï¢î class -è÷¤ù¢ Object ´è÷¢ õö¤ò£è Þò袰è¤ù¢«ø£ñ¢
Üô¢ôõ£? Üï¢î êñòé¢è÷¤ô¢ åõ¢ªõ£Á Object -ø¢è¢°ñ¢ âù¢Á îù¤î¢îù¤ò£è
variable -è÷¢ ºîô£ù¬õ à¼õ£°ñ¢. âï¢î variable âï¢î object -®Â¬ìò¶
âù¢ðî¬ù ªê£ô¢õîø¢è¢è£è this âÂñ¢ operator ðòù¢ð´è¤ù¢ø¶.
Listing 2.6
class student
{
int rollno;
String name;
public void store(int rollno, String name)
{
this.rollno = rollno;
this.name = name;
}
public void display()
{
System.out.println(“Roll Number: “ + this.rollno);
System.out.println(“Name: “ + this.name);
System.out.println(“”);
}
public static void main(String arg[])
{
student nancy = new student();
38. 38
student john = new student();
student george = new student();
nancy.store(1,”Nancy Jenifer”);
nancy.display();
john.store(2,”John Thomas”);
john.display();
george.store(3,”George Clinton”);
george.display();
}
}
Þ颰 student âù¢Âñ¢ class ô¢ õ¬óò¼è¢èð¢ð좴÷¢÷ RollNo, Name,
Ýè¤ò variable -è¬÷ Üï¢î class-ù¢ function è÷¤ô¢ this.Rollno, this.name âù¢Á
ðòù¢ð´î¢î¤ò¤¼ð¢ð¬îè¢ èõù¤»é¢è÷¢. Üî£õ¶ Student class -ø¢è¢°, ï£ñ¢ nancy,
john, george âù¢Á Íù¢Á Object -è÷¢ à¼õ£è¢è¤ò¤¼è¢è¤ù¢«ø£ñ¢. Þï¢î Íù¢Á
object -´èÀ袰ñ¢ îù¤î¢îù¤ò£è Rollno, Name ºîô£ù variable-è÷¢ Þ¼ð¢ð¬î
郎ùõ¤ô¢ ªè£÷¢Àé¢è÷¢. ï£ñ¢ nancy.store( ), john.store( ) âù¢Á õ¬èò¤ô¢
function -è¬÷ àð«ò£è¤è¢°ñ¢ ªð£¿¶, Üï¢î function -è÷¤ô¢ this.rollno, this.name
âù¢Á ªè£´î¢î¤¼ð¢ðîù¢ Íôñ¢ Üï¢îï¢î object -èÀ袰ø¤ò variable -è÷¢
ðòù¢ð´î¢îð¢ð´è¤ù¢øù.
°ø¤ð¢¹ :°ø¤ð¢¹ :°ø¤ð¢¹ :°ø¤ð¢¹ :°ø¤ð¢¹ :
Þ颰 this âù¢ð¶ ðòù¢ð´î¢îðð¢ì£õ¤ì¢ì£½ñ¢ åù¢Áñ¢ ð¤óê¢ê¬ù
Þô¢¬ô. ãªùù¤ô¢ Ü颰 Þòø¢¬èò£è«õ this operator Þ¼ð¢ðî£è Java
compiler 郎ùî¢¶î¢ ªè£÷¢Àñ¢.
super âù¢ð¶ inheritance ªêò¢»ñ¢ªð£¿¶ ðòù¢ð´è¤ù¢ø¶. Üî£õ¶
Child class -è÷¤ô¢ ޼ super class -ô¢ Þ¼è¢è¤ù¢ø function -è¬÷
àð«ò£èð¢ð´î¢¶õîø¢° Þ¶ ðòù¢ð´è¤ù¢ø¶. °ø¤ð¢ð£è å¼ super class ô¢
constructor function Þ¼ð¢ðî£è ªè£÷¢Àé¢è÷¢. ï£ñ¢ child class -ø¢° object -
´è¬÷ à¼õ£è¢°ñ¢ ªð£¿¶ child class -ô¢ Þ¼è¢è¤ù¢ø constructor function
î£ù¢ Þò颰ñ¢. Ýù£ô¢ inheritance Þ¼ð¢ðîù£ô¢ base class -ù¢ object åù¢Á
î£ù£è à¼õ£°ñ¢ function -è÷¤ø¢° ñî¤ð¢¹è¬÷ ªè£´ð¢ðîø¢° super âÂñ¢
º¬øò¤ù£ô¢ ðòù¢ð´î¢î¤è¢ ªè£÷¢÷ô£ñ¢.
Listing 2.7
39. 39
class state
{
String state_name;
state(String tstr)
{
state_name = tstr;
}
public void state_assign(String tstr)
{
state_name = tstr;
}
public void state_display()
{
System.out.println(“State Name : “ + state_name);
}
}
class city extends state
{
String city_name;
city(String tstr)
{
super(“Tamil Nadu”);
city_name = tstr;
}
public void city_assign(String tstr)
{
city_name = tstr;
}
public void city_display()
40. 40
{
System.out.println(“City Name : “ + city_name);
}
}
class country
{
public static void main(String arg[])
{
city chennai = new city(“Chennai”);
chennai.state_display();
chennai.city_display();
}
}
Þï¢î𢠹«ó£è¤ó£ñ¤ô¢ state ñø¢Áñ¢ city âù¢Á Þóí¢´ class -è÷¢ Þ¼è¢è¤ù¢øù.
Þóí¢´ñ¢ inheritance º¬øò¤ô¢ Þ¬íè¢èð¢ð좮¼è¢è¤ù¢ø¶. state âù¢ð¶ superclass
Ýè¾ñ¢city âù¢ð¶childclassÝè¾ñ¢ Þ¼è¢è¤ù¢ø¶. Þóí¢´ class è÷¤½«ñ constructor
function â¿îð¢ð좮¼è¢è¤ù¢ø¶. main() function-ù¤ô¢ ï£ñ¢ citychennai=newcity(‘chennai’)
âù¢Á ªè£´î¢¶ chennai âù¢Á å¼ Object -¬ù city âÂñ¢ class -ô¢ ޼
à¼õ£è¢°è¤ù¢«ø£ñ¢. ï£ñ¢ parameter Ýè ªè£´è¢°ñ¢ ‘Chennai’ âù¢ø ñî¤ð¢¹ cityclass-ù¢
constructor -¼è¢° àó¤ò¶. city class -ù¢ constructor function -ù¤ô¢ ï£ñ¢ super (‘Tamil nadu’);
âù¢Á ªè£´î¢î¤¼ð¢ð¬îè¢ èõù¤»é¢è÷¢. Þï¢î èì¢ì¬÷ super classÝù state ¬ìò
constructor function -ø¢° ñî¤ð¢¹è¬÷è¢ ªè£´è¢è¤ù¢ø¶.
Package
Package âù¢ð¶ ðô java ¹«ó£è¤ó£ñ¢è¬÷»ñ¢ ñø¢Áñ¢ class -è¬÷»ñ¢ ªè£í¢ì å¼
ªî£°ð¢¹ ݰñ¢. Üî£õ¶ å¼ º¬ø â¿îð¢ðì¢ì ¹«ó£è¤ó£ñ ¢è¬÷«ò£ Üô¢ô¶ class
-è¬÷«ò£ «õí¢®ò ªð£¿¶ «î¬õð¢ðì¢ì Þìé¢è÷¤ô¢ ñ¦í¢´ñ¢ ñ¦í¢´ñ¢ ðòù¢ð´î¢î¤è¢
ªè£÷¢õîø¢è¢è£è Package º¬øð¢ ðòù¢ð´è¤ù¢ø¶. ï£ñ¢ â¿¶è¤ù¢ø ðô¢«õÁ class -
è¬÷ å¼ Package Ýè à¼õ£è¢è¤õ¤ì¢´ ð¤ù¢ùó¢ Üî¬ù «õí¢®ò Þìî¢î¤ô¢
ðòù¢ð´î¢î¤è¢ ªè£÷¢÷ô£ñ¢.
Ü«î«ð£ô¢ java ªñ£ö¤ò¤½ñ¢ ðô¢«õÁ ðí¤è¬÷ê¢ ªêò¢õîø¢è¢è£è
Ëø¢Áè¢èíè¢è£ù class -èÀñ¢ ¹«ó£è¤ó£ñ¢èÀñ¢ ðô Package -è÷£è â¿îð¢ð좴
Þ¼è¢è¤ù¢øù. åõ¢ªõ£Á Package -ñ¢ °ø¤ð¢ð¤ì¢ì ê¤ô ðòù¢ð£´è¬÷ ªè£í¢ì
class -è¬÷ îù¢ùèî¢«î ªè£í¢´÷¢÷¶. Þõø¢¬ø ï£ñ¢ «õí¢®ò êñòé¢è÷¤ô¢
41. 41
ðòù¢ð´î¢î¤è¢ ªè£÷¢÷ «õí¢´ñ¢.
܆ìõ¬í܆ìõ¬í܆ìõ¬í܆ìõ¬í܆ìõ¬í 1.2
Standard Java Packages
Api Packages Description
java.applet Applet classes and interfaces
java.awt Absract windowing Toolkit ( Awt)
Class and interfaces.
java.awt.image (sub Package of Awt) Bitmap image
classes and interfaces.
java.awt.peer (subpackage of Awt) Platform-specific
AWT Classes and interfaces
(such ads Windows, sun, and machitosh)
java.io Input/Output classes and interfaces
java.lang Core java language classes and interfaces.
Java.net Network classes and interfaces.
java.util Utility classes and interface.
Sun.tool.debug Debuging classes and interface.
Import
å¼ Package -ô¢ à÷¢÷ class -è¬÷»ñ¢ ñø¢Áñ¢ ¹«ó£è¤ó£ñ¢è¬÷»ñ¢, ï£ñ¢
â¿¶è¤ù¢ø ¹«ó£è¤ó£ñ¢è÷¢ ðòù¢ð´î¢î «õí¢´ñ¢ âù¢ø£ô¢ ºîô¤ô¢ Üï¢î Package
-è¬÷ ï£ñ¢ ïñ¢º¬ìò ¹«ó£è¤ó£ñ¤ô¢ ðòù¢ð´î¢¶è¤ù¢«ø£ñ¢ âù¢Á ªê£ô¢õîø¢°
import âù¢ø èì¢ì¬÷ò¤¬ùð¢ ðòù¢ð´î¢î «õí¢´ñ¢. import -èì¢ì¬÷ò¤¬ùè¢
ªè£´î¢¶ Package -ù¢ ªðòó¤¬ùè¢ ªè£´ð¢ðîù¢ Íôñ¢ å¼ Package -ô¢ à÷¢÷
ܬù class -è¬÷»ñ¢ ïñ¢º¬ìò ¹«ó£è¤ó£ñ¢è÷¤ô¢ ðòù¢ð´î¢î¤è¢ ªè£÷¢÷
Þò½ñ¢.
import java.applet.*;
import java.awt.* ;
import java.net.URL ;
«ñ£ô¢ ªè£´è¢èð¢ð좴÷¢÷ â´î¢¶è¢ è£ì¢´è÷¤ù¢ ð® ï£ñ¢ applet, awt,
net ºîô¤ò Package è¬÷ import ªêò¢î¤¼è¢è¤ù¢«ø£ñ¢. Üõø¢ø¤ô¢ * âù¢Á
ªè£´è¢èð¢ð좮¼ð¢ðîù¢ ªð£¼÷¢ âù¢ù ªõù¢ø£ô¢. Üï¢î Package -ô¢ à÷¢÷
ܬù class -è¬÷»ñ¢ ðòù¢ð´î¢î¤è¢ ªè£÷¢÷ô£ñ¢ âù¢ð¶ ݰñ¢.
54. 54
Label totp=new Label(“Total:”,Label.RIGHT);
x=new TextField(5);
y=new TextField(5);
tot=new TextField(10);
add(xp);
add(x);
add(yp);
add(y);
add(totp);
add(tot);
x.addActionListener(this);
y.addActionListener(this);
tot.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
int a,b,c;
a=Integer.parseInt(x.getText());
b=Integer.parseInt(y.getText());
c=a+b;
tot.setText(“” + c);
}
}
Þï¢î𢠹«ó£è¤ó£ñ¤ô¢ x, y, tot âù¢Á Íù¢Á TextField object -è¬÷ à¼õ£è¢è¤
Üõø¢Á÷¢x ñø¢Áñ¢ y Ýè¤ò Object -è÷¤ô¢ Type ªêò¢òð¢ð´ñ¢ ñî¤ð¢¹è¬÷ Ã좮
tot âù¢Âñ¢ Object -ù¢ à÷¢ «ð£´ñ£Á â¿î¤ò¤¼è¢è¤ù¢«ø£ñ¢.
ï£ñ¢ x ñø¢Áñ¢ y Ýè¤ò Object -è÷¤ô¢ ñî¤ð¢¹è¬÷ Type ªêò¢î ð¤ø° Enter Key
ò¤¬ù Ü¿î¢î¤ò¾ìù¢ ïñ袰 Calculation ïìè¢è «õí¢´ñ¢ Üô¢ôõ£? Üîø¢è¢è£è
ï£ñ¢ implements ActionListener âù¢Âñ¢ õ£è¢è¤òî¢î¤¬ù
class-¬ù â¿¶ñ¢ Þìî¢î¤ù¤ô¢ Þ¬íî¢î¤¼è¢è¤ù¢«ø£ñ¢. Üî£õ¶
public class textsum extends Applet implements ActionListener
âù¢Á ªè£´è¢èð¢ð좮¼ð¢ð¬îè¢ èõù¤»é¢è÷¢. ActionListener âù¢ð¶ å¼
Interface ݰñ¢. Þ¶ «ð£ù¢Á 郎øò Interface -è÷¢ Þ¼è¢è¤ù¢øù. ïñ袰
55. 55
«õí¢®ò Interface-¬ùð¢ ðòù¢ð´î¢î¤è¢ ªè£÷¢÷ implements âù¢Âñ¢ èì¢ì¬÷
ðòù¢ð´è¤ù¢ø¶.
TextField x,y,tot; âù¢Á init( ) function-ø¢° «ñô¢ Global Ýè âù¢Á Íù¢Á
Object ´è¬÷ define ªêò¢¶ ¬õî¢î¤¼è¢è¤ù¢«ø£ñ¢. ãªùù¤ô¢ Þõ¢õ£Á Global
Ýè â¿¶ñ¢ ªð£¿¶ textsum class -¬ìò ܬù method è÷¤½ñ¢
Üõø¢¬øð¢ ðòù¢ð´î¢î¤è¢ ªè£÷¢÷ô£ñ¢ âù¢ðî¬ù 郎ùõ¤ô¢ ªè£÷¢Àé¢è÷¢.
init( ) function -ù¤ô¢ xp, yb, totp âù¢ø ªðòó¢è÷¤ô¢ Íù¢Á Label Object -è÷¢
à¼õ£è¢èð¢ð좮¼è¢è¤ù¢øù. ܬõ Right align Ýõîø¢è¢è£è
Label.RIGHT âù¢Á ªè£´è¢èð¢ð좮¼è¢è¤ù¢ø¶.
x, y, tot Ýè¤ò object-è÷¢ new operator -ø¤¬ùð¢ ðòù¢ð´î¢î¤ à¼õ£è¢èð
¢ðì¢ìð¤ù¢ ܬõ add function Íôñ¢ õó¤¬êò£è Applet -®Â÷¢ add ªêò¢òð¢
ð좮¼è¢è¤ù¢øù. ï£ñ¢ Þï¢î ¹«ó£è¤ó£ñ¤ô¢ ºè¢è¤òñ£è èõù¤è¢è «õí¢®ò¶.
x.addActionListener(this);
y.addActionListener(this);
tot.addActionListener(this);
âù¢Âñ¢ õó¤è¬÷î¢î£ù¢. Üî£õ¶ Þï¢î addActionListener( ) âù¢Âñ¢ function
-ä ªè£´ð¢ðîù¢ Íôñ¢, âð¢ªð£¿¶ªîô¢ô£ñ¢ ï£ñ¢ Þï¢î Object ´è÷¤ô¢ Enter
Key ò¤¬ù ܿè¤ù¢«ø£«ñ£ Üð¢ªð£¿ªîô¢ô£ñ¢ actioPerformed âù¢Âñ¢
event Ýù¶ î£ù£è Þòé¢è¤õ¤´ñ¢.
Public void actionperformed(ActionEvent ae)
âù¢ø õ®õ¤ô¢ ޼袰ñ¢ Þï¢î Event Ýù¶ ActionListener âù¢Âñ¢ Interface
-¬ù Þï¢î ¹«ó£è¤ó£ñ¤ô¢ implement ªêò¢î¤¼ð¢ðîù£ô¢ î£ù¢
ðòù¢ð´î¢îð¢ð´è¤ù¢ø¶ âù¢ðî¬ù 郎ùõ¤ô¢ ªè£÷¢Àé¢è÷¢.
Þï¢î actionperformed( ) function -ô¢ ï£ñ¢ a, b, c âù¢Á Íù¢Á integer
variable-è¬÷ int data type -ù¤ô¢ à¼õ£è¢è¤ò¤¼è¢è¤ù¢«ø£ñ¢. Þï¢î variable è÷¤ô¢
ï£ñ¢ x, y TexField è÷¤ô¢ Type ªêò¢î ñî¤ð¢¹è¬÷ â´î¢¶ð¢ «ð£´è¤ù¢«ø£ñ¢.
a = Integer.ParseInt(x.getText ( ));
b = Integer.ParseInt(y.getText ( ));
âù¢Á ªè£´î¢î¤¼ð¢ð¬îè¢ èõù¤»é¢è÷¢. Þî¤ô¢ getText âù¢ð¶ TextField class
-ù¢ member function ݰñ¢. Þî¬ùð¢ ðòù¢ð´î¢¶ñ¢ ªð£¿¶ ï£ñ¢ âù¢ù
ñî¤ð¢¹è¬÷ TextField-è÷¤ù¢ à÷¢ Type ªêò¢î¤¼è¢è¤ù¢«ø£«ñ£ Üõø¢¬øð¢
56. 56
ªðø¢Á袪è£÷¢÷ô£ñ¢.
Þï¢î º¬øò¤ô¢ ï£ñ¢ TextField -è÷¤ô¢ ޼袰ñ¢ ñî¤ð¢¹è¬÷ ªð¼ñ¢ªð£¿¶
ܬõ String data type Ýè ޼袰ñ¢. Þõø¢¬ø int data type Ýè ñ£ø¢ø¤ù£ô¢
î£ù¢ ïñ袰 Calculation è¬÷ê¢ ªêò¢ò º®»ñ£îô£ô¢ Integer.ParseInt âù¢Âñ¢
function ù¤¬ùð¢ ðòù¢ð´î¢î¤ò¤¼è¢è¤ù¢«ø£ñ¢. Þï¢î Integer.ParseInt function
Ýù¶ String õ®õ¤ô¢ ޼袰ñ¢ Number è¬÷ Integer data type Ýè ñ£ø¢ø¤è¢
ªè£´î¢¶õ¤´ñ¢.
ܴ c = a + b; âù¢Á ªè£´î¢î¤¼ð¢ðîù¢ Íôñ¢ x , y Object -®ô¢ à÷¢÷
ñî¤ð¢¹è¬÷ Ã좮 c âÂñ¢ variable-ô¢ «ð£ì¢´ õ¤ì¢«ì£ñ¢. Þð¢ªð£¿¶ Þï¢î c
ò¤ô¢ à÷¢÷ ñî¤ð¢ð¤¬ù tot âù¢Âñ¢ TextField -ù¢ à÷¢ «ð£ì «õí¢´ñ¢ Üô¢ôõ£?
Üîø¢° TextField class -ô¢ à÷¢÷ setText âù¢Âñ¢ function
ðòù¢ð´î¢îð¢ð´è¤ù¢ø¶.
tot.setText (“” + c);
âù¢Á ªè£´è¢èð¢ð좮¼ð¢ð¬îè¢ èõù¤»é¢è÷¢. SetText function -ù¤ù¢ Íôñ¢
String data è¬÷ ñ좴«ñ â¿î º®»ñ£îô£ô¢ c ò¤ô¢ à÷¢÷ Integer
ñî¤ð¢¹è¬÷ type cast ªêò¢õîø¢è¢è£è “” + c âù¢Á String -°ìù¢ «êó¢î¢¶è¢
ªè£´î¢î¤¼è¢è¤ù¢«ø£ñ¢.
Þï¢î𢠹«ó£è¤ó£ñ¤¬ù compile ªêò¢¶ 覫ö ªè£´è¢èð¢ð좴÷¢÷õ£Á
à÷¢÷ õó¤è¬÷ð¢ ðòù¢ð´î¢î¤ HTML file åù¢ø¤¬ù à¼õ£è¢è¤ Üî¬ù Þòè¢è¤ð¢
ð£¼é¢è÷¢.
<applet code="textsum.class" width=500 height=400>
</applet>
57. 57
ðì‹ðì‹ðì‹ðì‹ðì‹ 3.5
Listing 3.4
import java.awt.*;
import java.applet.*;
import java.util.*;
public class border extends Applet
{
Button top = new Button(“Top”);
Button bottom = new Button(“Bottom”);
Button right = new Button(“Right”);
Button left = new Button(“Left”);
String msg=”Java is intresting.......”;
TextArea hi = new TextArea(msg);
public void init()
58. 58
{
BorderLayout abc = new BorderLayout();
setLayout(abc);
add(top,BorderLayout.NORTH);
add(bottom,BorderLayout.SOUTH);
add(right,BorderLayout.WEST);
add(left,BorderLayout.EAST);
add(hi,BorderLayout.CENTER);
}
public boolean action(Event evt,Object arg)
{
if(evt.target instanceof Button)
{
String lab=(String)arg;
if (lab.equals(“Top”))
{
hi.setText(“I am in Top”);
}
if (lab.equals(“Bottom”))
{
hi.setText(“I am in Bottom”);
}
if (lab.equals(“Left”))
{
hi.setText(“I am in Left”);
}
if (lab.equals(“Right”))
{
hi.setText(“I am in Right”);
}
}
return true;
}
83. 83
public JavaDraw()
{
}
public String getAppletInfo()
{
return “Name:JavaDrawrn”+
“Author: Packia Nathanrn”;
}
public String[][]getParameterInfo()
{
String[][]info=
{
{ PARAM_forecolor,”String”,”Specifies default fore-
ground color of this program(must be color class variable)”},
{ PARAM_backcolor,”String”,”Specifies default
background color of this program(must be a color class variable)”},
};
return info;
}
public void init()
{
if(!m_fStandAlone)
GetParameter(null);
resize(400,350);
this.setLayout(new BorderLayout());
setBackground(Color.white);
guiPanel=new WidgetPanel();
add(“North”,guiPanel);
addMouseListener(getMouseListener());
addMouseMotionListener(getMouseMotionListener());
}
MouseListener getMouseListener()
84. 84
{
return new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
oldX=e.getX();
oldY=e.getY();
}
};
}
MouseMotionListener getMouseMotionListener()
{
return new MouseMotionAdapter()
{
public void mouseDragged(MouseEvent e)
{
JavaDraw.this.draw(e.getX(),e.getY());
}
};
}
void draw(int x,int y)
{
Graphics g=getGraphics();
boolean fill=guiPanel.isFilled(),
threeD=guiPanel.isThreeD();
switch (guiPanel.getShape())
{
case Shape.LINE:
g.drawLine(oldX,oldY,x,y);
break;
case Shape.RECT:
if(fill)
103. 103
JMenubar class Ýù¶ menu bar-¬ù à¼õ£è¢°õîø¢°ð¢ ðòù¢ð´è¤ù¢ø¶.
JMenu class -¬ùð¢ ðòù¢ð´î¢î¤ file, edit âù¢Á Þóí¢´ menu-è¬÷ à¼õ£è¢è¤
ò¤¼è¢è¤ù¢«ø£ñ¢. Þï¢î menu - è÷¤ô¢ JMenuItem class-¬ùð¢ ðòù¢ð´î¢î¤ menu
èì¢ì¬÷è¬÷ menu bar à¼õ£è¢è¤ò¤¼è¢è¤ù¢«ø£ñ¢. Þ¼î¤ò£è menu-è¬÷
MenuBar àìÂñ¢, MenuBar -ø¤¬ù frame àìù¢ Þ¬íî¢î¤¼è¢è¤ù¢«ø£ñ¢.
Toolbar-è¬÷ à¼õ£è¢°õîø¢° JToolBar âù¢Âñ¢ èì¢ì¬÷ ðòù¢ð´
è¤ù¢ø¶. Þîù¢ Íôñ¢ Toolbar-è¬÷ à¼õ£è¢è¤ ÜîÂìù¢ ï£ñ¢ îù¤ò£è Button
Object-è¬÷ à¼õ£è¢è¤ Þ¬íè¢è «õí¢´ñ¢. Þï¢î Button è÷¤ô¢ ï£ñ¢ Icon-
è¬÷ Þ¬íî¢î¤¼è¢è¤ù¢«ø£ñ¢ âù¢ð¬îè¢ èõù¤»é¢è÷¢.
ðì‹ðì‹ðì‹ðì‹ðì‹ 5.4
Listing 5.5
import javax.swing.*;
import java.awt.*;
public class tabbed extends JFrame
{
Container d = getContentPane();
tabbed()
104. 104
{
Icon one = new ImageIcon (“bullet1.gif”);
Icon two = new ImageIcon (“bullet2.gif”);
Icon three = new ImageIcon (“bullet3.gif”);
JTabbedPane country = new JTabbedPane();
country.addTab(“India”,one,new india());
country.addTab(“Pakistan”,two,new pakistan());
country.addTab(“China”,three,new china());
d.add(country);
}
public static void main(String argv[])
{
tabbed xyz = new tabbed();
xyz.setTitle(“Tabbed Demo”);
xyz.resize(400,400);
xyz.show();
}
}
class india extends JPanel
{
JLabel lbl_pre = new JLabel (“President : Mr. K.R.
Narayanan”);
JLabel lbl_pri = new JLabel (“Prime Minister : Mr. A.B.
Vajpayee”);
JLabel lbl_chi = new JLabel (“Chief Minister : Mr. M.
Karunanidhi”);
india()
{
setLayout(new FlowLayout(FlowLayout.LEFT,10,10));
add(lbl_pre);
add(lbl_pri);
add(lbl_chi);
107. 107
Listing 5.6
import java.awt.*;
import javax.swing.*;
import javax.swing.tree.*;
public class maram extends JFrame
{
maram()
{
Container x=getContentPane();
//**********
DefaultMutableTreeNode country = new
DefaultMutableTreeNode (“Country”);
//**********
DefaultMutableTreeNode india = new
DefaultMutableTreeNode (“India”);
DefaultMutableTreeNode pakistan = new
DefaultMutableTreeNode (“Pakistan”);
DefaultMutableTreeNode america = new
DefaultMutableTreeNode (“America”);
DefaultMutableTreeNode china = new
DefaultMutableTreeNode (“China”);
//***********
DefaultMutableTreeNode tamil = new
DefaultMutableTreeNode (“Tamil”);
DefaultMutableTreeNode andra= new
DefaultMutableTreeNode (“Andra”);
DefaultMutableTreeNode karnataka= new
DefaultMutableTreeNode (“Karnataka”);
DefaultMutableTreeNode bihar= new
DefaultMutableTreeNode (“Bihar”);
108. 108
//***********
DefaultMutableTreeNode lahore = new
DefaultMutableTreeNode (“Lahore”);
DefaultMutableTreeNode karachi = new
DefaultMutableTreeNode (“Karachi”);
DefaultMutableTreeNode sindhu = new
DefaultMutableTreeNode (“Sindhu”);
//**********
DefaultMutableTreeNode chennai = new
DefaultMutableTreeNode (“Chennai”);
DefaultMutableTreeNode madurai = new
DefaultMutableTreeNode (“Madurai”);
DefaultMutableTreeNode trichy = new
DefaultMutableTreeNode (“Trichy”);
country.add(india);
country.add(pakistan);
country.add(america);
country.add(china);
india.add(tamil);
india.add(andra);
india.add(karnataka);
india.add(bihar);
pakistan.add(lahore);
pakistan.add(karachi);
pakistan.add(bihar);
tamil.add(chennai);
tamil.add(madurai);
tamil.add(trichy);
111. 111
System.exit(0);
}
}
);
// Take the dummy data from SwingSet.
final String[] names = {“First Name”, “Last Name”, “Favorite
Color”,“Favorite Number”, “Vegetarian”};
final Object[][] data = {
{“Mark”, “Andrews”, “Red”, new Integer(2), new Boolean(true)},
{“Tom”, “Ball”, “Blue”, new Integer(99), new Boolean(false)},
{“Alan”, “Chung”, “Green”, new Integer(838), new
Boolean(false)},
{“Jeff”, “Dinkins”, “Turquois”, new Integer(8), new
Boolean(true)},
{“Amy”, “Fowler”, “Yellow”, new Integer(3), new
Boolean(false)},
{“Brian”, “Gerhold”, “Green”, new Integer(0), new
Boolean(false)},
{“James”, “Gosling”, “Pink”, new Integer(21), new
Boolean(false)},
{“David”, “Karlton”, “Red”, new Integer(1), new Boolean(false)},
{“Dave”, “Kloba”, “Yellow”, new Integer(14), new
Boolean(false)},
{“Peter”, “Korn”, “Purple”, new Integer(12), new
Boolean(false)},
{“Phil”, “Milne”, “Purple”, new Integer(3), new Boolean(false)},
{“Dave”, “Moore”, “Green”, new Integer(88), new
Boolean(false)},
{“Hans”, “Muller”, “Maroon”, new Integer(5), new
Boolean(false)},
{“Rick”, “Levenson”, “Blue”, new Integer(2), new
Boolean(false)},
{“Tim”, “Prinzing”, “Blue”, new Integer(22), new
Boolean(false)},
{“Chester”, “Rose”, “Black”, new Integer(0), new
112. 112
Boolean(false)},
{“Ray”, “Ryan”, “Gray”, new Integer(77), new Boolean(false)},
{“Georges”, “Saab”, “Red”, new Integer(4), new
Boolean(false)},
{“Willie”, “Walker”, “Phthalo Blue”, new Integer(4), new
Boolean(false)},
{“Kathy”, “Walrath”, “Blue”, new Integer(8), new
Boolean(false)},
{“Arnaud”, “Weber”, “Green”, new Integer(44), new
Boolean(false)}
};
// Create a model of the data.
TableModel dataModel = new AbstractTableModel()
{
// These methods always need to be implemented.
public int getColumnCount() { return names.length; }
public int getRowCount() { return data.length;}
public Object getValueAt(int row, int col) {return
data[row][col];
}
// The default implementations of these methods in
// AbstractTableModel would work, but we can refine them.
public String getColumnName(int column) {return
names[column];}
public Class getColumnClass(int col) {return
getValueAt(0,col).getClass();}
public boolean isCellEditable(int row, int col) {return (col==4);}
public void setValueAt(Object aValue, int row, int column)
{
data[row][column] = aValue;
}
113. 113
};
// Instead of making the table display the data as it would nor-
mally with:
// JTable tableView = new JTable(dataModel);
// Add a sorter, by using the following three lines instead of the
one above.
JTable tableView = new JTable(dataModel);
JScrollPane scrollpane = new JScrollPane(tableView);
scrollpane.setPreferredSize(new Dimension(700, 300));
frame.getContentPane().add(scrollpane);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args)
{
new Table();
}
}
Þï¢î ¹«ó£è¤ó£ñ¤ô¢ table âù¢ø¬öè¢èð¢ð´ñ¢ Component õ¤÷è¢èð¢
ð좴÷¢÷¶. table -è÷¤ô¢ ïñ¢ñ£ô¢ Record-è¬÷ display ªêò¢¶ ªè£÷¢÷
º®»ñ£îô£ô¢ Þîù¢ ðòù¢ð£´ Þù¢ø¤ò¬ñò£îî£è¤ø¶.Table Component-®¬ù
ï£ñ¢ ðòù¢ð´î¢î «õí¢´ñ£ù£ô¢ ºîô¤ô¢ javax.swing.table.* âù¢ø Package
-¬ù import ªêò¢ò «õí¢´ñ¢.
Þ颰 Table âù¢ªø£¼ class â¿î¤ò¤¼è¢è¤ù¢«ø£ñ¢. main function-ô¢ ޼
å¼ ¹î¤ò Object-¬ù à¼õ£è¢°è¤ù¢«ø£ñ¢.Üð¢ªð£¿¶ ïñ袰 Constructor function
Þò颰è¤ù¢ø¶. Üî¤ô¢ ï£ñ¢ ºîô¤ô¢
JFrame frame = new JFrame(‘Table’);
âù¢Á ªè£´î¢¶ å¼ frame-¬ù à¼õ£è¢è¤è¢ ªè£÷¢è¤«ø£ñ¢. ð¤ù¢ final String [
] names... âù¢Á ªè£´î¢¶ å¼ String array ò¤¬ù à¼õ£è¢è¤ Üî¤ô¢ ïñ¶
Table -ô¢ õó ޼袰ñ¢ Title-è÷¤ù¢ ªðòó¢è¬÷ð¢ ðî¤è¢è¤ù¢«ø£ñ¢. ܴ final
object [ ][ ] data ....... âù¢Á ªè£´î¢¶ å¼ array ò¤¬ù à¼õ£è¢è¤ Üî¤ô¢ ïñ¶
Table ø¢° «î¬õò£ù îèõô¢è÷¢ ܬùî»ñ¢ õó¤¬êò£è ðî¤ï¢¶
¬õî¢î¤¼è¢ è¤ù¢«ø£ñ¢. Þï¢î Object data type ¬ùð¢ ðòù¢ð´î¢î¤«ò Table
Java-15
114. 114
ø¢°î¢ «î¬õò£ù ܬù îèõô¢è¬÷»ñ¢ ðî¤ï¢¶ ¬õè¢
ªè£÷¢÷«õí¢´ñ¢. ܴ
TabelModel datamodel=new AbstractTableModel( )
{
......
}
âù¢Á å¼ èì¢ì¬÷î¢ ªî£°ð¢¹ ªè£´è¢èð¢ ð좮¼ð¢ð¬îè¢ èõù¤»é¢è÷¢.
Þï¢î TableModel ñø¢Áñ¢ ÞîÂ÷¢ ޼袰ñ¢ ܬù function è¬÷»ñ¢
¬õî Table-ù¢ ܬù Þòè¢èé¢è¬÷»ñ¢ è좴ð¢ð´î¢¶è¤ù¢«ø£ñ¢.
JTable tableview = new JTable (datamodel);
âù¢Á ªè£´î¢¶ ï£ñ¢ tableview âù¢Âñ¢ Object-¬ù à¼õ£è¢è¤ò¤¼è¢è¤ù¢«ø£ñ¢.
Þï¢î table Ýù¶ datamodel âÂñ¢ Object ô¢ à÷¢÷ îèõô¢ è¬÷ð¢ ðòù¢ð´î¢î¤
ªè£÷¢è¤ù¢ø¶. ܴ
JScrollPane scrollpane = new JScrollPane(tableview);
âù¢Á ªè£´è¢èð¢ð좮¼ð¢ð¬îè¢ èõù¤»é¢è÷¢. Þï¢î scrollpane Component Ýù¶
âï¢î å¼ Object-®¬ù»ñ¢ frame-ù¢ à÷¢ scroll ªêò¢»ñ¢ ð® ªêò¢õîø¢°ð¢
ðòù¢ð´è¤ù¢ø¶. Þ颰 ï£ñ¢ à¼õ£è¢èò¤¼è¢è¤ù¢ø table-¬ù scrollpane-ù¢ à÷¢
«ð£ì¢®¼è¢è¤ù¢«ø£ñ¢. Þ¼î¤ò£è Þï¢î Scroll Pane -¬ù ï£ñ¢ ïñ¢º¬ìò frame-
Â÷¢ add ªêò¢¶ õ¤´è¤ù¢«ø£ñ¢. Þ¶ Þò颰ñ¢ ªð£¿¶ 700, 300 âù¢ø ï¦÷,
Üèôî¢î¤ô¢ Þò颰ñ£Á Dimension ܬñè¢èð¢ð좴÷¢÷¶.
Classes Provided in the Swing Componenet Package
Component Description
Japplet Implements a Java applet.
JAppletBeanInfo Provides information about JApplett to
bean-based tools.
JButton Implements a button component.
JButtonBeanInfo Provides information about JButton
for bean-based tools.
JCheckBox Implements a check-box component.
JCheckBoxBeanInfo Provides information about JCheckBox
for bean-based tools.
JCheckBoxMenuItem Implements a check-box menu item.
115. 115
JCheckBoxMenuItemBeanInfo Provides information about
JCheckBoxMenuItem for bean-based
tools.
JColorChooser Displays and manages a color-chosser
dialog.
JColorChooserBeanInfo Provides information about
JColorChooser for bean-based tools.
JComboBox Implements a combo-box component.
JComboBoxBeanInfo Provides information about
JComboBox for bean-based tools.
JComponent The mother of all Swing components.
JComponentBeanInfo Provides information about
JComponent for bean-based tools.
JDesktopIcon Displays in iconified version of a
JInternalFrame.
JDesktopIconBeanInfo Provides information about
JDesktopIcon for bean-based tools.
JDesktopPane Provides a pluggable DesktopManager
object for JInternal Frame Objects.
JDesktopPaneBean Provides information about
JDesktopPane for bean-based Info
tools.
JDialog Adds enhancements to
java.awt.Dialog.
JDialogBeanInfo Provides information about JDialog for
bean-based tools.
JFileChooser Implements a file-chooser dialog box.
JFileChooserBeanInfo Provides information about
JFileChooser for bean-based tools.
JFrame Adds enhancements to
java.awt.Frame.
JFrameBeanInfo Provides information about JFrame for
bean-based tools.
JInternalFrame Implements a frame object that can
be placed inside a JDestopPane object
to emulate a native frame window.
JInternalFrameBeanInfo Provides information about
JInternalFrame for bean-based tools.
JLabel Creates a display area for displaying
116. 116
read-only text, an image, or both.
JLabelBeanInfo Provides information about JLabel for
bean-based tools.
JLayeredPane Can display multiple layered panes
(JInternalFrameobjects) inside a
frame.
JLayeredPaneBeanInfo Provides information about
JLayeredPane for bean-based tools.
JList Allows the user to selcect one or more
objects from a list. A separate mode,
ListModel, represents the contents of
the list.
JListBeanInfo Provides information about JList for
bean-based tools.
JMenu Implements a menu component.
JMenuBeanInfo Provides information about JMenu for
bean-based tools.
JMenuBar Implements a menu bar component.
JMenuBarBeanInfo Provides information about JMenuBar
for bean-based tools.
JMenuItem Implements a menu item component.
JMenuItemBeanInfo Provides information about
JMenuItem for bean-based tools.
JOptionPane Displays a dialog box that prompts the
user for a choice ane then passes that
choice on to the executing program.
JOptionPaneBeanInfo Provides information about
JOptionPane for bean-based tools.
JPanel Provides a generic container for
organizing other components.
JPanelBeanInfo Provides information about JPanel for
bean-based tools.
JPasswordField Displays a field in which the user can
type a passord. The text of the pass
word does not appear in the field
as it is being typed.
JPasswordFieldBeanInfo Provides information about
JPasswordField for bean-based tools.
JPopupMenu Implements a pop-up menu.
117. 117
JPopupMenuBeanInfo Provides information about
JPopupMenu for bean-based tools.
JProgressBar Implements a progress-bar compo-
nent.
JProgressBarBeanInfo Provides information about
JProgressBar for bean-based tools.
JRadioButton Implements a radio-button control.
JRadioButtonBeanInfo Provides information about
JRadioButton for bean-based tools.
JRadioButtonMenuItem Implements a radio-button menu item.
JradioButtonMenuItemBeanInfo Provides information about
JRadioButtonMenuItem for bean-
based tools.
JRootPane Instantiates in a single step an object
made up of a glass pane, a layered
pane, an optional menu bar, and
118. 118
a content pane.
JRootPaneBeanInfo Provides information about JRootPane
for bean-based tools.
JScrollBar Implements a scroll-bar object.
JScrollBarBeanInfo Provides information about JScrollBar
for bean-based tools.
JScrollPane Implements a scroll-pane object.
JScrollPaneBeanInfo Provides information about
JScrollPane for bean-based tools.
JSeparator Implements a menu separator object.
JSeparatorBeanInfo Provides information about JSeparator
for bean-based tools.
JSlider Implements a slider-bar object.
JSliderBeanInfo Provides information about JSlider for
bean-based tools.
JSplitPane Implements a split-pane component.
JSplitPaneBeanInfo Provides information about JSplitPane
for bean-based tools.
JTabbedPane Implements a tabbed-pane (“prop
erty-page”) component.
JTabbedPaneBeanInfo Provides information about
JTabbedPane for bean-based tools.
JTable Implements a table component.
JTableBeanInfo Provides information about JTable for
bean-based tools.
JTextArea Implements a multiline ara that can
display editable or read-only text.
JTextAreaBeanInfo Provides information about JTextArea
for bean-based tools.
JTextPane Implements a text component that can
be marked up with attributes to be represented graphically.
JTextPaneBean-Info Provides information about JTextPane
for bean-based tools.
JToggleButton Implements a two-stage button com
ponent.
JToggleButtonBeanInfo Provides information about
JToggleButton for bean-based tools.
119. 119
JToolBar Implements a dockable, floatable tool
bar.
JToolBarBeanInfo Provides information about JToolBar
for bean-based tools.
JToolTip Implements a tool-tip component (a
component that can display a short
string, such as the name of compo
nents or a user tip).
JToolTipBeanInfo Provides information about JToolTip
for bean-based tools.
JTree A component that can display a set of
hierarchical data in a graphical outline
format.
JTreeBeanInfo Provides information about JTree for
bean-based tools.
JViewport Provides a clipped view of an arbi
trarily large component. Used by
JScrollPane.
JViewportBeanInfo Provides information about JViewport
for bean-based tools.
JWindow Adds enhancements to
java.awt.Window.
JWindowBeanInfo Provides information about JWindow
for bean-based tools.