cs 106 introduction to computer science i 03 / 17 / 2008 instructor: michael eckmann

25
CS 106 Introduction to Computer Science I 03 / 17 / 2008 Instructor: Michael Eckmann

Post on 22-Dec-2015

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS 106 Introduction to Computer Science I 03 / 17 / 2008 Instructor: Michael Eckmann

CS 106Introduction to Computer Science I

03 / 17 / 2008

Instructor: Michael Eckmann

Page 2: CS 106 Introduction to Computer Science I 03 / 17 / 2008 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 106 - Spring 2008

Today’s Topics• Comments and/or Questions?• searching arrays

– linear search– binary search

• Object oriented programming

Page 3: CS 106 Introduction to Computer Science I 03 / 17 / 2008 Instructor: Michael Eckmann

Searching arrays (Linear search)

// Search array for specified key value public static int linearSearch( int list[], int key ) {

// loop through array elements

for ( int cntr = 0; cntr < list.length; cntr++ ){

// if array element equals key value, // return location

if ( list[ cntr ] == key ) return cntr;

}

return -1; // key not found }

Page 4: CS 106 Introduction to Computer Science I 03 / 17 / 2008 Instructor: Michael Eckmann

Searching arrays (Linear search)

• Let’s analyze the linear search.• We saw that sorting page counted the number of

comparisons and the number of copies (or assignments). Let's consider comparisons here.

• How long (that is, how many comparisons) does it take to find the value?– What’s the minimum number of comparisons it would take?– What’s the maximum number of comparisons it would take?– Any idea on the average number of comparisons?

Page 5: CS 106 Introduction to Computer Science I 03 / 17 / 2008 Instructor: Michael Eckmann

Searching arrays

• Can we do better? (That is, can we guarantee less comparisons on average to find a particular value?)

Page 6: CS 106 Introduction to Computer Science I 03 / 17 / 2008 Instructor: Michael Eckmann

Searching arrays

• If the array was sorted could we modify the linear search in any way to stop sooner if we don't find the key?

• How might we implement the change to the Java code implementation of linear search?

Page 7: CS 106 Introduction to Computer Science I 03 / 17 / 2008 Instructor: Michael Eckmann

Binary Search

• Now let's think beyond linear search.

• First question: Has anyone heard of binary search?

• What if we knew the array was sorted in ascending order?

• Could we use that to our advantage to reduce the number of comparisons a search algorithm would do on average?

• Yes --- how might you look up a person in the phone book?

Page 8: CS 106 Introduction to Computer Science I 03 / 17 / 2008 Instructor: Michael Eckmann

Binary Search• We could compare to the middle element of the array.

• If it is equal to the middle element, we’re done.

• If it is less than the middle element, where would we now concentrate our search?

• If it is greater than the middle element, where would we now concentrate our search?

Page 9: CS 106 Introduction to Computer Science I 03 / 17 / 2008 Instructor: Michael Eckmann

Binary Search

• Any idea how we might write code to implement this algorithm?

Page 10: CS 106 Introduction to Computer Science I 03 / 17 / 2008 Instructor: Michael Eckmann

Binary Search• Any idea how we might write code to implement this

algorithm?

• Let's discuss some ideas before we get right to the code.– What parameters might our method have?– What element to compare to first?

• How do we calculate that index?– How do we determine what part of the array to now do

a search?

• Let's take a look at an implementation and do an example call.

Page 11: CS 106 Introduction to Computer Science I 03 / 17 / 2008 Instructor: Michael Eckmann

Binary Search // method to perform binary search of an array

public static int binarySearch( int array2[], int key )

{

int low = 0; // low element subscript

int high = array2.length - 1; // high element subscript

int middle; // middle element subscript

// loop until low subscript is greater than high subscript

while ( low <= high )

{

// determine middle element subscript

middle = ( low + high ) / 2;

// if key matches middle element, return middle location

if ( key == array2[ middle ] )

return middle;

// if key less than middle element, set new high element

else if ( key < array2[ middle ] )

high = middle - 1;

// key greater than middle element, set new low element

else

low = middle + 1;

} // end while loop

return -1; // key not found

} // end method binarySearch

Page 12: CS 106 Introduction to Computer Science I 03 / 17 / 2008 Instructor: Michael Eckmann

Searching arrays (Binary search)

• Let’s analyze the binary search.• To simplify the discussion, we can count the 2

comparisons in the if/else/if/else together to be 1 comparison.

• How long (that is, how many comparisons) does it take to find the value?– What’s the minimum number of comparisons it would take?– What’s the maximum number of comparisons it would take?

Page 13: CS 106 Introduction to Computer Science I 03 / 17 / 2008 Instructor: Michael Eckmann

Object orientation

• Object orientation is a programming paradigm that views the pieces of large programs as objects with “attributes” and “behaviors”.

• Java is an object-oriented language. C++ is another widely used object-oriented language. Other languages with which you may be familiar, like C, Pascal, and Fortran, are not object-oriented. These are what are called procedural languages.

• So far in this course, we have used Java in a very procedural way and did not use it to its potential as an object-oriented language.

Page 14: CS 106 Introduction to Computer Science I 03 / 17 / 2008 Instructor: Michael Eckmann

Object orientation

• An example of an object.• If we wanted to write a program that worked with

rectangles:– The attributes of a rectangle include its length and

width.

–Other attributes might be its color, ...

–Behaviors of the rectangle could include: changing its size, computing its area, etc...

Page 15: CS 106 Introduction to Computer Science I 03 / 17 / 2008 Instructor: Michael Eckmann

Object orientation

• So, in our example, a rectangle would have as its data:– length

–width

– color

• As its methods we would have things like:– set_length

– set_width

– set_color

– calculate_area

Page 16: CS 106 Introduction to Computer Science I 03 / 17 / 2008 Instructor: Michael Eckmann

Object orientation• This translates to creating a class called Rectangle

which has length, width and color as instance variables. This class will represent a generic rectangle.

• The methods in the class Rectangle would be:– set_length

– set_width

– set_color

– calculate_area

– among other possible methods

Page 17: CS 106 Introduction to Computer Science I 03 / 17 / 2008 Instructor: Michael Eckmann

Class Rectangle• The Rectangle class would be defined in its own file called

Rectangle.java• The Rectangle class represents what it means to be a rectangle

-- it means having a length width and color and it means that we can change it's length width and color. We can also calculate the area of a rectangle. If we wanted to be able to do more things to a Rectangle then we could add more methods such as calculating the perimeter of the rectangle.

Page 18: CS 106 Introduction to Computer Science I 03 / 17 / 2008 Instructor: Michael Eckmann

class definition of Rectangle• Let's partially create the class Rectangle.• Essentially we are creating a new data type named Rectangle

to represent rectangles.

Page 19: CS 106 Introduction to Computer Science I 03 / 17 / 2008 Instructor: Michael Eckmann

Object of type Rectangle• An object of type Rectangle is ONE rectangle with a specific

length, width, and color. An object of type Rectangle must be created to use the data and methods in the Rectangle class.

• To do this, in some other class, we could instantiate an object of type Rectangle, by declaring a variable of type Rectangle (and calling it's constructor – we'll get to that shortly).

• Then, through this object reference, we would have access to the methods within the Rectangle class to set its length, set its width, compute its area, etc... for that particular instance of the class.

• We might want multiple variables of type rectangle, each with their own width, length and color. These would each be objects of type Rectangle.

Page 20: CS 106 Introduction to Computer Science I 03 / 17 / 2008 Instructor: Michael Eckmann

Constructor methods

• A constructor method is a special method of a class that is called when an object of the class is instantiated (created.)

• Constructor methods usually initialize the instance variables.

• Constructor methods have the same name as the class and are defined WITHOUT a return type.

• A constructor for our class Rectangle should contain code that sets the values of length, width and color.

Page 21: CS 106 Introduction to Computer Science I 03 / 17 / 2008 Instructor: Michael Eckmann

Constructor methods

• for example, we might have a constructor that looks like the following:

public Rectangle ( double len, double wid, String col ){

length = len;width = wid;color = col;

}• Recall that the instance variables were named length, width

and color that is why they are on the left side of the assignment statements --- they're the variables being set.

Page 22: CS 106 Introduction to Computer Science I 03 / 17 / 2008 Instructor: Michael Eckmann

Constructor methods

• We might also want a default constructor (one that takes no arguments) so that we can create an object of type Rectangle without specifying its length, width and color.

public Rectangle ( ){

length = 1.0;width = 1.0;color = “Green”;

}• This says the default rectangle is 1 by 1 and Green.

Page 23: CS 106 Introduction to Computer Science I 03 / 17 / 2008 Instructor: Michael Eckmann

Constructor methods

• Assuming that the two constructors just described are defined within the Rectangle class, to instantiate an object of type Rectangle we would do one of the following:

// this would create a rectangle with the specified values. Rectangle some_rect = new Rectangle ( 3.2, 5.1, “Red”);

// this would create a “Green” rectangle with length = 1 // and width = 1 Rectangle another_rect = new Rectangle ( );

Page 24: CS 106 Introduction to Computer Science I 03 / 17 / 2008 Instructor: Michael Eckmann

example• Let’s create a program containing two classes

– Rectangle• let's add the code from the previous slides into

Rectangle and also provide a method that prints the length, width and color of the rectangle.

– RectangleTester• will contain the main method and instantiate a

couple of objects of type Rectangle with which we'll call the methods of Rectangle.

Page 25: CS 106 Introduction to Computer Science I 03 / 17 / 2008 Instructor: Michael Eckmann

class / object / constructor terminology

• Programmer defined classes can be used as types, just like classes in the Java API can be used as types (e.g. String.)

• Objects have attributes (data) and behaviors (methods.)• The data are the instance variables in a class.• An object of a class is instantiated by declaring a variable of

that type and initializing the object by calling a constructor of that class. – e.g. Time1 time_object = new Time1( );

• here, we are creating an object (time_object), of a programmer defined class (Time1).