Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Chapter 2 First Java Programs Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.

Similar presentations


Presentation on theme: "1 Chapter 2 First Java Programs Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne."— Presentation transcript:

1 1 Chapter 2 First Java Programs Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne

2 Chapter 2 Lambert / OsborneFundamentals of Java 4E 222 Objectives Discuss why Java is an important programming language. Explain the Java virtual machine and byte code. Choose a user interface style. Describe the structure of a simple Java program. Write a simple program.

3 Chapter 2 Lambert / OsborneFundamentals of Java 4E 333 Objectives (continued) Edit, compile, and run a program using a Java development environment. Format a program to give a pleasing, consistent appearance. Understand compile-time errors. Write a simple graphics program.

4 Chapter 2 Lambert / OsborneFundamentals of Java 4E 444 Vocabulary applet assignment operator byte code DOS development environment graphical user interface (GUI) hacking import statement integrated development environment (IDE) interpreter Java virtual machine (JVM)

5 Chapter 2 Lambert / OsborneFundamentals of Java 4E 555 Vocabulary (continued) just-in-time compilation (JIT) panel panes parameter source code statement terminal I/O user interface variable

6 Chapter 2 Lambert / OsborneFundamentals of Java 4E 666 Why Java? 1 - Java is the fastest growing programming language in the world. – Sun, IBM use Java to develop applications. 2 - Java is ideal for distributed, network-based applications: – Enables the construction of virus-free, tamper-free systems (secure) – Supports the development of programs that do not overwrite memory (robust) – Yields programs that can be run on different types of computers without change (portable)

7 Chapter 2 Lambert / OsborneFundamentals of Java 4E 777 Why Java? 3- Java supports the use of advanced programming concepts such as threads. A thread is a process that can run concurrently with other processes. 4- Java bears a superficial resemblance to C++, which is currently the world’s most popular industrial-strength programming language. On the negative side, Java runs more slowly than most modern programming languages because it is interpreted.

8 Chapter 2 Lambert / OsborneFundamentals of Java 4E 888 The Java Virtual Machine and Byte Code Java compilers translate Java into Java byte code. – Not machine language – Must install JVM (Java Virtual Machine). A JVM is an interpreter. – An interpreter is a program that runs like a computer. – An interpreter runs slower than a computer.

9 Chapter 2 Lambert / OsborneFundamentals of Java 4E 999 The Java Virtual Machine and Byte Code (continued) Any computer can run an interpreter. – Makes Java byte code portable (is the machine language for an imaginary Java computer) – a technique called just-in-time compilation (JIT) when running the JVM Java applets – Applets are small programs already translated into byte code that are built into Web sites.

10 Chapter 2 Lambert / OsborneFundamentals of Java 4E 10 Choosing a User Interface Style Two user interfaces for a temperature conversion program Graphical user interface (GUI) Terminal I/O user interface

11 Chapter 2 Lambert / OsborneFundamentals of Java 4E 11 Choosing a User Interface Style (continued) Why use terminal I/O? – In Java, it’s easier to implement than GUI. – There are programming situations that require terminal I/O. – Terminal-oriented programs are similar in structure to programs that process files of sequentially organized data.

12 Chapter 2 Lambert / OsborneFundamentals of Java 4E 12 Hello World “Hello World” is traditionally the first program in a textbook. Hello world program executed

13 Chapter 2 Lambert / OsborneFundamentals of Java 4E 13 Hello World (continued) The Source Code: The bulk of the instructions of a program.

14 Chapter 2 Lambert / OsborneFundamentals of Java 4E 14 Hello World (continued) The Explanation: System.out is an object that displays characters in a terminal window. println is the message being sent to the object. The quotations indicate what is to be displayed. Semicolons mark the end of each statement. The characters between the parentheses are the parameters. The period (. ) is the method selector operator.

15 Chapter 2 Lambert / OsborneFundamentals of Java 4E 15 Hello World (continued) The Larger Framework: Program comments are in green, reserved words in blue, and code in black.

16 Chapter 2 Lambert / OsborneFundamentals of Java 4E 16 Edit, Compile, and Execute Edit – Save it as a text file with the extension.java. Compile – Translates the source code into Java byte code. Execute – The programmer instructs the JVM to load the byte code into memory and execute. – The user and program can now interact.

17 Chapter 2 Lambert / OsborneFundamentals of Java 4E 17 Edit, Compile, and Execute (continued) Editing, compiling, and running a program

18 Chapter 2 Lambert / OsborneFundamentals of Java 4E 18 Edit, Compile, and Execute (continued) Development Environments: Unix or LinuxStandard text editorFree Microsoft WindowsNotepad and DOS window Free Integrated development environment (IDE) BlueJ, Eclipse, or JGrasp Not free, but combines editor, compiler, debugger, and JVM

19 Chapter 2 Lambert / OsborneFundamentals of Java 4E 19 Edit, Compile, and Execute (continued) The program as typed into Notepad

20 Chapter 2 Lambert / OsborneFundamentals of Java 4E 20 Edit, Compile, and Execute (continued) Compile-Time Errors: Mistakes detected by the compiler are called syntax errors or compile-time errors. Compiler’s error message

21 Chapter 2 Lambert / OsborneFundamentals of Java 4E Edit, Compile, and Execute (continued) Readability: Programs may be maintained by other people. Layout affects readability. – Use indentation, blank lines, and spaces. 21

22 Chapter 2 Lambert / OsborneFundamentals of Java 4E 22 Temperature Conversion Temperature conversion program reads user input and performs computations. The first line of code is an import statement. Variables for Fahrenheit and Celsius. Assignment statements use an operator such as *, /, +, and -.

23 Chapter 2 Lambert / OsborneFundamentals of Java 4E 23 Temperature Conversion (continued) Variables and objects used in the conversion program

24 Chapter 2 Lambert / OsborneFundamentals of Java 4E 24 Summary In this chapter, you learned: Java is the fastest growing programming language in the world. It is secure, robust, and portable. It is also similar to C++, the world’s most popular programming language. 24

25 Chapter 2 Lambert / OsborneFundamentals of Java 4E 25 Summary (continued) 25 The Java compiler translates Java into a pseudomachine language called Java byte code. Byte code can be run on any computer that has a Java virtual machine installed. The Java virtual machine (JVM) is a program that behaves like a computer—an interpreter. Java programs include variables, arithmetic expressions, statements, objects, messages, and methods.

26 Chapter 2 Lambert / OsborneFundamentals of Java 4E 26 Summary (continued) Three basic steps in the coding process are editing, compiling, and running a program using a Java development environment. Programmers should pay attention to a program’s format to ensure readability. Java programs accomplish many tasks by sending messages to objects. Examples are sending text to the terminal window for output and receiving input data from the keyboard. There are several user interface styles, among them terminal based and graphical based. 26


Download ppt "1 Chapter 2 First Java Programs Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne."

Similar presentations


Ads by Google