Published on

Published in: Technology, News & Politics
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • First Class: Introduction, Prerequisites, Advices, Syllabus Lab 1: Create a Java Project, Compile, and Run. Show syntax errors Print program Capture screen shots, and save it in Word, and print it. Homework One: Check in the class randomly.
  • 01slide

    1. 1. Introduction toJava Programming, 4E Y. Daniel Liang
    2. 2. Introduction3 Course Objectives3 Organization of the Book 2
    3. 3. Course Objectives3 Upon completing the course, you will understand – Create, compile, and run Java programs – Primitive data types – Java control flow – Methods – Arrays (for teaching Java in two semesters, this could be the end) – Object-oriented programming – Core Java classes (Swing, exception, internationalization, multithreading, multimedia, I/O, networking, Java Collections Framework) 3
    4. 4. Course Objectives, cont.3 You will be able to – Develop programs using Forte – Write simple programs using primitive data types, control statements, methods, and arrays. – Create and use methods – Develop a GUI interface and Java applets – Write interesting projects – Establish a firm foundation on Java concepts 4
    5. 5. Book Chapters3 Part I: Fundamentals of Programming – Chapter 1 Introduction to Java – Chapter 2 Primitive Data Types and Operations – Chapter 3 Control Statements – Chapter 4 Methods – Chapter 5 Arrays 5
    6. 6. Book Chapters, cont.3 Part II: Object-Oriented Programming – Chapter 6 Objects and Classes – Chapter 7 Strings – Chapter 8 Class Inheritance and Interfaces – Chapter 9 Object-Oriented Software Development 6
    7. 7. Book Chapters, cont.3 Part III: GUI Programming – Chapter 10 Getting Started with GUI Programming – Chapter 11 Creating User Interfaces – Chapter 12 Applets and Advanced GUI 7
    8. 8. Book Chapters, cont.3 Part IV: Developing Comprehensive Projects – Chapter 13 Exception Handling – Chapter 14 Internationalization – Chapter 15 Multithreading – Chapter 16 Multimedia – Chapter 17 Input and Output – Chapter 18 Networking – Chapter 19 Java Data Structures 8
    9. 9. Chapter 1 Introduction to Java and Forte3 What Is Java?3 Getting Started With Java Programming – Create, Compile and Running a Java Application 9
    10. 10. What Is Java?3 History3 Characteristics of Java 10
    11. 11. History3 James Gosling and Sun Microsystems3 Oak3 Java, May 20, 1995, Sun World3 HotJava – The first Java-enabled Web browser3 JDK Evolutions3 J2SE, J2ME, and J2EE (not mentioned in the book, but could discuss here optionally) 11
    12. 12. Characteristics of Java3 Java is simple3 Java is object-oriented3 Java is distributed3 Java is interpreted3 Java is robust3 Java is secure3 Java is architecture-neutral3 Java is portable3 Java’s performance3 Java is multithreaded3 Java is dynamic 12
    13. 13. JDK Versions3 JDK 1.02 (1995)3 JDK 1.1 (1996)3 Java 2 SDK v 1.2 (a.k.a JDK 1.2, 1998)3 Java 2 SDK v 1.3 (a.k.a JDK 1.3, 2000)3 Java 2 SDK v 1.4 (a.k.a JDK 1.4, 2002) 13
    14. 14. JDK Editions3 Java Standard Edition (J2SE) – J2SE can be used to develop client-side standalone applications or applets.3 Java Enterprise Edition (J2EE) – J2EE can be used to develop server-side applications such as Java servlets and Java ServerPages.3 Java Micro Edition (J2ME). – J2ME can be used to develop applications for mobile devices such as cell phones.This book uses J2SE to introduce Java programming. 14
    15. 15. Java IDE Tools3 Forte by Sun MicroSystems3 Borland JBuilder3 Microsoft Visual J++3 WebGain Café3 IBM Visual Age for Java 15
    16. 16. Getting Started with Java Programming3A Simple Java Application3 Compiling Programs3 Executing Applications 16
    17. 17. A Simple ApplicationExample 1.1//This application program prints Welcome//to Java!package chapter1;public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); }} Source Run NOTE: To run the program, install slide files on hard disk. 17
    18. 18. Creating and Compiling Programs Create/Modify Source Code3 On command line – javac file.java Source Code Compile Source Code i.e. javac Welcome.java If compilation errors Bytecode Run Byteode i.e. java Welcome Result If runtime errors or incorrect result 18
    19. 19. Executing Applications3 On command line – java classname Bytecode Java Java Java Interpreter Interpreter Interpreter ... on Windows on Linux on Sun Solaris 19
    20. 20. Examplejavac Welcome.javajava Welcomeoutput:... 20
    21. 21. Compiling and Running a Program Where are the files Welcome.java stored in thec:example directory? chapter1 Welcome.class Welcome.java~ chapter2 Java source files and class files for Chapter 2 . . . chapter19 Java source files and class files for Chapter 19 21
    22. 22. Anatomy of a Java Program3 Comments3 Package3 Reserved words3 Modifiers3 Statements3 Blocks3 Classes3 Methods3 The main method 22
    23. 23. CommentsIn Java, comments arepreceded by two slashes (//)in a line, or enclosedbetween /* and */ in one ormultiple lines. When thecompiler sees //, it ignoresall text after // in thesame line. When it sees /*, 23
    24. 24. PackageThe second line in the program(package chapter1;) specifies a packagename, chapter1, for the class Welcome.Forte compiles the source code inWelcome.java, generatesWelcome.class, and storesWelcome.class in the chapter1 folder. 24
    25. 25. Reserved WordsReserved words or keywords arewords that have a specificmeaning to the compiler andcannot be used for otherpurposes in the program. Forexample, when the compiler seesthe word class, it understandsthat the word after class is thename for the class. Otherreserved words in Example 1.1 25
    26. 26. ModifiersJava uses certain reserved words calledmodifiers that specify the properties of thedata, methods, and classes and how theycan be used. Examples of modifiers arepublic and static. Other modifiers areprivate, final, abstract, and protected. Apublic datum, method, or class can beaccessed by other programs. A privatedatum or method cannot be accessed byother programs. Modifiers are discussed inChapter 6, "Objects and Classes." 26
    27. 27. StatementsA statement represents anaction or a sequence ofactions. The statementSystem.out.println("Welcometo Java!") in the program inExample 1.1 is a statementto display the greeting"Welcome to Java!" Everystatement in Java ends with 27
    28. 28. BlocksA pair of braces in a programforms a block that groupscomponents of a program. public class Test { Class block public static void main(String[] args) { System.out.println("Welcome to Java!"); Method block } } 28
    29. 29. ClassesThe class is the essential Javaconstruct. A class is a templateor blueprint for objects. Toprogram in Java, you mustunderstand classes and be ableto write and use them. Themystery of the class willcontinue to be unveiledthroughout this book. For now,though, understand that a 29
    30. 30. MethodsWhat is System.out.println? It is a method: acollection of statements that performs asequence of operations to display amessage on the console. It can be usedeven without fully understanding thedetails of how it works. It is used byinvoking a statement with a stringargument. The string argument is enclosedwithin parentheses. In this case, theargument is "Welcome to Java!" You cancall the same println method with a 30
    31. 31. main MethodThe main method provides thecontrol of program flow. TheJava interpreter executes theapplication by invoking the mainmethod.The main method looks like this:public static void main(String[]args) { 31
    32. 32. Displaying Text in a Message Dialog Boxyou can use the showMessageDialogmethod in the JOptionPane class.JOptionPane is one of the manypredefined classes in the Java system,which can be reused rather than“reinventing the wheel.” Source Run 32
    33. 33. The showMessageDialog MethodJOptionPane.showMessageDialog(null, "Welcometo Java!", "Example 1.2",JOptionPane.INFORMATION_MESSAGE)); 33
    34. 34. The exit MethodUse Exit to terminate the program and stopall threads.NOTE: When your program starts, a threadis spawned to run the program. When theshowMessageDialog is invoked, a separatethread is spawned to run this method. Thethread is not terminated even you close thedialog box. To terminate the thread, youhave to invoke the exit method. 34