csc204 – programming i lecture 4 august 28, 2002

31
CSC204 – Programming I Lecture 4 August 28, 2002

Upload: rudolf-cobb

Post on 31-Dec-2015

220 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: CSC204 – Programming I Lecture 4 August 28, 2002

CSC204 – Programming I

Lecture 4August 28, 2002

Page 2: CSC204 – Programming I Lecture 4 August 28, 2002

8/28/2002 CSC 204 Programming I 2

Recap: Compilation & Execution

CompiledFile

(a.out)

Source Code

(xxx.cpp)

text filesaved on disk

editor

compile

edit compiler(g++)

Instructions inmachine languageCPU specificsaved on disk

Memory

CPU

load

Execution

Compilation

Edition

Page 3: CSC204 – Programming I Lecture 4 August 28, 2002

8/28/2002 CSC 204 Programming I 3

Recap: Interpretation & Execution

Internalfile

Source Code

(xxx.bas)

text file

editor

interpret

edit interpreter

Instructions inmachine languageCPU specific

CPU

Compilation & Execution

Memory

Edition

Page 4: CSC204 – Programming I Lecture 4 August 28, 2002

8/28/2002 CSC 204 Programming I 4

Bytecodeprogram

(xxx.class)

Source Code

(xxx.java)

text filesaved on disk

editor

compile

edit Compiler(javac)

Instructions inbytecodesaved on disk

CPU

load

Execution

Compilation

Recap: Java’s Approach

Memory

JVM

interpret

Edition

Page 5: CSC204 – Programming I Lecture 4 August 28, 2002

8/28/2002 CSC 204 Programming I 5

A Simple Java Program The following program displays the

message Java rules! on the screen.

// Saved in a file namedJavaRules.java

// Displays the message "Java rules!"

public class JavaRules { public static void main(String[] args) { System.out.println("Java rules!"); }}

Page 6: CSC204 – Programming I Lecture 4 August 28, 2002

8/28/2002 CSC 204 Programming I 6

Program Layout The JavaRules program raises a couple of

issues: Why do we put comments into programs, and

what are the rules for doing so? How should we “lay out” a program? Does it

matter where we put spaces and blank lines? Where should the curly braces go?

Page 7: CSC204 – Programming I Lecture 4 August 28, 2002

8/28/2002 CSC 204 Programming I 7

Comments Comments are an important part of

every program. They provide information that’s useful

for anyone who will need to read the program in the future.

Page 8: CSC204 – Programming I Lecture 4 August 28, 2002

8/28/2002 CSC 204 Programming I 8

Typical Uses Of Comments To document who wrote the program,

when it was written, what changes have been made to it, and so on.

To describe the behavior or purpose of a particular part of the program, such as a variable or method.

To describe how a particular task was accomplished, which algorithms were used, or what tricks were employed to get the program to work.

Page 9: CSC204 – Programming I Lecture 4 August 28, 2002

8/28/2002 CSC 204 Programming I 9

Types of Comments Single-line comments:// Comment style 1

Multiline comments:/* Comment style 2 */

“Doc” comments:/** Comment style 3 */

Doc comments are designed to be extracted by a special program, javadoc.

Page 10: CSC204 – Programming I Lecture 4 August 28, 2002

8/28/2002 CSC 204 Programming I 10

Problems with Multiline Comments Forgetting to terminate a multiline comment may

cause the compiler to ignore part of a program:

System.out.print("My "); /* forgot to close this comment...System.out.print("cat ");System.out.print("has "); /* so it ends here */System.out.println("fleas");

Page 11: CSC204 – Programming I Lecture 4 August 28, 2002

8/28/2002 CSC 204 Programming I 11

Single-line Comments Many programmers prefer // comments to /* … */ comments, for several reasons: Ease of use Safety Program readability Ability to “comment out” portions of a program

Page 12: CSC204 – Programming I Lecture 4 August 28, 2002

8/28/2002 CSC 204 Programming I 12

Java Programs in General Building blocks of a Java program:

Classes. A class is a collection of related variables and/or methods (usually both). A Java program consists of one or more classes.

Methods. A method is a series of statements. Each class may contain any number of methods.

Statements. A statement is a single command. Each method may contain any number of statements.

Page 13: CSC204 – Programming I Lecture 4 August 28, 2002

8/28/2002 CSC 204 Programming I 13

Book Application

chapters classes

sections methods

sentences

words &punctuations

statements

tokens

parts packages

paragraphs blocks/segments

Natural V.S. Java Languages

Page 14: CSC204 – Programming I Lecture 4 August 28, 2002

8/28/2002 CSC 204 Programming I 14

Tokens A Java compiler groups the characters in a program

into tokens. The compiler then puts the tokens into larger

groups (such as statements, methods, and classes). Tokens in the JavaRules program:

public class JavaRules { public static void main ( String [ ] args ) { System . out . println ( "Java rules!" ) ; } }

Page 15: CSC204 – Programming I Lecture 4 August 28, 2002

8/28/2002 CSC 204 Programming I 15

Avoiding Problems with Tokens Always leave at least one space between

tokens that would otherwise merge together:publicclassJavaRules {

Don’t put part of a token on one line and the other part on the next line:public class JavaRules {

Page 16: CSC204 – Programming I Lecture 4 August 28, 2002

8/28/2002 CSC 204 Programming I 16

Indentation Programmers use indentation to indicate

nesting. An increase in the amount of indentation

indicates an additional level of nesting. The JavaRules program consists of a

statement nested inside a method nested inside a class:

Page 17: CSC204 – Programming I Lecture 4 August 28, 2002

8/28/2002 CSC 204 Programming I 17

How Much Indentation? Common amounts of indentation:

2 spaces: the bare minimum 3 spaces: the optimal amount 4 spaces: what many programmers use 8 spaces: probably too much

The JavaRules program with an indentation of four spaces:public class JavaRules { public static void main(String[] args) { System.out.println("Java rules!"); }}

Page 18: CSC204 – Programming I Lecture 4 August 28, 2002

8/28/2002 CSC 204 Programming I 18

Brace Placement Brace placement is another important issue. One technique is to put each left curly

brace at the end of a line. The matching right curly brace is lined up with the first character on that line:public class JavaRules {

public static void main(String[] args) {

System.out.println("Java rules!");

}

}

Page 19: CSC204 – Programming I Lecture 4 August 28, 2002

8/28/2002 CSC 204 Programming I 19

Brace Placement Some programmers prefer to put left curly

braces on separate lines: This makes it easier to verify that left and right

braces match up properly. However, program files become longer

because of the additional lines.

public class JavaRules{ public static void main(String[] args) { System.out.println("Java rules!"); }}

Page 20: CSC204 – Programming I Lecture 4 August 28, 2002

8/28/2002 CSC 204 Programming I 20

Brace Placement To avoid extra lines, the line containing

the left curly brace can be combined with the following line:

public class JavaRules{ public static void main(String[] args) { System.out.println("Java rules!"); }}

In a commercial environment, issues such as indentation and brace placement are often resolved by a “coding standard”

Page 21: CSC204 – Programming I Lecture 4 August 28, 2002

8/28/2002 CSC 204 Programming I 21

Using Variables In Java, every variable must be declared

before it can be used. Declaring a variable means informing the

compiler of the variable’s name and its properties, including its type.

int is one of Java’s types. Variables of type int can store integers (whole numbers).

Page 22: CSC204 – Programming I Lecture 4 August 28, 2002

8/28/2002 CSC 204 Programming I 22

Declaring Variables Form of a variable declaration:

The type of the variable The name of the variable A semicolon

Example:int i; // Declares i to be an int variable

Several variables can be declared at a time:int i, j, k;

It’s often better to declare variables individually.

Page 23: CSC204 – Programming I Lecture 4 August 28, 2002

8/28/2002 CSC 204 Programming I 23

Initializing Variables A variable is given a value by using =, the

assignment operator:i = 0;

Initializing a variable means to assign a value to the variable for the first time.

Variables always need to be initialized before the first time their value is used.

The Java compiler checks that variables declared in methods are initialized prior to their first use.

Page 24: CSC204 – Programming I Lecture 4 August 28, 2002

8/28/2002 CSC 204 Programming I 24

Initializers Variables can be initialized at the time

they’re declared:int i = 0;

0 is said to be the initializer for i. If several variables are declared at the

same time, each variable can have its own initializer:int i = 0, j, k = 1;

Page 25: CSC204 – Programming I Lecture 4 August 28, 2002

8/28/2002 CSC 204 Programming I 25

Changing the Value of a Variable The assignment operator can be used both

to initialize a variable and to change the value of the variable later in the program:

i = 1; // Value of i is now 1…i = 2; // Value of i is now 2

Page 26: CSC204 – Programming I Lecture 4 August 28, 2002

8/28/2002 CSC 204 Programming I 26

Program: Printing a Lottery Number

Lottery.java

// Displays the winning lottery number

public class Lottery {

public static void main(String[] args) {

int winningNumber = 973;

System.out.print("The winning number ");

System.out.print("in today's lottery is ");

System.out.println(winningNumber);

}

}

Page 27: CSC204 – Programming I Lecture 4 August 28, 2002

8/28/2002 CSC 204 Programming I 27

Types A partial list of Java types:int — An integerdouble — A floating-point numberboolean — Either true or falsechar — A character

Declarations of double, boolean, and char variables:double x, y;boolean b; char ch;

Page 28: CSC204 – Programming I Lecture 4 August 28, 2002

8/28/2002 CSC 204 Programming I 28

Literals A literal is a token that represents a

particular number or other value. Examples of int literals: 0 297 30303 Examples of double literals:

48.0 48. 4.8e1 4.8e+1 .48e2 480e-1

The only boolean literals are true and false.

char literals are enclosed within single quotes:'a' 'z' 'A' 'Z' '0' '9' '%' '.' ' '

Page 29: CSC204 – Programming I Lecture 4 August 28, 2002

8/28/2002 CSC 204 Programming I 29

Using Literals as Initializers Literals are often used as initializers:double x = 0.0, y = 1.0;

boolean b = true;

char ch = 'f';

Page 30: CSC204 – Programming I Lecture 4 August 28, 2002

8/28/2002 CSC 204 Programming I 30

Conventions A rule that we agree to follow, even though

it’s not required by the language, is said to be a convention.

A common Java convention is beginning a class name with an uppercase letter:Color

FontMetrics

String

Names of variables and methods, by convention, never start with an uppercase letter.

Page 31: CSC204 – Programming I Lecture 4 August 28, 2002

8/28/2002 CSC 204 Programming I 31

Keywords The following keywords can’t be used as

identifiers: abstract double int super boolean else interface switch break extends long synchronized byte final native this case finally new throw catch float package throws char for private transient class goto protected try const if public void continue implements return volatile default import short while do instanceof static

null, true, and false are also reserved.