COMP 110 Designing Programs - PowerPoint PPT Presentation

About This Presentation
Title:

COMP 110 Designing Programs

Description:

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

Number of Views:47
Avg rating:3.0/5.0
Slides: 28
Provided by: michele9
Learn more at: http://www.cs.unc.edu
Category:

less

Transcript and Presenter's Notes

Title: COMP 110 Designing Programs


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

1
2
Announcements
  • Office Hours
  • After class today
  • Come if you dont have jGRASP working

2
3
Questions?
3
4
Today in COMP 110
  • Writing algorithms in pseudocode
  • Variables and primitive types

5
Fun exercise time
5
6
Algorithm
  • A set of instructions for solving a problem

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

7
8
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!

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

main memory
10
How to use variables
  • Declare a variable
  • Assign a value to the variable
  • Change the value of the variable

11
Variable Declarations
  • Syntax
  • Type Variable_1, Variable_2,
  • Examples
  • int count, score, myInt
  • char letter
  • double totalCost, ratio
  • int fjiofeu // not a useful name!

11
12
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

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

13
14
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
15
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
16
Primitive Types small to big
17
Variables and Memory
  • When declaring a variable, a certain amount of
    memory is assigned based on the declared
    primitive type

int agedouble lengthchar letter
main memory
18
Assignment Statements
  • Change a variables value
  • Syntax
  • variable expression
  • Example
  • sleepNeeded 8
  • sleepDesired sleepNeeded 2

19
Behind the statement
  • variable expression
  • CPU calculates the value of the expression.
  • Send the value to the location of variable.
  • sleepDesired sleepNeeded 2
  • Calculate sleepNeeded 2
  • Get the current value of sleepNeeded from its
    memory location
  • Assign the value to the location of sleepDesired

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

21
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

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 (more info later)
  • , -, , --, !
  • Binary arithmetic operators
  • , /, , , -
  • raterate delta
  • 1/(time 3mass)
  • (a - 7)/(t 9v)

24
25
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
26
Homework
  • Program 1 is on the web page
  • We will look at code in recitation that will help
    you

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

27
Write a Comment
User Comments (0)
About PowerShow.com