COMP 14 Introduction to Programming - PowerPoint PPT Presentation

About This Presentation
Title:

COMP 14 Introduction to Programming

Description:

the brain of the computer, containing the CU, PC, IR, ALU, and ACC ... stores information permanently. Instructions executed by the CPU must be first loaded to ... – PowerPoint PPT presentation

Number of Views:16
Avg rating:3.0/5.0
Slides: 24
Provided by: ady3
Learn more at: http://www.cs.unc.edu
Category:

less

Transcript and Presenter's Notes

Title: COMP 14 Introduction to Programming


1
COMP 14Introduction to Programming
  • Adrian Ilie
  • July 11, 2005

2
Parts of the Computer
hardware
  • ________- computer components including the CPU,
    main memory, I/O devices, and secondary storage
  • ________ - the brain of the computer, containing
    the CU, PC, IR, ALU, and ACC
  • ________ - computer instructions to solve a
    problem
  • The digits 0 and 1 are called _________
  • or the shortened term ________

CPU
program
binary digits
bits
3
Questions - Hardware
  • ____________- points to the next instruction
    to be executed
  • ____- a unique location in memory
  • ____________- stores information permanently
  • Instructions executed by the CPU must be first
    loaded to ________

program counter (PC)
address
secondary storage
main memory
4
Questions - Software
  • Classify the following pieces of software as
    operating system or application
  • Microsoft Windows 2000
  • Microsoft PowerPoint
  • Linux
  • Your COMP 14 programs

OS
app
OS
app
5
Questions - Binary
  • Whats the maximum value a 6-bit number can
    represent?
  • Whats the decimal representation of 111010?
  • Whats the binary representation of 35?

26-163
25124123122021120058
35101000112
6
Questions Java Basics
syntax
  1. The ________ rules of a language determine which
    instructions are valid.
  2. True or False? Hello! is an example of a legal
    Java identifier. _______
  3. If an operator has an integer and a
    floating-point operand, the result of the
    operation is a ___________ number.
  4. The expression (int) (9.2) evaluates to ___.

False
floating-point
9
7
Questions Identifiers
  • Classify the following as legal or illegal
    identifiers
  • My First Program ____
  • my1stProgram ____
  • 1stProgram ____
  • money ____
  • an_identifier ____
  • Jane'sProgram ____

illegal
legal
illegal
legal
legal
illegal
8
Questions - Expressions
9 6 3
  1. (5 4) 6
  2. (5 6) 3.5
  3. (double) (13) / 2
  4. (double) (13 / 2)

11 3.5 not possible
13.0 / 2 6.5
(double) (6) 6.0
9
Questions - Assigning
  • What is stored in the memory location referred
    to by the identifier num after each of the
    following statements is executed in order?
  • int num ______
  • num 5 ______
  • num 5 4 - 2 ______
  • num num 3 ______
  • num 3.4 5 ______

unknown
5
7
21
error
would give an error since 3.4 5 would not
result in an int
10
Questions Valid Statements
  • Which of the following are valid Java assignment
    statements? Assume that i, x, and percent have
    been declared as double variables and properly
    initialized.
  • i i 5 ______
  • x 2 x ______
  • x 2.5 x ______
  • percent 10 ______

valid
invalid
valid
invalid
11
Questions Variables and Objects
  1. True or False. A primitive variable is a
    variable that stores the address of a memory
    space. ______
  2. The operator ____ is used to create a class
    object.
  3. In Java, the ________ operator is used to access
    members of a class. It separates the class (or
    object) name from the method name.
  4. True or False. Class objects are instances of
    that class. ______

False
new
dot (.)
True
12
Decimal Format Examples
  • DecimalFormat twoDecimal
  • new DecimalFormat("0.00")
  • DecimalFormat fmt
  • new DecimalFormat("0.")
  • System.out.println (twoDecimal.format(56.379)) __
    _____
  • System.out.println (fmt.format(56.379)) _______
  • System.out.println (twoDecimal.format(.3451)) ___
    ____
  • System.out.println (fmt.format(.3451)) _______
  • System.out.println (twoDecimal.format(.3)) ______
    _
  • System.out.println (fmt.format(.3)) _______

56.38
56.38
0.35
0.35
0.30
0.3
13
Comparing Characters
  • 'a' gt 'A'
  • '6' lt 7
  • ' ' lt 's'

97 gt 65 true
54 lt 7 false
32 lt 115 true
14
Comparing Floating-Point
  • 3.0 / 7.0
  • 2.0 / 7.0
  • (3.0 / 7.0) (2.0 / 7.0) (2.0 / 7.0) 1

0.4285714285714285
0.2857142857142857
false
If we did this math with exact arithmetic, this
expression would be true
15
Comparing Strings
  • Given String str "Homer"
  • str.compareTo("Marge")
  • str.equals("homer")
  • str.compareTo("Bart")

negative value
false
positive value
16
Boolean Operators
true
  • NOT ! (unary)
  • !(225)
  • AND (binary)
  • (225) (112)
  • OR (binary)
  • (225) (112)

false
true
17
Questions
  1. What type of primitive variable can the result of
    a logical expression be stored in? _______
  2. Under what conditions would the expression ((ch
    gt 'A') (ch lt 'Z')) evaluate to false?
    ______________________
  3. What method do you use to compare two Strings?
    __________
  4. Why is 'a' greater than 'A'? _____________________
    _________________

boolean
ch lt 'A' or ch gt 'Z'
compareTo
'a' comes after 'A' in the ASCII character table
18
Questions
  • Given the following declarations, evaluate each
    Boolean expression
  • int count 0, sum 54
  • double x 4.3, y 1.2
  • boolean wrong true
  • (wrong sum gt 60)
  • ((x gt 5) !(sum 55))
  • !((y gt 1.0) (x lt 4))
  • ((count ! 4) (sum gt 100) wrong)

false
(true false)
true
(false !(false))
true
!(true false)
true
(true false true)
(true false)
19
Questions
  • Assume movieRating is an int and movieName is
    a String
  • switch (movieRating)
  • case 1
  • System.out.println ("Run away!")
  • if (movieName.equals("Gigli"))
  • System.out.println ("Quickly!")
  • case 2
  • System.out.println ("Save your money")
  • break
  • case 3
  • System.out.println ("It's OK")
  • break
  1. What is printed if movieRating is 1 and movieName
    is "Gigli"?
  2. What is printed if movieRating is 5?

Run away! Quickly! Save your money
Nothing
20
Questions
  • if ((x gt 0) (y lt 0))
  • z xy
  • System.out.println (z z)
  • System.out.print (The sum is )
  • if (xy lt 0) System.out.println(negative.)
  • else System.out.println (positive.)
  • What is printed with the following values of x
    and y
  • x 5, y -4
  • x -9, y 5
  • x 5, y 5

z1 The sum is positive.
The sum is negative.
The sum is positive.
21
Selection Statements
  • What type of selection statement should be used?
  • Print "Male" if gender is 'M' and "Female" if
    gender is 'F'.
  • Print the state associated with areaCode, where
    areaCode is an int.
  • Print the maximum of three integers.

if-else
switch
nested if-else
22
Loops
  • final int MAX 20
  • int i
  • for (i 0 iltMAX i3)
  • System.out.println (i)
  • Translate this to a while loop.

final int MAX 20 int i 0 while (i lt
MAX) System.out.println (i) i 3
23
Object-Oriented DesignSimplified Methodology
  1. Write down detailed description of problem
  2. Identify all (relevant) nouns and verbs
  3. From list of nouns, select objects
  4. Identify data components of each object
  5. From list of verbs, select operations
Write a Comment
User Comments (0)
About PowerShow.com