CSCI 1112: Lecture 3 Mona Diab RoadMap High level view of Object - - PowerPoint PPT Presentation

csci 1112 lecture 3
SMART_READER_LITE
LIVE PREVIEW

CSCI 1112: Lecture 3 Mona Diab RoadMap High level view of Object - - PowerPoint PPT Presentation

CSCI 1112: Lecture 3 Mona Diab RoadMap High level view of Object Oriented Programming (Classes) In class exercise Arrays Object Oriented Programming OOP is an approach to programming which supports the creation of new data


slide-1
SLIDE 1

CSCI 1112: Lecture 3

Mona Diab

slide-2
SLIDE 2

RoadMap

  • High level view of Object Oriented

Programming (Classes)

  • In class exercise
  • Arrays
slide-3
SLIDE 3
  • OOP is an approach to programming which

supports the creation of new data types and

  • perations to manipulate those types.

Object Oriented Programming

slide-4
SLIDE 4

What is this Object ?

  • There is no real

answer to the question, but we’ll call it a “thinking cap”.

  • The plan is to

describe a thinking cap by telling you what actions can be done to it.

slide-5
SLIDE 5

Using the Object’s Slots

  • You may put a piece of

paper in each of the two slots (green and red), with a sentence written

  • n each.
  • You may push the green

button and the thinking cap will speak the sentence from the green slot’s paper.

  • And same for the red

button.

slide-6
SLIDE 6

Example

slide-7
SLIDE 7

Example

That ¡test ¡was ¡ ¡a ¡breeze ¡! ¡

slide-8
SLIDE 8

Example

I ¡should ¡ study ¡harder ¡! ¡

slide-9
SLIDE 9

Thinking Cap Implementation

  • We can implement

the thinking cap using a data type called a class.

public ¡class ¡ThinkingCap ¡ ¡ { ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡. ¡. ¡. ¡ ¡ ¡} ¡ ¡ ¡

slide-10
SLIDE 10

Thinking Cap Implementation

  • The class will have

two components called greenWords and redWords. These components are strings which hold the information that is placed in the two slots.

  • Using a class permits

two features . . .

public ¡class ¡ThinkingCap ¡ ¡ { ¡ ¡ ¡ ¡ ¡ ¡ ¡String ¡greenWords; ¡ ¡ ¡ ¡ ¡ ¡String ¡redWords; ¡ ¡ ¡ ¡ ¡ ¡ ¡. ¡. ¡. ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ } ¡ ¡ ¡

slide-11
SLIDE 11

Thinking Cap Implementation

The two components will be private instance

  • variables. This

ensures that nobody can directly access this information. The

  • nly access is through

methods that we provide for the class.

public ¡class ¡ThinkingCap ¡ ¡ { ¡ ¡ ¡ ¡private ¡String ¡greenWords; ¡ ¡ ¡ ¡private ¡String ¡redWords; ¡ ¡ ¡ ¡ ¡. ¡. ¡. ¡ } ¡ ¡ ¡

slide-12
SLIDE 12

Thinking Cap Implementation

In a class, the methods which manipulate the class are also listed.

class ¡ThinkingCap ¡ ¡ { ¡ ¡ ¡ ¡private ¡String ¡greenWords; ¡ ¡ ¡ ¡private ¡String ¡redWords; ¡ ¡ ¡ ¡ ¡. ¡. ¡. ¡ } ¡ ¡ ¡

Implementations of the thinking cap methods go here.

slide-13
SLIDE 13

Thinking Cap Implementation

public ¡class ¡ThinkingCap ¡ ¡ { ¡ ¡ ¡ ¡private ¡String ¡greenWords; ¡ ¡ ¡ ¡private ¡String ¡redWords; ¡ ¡ ¡ ¡ ¡public ¡void ¡slots(String ¡newGreen, ¡String ¡newRed)... ¡ ¡ ¡ ¡public ¡void ¡pushGreen( ¡)... ¡ ¡ ¡ ¡public ¡void ¡pushRed( ¡)... ¡ ¡ } ¡

Our thinking cap has at least three methods:

slide-14
SLIDE 14

Thinking Cap Implementation

package ¡edu.colorado.simulaCons; ¡ public ¡class ¡ThinkingCap ¡ ¡ { ¡ ¡ ¡ ¡private ¡String ¡greenWords; ¡ ¡ ¡ ¡private ¡String ¡redWords; ¡ ¡ ¡ ¡ ¡public ¡void ¡slots(String ¡newGreen, ¡String ¡newRed)... ¡ ¡ ¡ ¡public ¡void ¡pushGreen( ¡)... ¡ ¡ ¡ ¡public ¡void ¡pushRed( ¡)... ¡ ¡ } ¡

The code for a new class is generally put in a Java package, as shown here:

slide-15
SLIDE 15

Using the Thinking Cap

  • A program

that wants to use the thinking cap can import the ThinkingCap class.

import ¡ edu.colorado.simulaCons.ThinkingCap; ¡ ¡ ... ¡

slide-16
SLIDE 16

Using the Thinking Cap

  • Just for fun,

the example program will declare two ThinkingCap variables named student and fan.

import ¡ edu.colorado.simulaCons.ThinkingCap; ¡ ¡ public ¡class ¡Example ¡ { ¡ ¡ ¡ ¡ ¡ ¡public ¡staCc ¡void ¡main( ¡) ¡ ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ThinkingCap ¡student; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ThinkingCap ¡fan; ¡

slide-17
SLIDE 17

Using the Thinking Cap

  • The variables are

examples of reference variables, which means that they have the capability of referring to ThinkingCap

  • bjects that we

create with the “new” operator.

import ¡ edu.colorado.simulaCons.ThinkingCap; ¡ ¡ public ¡class ¡Example ¡ { ¡ ¡ ¡ ¡ ¡ ¡public ¡staCc ¡void ¡main( ¡) ¡ ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ThinkingCap ¡student; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ThinkingCap ¡fan; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡student ¡= ¡new ¡ThinkingCap( ¡); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡fan ¡= ¡new ¡ThinkingCap( ¡); ¡

slide-18
SLIDE 18

Using the Thinking Cap

  • Once the

ThinkingCap

  • bjects are

created, we can activate methods such as slot for the student thinking cap

  • bject.

import ¡ edu.colorado.simulaCons.ThinkingCap; ¡ ¡ public ¡class ¡Example ¡ { ¡ ¡ ¡ ¡ ¡ ¡public ¡staCc ¡void ¡main( ¡) ¡ ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ThinkingCap ¡student; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ThinkingCap ¡fan; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡student ¡= ¡new ¡ThinkingCap( ¡); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡fan ¡= ¡new ¡ThinkingCap( ¡); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡student.slots( ¡"Hello", ¡ ¡"Bye"); ¡

slide-19
SLIDE 19

Using the Thinking Cap

  • Once the

ThinkingCaps are created, we can activate methods such as slot for the student thinking cap.

import ¡edu.colorado.simulaCons.ThinkingCap; ¡ ¡ public ¡class ¡Example ¡ { ¡ ¡ ¡ ¡ ¡ ¡public ¡staCc ¡void ¡main(String[ ¡] ¡args) ¡ ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ThinkingCap ¡student; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ThinkingCap ¡fan; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡student ¡= ¡new ¡ThinkingCap( ¡); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡fan ¡= ¡new ¡ThinkingCap( ¡); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡student.slots( ¡"Hello", ¡ ¡"Bye"); ¡

slide-20
SLIDE 20

Using the Thinking Cap

The method activation consists of four parts, starting with the variable name.

¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡student.slots( ¡"Hello", ¡ ¡"Bye"); ¡

slide-21
SLIDE 21

Using the Thinking Cap

The variable name is followed by a period.

¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡student.slots( ¡"Hello", ¡ ¡"Bye"); ¡

slide-22
SLIDE 22

Using the Thinking Cap

After the period is the name of the method that you are activating.

¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡student.slots( ¡"Hello", ¡ ¡"Bye"); ¡

slide-23
SLIDE 23

Using the Thinking Cap

 Finally, the arguments for the method. In this example the first argument (newGreen) is "Hello" and the second argument (newRed) is "Bye".

¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡student.slots( ¡"Hello", ¡ ¡"Bye"); ¡

slide-24
SLIDE 24

A Quiz

How would you activate student's pushGreen method ? What would be the

  • utput of student's

pushGreen method at this point in the program ?

¡ ¡ ¡ ¡public ¡staCc ¡void ¡main(String[ ¡] ¡args) ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ThinkingCap ¡student; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ThinkingCap ¡fan; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡student ¡= ¡new ¡ThinkingCap( ¡); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡fan ¡= ¡new ¡ThinkingCap( ¡); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡student.slots( ¡"Hello", ¡ ¡"Bye"); ¡

slide-25
SLIDE 25

A Quiz

Notice that the

pushGreen method

has no arguments. At this point, activating

student.pushGreen

will print the string

Hello.

¡ ¡ ¡ ¡public ¡staCc ¡void ¡main(String[ ¡] ¡args) ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ThinkingCap ¡student; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ThinkingCap ¡fan; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡student ¡= ¡new ¡ThinkingCap( ¡); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡fan ¡= ¡new ¡ThinkingCap( ¡); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡student.slots( ¡"Hello", ¡ ¡"Bye"); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡student.pushGreen( ¡); ¡

slide-26
SLIDE 26

A Quiz

Trace through this program, and tell me the complete

  • utput.

public ¡staCc ¡void ¡main(String[ ¡] ¡args) ¡ ¡ { ¡ ¡ ¡ ¡ ¡ThinkingCap ¡student; ¡ ¡ ¡ ¡ ¡ThinkingCap ¡fan; ¡ ¡ ¡ ¡ ¡student ¡= ¡new ¡ThinkingCap( ¡); ¡ ¡ ¡ ¡ ¡fan ¡= ¡new ¡ThinkingCap( ¡); ¡ ¡ ¡ ¡ ¡student.slots( ¡"Hello", ¡ ¡"Bye"); ¡ ¡ ¡ ¡ ¡fan.slots( ¡"Go ¡Cougars!", ¡"Boo!"); ¡ ¡ ¡ ¡ ¡student.pushGreen( ¡); ¡ ¡ ¡ ¡ ¡fan.pushGreen( ¡); ¡ ¡ ¡ ¡ ¡student.pushRed( ¡); ¡ ¡ ¡ ¡ ¡. ¡. ¡. ¡

slide-27
SLIDE 27

A Quiz

Hello Go Cougars! Bye

public ¡staCc ¡void ¡main(String[ ¡] ¡args) ¡ ¡ { ¡ ¡ ¡ ¡ ¡ThinkingCap ¡student; ¡ ¡ ¡ ¡ ¡ThinkingCap ¡fan; ¡ ¡ ¡ ¡ ¡student ¡= ¡new ¡ThinkingCap( ¡); ¡ ¡ ¡ ¡ ¡fan ¡= ¡new ¡ThinkingCap( ¡); ¡ ¡ ¡ ¡ ¡student.slots( ¡"Hello", ¡ ¡"Bye"); ¡ ¡ ¡ ¡ ¡fan.slots( ¡"Go ¡Cougars!", ¡"Boo!"); ¡ ¡ ¡ ¡ ¡student.pushGreen( ¡); ¡ ¡ ¡ ¡ ¡fan.pushGreen( ¡); ¡ ¡ ¡ ¡ ¡student.pushRed( ¡); ¡ ¡ ¡ ¡ ¡. ¡. ¡. ¡

slide-28
SLIDE 28

What you know about Objects

➼ Class = Data + Methods. ➼ You know how to write a new class type,

and place the new class in a package.

➼ You know how to import the class into a

program that uses class type.

➼ You know how to activate methods.

➻ But you still need to learn how to write the implementations of a class’s methods.

slide-29
SLIDE 29

Thinking Cap Implementation

public ¡class ¡ThinkingCap ¡ ¡ { ¡ ¡ ¡ ¡ ¡ ¡private ¡String ¡greenWords; ¡ ¡ ¡ ¡ ¡ ¡private ¡String ¡redWords; ¡ ¡ ¡ ¡ ¡ ¡ ¡public ¡void ¡slots(String ¡newGreen, ¡String ¡newRed)... ¡ ¡ ¡ ¡ ¡ ¡public ¡void ¡pushGreen( ¡)... ¡ ¡ ¡ ¡ ¡ ¡public ¡void ¡pushRed( ¡)... ¡ ¡ } ¡

We will look at the body of slots, which must copy its two arguments to the two private instance variables.

slide-30
SLIDE 30

Thinking Cap Implementation

¡ public ¡void ¡slots(String ¡newGreen, ¡String ¡newRed) ¡

{ ¡

¡ ¡ ¡ ¡ ¡greenWords ¡= ¡newGreen; ¡ ¡ ¡ ¡ ¡ ¡redWords ¡= ¡newRed; ¡ } ¡ ¡

The method’s implementation occurs after the parameter list

There is one feature about a method’s implementation . . .

slide-31
SLIDE 31

Thinking Cap Implementation

¡ public ¡void ¡slots(String ¡newGreen, ¡String ¡newRed) ¡

{ ¡

¡ ¡ ¡ ¡ ¡greenWords ¡= ¡newGreen; ¡ ¡ ¡ ¡ ¡ ¡redWords ¡= ¡newRed; ¡ } ¡ ¡

Within the body of the method, the class’s instance variables and other methods may all be accessed.

slide-32
SLIDE 32

Thinking Cap Implementation

? ¡

Within the body of the method, the class’s instance variables and other methods may all be accessed.

¡ public ¡void ¡slots(String ¡newGreen, ¡String ¡newRed) ¡

{ ¡

¡ ¡ ¡ ¡ ¡greenWords ¡= ¡newGreen; ¡ ¡ ¡ ¡ ¡ ¡redWords ¡= ¡newRed; ¡ } ¡ ¡ But, ¡whose ¡instance ¡variables ¡are ¡ ¡ these? ¡ ¡Are ¡they ¡ ¡ ¡student.greenWords ¡ ¡ ¡student.redWords ¡ ¡ ¡fan.greenWords ¡ ¡ ¡fan.redWords ¡

slide-33
SLIDE 33

Thinking Cap Implementation

Within the body of the method, the class’s instance variables and other methods may all be accessed.

¡ public ¡void ¡slots(String ¡newGreen, ¡String ¡newRed) ¡

{ ¡

¡ ¡ ¡ ¡ ¡greenWords ¡= ¡newGreen; ¡ ¡ ¡ ¡ ¡ ¡redWords ¡= ¡newRed; ¡ } ¡ ¡ If ¡we ¡ac?vate ¡student.slots: ¡ ¡ ¡student.greenWords ¡ ¡ ¡student.redWords

slide-34
SLIDE 34

Thinking Cap Implementation

¡ public ¡void ¡slots(String ¡newGreen, ¡String ¡newRed) ¡

{ ¡

¡ ¡ ¡ ¡ ¡greenWords ¡= ¡newGreen; ¡ ¡ ¡ ¡ ¡ ¡redWords ¡= ¡newRed; ¡ } ¡ ¡ If ¡we ¡ac?vate ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡fan.slots: ¡ ¡ ¡fan.greenWords ¡ ¡ ¡fan.redWords ¡ ¡

Within the body of the method, the class’s instance variables and other methods may all be accessed.

slide-35
SLIDE 35

Thinking Cap Implementation

¡ public ¡void ¡pushGreen( ¡) ¡ { ¡ ¡ ¡ ¡ ¡ ¡System.out.println(greenWords); ¡ } ¡ ¡

Here is the implementation of the pushGreen method, which prints the green Words:

slide-36
SLIDE 36

Thinking Cap Implementation

Here is the implementation of the pushGreen method, which prints the green Words:

Notice how this method implementation uses the greenWords instance variable of the object.

¡ public ¡void ¡pushGreen( ¡) ¡ { ¡ ¡ ¡ ¡ ¡ ¡System.out.println(greenWords); ¡ } ¡ ¡

slide-37
SLIDE 37

A Common Pattern

  • Often, one or more methods will place

data in the instance variables...

public ¡class ¡ThinkingCap ¡{ ¡ ¡ ¡ ¡ ¡ ¡private ¡String ¡greenWords; ¡ ¡ ¡ ¡ ¡ ¡private ¡String ¡redWords; ¡ ¡ ¡ ¡ ¡ ¡... ¡ } ¡

 ...so that other methods may use that data.

slots ¡

¡ ¡ ¡ ¡pushGreen ¡& ¡pushRed ¡

slide-38
SLIDE 38

Members of a class

  • Instance Variables
  • Methods
  • Constructor
slide-39
SLIDE 39

Constructors

  • Initializes a class
  • Can have more than one constructor but

they have to take on different number of parameters

  • Same name as the class
  • Has no return value (not really a method)

(try writing void – error)

  • If you don’t write a constructor in a class,

Java automatically creates one with default initialization values

slide-40
SLIDE 40

methods

  • Typically control the operations on the

class and manipulation of the instance variables

  • Two types in general

– Accessor methods

  • Get information about an object without altering

values

  • Allows for better testing by knowing what the values

are to avoid errors

  • Pattern of private data public methods forbids
  • ther programmers from using our instance variables

in unintended ways

– Modification methods

  • Change the status of an object
slide-41
SLIDE 41

Throttle Example

  • A throttle object that simulates a throttle

controlling fuel flow

  • Specs

– Instance variables:

  • top total number of on positions
  • Position the current position of the throttle

– Constructor: initialize with a specified number of on positions – Methods

  • Get flow: get the current flow of the throttle, returns flow

rate [0.0-1.0) as a proportion of the max flow

  • Is throttle on: checks whether throttle is on, returns true if

flow is above 0 otherwise returns false

  • Shift: move throttle position up or down, takes on a

parameter of the amount to move (positive is up and negative is down), can’t exceed top position or go below 0

  • Shutoff: turns of the throttle (set position to 0)
slide-42
SLIDE 42
  • Classes have instance variables and methods.

An object is a variable where the data type is a class.

  • You should know how to declare a new class

type, how to implement its methods, how to use the class type.

  • Frequently, the methods of a class type place

information in the instance variables, or use information that's already in the instance variables.

Summary

slide-43
SLIDE 43

Data Structures: Arrays

  • Goals
  • Scientific Engineering
  • Numerical computations
  • Array are very efficient way of organizing

data since accessing array elements requires O(1).

slide-44
SLIDE 44

What is an array

A unidimensional array is a vector of data elements A multidimensional array could be a 2D matrix of data elements

slide-45
SLIDE 45

Characteristics of an Array

  • Arrays is one the main data organization structures in

programming languages

  • Arrays group “homogenous” values
  • Arrays permit fast access by numeric index
  • An Array Base @: Address of the first element of the

array

  • Array Type: is the data type it stores

– Basic such as int, char, etc. – Object such as String, your defined class objects, etc,

  • Array Size: is typically length of the array (number of

elements * the size of each element)

  • Array dimensions (upper bound, lower bound)

– An array of size 5 has a lower bound of 0 and an upper bound

  • f 4, i.e. 0 to n-1
slide-46
SLIDE 46

Representation of Array

  • Internal representation of an array
  • Memory cells have to be reserved for the array
  • ==> is achieved by declaration statements
  • Requires a mapping or accessing function to be decoded

uses the characteristics of the array In Java:

  • An array of size n: int [10] myarray;
  • the lower bound is 0 and the upper bound is 9
slide-47
SLIDE 47

Array Physical Representation

MT[0] ¡ MT[1] ¡ MT[2] ¡ MT[n-­‑1] ¡ A unidimensional array is a vector of data elements MT is an example of a single dimensional array

  • Where is the location of MT [i] ?
  • Mapping: Let size_of_element be the size of each element (Type of the

array)

  • The mapping function is:

MT [0] is at Base MT [2] is at Base+ size_of_element . . . MT [i] is at Base+(I - lower_bound)* size_of_element

  • In Java, where the lower_bound is 0

MT[i] = base address + i * size_of_element

slide-48
SLIDE 48

2 Dimensional Array Representation

  • Referred to as matrices or tables
  • Requires two dimensions:

– Rows – Columns

  • Two common schemes:

– Row major order: rows are placed one after another in memory. Examples: Java, C, C++, Pascal, etc.

  • 10, 20, 42, 71, 9, 6, 8, -2, -34, 1, 9, 28, 29, 12, -3, etc.

– Column major order: columns are placed one after another in memory. Example: Fortran

  • 10, 6, 9, 8, 20, 8, 28, 81, 42, -2, 29, 91. etc.

10 ¡ 20 ¡ 42 ¡ 71 ¡ 9 ¡ 6 ¡ 8 ¡

  • ­‑2 ¡
  • ­‑34 ¡

1 ¡ 9 ¡ 28 ¡ 29 ¡ 12 ¡

  • ­‑3 ¡

8 ¡ 81 ¡ 91 ¡ 100 ¡ 1000 ¡ User’s ¡Abstract ¡view ¡