Title: COMP 110 Algorithm and Pseudocode
1COMP 110Algorithm and Pseudocode
- Feng Pan
- August 27, 2008
- MWF 1000 am - 1050 am
- FB 007
1
2Algorithm Pseudocode
- Algorithm
- A set of instructions for solving a problem.
- Pseudocode
- A way to express the algorithm.
- Java Pseudocode use any convenient combination
of Java and English to express an algorithm. - Must be accurate, specific and feasible
3Sample Java Program p.23
Java Code
Pseudocode
- import java.util. public class
FirstProgram public static void
main(String args)
System.out.println("Hello out there.")
System.out.println("I will add two numbers.")
System.out.println("Enter two whole
numbers") int n1, n2
Scanner keyboard new
Scanner(System.in) n1
keyboard.nextInt() n2
keyboard.nextInt()
System.out.println("The sum is")
System.out.println(n1 n2)
Print Hello out there on the screen. Print I
will add two numbers. on the screen. Print
Enter two whole numbers on the screen. Read a
number from the keyboard input, save to variable
n1. Read a number from the keyboard input, save
to variable n2. Print The sum is on the
screen. Print the sum of n1 and n2 on the screen.
3
4Exercise Tower of Hanoi
- Rules
- Move one disk at a time.
- Never place a big disk onto a small one.
5COMP 110Variables, Primitive Types, Expressions
and Operators
- Feng Pan
- August 27, 2008
- MWF 1000 am - 1050 am
- FB 007
5
6Java Basic
- Variables
- Primitive Types
- Operators
- Expressions
6
7Variables
- Used to store data in program.
- The data currently in a variable is its value.
- Name of variable is an identifier.
- Can change value throughout program.
7
8Variable Memory
- A variable corresponds to a location in the main
memory.
- variable n1
- Use this cell to store the value of n1
- Prevent this cell from being used by other
variables later.
main memory
9Use variables in your program
- In order to use a variable
- Declare a variable
- Assign a value to the variable
- Change the value of the variable
- Free the variable (optional)
10Variable Declarations
- Syntax
- Type Variable_1, Variable_2,
- Examples
- int count, score
- char letter
- double totalCost, ratio
- int wdsdf (not helpful)
10
11How to name a variable
- Letters, digits(0-9), underscore (_)
- First character cannot be a digit
- Java is case sensitive
- Legal names
- pinkFloyd, the_coup, b3atles
- Illegal names
- michael.bolton, kenny-G, 1CP
11
12Keywords
- Reserved words with predefined meanings
- You cannot name your variables with keywords
- if, else, return, new
- see Appendix 1 in the text book
12
13Type
- 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 display 2.2, p 53 for full list
- Class type - objects with both data and methods
- Name begins with uppercase letter
- Scanner, String
13
14Primitive 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
14
15Primitive Types small to big
16Declare Variable Memory
- When declare a variable, a certain amount of
memory is assigned based on the declared
primitive type.
int age
double length
char letter1
17Assignment Statement
- Give a variable a value or change its value
- Syntax
- Variable Expression
- Example
- age 20
- length length 10.1
18Behind the statement
- Variable Expression
- CPU calculates the value of the Expression.
- Send the value to the location of the Variable.
- length length 10.1
- Calculate length 10.1.
- Get the current value of length from its memory
location. - Assign the value to the location of length.
19Example
- int age
- int score
- age 2
- score 5
- score score ( score age )
- score ? 49, 35 ?
20Specialized Assignment Operators
- length 5 // is the same as
- length length 5
- age // is the same as
- age age 1
20
21Assignment Compatibilities
- Usually, we assign value of a type to a variable
of the same type. - But, you can assign a value of a type to a
variable of any type that is on the outside. - Computer converts the type by default.
- int age
- age 10
- double length
- length age
21
22Assignment Compatibilities
- byte-gtshort-gtint-gtlong-gtfloat-gtdouble
- myShort ? myInt
- myByte ? myLong
- myFloat ? mybyte
- myLong ? myInt
22
23Type Casting
- You can ask the computer to change the type of
values which are against the compatibility. - myFloat myDouble
- myByte myInt
- myShort myFloat
- myFloat (float)myDouble
- myByte (byte)myInt
- myShort (short)myFloat
23
24Arithmetic Operators
- Unary operators has only one argument
- , -, , --, !
- balance - cost
- status ! test
- For boolean variable, if test is true, then
status is false - age --
- age age -1
24
25Arithmetic Operators
- Binary arithmetic operators
- , /, , , -
- raterate delta
- 1/(time 3mass)
- (a - 7)/(t 9v)
- Modular Arithmetic -
- 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
26Friday
- Recitation (bring charged laptop)
26
27Named constants
- public static final Type Variable Constant
- Named in uppercase.
- Place the declaration outside any methods.
- The value of a constant can not be changed in the
program after the declaration. - public class FirstProgram public static final
double PI 3.14159 - public static void main(String args)
-
- .
-
-
- See example in Display 2.15 on page 104
27