COMP 110 Algorithm and Pseudocode - PowerPoint PPT Presentation

1 / 27
About This Presentation
Title:

COMP 110 Algorithm and Pseudocode

Description:

Illegal names. michael.bolton, kenny-G, 1CP. 11. Keywords. Reserved words with predefined meanings. You cannot name your variables with keywords ... – PowerPoint PPT presentation

Number of Views:176
Avg rating:3.0/5.0
Slides: 28
Provided by: michele9
Category:

less

Transcript and Presenter's Notes

Title: COMP 110 Algorithm and Pseudocode


1
COMP 110Algorithm and Pseudocode
  • Feng Pan
  • August 27, 2008
  • MWF 1000 am - 1050 am
  • FB 007

1
2
Algorithm 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

3
Sample 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
4
Exercise Tower of Hanoi
  • Rules
  • Move one disk at a time.
  • Never place a big disk onto a small one.

5
COMP 110Variables, Primitive Types, Expressions
and Operators
  • Feng Pan
  • August 27, 2008
  • MWF 1000 am - 1050 am
  • FB 007

5
6
Java Basic
  • Variables
  • Primitive Types
  • Operators
  • Expressions

6
7
Variables
  • 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
8
Variable 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
9
Use 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)

10
Variable Declarations
  • Syntax
  • Type Variable_1, Variable_2,
  • Examples
  • int count, score
  • char letter
  • double totalCost, ratio
  • int wdsdf (not helpful)

10
11
How 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
12
Keywords
  • Reserved words with predefined meanings
  • You cannot name your variables with keywords
  • if, else, return, new
  • see Appendix 1 in the text book

12
13
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 display 2.2, p 53 for full list
  • Class type - objects with both data and methods
  • Name begins with uppercase letter
  • Scanner, String

13
14
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

14
15
Primitive Types small to big
16
Declare 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
17
Assignment Statement
  • Give a variable a value or change its value
  • Syntax
  • Variable Expression
  • Example
  • age 20
  • length length 10.1

18
Behind 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.

19
Example
  • int age
  • int score
  • age 2
  • score 5
  • score score ( score age )
  • score ? 49, 35 ?

20
Specialized Assignment Operators
  • length 5 // is the same as
  • length length 5
  • age // is the same as
  • age age 1

20
21
Assignment 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
22
Assignment Compatibilities
  • byte-gtshort-gtint-gtlong-gtfloat-gtdouble
  • myShort ? myInt
  • myByte ? myLong
  • myFloat ? mybyte
  • myLong ? myInt

22
23
Type 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
24
Arithmetic 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
25
Arithmetic 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
26
Friday
  • Recitation (bring charged laptop)

26
27
Named 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
Write a Comment
User Comments (0)
About PowerShow.com