CS 177 Recitation - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

CS 177 Recitation

Description:

... that are enclosed in quotes. How about the operations for ... Example: A car object would not represent the week-old french fry on the floor of the back seat ... – PowerPoint PPT presentation

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

less

Transcript and Presenter's Notes

Title: CS 177 Recitation


1
CS 177 Recitation
  • November 2, 2006

2
Announcements
  • Project 3 due next Wednesday
  • Exam 2 will come back next week

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 classroom simulator could have objects
    for the teacher and students, desks, projector,
    chalkboard, 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
    week-old french fry on the floor of 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 an object
  • Example For a string object, the length is a
    property
  • str Strings rock!
  • str.length would give us 13
  • What are methods of objects?
  • The operations that can be performed on the
    object
  • 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
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 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)
  • i str.search(\klmn\)
  • (i 5)
  • i str.search(\the\)
  • (i 2)

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