Chapter 2 First Java Programs Fundamentals of Java.

Slides:



Advertisements
Similar presentations
Designing a Program & the Java Programming Language
Advertisements

Chapter 8 Improving the User Interface
Chapter 3 Syntax, Errors, and Debugging
Chapter 2 First Java Programs
Chapter 1 Introduction to JAVA. Why Learn JAVA? Java is one of the fastest growing programming language in the world. Java is one of the fastest growing.
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Frameworks. 2 Framework Set of cooperating classes/interfaces –Structure essential mechanisms of a problem domain –Programmer can extend framework classes,
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design First Edition by Tony Gaddis.
1 Fall 2008ACS-1903 Chapter 1 Topics Java History Java Programs Why Program? Computer Systems: Hardware and Software Programming Languages What Is a Program.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
Chapter 7 Improving the User Interface
1 An Introduction to Visual Basic Objectives Explain the history of programming languages Define the terminology used in object-oriented programming.
1. 2 Chapter 1 Introduction to Computers, Programs, and Java.
Chapter 2 First Java Programs
COMP Computer Basics Yi Hong May 13, 2015.
Computer Programming 12 Mr. Jean March 3 rd, 2014.
Chapter 2 Primitve Data Types, Input, Output, and Assignment Sect 1 - Steps for Creating and Running a Java Program Sect 2 - Framework of a Simple Java.
1.1 History of Computers 1940s: The ENIAC was one of the world’s first computers. Large stand-alone machine Used large amounts of electricity Contained.
Introducing Java.
Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet.
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
A First Program Using C#
Chapter 2 How to Compile and Execute a Simple Program.
Lesson 2: First Java Programs
Topics Introduction Hardware and Software How Computers Store Data
Microsoft Visual Basic 2005: Reloaded Second Edition
Lesson 7: Improving the User Interface
Java Programming, 3e Concepts and Techniques Chapter 2 - Part 2 Creating a Java Application and Applet.
 Java Programming Environment  Creating Simple Java Application  Lexical Issues  Java Class Library.
1 Chapter 2 First Java Programs Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
Computer Programming 12 Mr. Jean March 19 th, 2013.
Introduction to Computers and Java Chapter 1.3. A Sip of Java: Outline History of the Java Language Applets A First Java Program Compiling a Java Program.
Chapter 1 CSIS-120: Java Intro. What is Programming?  A: It is what makes computer so useful.  The flexibility of a computer is amazing  Write a term.
Lesson 2: First Java Programs. Objectives: –Discuss why Java is an important programming language. –Explain the Java virtual machine and byte code. –Choose.
Jaeki Song ISQS6337 JAVA Lecture 03 Introduction to Java -The First Java Application-
© 2012 Pearson Education, Inc. All rights reserved. 1-1 Why Java? Needed program portability – Program written in a language that would run on various.
Intro and Review Welcome to Java. Introduction Java application programming Use tools from the JDK to compile and run programs. Videos at
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
Chapter 2 Data Types, Input, and Output Section 1 - Java Data Types Section 2 - Declaring and Initializing Variables Section 3 - Arithmetic Operators.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Timer class and inner classes. Processing timer events Timer is part of javax.swing helps manage activity over time Use it to set up a timer to generate.
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Applets and Frames. Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI Slide 2 Applets Usually.
JAVA PROGRAMMING WITH ECLIPSE PART 1 PLEASE READ ALL THE CONTENT ON THE SLIDES. WATCH THE RECOMMENDED VIDEOS LISTED.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Chapter 2 Input, Variables and Data Types. JAVA Input JAVA input is not straightforward and is different depending on the JAVA environment that you are.
Chapter 2 An Intro to Java and Your First Program What is Java? Advantages of Java Four Steps for Creating and Running a Java Program Framework of a Simple.
Chapter 1 Introduction. Chapter Goals To understand the activity of programming To learn about the architecture of computers To learn about machine code.
Chapter 5 Introduction To Form Builder. Lesson A Objectives  Display Forms Builder forms in a Web browser  Use a data block form to view, insert, update,
By Mr. Muhammad Pervez Akhtar
© 2012 Pearson Education, Inc. All rights reserved types of Java programs Application – Stand-alone program (run without a web browser) – Relaxed.
Creating a Java Application and Applet
Microsoft Visual Basic 2008: Reloaded Third Edition Chapter One An Introduction to Visual Basic 2008.
Execution ways of program References: www. en.wikipedia.org/wiki/Integrated_development_environment  You can execute or run a simple java program with.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 1: Introduction to Computers and Programming.
Computer Programming Week 1: The Basics of CP 1 st semester 2012 School of Information Technology Website:
1 Sections Java Virtual Machine and Byte Code Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Lesson 2: First Java Programs. 2.1 Why Java? Java is one of the most popular programming languages in the world. Java is a modern object-oriented programming.
Programming Logic and Design Seventh Edition Chapter 1 An Overview of Computers and Programming.
2.5 Edit, Compile, and Execute Figure 2-3 illustrates the edit, compile and execute steps. Java bytecode.
Introduction to Algorithm. What is Algorithm? an algorithm is any well-defined computational procedure that takes some value, or set of values, as input.
Chapter 3 Syntax, Errors, and Debugging
Objectives You should be able to describe: Interactive Keyboard Input
Text by: Lambert and Osborne
Chapter 2 First Java Programs
Java programming lecture one
Computer Programming-1 CSC 111
Presentation transcript:

Chapter 2 First Java Programs Fundamentals of Java

2 Essential Questions What is Java? What are the choices for user interface style? What is the structure of a simple Java program. How do I write and run my first Java programs? How do I write a GUI program?

Fundamentals of Java 3 Vocabulary Applet Assignment operator Byte code DOS development environment Graphical user interface (GUI) Hacking Integrated development environment (IDE)

Fundamentals of Java 4 Vocabulary (cont.) Java virtual machine (JVM) Just-in-time compilation (JIT) Parameter Source code Statement Terminal I/O user interface Variable

Fundamentals of Java 5 Why Java? Java is: – 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 6 Why Java? (cont.) Java is well-suited for distributed, network- based applications. Java supports threads. – A process that can run concurrently with other processes Java resembles C++. – Easy for C++ programmers to learn Java (Negative) Java runs more slowly than other languages.

Fundamentals of Java 7 The Java Virtual Machine and Byte Code The Java compiler translates Java source code into Java byte code. – A pseudo-machine language Byte code is executed by the Java Virtual Machine (JVM). – Interpreter: A program that behaves like a computer – Any computer with the JVM installed can execute Java byte code (portability).

Fundamentals of Java 8 The Java Virtual Machine and Byte Code (cont.) Some JVMs support just-in-time compilation (JIT). – Translate byte code instructions into machine language when they are first encountered – When code is encountered a second time, it is executed faster Java applets: Java programs that run in a Web browserJava programs that run in a Web browser – A JVM is incorporated into the browser.

Fundamentals of Java 9 Choosing a User Interface Style 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 MS-DOS console or Unix terminal

Fundamentals of Java 10 Choosing a User Interface Style (cont.) Figure 2-1: Two user interfaces for a temperature conversion program

Fundamentals of Java 11 Hello World Figure 2-2: Hello World

Fundamentals of Java 12 Hello World (cont.) A program is a sequence of instructions to perform a task. Source code: The programming language instructions for a program

Fundamentals of Java 13 Hello World (cont.) Example 2-1: Our first program

Fundamentals of Java 14 Hello World (cont.) 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. Parameters

Fundamentals of Java 15 Hello World (cont.) Semicolons ( ; ) mark the end of each statement. – A “sentence” in a program A message may require zero, one, or multiple parameters. Format for sending messages to objects: –. ( )

Fundamentals of Java 16 Hello World (cont.) Method selector operator: The period (.) between the object’s name and the message’s name Framework for any Java program:

Fundamentals of Java 17 Edit, Compile, and Execute Figure 2-3: Editing, compiling, and running a program. Netbeans or other IDE’s allow us to edit, compile, and execute in one step.

Fundamentals of Java 18 Edit, Compile, and Execute (cont.) The file extension for a Java source code file is.java. – This is what you will upload to moodle The file extension for a Java class file is.class.

Fundamentals of Java 19 Development Environments Use UNIX or Linux using a standard text editor with command-line activation of the compiler and the JVM. DOS development environment: – 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 20 Development Environments (cont.) Integrated development environment (IDE): Combines an editor, Java compiler, debugger, and JVM in one program – Increases programmer productivity – Usually costs money – Examples: Netbeans, Eclipse, Metrowerks Code Warrior, Borland JBuilder, BlueJ, and JGrasp

Fundamentals of Java 21 Compile-Time Errors Mistakes detected by the compiler – Also called syntax errors Compiler displays errors in terminal window – Indicates file and line number where error was detected – Indicates type of error detected – May suggest solutions to error

Fundamentals of Java 22 Compile-Time Errors (cont.) Figure 2-8: Program with a compile-time error on line 6

Fundamentals of Java 23 Compile-Time Errors (cont.) Figure 2-9: Compiler’s error message

Fundamentals of Java 24 Readability The main factor affecting readability is layout. – Spacing – Indentation The compiler ignores layout. It is important for your code to be readable by others. – Your programs may be maintained by others.

Fundamentals of Java 25 Temperature Conversion Figure 2-10: User interface for the temperature conversion program

Fundamentals of Java 26 Temperature Conversion: The Source Code

Fundamentals of Java 27 Temperature Conversion: The Explanation First line of code is an import statement. – 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: Scanner reader = new Scanner(System.in);

Fundamentals of Java 28 Temperature Conversion: The Explanation (cont.) In general, instantiation takes the form: – SomeClass someObject = new SomeClass(some parameters); 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.

Fundamentals of Java 29 Temperature Conversion: The Explanation (cont.) Assignment operator: The symbol = – Used to assign a value to a variable – Examples: fahrenheit = reader.nextDouble(); celsius = (fahrenheit ) * 5.0/9.0; Assignment statements: Statements using the assignment operator

Fundamentals of Java 30 Temperature Conversion: The Explanation (cont.) Mathematical operators: – + : Addition – - : Subtraction – * : Multiplication – / : Division System.out.println() can be used to print values of variables. – System.out.println(fahrenheit);

Fundamentals of Java 31 Temperature Conversion: Variables and Objects Variables fahrenheit and celsius each hold a single floating-point number. Variables reader and System.out hold references to objects. – References used to send messages to the objects

Fundamentals of Java 32 Temperature Conversion: Variables and Objects (cont.) Figure 2-11: Variables and objects used in the conversion program fahrenheit = reader.nextDouble(); Variable reference to scanner object message to scanner object system.out.println(celsuis); Variable reference to output object message to output object

Fundamentals of Java 33 Graphics and GUIs: Windows and Panels Standalone GUI applications run in windows. – Containers for graphical components to be displayed to the user Windows have numerous properties. – Width and height – Title bar – Ability to be dragged or resized by the user

Fundamentals of Java 34 Graphics and GUIs: Windows and Panels (cont.) Example 2-3: Empty frame

Fundamentals of Java 35 Graphics and GUIs: Windows and Panels (cont.) Figure 2-12: GUI program with an empty window

Practice Create your own GUI window by entering the code in example 2.3. Fundamentals of Java 36

Fundamentals of Java 37 Graphics and GUIs: Windows and Panels (cont.) Table 2-1: Some commonly used JFrame methods

Fundamentals of Java 38 Panels and Colors Panel: A flat, rectangular area suitable for displaying other objects – Geometric shapes and images – JPanel class in javax.swing package Color class: Can be used to create any RGB color value – Color aColor = new Color(redValue, greenValue, blueValue)

Fundamentals of Java 39 Panels and Colors (cont.) Table 2-2: Some Color constants

Fundamentals of Java 40 Panels and Colors (cont.) Example 2-4: Frame with an empty, pink panel

Practice Edit your GUI Window code to match Example 2.4 to display a frame with an empty pink panel. Fundamentals of Java 41

Fundamentals of Java 42 Layout Managers and Multiple Panels 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 BorderLayout Can arrange up to five graphical objects in a container – NORTH, SOUTH, EAST, WEST, and CENTER – GridLayout class: Divides a container into rows and columns

Practice Once again edit your GUI window code to match Example 2.5. Your window should now include border panels. Fundamentals of Java 43

Fundamentals of Java 44 Layout Managers and Multiple Panels (cont.) Example 2-6: Frame with a 2-by-2 grid of colored panels

Fundamentals of Java 45 Layout Managers and Multiple Panels (cont.) Figure 2-14: 2-by-2 grid layout with four panels

Practice Finally, edit your code one more time to include a 2x2 grid (or more panels if you wish). Each panel should have a color of your choosing. Fundamentals of Java 46

Fundamentals of Java 47 Summary 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 48 Summary (cont.) 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 49 Summary (cont.) 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.

Projects-Complete two of the following. Project 2-1 Project 2-2 Project 2-3 Project 2-5 Project 2-7 Fundamentals of Java 50