Вы находитесь на странице: 1из 4

TCP1311 Object Oriented Programming

Lab-1

Page 1

Lab 1 (Introductory Programs)


Java programs can be broadly categorized into two types, namely, (i) applications, and (ii) applets. All Java programs ave names wit .java e!tension. "eing an ob#ect oriented lang$age, Java organizes code in a set o% classes. T e name o% t e %ile containing a program m$st e!actly matc wit t e class name. &se a te!t editor to create t e %ollowing program code. class Hi { public static void main (String[] a) { System.out.println(Hello World! ) ! " " 'ince t e above class as a name ()i*, t e above %ile m$st be stored as Hi.java Java Applications: T e above program is a simple e!ample o% a Java application. A Java application is a standalone Java program t at can be r$n by $sing #$st t e Java interpreter. Java applications don+t re,$ire a browser to r$n. T e above program is compiled and r$n as %ollows#$%& javac Hi.java #$%& java Hi Hello World! (.nvo/ing t e compiler) (.nvo/ing r$n0time interpreter) (T e o$tp$t o% t e program)

1o$ will act$ally need to set t e appropriate pat to t e directory w ere t e Java compiler resides. Typically t e command %or compilation loo/s li/e & c$%jd'(.).*%bin%javac Hi.java Java applications re,$ire t at main be de%ined wit t e attrib$tes public and static. main is declared as void since t e met od does not ret$rn a val$e. main e!pects one arg$ment, a re%erence to an array o% String. T e array olds any command0line arg$ments passed to t e program w en it is e!ec$ted %rom t e command line. To ill$strate t is %eat$re, we will modi%y t e program as %ollows-

class Hi{

TCP1311 Object Oriented Programming

Lab-1

Page 2

public static void main(String[] a){ i+(a.lengt,--.) System.out.println(/Welcome to 0ava/)! else System.out.println(/1,is is t,e +irst 0ava program o+ / 2 a[.])! " " Compile and r$n t e above application $sing t e command- java 1o$ will get t e message- Welcome to 0ava 3$n t e program again wit a name as a command line arg$ment. 4!ample- 5 java Hi 3alin T e o$tp$t is- 1,is is t,e +irst 0ava program o+ 3alin 4!ercise- C ange t e program to accept any n$mber o% strings in t e command line s$c as & java Hi 4artin 3alin Java Applets: Java applets r$n inside a Java enabled web browser (applets can be embedded in a web page $sing a special )T67 Tag). T ey ave t e advantages o% t e str$ct$re t e browser provides 0 an e!isting window, event andling, grap ics conte!t and t e s$rro$nding $ser inter%ace- An e!ample is given below. 'ave t e %ile as 8elcome.#ava
import java.a5t.*! import java.applet.*! import java.util.*! public class Welcome e6tends 7pplet{ public void paint(8rap,ics g){ 9ont +-ne5 9ont(/1imes :oman/;9ont.<=>?;@.)! g.set#olor(#olor.cyan)! g.+ill:ect(.;.;A..;B..)! g.set9ont(+)! g.set#olor(#olor.yello5)! g.+ill:ect((.;(.;)..;B.)! g.set#olor(#olor.pin')! g.+ill:ect((.;C.;)..;B.)! g.set#olor(#olor.blue)! g.dra5String(/Welcome to t,e 5orld o+ 0ava 7pplets/;@.;).)! g.dra5String(/1oday is /2 ne5 ?ate();@.;(@.)! " "

Hi

9ote t at all applets are s$bclasses o% (Java.applet.Applet*. T e so$rce %ile can contain more t an one class, b$t only one class m$st be t e s$bclass o% (Applet* (t is is speci%ied

TCP1311 Object Oriented Programming

Lab-1

Page 3

as extends Applet), and t e name o% t e %ile m$st be t e same as t e name o% t is class. An applet+s e!ec$tion proceeds t o$g t e invocation o% a series o% met ods s$c as init (w ere all initialization code is placed) and paint (w ere all code related to t e o$tp$t is placed). Compile t e above program. Create a %ile view.html wit t e %ollowing contentsD,tml& Dapplet code-/Welcome.class/ 5idt,-E.. ,eig,t-F..& DGapplet& DG,tml& .n t e :;' prompt type t e command5 appletvie5er vie5.,tml

)ere again, yo$ may ave to speci%y t e pat o% t e %ile s$c as 5 c$%jd'(.).*%bin%appletvie5er vie5.,tml 'tandard classes s$c as (:ate* are organized into pac/ages, w ic are collections o% related classes. T e $sage o% t e class (:ate* will t ere%ore re,$ire t e pac/age to be made visible to t e program, and ence t e statement import java.util.* T e %ollowing Java Application generates a speci%ied n$mber o% random n$mbers, and sorts t em in ascending order. import java.util.*! class 9ill7ndSort { public static void main( String[ ] args ) { int n - Hnteger.parseHnt( args[ . ] )! double[ ] nums - ne5 double[ n ]! :andom r - ne5 :andom()!int i - .! 5,ile ( i D n ) { nums[ i ] - r.ne6t?ouble()! i - i 2 (! " print( nums )! 7rrays.sort( nums )! print( nums )! " static void print( double[ ] a ) { System.out.println()! +or ( int i - .! i D a.lengt,! i22 ) System.out.println( a[ i ] )! " "

TCP1311 Object Oriented Programming

Lab-1

Page <

Compile t e above program. 3$n t e program by speci%ying a n$mber as a command0line arg$ment. =or e!ample, & java 9ill7ndSort E T e above command will ca$se t e program to generate > n$mbers and sort t em. .% yo$ want t e o$tp$t to be stored in a %ile (say, a.o$t), t en give t e command as %ollows& java 9ill7ndSort E & a.out

Вам также может понравиться