Chapter 2 First Java Programs

Similar documents
Lesson 2: First Java Programs

Full file at

1. Java is the fastest growing programming language in the world.

Chapter 3 Syntax, Errors, and Debugging. Fundamentals of Java

Introduction to JAVA Programming Language

Command-Line Applications. GUI Libraries GUI-related classes are defined primarily in the java.awt and the javax.swing packages.

What is Widget Layout? Laying Out Components. Resizing a Window. Hierarchical Widget Layout. Interior Design for GUIs

C++ Spring Break Packet 11 The Java Programming Language

Full file at

Introduction to Computers and Java

CS 315 Software Design Homework 1 First Sip of Java Due: Sept. 10, 11:30 PM

Java Programming Lecture 6

Chapter 1: Introduction to Computers and Java

8/23/2014. Chapter Topics. Introduction. Java History. Why Program? Java Applications and Applets. Chapter 1: Introduction to Computers and Java

Laying Out Components. What is Widget Layout?

Introduction to OOP Using Java Pearson Education, Inc. All rights reserved.

9/11/08 (c) 2008 Matthew J. Rutherford Class (c) 2008 Matthew J. Rutherford Class

Introduction to Computers and Java

CHAPTER 1 Introduction to Computers and Java

USING THE OOSIML/JAVA COMPILER. With the Command Window

Learning objectives. The Java Environment. Java timeline (cont d) Java timeline. Understand the basic features of Java

Introduction to Computers and Java

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

Outline. Introduction to Java. What Is Java? History. Java 2 Platform. Java 2 Platform Standard Edition. Introduction Java 2 Platform

Introduction to Computers and Java. Objectives. Outline. Harald Gall, Prof. Dr. Institut für Informatik Universität Zürich.

Introduction to Computers and Java

Goals. Java - An Introduction. Java is Compiled and Interpreted. Architecture Neutral & Portable. Compiled Languages. Introduction to Java

Chapter 1. Introduction

CSI Lab 02. Tuesday, January 21st

CMSC 150 LECTURE 1 INTRODUCTION TO COURSE COMPUTER SCIENCE HELLO WORLD

Introduction to Java Programming

Introduction to Computers and Java

Introduction to Computers and Java. Objectives. Outline. Harald Gall, Prof. Dr. Institut für Informatik Universität Zürich.

CT 229. CT229 Lecture Notes. Labs. Tutorials. Lecture Notes. Programming II CT229. Objectives for CT229. IT Department NUI Galway

CHAPTER 1. Introduction to JAVA Programming

Identifiers. Identifiers are the words a programmer uses in a program Some identifiers are already defined. Some are made up by the programmer:

Introduction Basic elements of Java

Objects and Classes Lecture 1

COSC 123 Computer Creativity. Introduction to Java. Dr. Ramon Lawrence University of British Columbia Okanagan

Introduction to Java

Introduction to Java. Lecture 1 COP 3252 Summer May 16, 2017

Getting Started (1.8.7) 9/2/2009

Which statement regarding computer programs is correct? Which statement best describes a computer program?

Programming with Java

Resizing a Window. COSC 3461: Module 5B. What is Widget Layout? Size State Transitions. What is Widget Layout? Hierarchical Widget Layout.

USING THE OOSIML/JAVA. With a Terminal Window

Lesson 01 Introduction

Outline. Topic 9: Swing. GUIs Up to now: line-by-line programs: computer displays text user types text AWT. A. Basics

2 Getting Started. Getting Started (v1.8.6) 3/5/2007

IT151: Introduction to Programming (java)

Chapter 1 Introduction to Computers, Programs, and Java. What is a Computer? A Bit of History

Pace University. Fundamental Concepts of CS121 1

Computer Hardware. Java Software Solutions Lewis & Loftus. Key Hardware Components 12/17/2013

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 2: SEP. 8TH INSTRUCTOR: JIAYIN WANG

COMP 110 Project 1 Programming Project Warm-Up Exercise

Chapter 12 Advanced GUIs and Graphics

Objectives. Problem Solving. Introduction. An overview of object-oriented concepts. Programming and programming languages An introduction to Java

CPS109 Lab 1. i. To become familiar with the Ryerson Computer Science laboratory environment.

Sri Vidya College of Engineering & Technology

Chapter 1 - Introduction. Copyright 2014 by John Wiley & Sons. All rights reserved. 1

Topic 9: Swing. Swing is a BIG library Goal: cover basics give you concepts & tools for learning more

Topic 9: Swing. Why are we studying Swing? GUIs Up to now: line-by-line programs: computer displays text user types text. Outline. 1. Useful & fun!

GUI Programming. Chapter. A Fresh Graduate s Guide to Software Development Tools and Technologies

ICOM 4015: Advanced Programming

An Introduction to Software Engineering. David Greenstein Monta Vista High School

Programming. Syntax and Semantics

What is Widget Layout? COSC 3461 User Interfaces. Hierarchical Widget Layout. Resizing a Window. Module 5 Laying Out Components

SELF-STUDY. Glossary

Working with JavaScript

Introduction to Software Development (ISD) David Weston and Igor Razgon

Tutorial 1 CSC 201. Java Programming Concepts عؾادئماظربجمةمبادؿكدامماجلاصا

Eclipse Tutorial. For Introduction to Java Programming By Y. Daniel Liang

Getting Started with Java. Atul Prakash

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Java Programming. Manuel Oriol, March 22nd, 2007

2 rd class Department of Programming. OOP with Java Programming

Excel Second Edition.

EXPERIMENT 1. FAMILIARITY WITH DEBUG, x86 REGISTERS and MACHINE INSTRUCTIONS

4. Structure of a C++ program

Eclipse. JVM, main method and using Eclipse. Dr. Siobhán Drohan. Produced by: Department of Computing and Mathematics

The Command Shell. Fundamentals of Computer Science

Chapter. Focus of the Course. Object-Oriented Software Development. program design, implementation, and testing

AP Computer Science A Summer Assignment

You should now start on Chapter 4. Chapter 4 introduces the following concepts

Introduction to Computers and Java

All the Swing components start with J. The hierarchy diagram is shown below. JComponent is the base class.

COMP 202 Java in one week

Chapter 4 Introduction to Control Statements

Java Programming Constructs Java Programming 2 Lesson 1

Course Outline. Introduction to java

Graphic User Interfaces. - GUI concepts - Swing - AWT

Lecture Set 2: Starting Java

Geographic Information Systems (GIS) - Hardware and software in GIS

COT 3530: Data Structures. Giri Narasimhan. ECS 389; Phone: x3748

Chapter 2: Programming Concepts

CSCI 311 WEB ACCESSIBILITY

Tcl/Tk for XSPECT a Michael Flynn

Lecture Set 2: Starting Java

Computer Hardware and Software Introduction. Mr. Smith AP Computer Science A

Today. cisc3120-fall2012-parsons-lectiii.3 2

Transcription:

First Java Programs Fundamentals of Java: AP Computer Science Essentials, 4th Edition 1

Objectives Why is Java an important programming language? What is the Java virtual machine and byte code? What are the user interface style options? What is the structure of a simple Java program? How do you write a simple program? 2

Objectives (continued) How do you edit, compile, and run a program using a Java development environment? How do you format a program to give a pleasing, consistent appearance? What are compile-time errors? How do you write a simple graphics program? 3

Vocabulary 4 applet assignment operator byte code DOS development environment graphical user interface (GUI) hacking import statement integrated development environment (IDE) interpreter Java virtual machine (JVM)

Vocabulary (continued) just-in-time compilation (JIT) panel panes parameter source code statement terminal I/O user interface variable 5

Why Java? Java is the fastest growing programming language in the world. Sun, IBM use Java to develop applications. Java is a modern object-oriented programming language. 6

Why Java? (continued) Java is ideal for distributed, network-based applications. 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. 7

Why Java? (continued) Java supports advanced programming concepts. Thread: A process that can run concurrently with other processes. Java resembles C++. Easy for a C++ programmer to learn Java. Java does run more slowly than other languages because it is interpreted. 8

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

The Java Virtual Machine and Byte Code (continued) JVMs are getting faster. Using JIT (just-in-time) compilations, which translate byte code into machine language. Any computer can run an interpreter. 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. 10

Choosing a User Interface Style Two user interfaces for a temperature conversion program Graphical user interface (GUI) Terminal I/O user interface 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

Hello World Hello World is traditionally the first program in a textbook. 13 Hello world program executed

Hello World (continued) The Source Code: The bulk of the instructions of a program. 14

Hello World (continued) 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.

Hello World (continued) The Larger Framework: The program must be embedded in several lines of code, such as: 16 Program comments are in green, reserved words in blue, and code in black.

Edit, Compile, and Execute Edit 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. 17

Edit, Compile, and Execute (continued) Execute The programmer instructs the JVM to load the byte code into memory and execute. The user and program can now interact. 18

Edit, Compile, and Execute (continued) Editing, compiling, and running a program 19

Edit, Compile, and Execute (continued) Development Environments: Unix or Linux Standard text editor Free Microsoft Windows Notepad and DOS window Free Integrated development environment (IDE) BlueJ, Eclipse, or JGrasp Not free, but combines editor, compiler, debugger, and JVM 20

Edit, Compile, and Execute (continued) Preparing Your Development Environment: 1. Create the directory, open a terminal window, and use the cd command. 2. Open Notepad, create the file HelloWorld.java, then type the code. 3. Save the file, switch back to the terminal window, and compile the program. 4. Run the program. 21

Edit, Compile, and Execute (continued) The program as typed into Notepad 22

Edit, Compile, and Execute (continued) 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

Edit, Compile, and Execute (continued) Readability: Programs may be maintained by other people. Layout affects readability. Use indentation, blank lines, and spaces. 24

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 -. 25

Temperature Conversion (continued) Variables and objects used in the conversion program 26

Graphics and GUIs: Windows and Panels A Simple Application Window: Graphics and GUI programs in Java can be stand- alone applications or applets. Consistent features: 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. 27

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

Graphics and GUIs: Windows and Panels (continued) 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 29

Graphics and GUIs: Windows and Panels (continued) 30 Layout Managers and Multiple Panels: Each container object uses a layout manager to control panel placement. BorderLayout class allows arrangement of up to five objects. North, south, east, west, center GridLayout uses rows and columns to arrange objects.

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. 31

Summary (continued) 32 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.

Summary (continued) 33 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.