©2004 brooks/cole chapter 1: getting started sections covered: 1.1introduction to programming...

34
©2004 Brooks/Cole Chapter 1: Getting Started Sections Covered: 1.1 Introduction to Programming 1.2 Constructing a Java Program 1.3 The print() and println() Methods 1.4 Programming Style 1.5 Creating a Dialog Box

Post on 20-Dec-2015

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: ©2004 Brooks/Cole Chapter 1: Getting Started Sections Covered: 1.1Introduction to Programming 1.2Constructing a Java Program 1.3The print() and println()

©2004 Brooks/Cole

Chapter 1: Getting Started

Sections Covered:1.1 Introduction to Programming1.2 Constructing a Java Program1.3 The print() and println() Methods1.4 Programming Style1.5 Creating a Dialog Box

Page 2: ©2004 Brooks/Cole Chapter 1: Getting Started Sections Covered: 1.1Introduction to Programming 1.2Constructing a Java Program 1.3The print() and println()

Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005

Programming

• Computer hardware by itself doesn't do anything

• Software in the form of a program tells the computer what to do– Low-level programs are written in the

processor's language of 0s and 1s– High-level programming languages are easier

for people to understand

Page 3: ©2004 Brooks/Cole Chapter 1: Getting Started Sections Covered: 1.1Introduction to Programming 1.2Constructing a Java Program 1.3The print() and println()

Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005

Programs process data

A typical program does three things1. Get some input data2. Transform the input data to produce a result3. Output the result

Page 4: ©2004 Brooks/Cole Chapter 1: Getting Started Sections Covered: 1.1Introduction to Programming 1.2Constructing a Java Program 1.3The print() and println()

Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005

The Two Distinct Java Environments

Page 5: ©2004 Brooks/Cole Chapter 1: Getting Started Sections Covered: 1.1Introduction to Programming 1.2Constructing a Java Program 1.3The print() and println()

Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005

Sample java Programs

• DisplayHelloWorld.java– A console application

• HelloDisplay.java– Graphical (windowed) applications

• HelloApplet.java– An Applet is a program that is run by a web

browser

Page 6: ©2004 Brooks/Cole Chapter 1: Getting Started Sections Covered: 1.1Introduction to Programming 1.2Constructing a Java Program 1.3The print() and println()

Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005

Algorithms

• Before you can tell the computer what to do, you have to figure out how to do what needs to be done.

• An algorithm is a step-by-step procedure describing what needs to be done.

• Two ways to describe an algorithm– Pseudocode - use words to describe the process– Flowcharts

Page 7: ©2004 Brooks/Cole Chapter 1: Getting Started Sections Covered: 1.1Introduction to Programming 1.2Constructing a Java Program 1.3The print() and println()

Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005

Algorithms for

Summing the Numbers from1 through 100

Page 8: ©2004 Brooks/Cole Chapter 1: Getting Started Sections Covered: 1.1Introduction to Programming 1.2Constructing a Java Program 1.3The print() and println()

Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005

•Flowchart Symbols

Page 9: ©2004 Brooks/Cole Chapter 1: Getting Started Sections Covered: 1.1Introduction to Programming 1.2Constructing a Java Program 1.3The print() and println()

Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005

Flowchart for Calculating the Average

of Three Numbers

Page 10: ©2004 Brooks/Cole Chapter 1: Getting Started Sections Covered: 1.1Introduction to Programming 1.2Constructing a Java Program 1.3The print() and println()

Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005

A Simple Paint-by-Number Figure

Page 11: ©2004 Brooks/Cole Chapter 1: Getting Started Sections Covered: 1.1Introduction to Programming 1.2Constructing a Java Program 1.3The print() and println()

Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005

Coding an Algorithm

Once you have a correct algorithm, writing the program is easy.

Page 12: ©2004 Brooks/Cole Chapter 1: Getting Started Sections Covered: 1.1Introduction to Programming 1.2Constructing a Java Program 1.3The print() and println()

Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005

Object-Oriented Programming

• Java is an object-oriented language

• In object-oriented programs we use classes and objects to help organize our program

• Objects have– Data– Behavior

• A class defines what the data and behavior should be for a particular kind of object

Page 13: ©2004 Brooks/Cole Chapter 1: Getting Started Sections Covered: 1.1Introduction to Programming 1.2Constructing a Java Program 1.3The print() and println()

Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005

Using UML Diagrams

• Pictures can be easier to understand than large amounts of text

• UML is a diagram language for describing object-oriented programs

• There are many different kinds of diagrams– We'll use only a few of them

Page 14: ©2004 Brooks/Cole Chapter 1: Getting Started Sections Covered: 1.1Introduction to Programming 1.2Constructing a Java Program 1.3The print() and println()

Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005

The Rectangle Class

• Suppose we have a class to represent a rectangle.– The properties of a rectangle

are the length and width– From these we can determine

the area or the perimeter– We should be able to change

the properties– We might want to dispplay the

properties

Page 15: ©2004 Brooks/Cole Chapter 1: Getting Started Sections Covered: 1.1Introduction to Programming 1.2Constructing a Java Program 1.3The print() and println()

Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005

A Set of Rectangle Objects

Once the Rectangle class is defined, you can create rectangle objects

Each Rectangle has its own value of length and width

Page 16: ©2004 Brooks/Cole Chapter 1: Getting Started Sections Covered: 1.1Introduction to Programming 1.2Constructing a Java Program 1.3The print() and println()

Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005

Sending a Message to a Rectangle Object

We can send a message to a Rectangle object

In this case, the object replies to our message by sending back the area

Page 17: ©2004 Brooks/Cole Chapter 1: Getting Started Sections Covered: 1.1Introduction to Programming 1.2Constructing a Java Program 1.3The print() and println()

Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005

The Traditional Translation Process

From code to executable

•Program code is written in a high-level language•A processor understands binary code (0s and 1s)•A program needs to be translated from high-level code to the low-level code that the computer understands

Page 18: ©2004 Brooks/Cole Chapter 1: Getting Started Sections Covered: 1.1Introduction to Programming 1.2Constructing a Java Program 1.3The print() and println()

Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005

Compiling and Executing a Java

Program

Java works a little differently1. Convert high-level code to byte-code2. Use another program to execute the

byte-code

Under Linux1. javac converts source code into byte

code• Result has extension .class

2. java executes the byte-code file

Page 19: ©2004 Brooks/Cole Chapter 1: Getting Started Sections Covered: 1.1Introduction to Programming 1.2Constructing a Java Program 1.3The print() and println()

Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005

Constructing a Java Program

• Every Java program consists of one or more classes

• One class must be a main class– The main class must contain a method called

main

Page 20: ©2004 Brooks/Cole Chapter 1: Getting Started Sections Covered: 1.1Introduction to Programming 1.2Constructing a Java Program 1.3The print() and println()

Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005

A Simple Java Program

Page 21: ©2004 Brooks/Cole Chapter 1: Getting Started Sections Covered: 1.1Introduction to Programming 1.2Constructing a Java Program 1.3The print() and println()

Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005

Class Header

public class Hello

• public - modifier specifying visibility of the class

• class - keyword that is part of the Java syntax for creating a class

• Hello - an identifier used to name the class

Page 22: ©2004 Brooks/Cole Chapter 1: Getting Started Sections Covered: 1.1Introduction to Programming 1.2Constructing a Java Program 1.3The print() and println()

Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005

Method Header

public static void main( String[] args)

• public - modifier specifying visibility of the method

• static - modifier specifying class method• void - keyword specifying no return value• main - an identifier used to name the method• (String[] args) - parameter list

Page 23: ©2004 Brooks/Cole Chapter 1: Getting Started Sections Covered: 1.1Introduction to Programming 1.2Constructing a Java Program 1.3The print() and println()

Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005

A Well-Designed ProgramIs Built Using Modules

Page 24: ©2004 Brooks/Cole Chapter 1: Getting Started Sections Covered: 1.1Introduction to Programming 1.2Constructing a Java Program 1.3The print() and println()

Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005

Modules

• We use classes to construct modules in Java

• Within a class, methods can be used to modularize functionality– If you have some operation that is done in

several different places, make a method

Page 25: ©2004 Brooks/Cole Chapter 1: Getting Started Sections Covered: 1.1Introduction to Programming 1.2Constructing a Java Program 1.3The print() and println()

Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005

Keywords and Identifiers

• Keywords are words that have special meaning in the java language– Keywords are reserved; they can't be used as names

– Keywords need to appear in the correct context

• Identifiers are used for naming things– Objects, classes, methods all need names

– Rules for naming• First character must be letter or underscore (_)

• Remaining characters can be letters, digits or underscore

Page 26: ©2004 Brooks/Cole Chapter 1: Getting Started Sections Covered: 1.1Introduction to Programming 1.2Constructing a Java Program 1.3The print() and println()

Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005

Syntax

• Syntax is the rules that specify how the words in your program can be arranged.

• Programming language syntax is simpler that natural language syntax.– Because it has to be translated by a program -

programs are less flexible than people

Page 27: ©2004 Brooks/Cole Chapter 1: Getting Started Sections Covered: 1.1Introduction to Programming 1.2Constructing a Java Program 1.3The print() and println()

Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005

Output to the Console

• Most of our programs will be console applications– Output appears on the console

• The System class has an object called out that you use to send output to the console– out is a PrintStream object

– PrintStream has two methods that send text to the console

• print( text)

• println( text)

Page 28: ©2004 Brooks/Cole Chapter 1: Getting Started Sections Covered: 1.1Introduction to Programming 1.2Constructing a Java Program 1.3The print() and println()

Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005

print and println

• Text output with print starts at the current cursor position and continues until the text ends

• Text output with println start starts at the current cursor position and continues until the text ends and then appends a newline– Next output will appear at the beginning of the

next line.

Page 29: ©2004 Brooks/Cole Chapter 1: Getting Started Sections Covered: 1.1Introduction to Programming 1.2Constructing a Java Program 1.3The print() and println()

Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005

Escape sequences

• What if you need to make a single String extend over more than one line?– Text enclosed in double quotes needs to fit on a single

line in your program. Typing return breaks it onto two lines.

• The backslash (\) character is used for representing characters that you can't just type.– \n is the newline character– \" allows you to put double quotes in a double-quoted

string– \\ allows you to put a backslash in your string

Page 30: ©2004 Brooks/Cole Chapter 1: Getting Started Sections Covered: 1.1Introduction to Programming 1.2Constructing a Java Program 1.3The print() and println()

Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005

Programming Style

• The java compiler doesn't care how many lines you use for your program statements

• The people who have to read your code will find it easier if you format it nicely.

• Look at the examples in the book for one common formatting style

• Sun has a recommended style for Java programs

Page 31: ©2004 Brooks/Cole Chapter 1: Getting Started Sections Covered: 1.1Introduction to Programming 1.2Constructing a Java Program 1.3The print() and println()

Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005

Comments

• We often need to put text into a program to help explain the code

– We call this kind of text comments– We want the compiler to ignore comments

• Java has three types of comment1. // starts a one-line comment2. /* … */ encloses multiline comments3. /** … */ encloses javadoc comments

Page 32: ©2004 Brooks/Cole Chapter 1: Getting Started Sections Covered: 1.1Introduction to Programming 1.2Constructing a Java Program 1.3The print() and println()

Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005

Dialog Boxes

• The JOptionPane class has several class methods for creating common dialog boxes.

• showMessageDialog is used for providing information to the user of a GUI program

• Creating GUI programs entirely out of dialog boxes is rather tedious for the user.

Page 33: ©2004 Brooks/Cole Chapter 1: Getting Started Sections Covered: 1.1Introduction to Programming 1.2Constructing a Java Program 1.3The print() and println()

Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005

showMessageDialog() Dialog Boxes

WARNING_MESSAGE QUESTION_MESSAGE

PLAIN_MESSAGE

ERROR_MESSAGEINFORMATION_MESSAGE

Page 34: ©2004 Brooks/Cole Chapter 1: Getting Started Sections Covered: 1.1Introduction to Programming 1.2Constructing a Java Program 1.3The print() and println()

Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005

The Dialog Produced by MultiLineDialog.java