Object-Oriented Programming in Javafaculty.utrgv.edu/dongchul.kim/CSCI3326/01_intro.pdf · ... Head...

49
CSCI/CMPE 3326 Object-Oriented Programming in Java Dr. Dongchul Kim Department of Computer Science University of Texas Rio Grande Valley Course Information, JVM, Compile and Run, IDE, Android Studio

Transcript of Object-Oriented Programming in Javafaculty.utrgv.edu/dongchul.kim/CSCI3326/01_intro.pdf · ... Head...

Page 1: Object-Oriented Programming in Javafaculty.utrgv.edu/dongchul.kim/CSCI3326/01_intro.pdf · ... Head First Java, 2nd Edion • Author: Kathy Sierra, Bert Bates • Publisher: O'Reilly

CSCI/CMPE 3326

Object-Oriented Programming in Java

Dr. Dongchul Kim

Department of Computer Science University of Texas Rio Grande Valley

Course Information, JVM, Compile and Run, IDE, Android Studio

Page 2: Object-Oriented Programming in Javafaculty.utrgv.edu/dongchul.kim/CSCI3326/01_intro.pdf · ... Head First Java, 2nd Edion • Author: Kathy Sierra, Bert Bates • Publisher: O'Reilly

Course Information •  Instructor:Dr.DongchulKim•  Office:ENGR3.272•  Email:[email protected]•  Homepage:h"p://faculty.utrgv.edu/dongchul.kim/•  OfficeHours:Mon&Wed,1:30~2:30pm,byappointment,

oranyNmetheofficedoorisopen.•  CourseWebPage:

–  h"p://faculty.utrgv.edu/dongchul.kim/CSCI3326/

Page 3: Object-Oriented Programming in Javafaculty.utrgv.edu/dongchul.kim/CSCI3326/01_intro.pdf · ... Head First Java, 2nd Edion • Author: Kathy Sierra, Bert Bates • Publisher: O'Reilly

Course Information •  TAName:AdolfoGonzalezIII•  Email:[email protected]•  Office:ENGR3.273A•  OfficeHours:TBA

Page 4: Object-Oriented Programming in Javafaculty.utrgv.edu/dongchul.kim/CSCI3326/01_intro.pdf · ... Head First Java, 2nd Edion • Author: Kathy Sierra, Bert Bates • Publisher: O'Reilly

Textbook •  OpNonal•  Title: Starting Out with Java: From Control Structures •  Through Objects, 6th Edition •  Author: Tony Gaddis •  Publisher: Addison-Wesley, March 22, 2015 •  ISBN-13: 978-0133957051 •  Link: http://goo.gl/lHXMxJ

Page 5: Object-Oriented Programming in Javafaculty.utrgv.edu/dongchul.kim/CSCI3326/01_intro.pdf · ... Head First Java, 2nd Edion • Author: Kathy Sierra, Bert Bates • Publisher: O'Reilly

Textbook •  OpNonal•  Title:HeadFirstJava,2nd

EdiNon•  Author:KathySierra,BertBates•  Publisher:O'ReillyMedia,

February9,2005•  ISBN-13:978-0596009205•  Link:h`p://goo.gl/dA6rVy

Page 6: Object-Oriented Programming in Javafaculty.utrgv.edu/dongchul.kim/CSCI3326/01_intro.pdf · ... Head First Java, 2nd Edion • Author: Kathy Sierra, Bert Bates • Publisher: O'Reilly

Evaluation •  LabAssignments15%•  ProgrammingAssignments25%•  Exams40%•  Project20%

Page 7: Object-Oriented Programming in Javafaculty.utrgv.edu/dongchul.kim/CSCI3326/01_intro.pdf · ... Head First Java, 2nd Edion • Author: Kathy Sierra, Bert Bates • Publisher: O'Reilly

Rule 1 •  Any OS is okay (Windows, Mac OS, Linux,

Chrome OS, and so on) •  Please come to my office if you couldn’t

understand the course materials •  or ask TA •  Please don’t be shy •  There is no such thing as a stupid question

–  Exception? –  Respect your classmates

Page 8: Object-Oriented Programming in Javafaculty.utrgv.edu/dongchul.kim/CSCI3326/01_intro.pdf · ... Head First Java, 2nd Edion • Author: Kathy Sierra, Bert Bates • Publisher: O'Reilly

Rule 2 •  No late submission •  No cheating

–  Respect your classmates

•  Frequently check your UTRGV email, course website, and blackboard

•  Come to class on time •  Start HW early •  Enjoy (Best chance to improve your

programming skill)

Page 9: Object-Oriented Programming in Javafaculty.utrgv.edu/dongchul.kim/CSCI3326/01_intro.pdf · ... Head First Java, 2nd Edion • Author: Kathy Sierra, Bert Bates • Publisher: O'Reilly

Rule 3

•  Excuse me – Flexible course schedule

•  Bring your laptop with fully charged battery

Page 10: Object-Oriented Programming in Javafaculty.utrgv.edu/dongchul.kim/CSCI3326/01_intro.pdf · ... Head First Java, 2nd Edion • Author: Kathy Sierra, Bert Bates • Publisher: O'Reilly

Rule 4

•  I will do my best •  You should also do your best

– Everyday do Java programming

Page 11: Object-Oriented Programming in Javafaculty.utrgv.edu/dongchul.kim/CSCI3326/01_intro.pdf · ... Head First Java, 2nd Edion • Author: Kathy Sierra, Bert Bates • Publisher: O'Reilly

The Way Java Works

Source Compiler Output Virtual Machine

1.Createasourcedocument.Useanestablishedprotocol(inthiscase,theJavalanguage).

2. Run your document through a sourcecode compiler. The compiler checks forerrorsandwon’tletyoucompileunNlit’ssaNsfiedthateverythingwillruncorrectly.

3. The compiler creates a new document, codedintoJavabytecode.AnydevicecapableofrunningJavawillbeabletointerpret/translatethisfileintosomething it can run. The compiled bytecode isplamormindependent.

4.Thevirtualmachinereadsandrunsthebytecode.

Page 12: Object-Oriented Programming in Javafaculty.utrgv.edu/dongchul.kim/CSCI3326/01_intro.pdf · ... Head First Java, 2nd Edion • Author: Kathy Sierra, Bert Bates • Publisher: O'Reilly

What you’ll do in Java

Source Compiler Output Virtual Machine

1.Typeyoursourcecode.Saveas:Hello.java

2. Compile theHello.javafile by runningjavac (the compiler applicaNon). If youdon’t have errors, you’ll get a seconddocumentnamedHello.classThe compiler-generatedHello.class file ismadeupofbytecode

4.RuntheprogrambystarNngtheJava Virtual Machine (JVM) withthe Hello.class file. The JVMtranslates the bytecode intosomethingtheunderlyingplamormunderstands, and runs yourprogram.

3.Compiledcode:Hello.class

Page 13: Object-Oriented Programming in Javafaculty.utrgv.edu/dongchul.kim/CSCI3326/01_intro.pdf · ... Head First Java, 2nd Edion • Author: Kathy Sierra, Bert Bates • Publisher: O'Reilly

Java Compiler and JVM • Withmostprogramminglanguages,portabilityisachievedbycompilingaprogramforeachCPUitwillrunon.

•  JavaprovidesanJVMforeachplamormsothatprogrammersdonothavetorecompilefordifferentplamorms.

Page 14: Object-Oriented Programming in Javafaculty.utrgv.edu/dongchul.kim/CSCI3326/01_intro.pdf · ... Head First Java, 2nd Edion • Author: Kathy Sierra, Bert Bates • Publisher: O'Reilly

Java Software Edition •  The software you use to write Java programs is

called the Java Development Kit, or JDK. •  There are different editions of the JDK:

–  Java SE – Standard Edition •  Provides essential tools for developing applications and applets

–  Java EE – Enterprise Edition •  Provides tools for creating large business applications that employ servers

and provide services over the Web

–  Java ME – Micro Edition •  Provides a small, optimized runtime environment for consumer products

such as cell phones, pagers, and appliances •  Available for download at https://

www.oracle.com/java/index.html

Page 15: Object-Oriented Programming in Javafaculty.utrgv.edu/dongchul.kim/CSCI3326/01_intro.pdf · ... Head First Java, 2nd Edion • Author: Kathy Sierra, Bert Bates • Publisher: O'Reilly

Plz. Memorize Now! **Details will be explained later on.

Page 16: Object-Oriented Programming in Javafaculty.utrgv.edu/dongchul.kim/CSCI3326/01_intro.pdf · ... Head First Java, 2nd Edion • Author: Kathy Sierra, Bert Bates • Publisher: O'Reilly

Install Java •  Java SE •  http://www.oracle.com/technetwork/java/

javase/downloads/index-jsp-138363.html

Page 17: Object-Oriented Programming in Javafaculty.utrgv.edu/dongchul.kim/CSCI3326/01_intro.pdf · ... Head First Java, 2nd Edion • Author: Kathy Sierra, Bert Bates • Publisher: O'Reilly
Page 18: Object-Oriented Programming in Javafaculty.utrgv.edu/dongchul.kim/CSCI3326/01_intro.pdf · ... Head First Java, 2nd Edion • Author: Kathy Sierra, Bert Bates • Publisher: O'Reilly
Page 19: Object-Oriented Programming in Javafaculty.utrgv.edu/dongchul.kim/CSCI3326/01_intro.pdf · ... Head First Java, 2nd Edion • Author: Kathy Sierra, Bert Bates • Publisher: O'Reilly

Install Java

Page 20: Object-Oriented Programming in Javafaculty.utrgv.edu/dongchul.kim/CSCI3326/01_intro.pdf · ... Head First Java, 2nd Edion • Author: Kathy Sierra, Bert Bates • Publisher: O'Reilly

Install Java

Page 21: Object-Oriented Programming in Javafaculty.utrgv.edu/dongchul.kim/CSCI3326/01_intro.pdf · ... Head First Java, 2nd Edion • Author: Kathy Sierra, Bert Bates • Publisher: O'Reilly

Setting Path in Windows •  Windows 8

–  Drag the Mouse pointer to the Right bottom corner of the screen –  Click on the Search icon and type: Control Panel –  Click on → Control Panel → System → Advanced –  Click on Environment Variables, under System Variables, find PATH, and click on it. –  In the Edit windows, modify PATH by adding the location of the class (e.g. C:\Program Files\Java

\jdk1.8.0_31\bin\) to the value for PATH. If you do not have the item PATH, you may select to add a new variable and add PATH as the name and the location of the class as the value.

–  Close the window. –  Reopen Command prompt window, and run your java code.

•  Windows 7 –  Select Computer from the Start menu –  Choose System Properties from the context menu –  Click Advanced system settings → Advanced tab –  Click on Environment Variables, under System Variables, find PATH, and click on it. –  In the Edit windows, modify PATH by adding the location of the class (e.g. C:\Program Files\Java

\jdk1.8.0_31\bin\) to the value for PATH. If you do not have the item PATH, you may select to add a new variable and add PATH as the name and the location of the class as the value.

–  Reopen Command prompt window, and run your java code.

Page 22: Object-Oriented Programming in Javafaculty.utrgv.edu/dongchul.kim/CSCI3326/01_intro.pdf · ... Head First Java, 2nd Edion • Author: Kathy Sierra, Bert Bates • Publisher: O'Reilly

Setting CLASSPATH Variable •  Right-click the My Computer icon on •  your desktop and select Properties. •  Click the Advanced tab. Click the •  Environment Variables button. Under System Variables, click

New. •  Enter the variable name as CLASSPATH. •  Enter the variable value (e.g. .;) •  The CLASSPATH variable is one way to tell applications,

including the JDK tools, where to look for user classes. •  Click OK. •  Click Apply Changes.

Page 23: Object-Oriented Programming in Javafaculty.utrgv.edu/dongchul.kim/CSCI3326/01_intro.pdf · ... Head First Java, 2nd Edion • Author: Kathy Sierra, Bert Bates • Publisher: O'Reilly

Setting JAVA_HOME Variable •  Right-click the My Computer icon on •  your desktop and select Properties. •  Click the Advanced tab. Click the •  Environment Variables button. Under System Variables, click

New. •  Enter the variable name as JAVA_HOME. •  Enter the variable value (e.g. C:\Program Files\Java\jdk1.8.0_31)

as the installation path for the Java Development Kit. •  Click OK. •  Click Apply Changes.

Page 24: Object-Oriented Programming in Javafaculty.utrgv.edu/dongchul.kim/CSCI3326/01_intro.pdf · ... Head First Java, 2nd Edion • Author: Kathy Sierra, Bert Bates • Publisher: O'Reilly

Compile •  TheJavacompilerisacommandlineuNlity.•  Thecommandtocompileaprogramis:

–  javac filename.java –  javacmeansJavacompiler.

•  The.java fileextensionmustbeused.–  Example:TocompileajavasourcecodefilenamedHello.javayouwouldusethecommand:

– javac Hello.java

Page 25: Object-Oriented Programming in Javafaculty.utrgv.edu/dongchul.kim/CSCI3326/01_intro.pdf · ... Head First Java, 2nd Edion • Author: Kathy Sierra, Bert Bates • Publisher: O'Reilly

Run •  ToruntheJavaprogram,youusethejavacommandinthefollowing

form:–  java ClassFilename

•  ClassFilenameisthenameofthe.class filethatyouwishtoexecute.–  However,youdonottypethe.class extension.

•  Forexample,toruntheprogramthatisstoredintheHello.classfile,youwouldenterthefollowingcommand:–  java Hello –  ThiscommandrunstheJavainterpreter(theJVM)andexecutesthe

program.

Page 26: Object-Oriented Programming in Javafaculty.utrgv.edu/dongchul.kim/CSCI3326/01_intro.pdf · ... Head First Java, 2nd Edion • Author: Kathy Sierra, Bert Bates • Publisher: O'Reilly

Windows Command Prompt

Page 27: Object-Oriented Programming in Javafaculty.utrgv.edu/dongchul.kim/CSCI3326/01_intro.pdf · ... Head First Java, 2nd Edion • Author: Kathy Sierra, Bert Bates • Publisher: O'Reilly

IDE •  InaddiNontothecommandpromptprograms,thereare

alsoseveralJavaIntegratedDevelopmentEnvironments(IDEs).

•  Theseenvironmentsconsistofatexteditor,compiler,debugger,andotheruNliNesintegratedintoapackagewithasinglesetofmenus.

•  Aprogramiscompiledandexecutedwithasingleclickofabu`on,orbyselecNngasingleitemfromamenu.

Page 28: Object-Oriented Programming in Javafaculty.utrgv.edu/dongchul.kim/CSCI3326/01_intro.pdf · ... Head First Java, 2nd Edion • Author: Kathy Sierra, Bert Bates • Publisher: O'Reilly

Most Popular IDE: Eclipse

Page 29: Object-Oriented Programming in Javafaculty.utrgv.edu/dongchul.kim/CSCI3326/01_intro.pdf · ... Head First Java, 2nd Edion • Author: Kathy Sierra, Bert Bates • Publisher: O'Reilly

IDE from Oracle: NetBeans

Page 30: Object-Oriented Programming in Javafaculty.utrgv.edu/dongchul.kim/CSCI3326/01_intro.pdf · ... Head First Java, 2nd Edion • Author: Kathy Sierra, Bert Bates • Publisher: O'Reilly

Install Eclipse

Page 31: Object-Oriented Programming in Javafaculty.utrgv.edu/dongchul.kim/CSCI3326/01_intro.pdf · ... Head First Java, 2nd Edion • Author: Kathy Sierra, Bert Bates • Publisher: O'Reilly

Install Eclipse

Page 32: Object-Oriented Programming in Javafaculty.utrgv.edu/dongchul.kim/CSCI3326/01_intro.pdf · ... Head First Java, 2nd Edion • Author: Kathy Sierra, Bert Bates • Publisher: O'Reilly

Install Eclipse

Page 33: Object-Oriented Programming in Javafaculty.utrgv.edu/dongchul.kim/CSCI3326/01_intro.pdf · ... Head First Java, 2nd Edion • Author: Kathy Sierra, Bert Bates • Publisher: O'Reilly

Install Eclipse

Page 34: Object-Oriented Programming in Javafaculty.utrgv.edu/dongchul.kim/CSCI3326/01_intro.pdf · ... Head First Java, 2nd Edion • Author: Kathy Sierra, Bert Bates • Publisher: O'Reilly

Install Eclipse

Page 35: Object-Oriented Programming in Javafaculty.utrgv.edu/dongchul.kim/CSCI3326/01_intro.pdf · ... Head First Java, 2nd Edion • Author: Kathy Sierra, Bert Bates • Publisher: O'Reilly

Install Eclipse

Page 36: Object-Oriented Programming in Javafaculty.utrgv.edu/dongchul.kim/CSCI3326/01_intro.pdf · ... Head First Java, 2nd Edion • Author: Kathy Sierra, Bert Bates • Publisher: O'Reilly

Comment

•  The // in line 1 marks the beginning of a comment. •  The compiler ignores everything from the double slash

to the end of the line. •  Comments are not required, but comments are very

important because they help explain what is going on in the program.

Page 37: Object-Oriented Programming in Javafaculty.utrgv.edu/dongchul.kim/CSCI3326/01_intro.pdf · ... Head First Java, 2nd Edion • Author: Kathy Sierra, Bert Bates • Publisher: O'Reilly

Lab1-1 •  Install Eclipse •  Make a java program that displays “hello yourname” •  In the first line of your program, please put a comment

with your name and lab number –  //Dongchul Kim, lab1-1

•  Save as “Hello.java” •  Compile and Run •  Give a demo to your instructor or TA.

Page 38: Object-Oriented Programming in Javafaculty.utrgv.edu/dongchul.kim/CSCI3326/01_intro.pdf · ... Head First Java, 2nd Edion • Author: Kathy Sierra, Bert Bates • Publisher: O'Reilly

Install IDE for Android App •  http://developer.android.com/sdk/index.html

Page 39: Object-Oriented Programming in Javafaculty.utrgv.edu/dongchul.kim/CSCI3326/01_intro.pdf · ... Head First Java, 2nd Edion • Author: Kathy Sierra, Bert Bates • Publisher: O'Reilly

Install Android Studio

Page 40: Object-Oriented Programming in Javafaculty.utrgv.edu/dongchul.kim/CSCI3326/01_intro.pdf · ... Head First Java, 2nd Edion • Author: Kathy Sierra, Bert Bates • Publisher: O'Reilly

Install Android Studio

Page 41: Object-Oriented Programming in Javafaculty.utrgv.edu/dongchul.kim/CSCI3326/01_intro.pdf · ... Head First Java, 2nd Edion • Author: Kathy Sierra, Bert Bates • Publisher: O'Reilly

Install Android Studio

Page 42: Object-Oriented Programming in Javafaculty.utrgv.edu/dongchul.kim/CSCI3326/01_intro.pdf · ... Head First Java, 2nd Edion • Author: Kathy Sierra, Bert Bates • Publisher: O'Reilly

Install Android Studio

Page 43: Object-Oriented Programming in Javafaculty.utrgv.edu/dongchul.kim/CSCI3326/01_intro.pdf · ... Head First Java, 2nd Edion • Author: Kathy Sierra, Bert Bates • Publisher: O'Reilly

Install Android Studio

Page 44: Object-Oriented Programming in Javafaculty.utrgv.edu/dongchul.kim/CSCI3326/01_intro.pdf · ... Head First Java, 2nd Edion • Author: Kathy Sierra, Bert Bates • Publisher: O'Reilly

Hello World for Android App

Page 45: Object-Oriented Programming in Javafaculty.utrgv.edu/dongchul.kim/CSCI3326/01_intro.pdf · ... Head First Java, 2nd Edion • Author: Kathy Sierra, Bert Bates • Publisher: O'Reilly

Hello World for Android App

Page 46: Object-Oriented Programming in Javafaculty.utrgv.edu/dongchul.kim/CSCI3326/01_intro.pdf · ... Head First Java, 2nd Edion • Author: Kathy Sierra, Bert Bates • Publisher: O'Reilly

Hello World for Android App

Page 47: Object-Oriented Programming in Javafaculty.utrgv.edu/dongchul.kim/CSCI3326/01_intro.pdf · ... Head First Java, 2nd Edion • Author: Kathy Sierra, Bert Bates • Publisher: O'Reilly

Hello World for Android App

Page 48: Object-Oriented Programming in Javafaculty.utrgv.edu/dongchul.kim/CSCI3326/01_intro.pdf · ... Head First Java, 2nd Edion • Author: Kathy Sierra, Bert Bates • Publisher: O'Reilly

Hello World for Android App

Page 49: Object-Oriented Programming in Javafaculty.utrgv.edu/dongchul.kim/CSCI3326/01_intro.pdf · ... Head First Java, 2nd Edion • Author: Kathy Sierra, Bert Bates • Publisher: O'Reilly

Lab1-2 •  Make a Android app that displays “Hello yourname” on

Android phone •  Demonstrate it to your instructor!