COS 130 Computer Programming Methodology Introduction to Java

  • Slides: 37
Download presentation
COS 130 Computer Programming Methodology Introduction to Java

COS 130 Computer Programming Methodology Introduction to Java

Sign in sheet

Sign in sheet

Algorithm “In mathematics and computing, an algorithm is a procedure (a finite set of

Algorithm “In mathematics and computing, an algorithm is a procedure (a finite set of well-defined instructions) for accomplishing some task which, given an initial state, will terminate in a defined end-state. ” Wikipedia Like a Recipe, but. . . n Strictly defines the order of steps

Program “A computer program is a list of instructions to be executed by the

Program “A computer program is a list of instructions to be executed by the computer. ” - Wikipedia • • • Programs implement algorithms Programs are executable by “machines” Programs group instructions into a unit of work

Programs Exist in 2 Forms • Source – Designed to be understood by people

Programs Exist in 2 Forms • Source – Designed to be understood by people – Letters, words • Executable – Designed to drive the computer – Binary – 1’s and 0’s

Computing a 2 Step Process Compilation Source Compiler Executable Execution Computer Data In Executable

Computing a 2 Step Process Compilation Source Compiler Executable Execution Computer Data In Executable Data Out

Java • Recent language developed by Sun • Primarily OO, procedural programming also supported

Java • Recent language developed by Sun • Primarily OO, procedural programming also supported (somewhat awkwardly) • Most “popular” computer language – Tiobe Software’s TPC index (www. tiobe. com) • Write once, run everywhere

First Java Program A name you pick Capitalization Starts a java file important Must

First Java Program A name you pick Capitalization Starts a java file important Must inside call fileclass the same name All code We will only use 1 public class Hello { public static void main(String args[]) Braces group code { They always come in pairs Special. World”); Method System. out. println(“Hello Your program starts running here } Make it just like this Weinstruction will alwaystohave one An printexactly something Statements always end in a semicolon } Your program will do this Or a brace

Literals • Do not change while program is running • Must edit and re-compile

Literals • Do not change while program is running • Must edit and re-compile program to change

Literals (cont. ) • String Literal – Group of characters (letters) – Inside quotes

Literals (cont. ) • String Literal – Group of characters (letters) – Inside quotes – “Hello World” • Character Literals – One character (letter) – Inside single quotes – Example: ‘A’ Note: ‘A’ is not the same as “A”

Literals (cont) • Integer Literals – Numbers with no decimal point – Just by

Literals (cont) • Integer Literals – Numbers with no decimal point – Just by itself – Example: 7 • Floating Point Literals – Numbers with a decimal point – Just by itself – Example: 3. 14 Note: 7 is not the same as 7. 0 Note: 7 is not the same as “ 7” Note: 7 is not the same as ‘ 7’

Variable Declarations • Sets aside space in memory • Labels the space with a

Variable Declarations • Sets aside space in memory • Labels the space with a name • Declaration sets variable for a particular type – numbers, letters, etc. • Once type is set it can not be changed • Variable must be declared before it can be used

Name rules • • • First letter: A-Z a-z Remaining letters: A-Z a-z 0

Name rules • • • First letter: A-Z a-z Remaining letters: A-Z a-z 0 -9 May be as long as you want Capitalization is important May not include spaces

int declaration int num. Students; • Declares a variable to hold an integer number

int declaration int num. Students; • Declares a variable to hold an integer number • Start variable names with a small letter • This variable will be named num. Students • It will set aside 1 32 bit word of space • It can hold values between -2, 147, 483, 648 and +2, 147, 483, 647

A Shortcut • When we create a variable we can also set it to

A Shortcut • When we create a variable we can also set it to a particular value • This is called “initialization” • Example: int max. Students = 30;

Integer Operators • • • + Addition - Subtraction * Multiplication / Division %

Integer Operators • • • + Addition - Subtraction * Multiplication / Division % Remainder

Division and Remainder What is 5 / 9 ? 0 What is 5 %

Division and Remainder What is 5 / 9 ? 0 What is 5 % 9 ? 5 What is 28 / 9 ? 3 What is 28 % 9 ? 1

Integer Expressions • Use the integer operators on int variables and literals • Operators

Integer Expressions • Use the integer operators on int variables and literals • Operators are applied left to right • Multiplication, Division, and Remainder are done before addition and subtraction What is 7 + 10 * 30 ? 307

Integer Expression (cont) • To control the order things happen use parens “(“ and

Integer Expression (cont) • To control the order things happen use parens “(“ and “)” What is (7 + 10) * 30 ? n 510 Notice that parens do not imply multiplication What is (7 + 10) 30 ?

With Variables How many seats are left in a class? max. Students - cur.

With Variables How many seats are left in a class? max. Students - cur. Enroll Where does the result go? How do we save the result? How do we use the result?

= Equal Sign?

= Equal Sign?

Assignment Operator = max. Students = 30; • Left side must be a variable

Assignment Operator = max. Students = 30; • Left side must be a variable • The value from the right is put in the spot in memory named on the left. . . 30 max. Students . . .

Example seats. Left = max. Students – cur. Enroll ; . . . 33

Example seats. Left = max. Students – cur. Enroll ; . . . 33 cur. Enroll -3. . . 30 max. Students . . . -3 seats. Left . . .

Example • How to increase a number by 1 count = count + 1;

Example • How to increase a number by 1 count = count + 1; n n This is a very common case, be sure you understand it. This is mathematical nonsense.

More Data Types • long • double • boolean

More Data Types • long • double • boolean

long • What if I need a number that is too big to fit

long • What if I need a number that is too big to fit into an int? long population; n Use pretty much like an int long pop. North. America, pop. South. America, pop. New. World; . . . pop. New. World = pop. North. America + pop. South. America; . . .

Caution • An int will fit into a long • But, a long will

Caution • An int will fit into a long • But, a long will not fit into an int long my. Long; int my. Int; . . . my. Long = my. Int + 17; // this is ok my. Int = my. Long + 1; // this will not work

What if I need a fraction? • float and double give you numbers that

What if I need a fraction? • float and double give you numbers that have places to the right of the decimal point float pi = 3. 14; Using float is a bad idea double pi = 3. 14; Use double instead!

Arithmetic with doubles double area; double pi=3. 14; double radius=7. 5; …. . area

Arithmetic with doubles double area; double pi=3. 14; double radius=7. 5; …. . area = pi * (radius * radius);

Simple conversion int my. Int=7; long my. Long=77; double my. Double=7. 77; my. Int

Simple conversion int my. Int=7; long my. Long=77; double my. Double=7. 77; my. Int = my. Long; No! my. Long = my. Int; my. Int = my. Double; No! my. Long = my. Double; No! my. Double = my. Int; my. Double = my. Long;

boolean • A variable that can be true or false boolean is. Vegetarian; is.

boolean • A variable that can be true or false boolean is. Vegetarian; is. Vegetarian = true; is. Vegetarian = false; Note: true is not the same as “true”

Variable Declarations (Review) • Sets aside space in memory • Labels the space with

Variable Declarations (Review) • Sets aside space in memory • Labels the space with a name • Declaration sets variable for a particular type – numbers, letters, etc. • Once type is set it can not be changed • Variable must be declared before it can be used

Comments • Comments are a way to stick notes to other people (and your

Comments • Comments are a way to stick notes to other people (and your self) in your program code • The compiler skips over comments • Therefore, no trace of your comments goes into the executable

Comments (cont) • Two slashes comments out the rest of the line • Example:

Comments (cont) • Two slashes comments out the rest of the line • Example: num. Students = 30; // current enrollment

Comments (cont) • A slash asterisk comments out every line until an asterisk slash

Comments (cont) • A slash asterisk comments out every line until an asterisk slash is found • Example: /* Current enrollment */ num. Students = 30;

Comments (cont) • What if we do this: /* Current enrollment num. Students =

Comments (cont) • What if we do this: /* Current enrollment num. Students = 30; */

Comments (cont) • Use comments to put your name on your programs • Use

Comments (cont) • Use comments to put your name on your programs • Use comments to identify what your program does