CS 177 Recitation - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

CS 177 Recitation

Description:

... of characters that are enclosed in quotes. How about the operations for a ... Example: A car object would not represent the pizza leftovers on the back seat ... – PowerPoint PPT presentation

Number of Views:23
Avg rating:3.0/5.0
Slides: 26
Provided by: aaron6
Category:
Tags: recitation

less

Transcript and Presenter's Notes

Title: CS 177 Recitation


1
CS 177 Recitation
  • March 29, 2007

2
Announcements
  • Project4 due next Wednesday!
  • Exam2 results now available on WebCT
  • Question 17 is problematic, so any answer will be
    accepted (grades will be adjusted)
  • Key to Exam2 will be posted on website
  • Will revisit exam2 later today
  • Project3 grades posted on WebCT
  • How to access Project3 grading sheet
  • Login to lore.cs.purdue.edu
  • Your grading sheet (.html) is located at
    /u/u98/cs177/2007Spring/Grades/Project3/

3
Objects
  • What is an object?
  • Something which has data and operations
  • Could an integer be considered an object?
  • Yes.
  • What are its data and operations?
  • Data ? integer number
  • Operations ? , -, /, , gt, lt, ,

4
Objects
  • Think of a car as an object.
  • What are its data?
  • Color, model, year, horsepower, mileage, etc
  • What are its operations?
  • Start engine, steer, accelerate, brake, change
    gears, etc
  • How about the data for a string?
  • The sequence of characters that are enclosed in
    quotes
  • How about the operations for a string?
  • Convert to uppercase, lowercase, access a
    particular character, search for a substring,
    find first occurrence of a character

5
Object Oriented Programming
  • What is object-oriented programming?
  • A programming approach in which the problem is
    broken down into objects
  • Example A library management system could have
    objects for books, librarians, readers, orders,
    etc.
  • What makes object-oriented programming so useful?
  • Large systems can be broken into objects, and the
    objects can be implemented by different teams and
    later brought together
  • Objects can be reused in other applications
  • Why is an object an abstract representation?
  • It does not represent all possible data and
    operations
  • Example A car object would not represent the
    pizza leftovers on the back seat
  • We only represent what is necessary to solve the
    problem

6
Properties and Methods
  • What are properties of objects?
  • The data or characteristics of objects (like
    variables)
  • Example For a string object, the length is a
    property
  • str Strings rock!
  • str.length would give us 13
  • What are methods (functions) of objects?
  • The operations that can be performed on the
    objects
  • str MaKe Me LOwEr
  • lowstr str.toLowerCase()
  • lowstr is now make me lower
  • None of the methods for strings actually modify
    the string, they simply return a new string

7
Properties and Methods
  • What are some properties that we already know?
  • document.myForm.myInput.value
  • document.images.myImage.src
  • What is a method that we know?
  • document.write()
  • In the above examples, what is the object?
  • document
  • So how can we access properties or methods?
  • ltObject namegt.ltProperty or Method namegt

8
String Manipulations
  • str remains the same Foo 2 You!

9
Example 1
10
More String Manipulations
  • What does the charAt() method do?
  • Given an integer, it returns the character at
    that index in the string
  • str Clark Kent
  • ch str.charAt(3) (ch now holds r)
  • ch str.charAt(5) (ch now holds )
  • Note for a string of length n, the first
    character is at index 0, and the last is at n-1.
  • What does the substring() method do?
  • Given a beginning index and an ending index, it
    returns the part of the total string from the
    beginning index up to, but not including, the
    ending index.
  • str My TA is AWESOME!
  • sub str.substring(9,16) (sub now holds
    AWESOME)
  • sub str.substring(4,12) (sub now holds A is
    AWE)
  • Note first index is included in the substring,
    but the last is not.

11
Example 2
  • Using charAt() and substring(), write a function
    Capitalize(str) that receives a string and
    returns the same string with the first letter
    capitalized and all others lowercase.

12
More string methods
  • What does the search() method do?
  • Given either a string or list of characters, it
    returns the index of the beginning of the first
    occurrence of the string or any character
    specified in the list.
  • str I think therefore I am
  • i str.search(I)
  • (i 0)
  • i str.search(f)
  • (i 13)
  • i str.search(here)
  • (i 9, index of h)
  • i str.search(\klmn\)
  • (i 5, index of n)
  • i str.search(\the\)
  • (i 2, not i 8!)
  • i str.search(you)
  • (i -1, there is no you in str!)

13
General searching
14
Example 3
  • Write JavaScript code which removes the all
    characters a from the string str
  • str banana
  • result
  • i 0
  • while(i lt str.length)
  • if (str.charAt(i) ! a)
  • result result str.charAt(i)
  • i i 1
  • str result
  • What would result be here?
  • bnn

15
Example 4
  • Write JavaScript code to look through the string
    str and find the index of the first occurrence of
    help, but is not case-sensitive (i.e. it would
    also find HeLP).
  • str IsohHeLpelPmonhEllpious
  • str str.toLowerCase()
  • index str.search(help)
  • What would index be here?
  • 4

16
Exam2 Revisit
  • Statistics
  • Total  grade  number  percent   75-100  
      A       8       7   65- 74     B  
        41       34   55- 64     C      
    45       38   45- 54     D       19    
      16  below 45     F       7      
    5
  • Average 61, highest 91, lowest 28
  • These are statistics before grade update
    (everyone will get 3pts for question 17)

17
Exam2 Revisit
  • Questions that only 20-30 people got right
    1, 5, 6, 9, 13, 16, 21, 22, 29
  • Now review each one. Answers rendered green.
  • 1. Which of the following is not an advantage of
    von Neumann architecture over its predecessors?
  • Data is stored in memory for CPU to fetch when
    executing programs
  • Stored program
  • (c) I/O devices allow for interaction with the
    user
  • (d) CPU executes by loading program instructions
    from memory and executing them in sequence

18
Exam2 Revisit (cont.)
  • 5. Given the definition of the function check()
    below, which of the following statements is FALSE
    regarding check()?
  • function check()
  • input prompt("Please input a positive number
    ", "")
  • input parseFloat(input) // if input is
    initially , after parseFloat, input is NaN
  • if(input ! "")
  • document.write("Input is valid!")
  • else
  • document.write("Input is invalid!")
  • (a) the function does not check whether the input
    is a number
  • (b) the output will be Input is invalid! if
    nothing is entered in the prompt
  • (c) the function does not check whether the
    number is positive
  • (d) the function uses prompt correctly

19
Exam2 Revisit (cont.)
  • 6. What does the following function do? Assume
    parameters x and y are both positive integers.
  • function guessWhat(x, y)
  • var res, dif
  • if(x lt y) dif y - x
  • res Math.random() dif x
  • else dif x - y
  • res Math.floor(Math.random() dif) y
  • return res
  • (a) if x is no greater than y, return a random
    number in (x, y otherwise return a random
    integer in y, x
  • (b) if x is no greater than y, return a random
    number in x, y) otherwise return a random
    integer in y, x-1
  • (c) if x is no greater than y, return a random
    number in x, y) otherwise return a random
    integer in y, x
  • (d) if x is no greater than y, return a random
    number in (x, y otherwise return a random
    integer in y1, x

20
Exam2 Revisit (cont.)
  • 9. Suppose that a TA is handing back grade sheets
    for a programming project to her students. The TA
    walks up to the first student, asks for her name,
    and then proceeds to search through the stack of
    gradesheets one-by-one from the beginning. How
    many sheets must the TA look through in the worst
    case scenario where each students grade sheet
    turns out to be the last sheet in the stack by
    the time she finishes? Give the exact answer
    assuming there are N students. That is, she had
    to compare N names for the first student.
  • (a) N(N1)/2 (i.e. N (N-1) (N-2)
    1)
  • (b) N2
  • (c) N
  • (d) N!

21
Exam2 Revisit (cont.)
  • 13. Identify the TRUE statement(s) regarding
    encryption
  • I. Private key encryption requires the sharing of
    a private key
  • II. Public key encryption does not require the
    sharing of any key
  • III. Public key encryption allows the receiver to
    authenticate the sender
  • IV. Almost all secure communications on the
    Internet use private key encryption
    (answer a)
  • 16. In project 2, you were asked to create an
    object which is used to represent an element in
    the periodic table. What are the two things that
    define an object?
  • (a) Variables and Constructors
  • (b) Properties and Methods
  • (c) Methods and Constructors
  • (d) Properties and Variables

22
Exam2 Revisit (cont.)
  • 21. What are the values of the variables s and t
    after executing the following code fragment? Be
    careful.
  • var s 20
  • var t 30
  • if (s gt 100) // condition not satisfied!
  • s 40 // this is not executed!
  • t 20
  • if (t gt 20) // condition not satisfied!
  • s 80 // this is not executed!
  • t 10
  • (a) s 40, t 20
  • (b) s 20, t 10
  • (c) s 80, t 10
  • (d) s 20, t 30

23
Exam2 Revisit (cont.)
  • 22. What will be the value of the variable z
    after the following code fragment is executed? Be
    very careful!
  • var x 20
  • var y 20
  • var z 10
  • if (x lt y)
  • z x y - 40
  • else
  • if (x 10) // asssignment here,
    not equivalence! Condition satisfied!
  • z x10 - y - 60
  • else
  • if (x y)
  • z 4x - 2y
  • Answer b, variable z x10 y 60 20!

24
Exam2 Revisit (cont.)
  • 29. What will the following code output?
  • if (A gt a) // condition not satisfied
  • document.write("Maybe Right")
  • else
  • if (z gt Z) // condition satisfied
  • document.write("Maybe Wrong")
  • else
  • document.write("Correct")
  • (a) There will be no output.
  • (b) Correct
  • (c) Maybe Wrong
  • (d) Maybe Right

25
Questions?
Write a Comment
User Comments (0)
About PowerShow.com