Chapter 2 First Java Programs Fundamentals of Java
- Slides: 50
Chapter 2 First Java Programs Fundamentals of Java
Objectives 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. Fundamentals of Java
Objectives (cont. ) l l l 3 Write a simple program. 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. Fundamentals of Java
Vocabulary l l l l 4 Applet Assignment operator Byte code DOS development environment Graphical user interface (GUI) Hacking Integrated development environment (IDE) Fundamentals of Java
Vocabulary (cont. ) l l l l 5 Java virtual machine (JVM) Just-in-time compilation (JIT) Parameter Source code Statement Terminal I/O user interface Variable Fundamentals of Java
Why Java? l Java is: – – 6 The world’s fastest growing programming language Secure: Enables construction of virus-free, tamper-free systems Robust: Supports development of programs that do not overwrite memory Portable: Programs can be run on different types of computers without change Fundamentals of Java
Why Java? (cont. ) l l Java is well-suited for distributed, networkbased applications. Java supports threads. – l Java resembles C++. – l 7 A process that can run concurrently with other processes Easy for C++ programmers to learn Java runs more slowly than other languages. Fundamentals of Java
The Java Virtual Machine and Byte Code l The Java compiler translates Java source code into Java byte code. – l Byte code is executed by the Java Virtual Machine (JVM). – – 8 A pseudo-machine language Interpreter: A program that behaves like a computer Any computer with the JVM installed can execute Java byte code (portability). Fundamentals of Java
The Java Virtual Machine and Byte Code (cont. ) l Some JVMs support just-in-time compilation (JIT). – l Java applets: Java programs that run in a Web browser – 9 Translate byte code instructions into machine language when they are first encountered A JVM is incorporated into the browser. Fundamentals of Java
Choosing a User Interface Style l Two choices: – – Graphical user interface (GUI): Program interacts with users via “windows” with graphical components Terminal I/O user interface: Programs interact with users via a command terminal l MS-DOS 10 console or Unix terminal Fundamentals of Java
Choosing a User Interface Style (cont. ) Figure 2 -1: Two user interfaces for a temperature conversion program 11 Fundamentals of Java
Hello World Figure 2 -2: Hello World 12 Fundamentals of Java
Hello World (cont. ) l l 13 A program is a sequence of instructions to perform a task. Source code: The programming language instructions for a program Fundamentals of Java
Hello World (cont. ) Example 2 -1: Our first program 14 Fundamentals of Java
Hello World (cont. ) l System. out: An object that knows how to display or print characters in a terminal window – println: The message being sent to the System. out object – Strings in quotation marks contain characters to be printed. l Parameters 15 Fundamentals of Java
Hello World (cont. ) l Semicolons (; ) mark the end of each statement. – l l A message may require zero, one, or multiple parameters. Format for sending messages to objects: – 16 A “sentence” in a program <name of object>. <name of message>(<parameters>) Fundamentals of Java
Hello World (cont. ) l l 17 Method selector operator: The period (. ) between the object’s name and the message’s name Framework for any Java program: Fundamentals of Java
Edit, Compile, and Execute Figure 2 -3: Editing, compiling, and running a program 18 Fundamentals of Java
Edit, Compile, and Execute (cont. ) 19 l The file extension for a Java source code file is. java. l The file extension for a Java class file is. class. Fundamentals of Java
Development Environments l l Use UNIX or Linux using a standard text editor with command-line activation of the compiler and the JVM. DOS development environment: – – 20 Use a text editor to write source code. Activate compiler and JVM from the command line in a command or DOS Window. Fundamentals of Java
Development Environments (cont. ) l Integrated development environment (IDE): Combines an editor, Java compiler, debugger, and JVM in one program – – – 21 Increases programmer productivity Usually costs money Examples: Metrowerks Code Warrior, Borland JBuilder, Blue. J, and JGrasp Fundamentals of Java
Preparing Your Development Environment Figure 2 -4: Using the cd command to move to the working directory 22 Fundamentals of Java
Preparing Your Development Environment (cont. ) Figure 2 -5: Activating Notepad to edit the program 23 Fundamentals of Java
Preparing Your Development Environment (cont. ) 24 Figure 2 -6: Program as typed into Notepad Fundamentals of Java
Preparing Your Development Environment (cont. ) Figure 2 -7: Compiling and running the program 25 Fundamentals of Java
Compile-Time Errors l Mistakes detected by the compiler – l Compiler displays errors in terminal window – – – 26 Also called syntax errors Indicates file and line number where error was detected Indicates type of error detected May suggest solutions to error Fundamentals of Java
Compile-Time Errors (cont. ) Figure 2 -8: Program with a compile-time error on line 6 27 Fundamentals of Java
Compile-Time Errors (cont. ) Figure 2 -9: Compiler’s error message 28 Fundamentals of Java
Readability l The main factor affecting readability is layout. – – l l The compiler ignores layout. It is important for your code to be readable by others. – 29 Spacing Indentation Your programs may be maintained by others. Fundamentals of Java
Temperature Conversion Figure 2 -10: User interface for the temperature conversion program 30 Fundamentals of Java
Temperature Conversion: The Source Code 31 Fundamentals of Java
Temperature Conversion: The Explanation l First line of code is an import statement. – l Tells compiler where to find a class that will be used during the program Scanner object: Provides functionality for reading input entered at the keyboard – Scanner object instantiated by the code: l 32 Scanner reader = new Scanner(System. in); Fundamentals of Java
Temperature Conversion: The Explanation (cont. ) l In general, instantiation takes the form: – l A numeric variable names a location in RAM in which a number can be stored. – e. g. , double celsius; – A variable’s value may change during a program. A variable’s type defines what kind of values it can have. – 33 Some. Class some. Object = new Some. Class(some parameters); Fundamentals of Java
Temperature Conversion: The Explanation (cont. ) l Assignment operator: The symbol = – – Used to assign a value to a variable Examples: fahrenheit = reader. next. Double(); l celsius = (fahrenheit - 32. 0) * 5. 0/9. 0; l l 34 Assignment statements: Statements using the assignment operator Fundamentals of Java
Temperature Conversion: The Explanation (cont. ) l Mathematical operators: – – l System. out. println() can be used to print values of variables. – 35 +: Addition -: Subtraction *: Multiplication /: Division System. out. println(fahrenheit); Fundamentals of Java
Temperature Conversion: Variables and Objects l l Variables fahrenheit and celsius each hold a single floating-point number. Variables reader and System. out hold references to objects. – 36 References used to send messages to the objects Fundamentals of Java
Temperature Conversion: Variables and Objects (cont. ) Figure 2 -11: Variables and objects used in the conversion program 37 Fundamentals of Java
Graphics and GUIs: Windows and Panels l Standalone GUI applications run in windows. – l Windows have numerous properties. – – – 38 Containers for graphical components to be displayed to the user Width and height Title bar Ability to be dragged or resized by the user Fundamentals of Java
Graphics and GUIs: Windows and Panels (cont. ) Example 2 -3: Empty frame 39 Fundamentals of Java
Graphics and GUIs: Windows and Panels (cont. ) Figure 2 -12: GUI program with an empty window 40 Fundamentals of Java
Graphics and GUIs: Windows and Panels (cont. ) 41 Table 2 -1: Some commonly used JFrame methods Fundamentals of Java
Panels and Colors l Panel: A flat, rectangular area suitable for displaying other objects – – l Color class: Can be used to create any RGB color value – 42 Geometric shapes and images JPanel class in javax. swing package Color a. Color = new Color(red. Value, green. Value, blue. Value) Fundamentals of Java
Panels and Colors (cont. ) 43 Table 2 -2: Some Color constants Fundamentals of Java
Panels and Colors (cont. ) Example 2 -4: Frame with an empty, pink panel 44 Fundamentals of Java
Layout Managers and Multiple Panels l Every container object (frame or panel) uses a layout manager object to organize and lay out contained graphical components. – Default layout manager for frames is an instance of the class Border. Layout l Can arrange up to five graphical objects in a container – – 45 NORTH, SOUTH, EAST, WEST, and CENTER Grid. Layout class: Divides a container into rows and columns Fundamentals of Java
Layout Managers and Multiple Panels (cont. ) Example 2 -6: Frame with a 2 -by-2 grid of colored panels 46 Fundamentals of Java
Layout Managers and Multiple Panels (cont. ) Figure 2 -14: 2 -by-2 grid layout with four panels 47 Fundamentals of Java
Summary l l 48 Java is the fastest growing programming language in the world. It is secure, robust, and portable. 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. Fundamentals of Java
Summary (cont. ) l l 49 Java programs include variables, arithmetic expressions, statements, objects, messages, and methods. 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. Fundamentals of Java
Summary (cont. ) l l 50 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. Fundamentals of Java
- Fundamentals of python first programs
- Fundamentals of python: first programs
- Overview of software engineering
- Chapter 2 lab java fundamentals
- First time buyer programs riverside county
- Fundamentals of first aid
- Java list exercises
- Increment operator in c
- Java string manipulation
- Java programs perform i/o through ……….. *
- [author] java message service - jms fundamentals course
- [author] java message service - jms fundamentals course
- Bonus scoring system softuni
- What is class
- Language fundamentals in java
- Types of early childhood programs activity a chapter 2
- Fundamental of nursing chapter 1
- Fundamentals of electric circuits chapter 4 solutions
- Fundamentals of corporate finance chapter 6 solutions
- Digital fundamentals chapter 4
- Chapter 24 magnetism magnetic fundamentals answers
- Chapter 73 tire wheel and wheel bearing fundamentals
- Understanding your health and wellness
- 9
- Fundamentals of electric circuits chapter 7 solutions
- Building the right foundations/fundamentals
- Computer fundamentals chapter 1
- Fundamentals of corporate management
- Fundamentals of thermal-fluidsciences chapter 1 problem 24p
- White-collar workers คือ
- Fundamental of information system
- Chapter 17 fundamentals of nursing
- Forensic science fundamentals and investigations chapter 6
- Fundamentals in lodging operations book
- Back channeling nursing
- Examples of the respiratory system
- Chapter 39 electrical fundamentals
- Fundamentals of thermal-fluidsciences chapter 2 problem 13p
- Fundamentals of thermal-fluidsciences chapter 2 problem 18p
- Kataoka-yahiro and saylor's model
- Glencoe health chapter 1 understanding health and wellness
- Fundamentals of thermal-fluidsciences chapter 1 problem 18p
- Fundamentals of thermal-fluidsciences chapter 1 problem 1p
- Fundamentals of nursing chapter 16
- Fundamentals of thermal-fluidsciences chapter 1 problem 24p
- Fundamentals of corporate finance, chapter 1
- Fundamentals of thermal-fluidsciences chapter 2 problem 30p
- Fundamentals of thermal-fluidsciences chapter 2 problem 28p
- Chapter 23 computer system fundamentals
- Maturity continuum model by stephen
- Breadth first vs depth first