Sunteți pe pagina 1din 5

UniversityofWollongonginDubai CSCI213:JavaProgrammingandtheInternet Lab1 Topic:UnderstandingJDKandEclipse.

Spring2013
Objectives: Writesimplejavaapplicationsusingjdkenvironment. WritejavaapplicationsusingtheIDEofEclipse WritecommandlineargumentsanduseJAVAAPI Task1: Thefollowingprogramisreadingargumentsprovidedatthecommandline. 1. Createthejavasourcecodefileusinganyeditor(E.g.:Notepad). 2. SavethefileasfirstDemo.java.MakesurethefiletypeisAllFiles 3. SaveitinH:drivemakeafolderofyourchoiceforbetterorganizationof labfilesE.g.H:/Lab1/firstDemo.java 4. CompilethecodeasjavacfirstDemo.java(ReadPartA13) 5. RunthecodeasjavafirstDemo(ReadPartA4) class firstDemo { public static void main(String args[]) { System.out.println("\nRunning Demo . . ");} } PartAUsingJDK J2SEisanacronymforJava2StandardEdition,whichcomesindifferentflavours.Ifyoujustwanttobe able to "run" Java applications you just need the Java Runtime Environment (JRE) of it. If you want to developthingsinJava,youwanttheJDK(JavaDevelopmentKit). (1)AttheDOSprompt,typethefollowingcommandtochangetheworkingfolder. C:\>H: thentype H:\>cdLab1 (2)Compiletheprogramusingthefollowingcommand. H:\Lab1>javacfirstDemo.java (3)ChecktheLab1foldertoconfirmthecreationoffirstDemo.classfile (4)Executetheprogramusingthefollowingcommand. H:\Lab1>javafirstDemo PartBUsingEclipse(IDE) (1)ClickontheiconofEclipsetolaunchtheIDE. (2)ClickonFile>New>Project.
CSCI213 Spring 2013

University of Wollongong in Dubai CSCI213 Lab 01 (3)SelectJavaProject.Thiswilllaunchthewizardtocreatenewproject. (4)TypetheProjectnameasLabExercises.SaveitinH:\Spring2013CS213(makethisfolderfirst) (5)Youwillseeanewfolder,namedasLabExercisescreatedwithinthepackageexplorerviewonthe leftsideoftheEclipsewindow.Rightclickonthisfolder. (6)Selectnew>class.Thiswilllaunchthewizardforcreatinganewclass. (7)TypethenameofthepackageasWeek1. (8)TypethenameoftheclassasLab01. (9)Selectmodifier'public'. (10)Checkfortheoptionof'mainmethod'(publicstaticvoidmain).UncheckInherited/Abstractclass options. (11)Clickon'Finish'button. (12)Typethecodebelow: classLab01 {publicstaticvoidmain(Stringargs[]) {intn=args.length; Stringnames[]=newString[n]; for(inti=0;i<n;i++) {names[i]=args[i]; System.out.println(names[i]);} } } (13)ChooseProject>Build. (14)ChooseRun>RunConfiguration... (15)ChooseLab01fromthelefthandsidemenuandgototheArgumentstab. (16)Enterany4argumentsunderProgramArgumentsandhitRun Modifytheabovecodetodisplaythelengthofeachstringenterthroughcommandlineargument.Visit http://java.sun.com/j2se/1.5.0/docs/api/tofindtheappropriatemethodandimplementit. Task2: Go to the Math class in Java API and identify the value of x after each of the following statement is executed.Assumextobeofthetypedoublehavingastartingvalueof(6)

-2-

University of Wollongong in Dubai CSCI213 Lab 01 a. b. c. d. Task3: WriteaJavaapplicationprogramLab02.javatodothefollowingtasks: Task4: ThisisourfirstencounterwithJAVAAPI. Recallthatwehadusedthefollowingstatementinsampleprogramsdiscussedduringthelecture. number1=Integer.parseInt(args[0]); args[0] is of data type String. The String has to be converted it into a number before applying any mathematical operator. The method parseInt takes a string as an argument and converts it into data typeinteger.ThismethodisdefinedinclassInteger.Notehere,thatwearenotcreatinganyobjectof thisclass;ratherweareinvokingthemethodusingtheclassnameitself.Letusunderstandmoreabout classIntegerandthemethodparseInt(). Visithttp://java.sun.com/j2se/1.5.0/docs/api/ TheframeintheupperleftcornershowsallpackagesofJava. Clickonjava.lang. Thelowerleftframeshowsallclassesincludedinthispackage. ClickontheclassnamedasInteger. ReadtemperatureinFahrenheitfromcommandline. ConvertanddisplaythesametemperatureindegreeCelsius.
Illustration java Lab02 100 (if using command line) Output: Temperature given in Fahrenheit = 95 Temperature in Celsius = 35

x=Math.abs(x); x=Math.floor(3.67); x=Math.ceil(8.56); x=Math.ceil(5Math.abs(2+Math.floor(3.67)));

Q. Why have we not imported this package in our program?

-3-

University of Wollongong in Dubai CSCI213 Lab 01 Notethatthisisdifferentthanthedatatypeint. ThemainframewilldisplaythedescriptionofclassInteger. Read the description of the method parseInt. This method is declared as static. All the methods declaredasstaticareinvokedusingtheclassnameandnottheobjectname. Answerthefollowingquestion: 1)WhatisthepurposeoftheclassInteger? Task5: Writeasmallprogramthatreadsanintegerfromcommandlineargumentanddisplays binaryrepresentation hexadecimalrepresentationofthatnumber.

Visit http://java.sun.com/j2se/1.5.0/docs/api/ to decide on the appropriate methods to be used in the program. DetectingSyntaxandLogicErrors Therearenumerousopportunitiesforerrorsinanyprogram,ofteninplacesthatseemtoosimpleto requirecloseattention. Whatdoyouthinkthefollowingprogramisdesignedtodo? publicclassCube { publicstaticvoidmain() { doubleheight=3.0;\\inches doublecubeVolume=height*height*height; doublesurfaceArea=8*height System.out.print("Volume=" System.out.println(cubeVolume); System.out.print("Surfacearea=); System.out.println(surfaceArea); } 1. Willitworkasshown?Ifnot,whatproblemscanyouidentify? 2. Trycompilingtheprogram.Whatweretheresults?(Supplythespecificerrormessagesthatthe compilerreported.) 3. Fixthesyntaxerrors.Showtheprogramafteryoufixedthesyntaxerrors. 4. Theprogramhastwologicerrors.Fixthemandsupplythecorrectedprogram. Extratask Thefollowingprogramillustrateshowtodeclareandusearraysinjava.

-4-

University of Wollongong in Dubai CSCI213 Lab 01 class arrayTest { public static void main(String args[]) { int A[] = {3,4,5,1,2,10}; System.out.println(A.length); for (int i=0; i<A.length; i++) System.out.println(A[i]); } } Modifythisprogramto 1. Defineanarrayof10doubles. 2. Printthegivenarray. 3. Sortthegivenarray. 4. Printthesortedarray.

-5-

S-ar putea să vă placă și