Chapter 2 First Java Programs Fundamentals of Java

  • Slides: 33
Download presentation
Chapter 2 First Java Programs Fundamentals of Java: AP Computer Science Essentials, 4 th

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

Objectives l Chapter 2 l l 2 Discuss why Java is an important programming

Objectives l Chapter 2 l l 2 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. Lambert / Osborne Fundamentals of Java 4 E

Objectives (continued) l Chapter 2 l 3 l l Edit, compile, and run a

Objectives (continued) l Chapter 2 l 3 l l 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. Lambert / Osborne Fundamentals of Java 4 E

Vocabulary l l l Chapter 2 l 4 l applet assignment operator byte code

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

Vocabulary (continued) l l Chapter 2 l 5 l just-in-time compilation (JIT) panel panes

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

Why Java? l Java is the fastest growing programming language in the world. –

Why Java? l Java is the fastest growing programming language in the world. – Java is a modern object-oriented programming language. Chapter 2 l Sun, IBM use Java to develop applications. 6 Lambert / Osborne Fundamentals of Java 4 E

Why Java? (continued) l Java is ideal for distributed, network-based applications. – Chapter 2

Why Java? (continued) l Java is ideal for distributed, network-based applications. – Chapter 2 – 7 – Secure: Virus-free, tamper-free systems. Robust: Supports development of programs that do not overwrite memory. Portable: Yields programs that can be run on different computer types. Lambert / Osborne Fundamentals of Java 4 E

Why Java? (continued) l Java supports advanced programming concepts. – Chapter 2 l 8

Why Java? (continued) l Java supports advanced programming concepts. – Chapter 2 l 8 Java resembles C++. – l Thread: A process that can run concurrently with other processes. Easy for a C++ programmer to learn Java does run more slowly than other languages because it is interpreted. Lambert / Osborne Fundamentals of Java 4 E

The Java Virtual Machine and Byte Code l Java compilers translate Java into Java

The Java Virtual Machine and Byte Code l Java compilers translate Java into Java byte code. – – Chapter 2 l 9 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. Lambert / Osborne Fundamentals of Java 4 E

The Java Virtual Machine and Byte Code (continued) l JVMs are getting faster. –

The Java Virtual Machine and Byte Code (continued) l JVMs are getting faster. – l Any computer can run an interpreter. Chapter 2 – 10 l Using JIT (just-in-time) compilations, which translate byte code into machine language. Makes Java byte code portable. Java applets – – Applets are small programs already translated into byte code that are built into Web sites. Can be decorative or practical. Lambert / Osborne Fundamentals of Java 4 E

Choosing a User Interface Style Chapter 2 l 11 Two user interfaces for a

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

Choosing a User Interface Style (continued) l Why use terminal I/O? – Chapter 2

Choosing a User Interface Style (continued) l Why use terminal I/O? – Chapter 2 – 12 – 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. Lambert / Osborne Fundamentals of Java 4 E

Hello World “Hello World” is traditionally the first program in a textbook. Chapter 2

Hello World “Hello World” is traditionally the first program in a textbook. Chapter 2 l Hello world program executed 13 Lambert / Osborne Fundamentals of Java 4 E

Hello World (continued) l Chapter 2 l The Source Code: The bulk of the

Hello World (continued) l Chapter 2 l The Source Code: The bulk of the instructions of a program. 14 Lambert / Osborne Fundamentals of Java 4 E

Hello World (continued) l l l Chapter 2 l l 15 The Explanation: System.

Hello World (continued) l l l Chapter 2 l l 15 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. Lambert / Osborne Fundamentals of Java 4 E

Hello World (continued) l Chapter 2 l 16 l The Larger Framework: The program

Hello World (continued) l Chapter 2 l 16 l The Larger Framework: The program must be embedded in several lines of code, such as: Program comments are in green, reserved words in blue, and code in black. Lambert / Osborne Fundamentals of Java 4 E

Edit, Compile, and Execute l Edit – – Chapter 2 l 17 The programmer

Edit, Compile, and Execute l Edit – – Chapter 2 l 17 The programmer uses a word processor or editor to enter the source code. Save it as a text file with the extension. java. Compile – – The programmer invokes the Java language compiler. Translates the source code into Java byte code. Lambert / Osborne Fundamentals of Java 4 E

Edit, Compile, and Execute (continued) l Execute – Chapter 2 – The programmer instructs

Edit, Compile, and Execute (continued) l Execute – Chapter 2 – The programmer instructs the JVM to load the byte code into memory and execute. The user and program can now interact. 18 Lambert / Osborne Fundamentals of Java 4 E

Edit, Compile, and Execute (continued) Editing, compiling, and running a program Chapter 2 l

Edit, Compile, and Execute (continued) Editing, compiling, and running a program Chapter 2 l 19 Lambert / Osborne Fundamentals of Java 4 E

Edit, Compile, and Execute (continued) Chapter 2 l 20 Development Environments: Unix or Linux

Edit, Compile, and Execute (continued) Chapter 2 l 20 Development Environments: Unix or Linux Standard text editor Free Microsoft Windows Notepad and DOS window Free Integrated development environment (IDE) Blue. J, Eclipse, or JGrasp Not free, but combines editor, compiler, debugger, and JVM Lambert / Osborne Fundamentals of Java 4 E

Edit, Compile, and Execute (continued) l Preparing Your Development Environment: 1. Chapter 2 2.

Edit, Compile, and Execute (continued) l Preparing Your Development Environment: 1. Chapter 2 2. 21 3. 4. Create the directory, open a terminal window, and use the cd command. Open Notepad, create the file Hello. World. java, then type the code. Save the file, switch back to the terminal window, and compile the program. Run the program. Lambert / Osborne Fundamentals of Java 4 E

Edit, Compile, and Execute (continued) The program as typed into Notepad Chapter 2 l

Edit, Compile, and Execute (continued) The program as typed into Notepad Chapter 2 l 22 Lambert / Osborne Fundamentals of Java 4 E

Edit, Compile, and Execute (continued) l l l Chapter 2 l 23 Compile-Time Errors:

Edit, Compile, and Execute (continued) l l l Chapter 2 l 23 Compile-Time Errors: Mistakes detected by the compiler are called syntax errors or compile-time errors. Typos made when editing. Compiler prints a list of errors in the terminal window. Compiler’s error message Lambert / Osborne Fundamentals of Java 4 E

Edit, Compile, and Execute (continued) l l l Readability: Programs may be maintained by

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

Temperature Conversion l l Chapter 2 l 25 l Temperature conversion program reads user

Temperature Conversion l l Chapter 2 l 25 l 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 -. Lambert / Osborne Fundamentals of Java 4 E

Temperature Conversion (continued) Variables and objects used in the conversion program Chapter 2 l

Temperature Conversion (continued) Variables and objects used in the conversion program Chapter 2 l 26 Lambert / Osborne Fundamentals of Java 4 E

Graphics and GUIs: Windows and Panels l l l A Simple Application Window: Graphics

Graphics and GUIs: Windows and Panels l l l A Simple Application Window: Graphics and GUI programs in Java can be standalone applications or applets. Consistent features: Chapter 2 – 27 – l Title bar with controls (maximize, zoom, etc. ) Width and height can be resized Code for application windows is in the class Jframe. – JFrame responds to messages to set the title bar and window size. Lambert / Osborne Fundamentals of Java 4 E

Graphics and GUIs: Windows and Panels (continued) Some commonly used JFrame methods Chapter 2

Graphics and GUIs: Windows and Panels (continued) Some commonly used JFrame methods Chapter 2 l 28 Lambert / Osborne Fundamentals of Java 4 E

Graphics and GUIs: Windows and Panels (continued) l l l Chapter 2 l 29

Graphics and GUIs: Windows and Panels (continued) l l l Chapter 2 l 29 l Panels and Colors: A Jframe has a container or pane to fill with objects. A panel is a rectangle used to display objects such a shapes and images. Panes are panels that contain related objects such as images and widgets. Colors in most computer system use RGB. – – Red, green, blue Values 0 -255 Lambert / Osborne Fundamentals of Java 4 E

Graphics and GUIs: Windows and Panels (continued) l l Chapter 2 l 30 Layout

Graphics and GUIs: Windows and Panels (continued) l l Chapter 2 l 30 Layout Managers and Multiple Panels: Each container object uses a layout manager to control panel placement. Border. Layout class allows arrangement of up to five objects. – l North, south, east, west, center Grid. Layout uses rows and columns to arrange objects. Lambert / Osborne Fundamentals of Java 4 E

Summary Chapter 2 In this chapter, you learned: l Java is the fastest growing

Summary Chapter 2 In this chapter, you learned: l 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. 31 Lambert / Osborne Fundamentals of Java 4 E

Summary (continued) Chapter 2 l 32 l The Java compiler translates Java into a

Summary (continued) Chapter 2 l 32 l 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. Lambert / Osborne Fundamentals of Java 4 E

Summary (continued) l Chapter 2 l 33 l Three basic steps in the coding

Summary (continued) l Chapter 2 l 33 l 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. Lambert / Osborne Fundamentals of Java 4 E