COMP 110 Designing Programs Luv Kohli August 27

  • Slides: 27
Download presentation
COMP 110 Designing Programs Luv Kohli August 27, 2008 MWF 2 -2: 50 pm

COMP 110 Designing Programs Luv Kohli August 27, 2008 MWF 2 -2: 50 pm Sitterson 014 1

Announcements Office Hours ◦ After class today ◦ Come if you don’t have j.

Announcements Office Hours ◦ After class today ◦ Come if you don’t have j. GRASP working 2

Questions? 3

Questions? 3

Today in COMP 110 Writing algorithms in pseudocode Variables and primitive types

Today in COMP 110 Writing algorithms in pseudocode Variables and primitive types

Fun exercise time 5

Fun exercise time 5

Algorithm A set of instructions for solving a problem 6

Algorithm A set of instructions for solving a problem 6

Pseudocode combination of code and English used to express an algorithm before writing algorithm

Pseudocode combination of code and English used to express an algorithm before writing algorithm into code 7

Variables Used to store data in a program The data currently in a variable

Variables Used to store data in a program The data currently in a variable is its value Name of variable is an identifier Can change value throughout program Choose variable names that are meaningful!

Variables and Memory A variable corresponds to a location in memory variable n 1

Variables and Memory A variable corresponds to a location in memory variable n 1 • Use this cell to store the value of n 1 main memory • Prevent this cell from being used by other variables later

How to use variables Declare a variable Assign a value to the variable Change

How to use variables Declare a variable Assign a value to the variable Change the value of the variable

Variable Declarations Syntax: ◦ Type Variable_1, Variable_2, …; Examples: ◦ int count, score, my.

Variable Declarations Syntax: ◦ Type Variable_1, Variable_2, …; Examples: ◦ int count, score, my. Int; ◦ char letter; ◦ double total. Cost, ratio; ◦ int fjiofeu; // not a useful name! 11

How to name a variable Letters, digits (0 -9), underscore (_) First character cannot

How to name a variable Letters, digits (0 -9), underscore (_) First character cannot be a digit Java is case sensitive Legal names ◦ pink. Floyd, the_coup, b 3 atles Illegal names ◦ michael. bolton, kenny-G, 1 CP 12

Keywords Reserved words with predefined meanings You cannot name your variables keywords if, else,

Keywords Reserved words with predefined meanings You cannot name your variables keywords if, else, return, new See Appendix 1 in the textbook 13

Type What kind of value the variable can hold. Two kinds of types. ◦

Type What kind of value the variable can hold. Two kinds of types. ◦ Primitive type - indecomposable values Names begin with lowercase letters int, double, char, boolean See section 2. 1 for a full list ◦ Class type - objects with both data and methods Names begin with uppercase letter Scanner, String 14

Primitive Types Integer (byte, short, int, long) ◦ 0, -3, 5, 43 Floating-point number

Primitive Types Integer (byte, short, int, long) ◦ 0, -3, 5, 43 Floating-point number (float, double) ◦ 0. 5, 12. 4863, -4. 3 Characters (char) ◦ A, r, %, T Boolean (boolean) ◦ true, false 15

Primitive Types: small to big

Primitive Types: small to big

Variables and Memory When declaring a variable, a certain amount of memory is assigned

Variables and Memory When declaring a variable, a certain amount of memory is assigned based on the declared primitive type int age; double length; char letter; main memory

Assignment Statements Change a variable’s value Syntax: ◦ variable = expression; Example: ◦ sleep.

Assignment Statements Change a variable’s value Syntax: ◦ variable = expression; Example: ◦ sleep. Needed = 8; ◦ sleep. Desired = sleep. Needed * 2;

Behind the statement variable = expression; ◦ CPU calculates the value of the expression.

Behind the statement variable = expression; ◦ CPU calculates the value of the expression. ◦ Send the value to the location of variable. sleep. Desired = sleep. Needed * 2; ◦ Calculate sleep. Needed * 2 Get the current value of sleep. Needed from its memory location ◦ Assign the value to the location of sleep. Desired

Specialized Assignment Operators length *= 5; // is the same as length = length

Specialized Assignment Operators length *= 5; // is the same as length = length * 5; age ++; // is the same as age = age + 1;

Assignment compatibilities Usually, we need to put values of a certain type into variables

Assignment compatibilities Usually, we need to put values of a certain type into variables of the same type However, in some cases, the value will automatically be converted when types are different int age; age = 10; double length; length = age ;

Assignment Compatibilities byte->short->int->long->float->double ◦ my. Short my. Int; ◦ my. Byte my. Long; ◦

Assignment Compatibilities byte->short->int->long->float->double ◦ my. Short my. Int; ◦ my. Byte my. Long; ◦ my. Float my. Byte; ◦ my. Long my. Int; 22

Type Casting You can ask the computer to change the type of values which

Type Casting You can ask the computer to change the type of values which are against the compatibility. my. Float = my. Double; my. Byte = my. Int; my. Short = my. Float; my. Float = (float)my. Double; my. Byte = (byte)my. Int; my. Short = (short)my. Float; 23

Arithmetic Operators Unary operators (more info later) ◦ +, -, ++, --, ! Binary

Arithmetic Operators Unary operators (more info later) ◦ +, -, ++, --, ! Binary arithmetic operators ◦ *, /, %, +, rate*rate + delta 1/(time + 3*mass) (a - 7)/(t + 9*v) 24

Modular Arithmetic - % “clock arithmetic” ◦ Minutes on a clock are mod 60

Modular Arithmetic - % “clock arithmetic” ◦ Minutes on a clock are mod 60 Remainder 7 % 3 = 1 (7 / 3 = 2, remainder 1) 8 % 3 = 2 (8 / 3 = 2, remainder 2) 9 % 3 = 0 (9 / 3 = 3, remainder 0) 25

Homework Program 1 is on the web page We will look at code in

Homework Program 1 is on the web page We will look at code in recitation that will help you

Friday Recitation (bring charged laptop and textbook) Lab 1 Programming help for Program 1

Friday Recitation (bring charged laptop and textbook) Lab 1 Programming help for Program 1 27