CS 106 Introduction to Computer Science I 02 / 28 / 2007 Instructor: Michael Eckmann.

28
CS 106 Introduction to Computer Science I 02 / 28 / 2007 Instructor: Michael Eckmann
  • date post

    21-Dec-2015
  • Category

    Documents

  • view

    217
  • download

    2

Transcript of CS 106 Introduction to Computer Science I 02 / 28 / 2007 Instructor: Michael Eckmann.

Page 1: CS 106 Introduction to Computer Science I 02 / 28 / 2007 Instructor: Michael Eckmann.

CS 106Introduction to Computer Science I

02 / 28 / 2007

Instructor: Michael Eckmann

Page 2: CS 106 Introduction to Computer Science I 02 / 28 / 2007 Instructor: Michael Eckmann.

Michael Eckmann - Skidmore College - CS 106 - Spring 2007

Today’s Topics• questions, comments?

• factorial method example

• we'll write a method together as an exercise

• Sorting algorithms

Page 3: CS 106 Introduction to Computer Science I 02 / 28 / 2007 Instructor: Michael Eckmann.

variables passed in as arguments when calling a method

• the values of variables that are passed into a method as arguments remain unchanged

• even if the corresponding parameter in the method changes its value, this change is only local to the method, and the value is not “passed back out” of the method.

• we’ll see this in the factorial method we create.

Page 4: CS 106 Introduction to Computer Science I 02 / 28 / 2007 Instructor: Michael Eckmann.

compute_factorial method

public int compute_factorial(int num){

int temp_factorial=1;while (num > 0)

{temp_factorial = temp_factorial * num;num--;

}return temp_factorial;

}

Page 5: CS 106 Introduction to Computer Science I 02 / 28 / 2007 Instructor: Michael Eckmann.

compute_factorial method• this method is a programmer-defined method (we make

up the name and the purpose of the method.)• the name of the method is: compute_factorial • the return type of the method is: int• there is one parameter to the method, which is: num (of

type int)• note that the value of num inside the method changes (it

gets decremented in the loop) but this is only a local change

• the other local variable (temp_factorial) is used to compute the factorial and its value is the one that is returned to the caller.

Page 6: CS 106 Introduction to Computer Science I 02 / 28 / 2007 Instructor: Michael Eckmann.

compute_factorial method• note that the value of num inside the method

changes (it gets decremented in the loop) but this is only a local change

• Let’s write a quick program to show how the variable that is “passed in” to the method doesn’t actually get changed.

• We’ll print the parameter variable’s value before and after the call to the method.

Page 7: CS 106 Introduction to Computer Science I 02 / 28 / 2007 Instructor: Michael Eckmann.

promotion of argument types

• if a method is called with arguments of types different from those specified in the parameter list of the method definition, then Java will try to convert the value to the required type.

• The conversion is done according to Java’s promotion rules

• Java only allows conversions such that no information would be lost by the type conversion.

• If information would be lost, then a compiler error would result.

Page 8: CS 106 Introduction to Computer Science I 02 / 28 / 2007 Instructor: Michael Eckmann.

promotion of argument types

some valid type conversions:– a float can be passed into a method requiring a

double– an int can be passed into a method requiring a

long, or a float or a double

some type conversions that would cause compiler errors:– a float cannot be passed into a method requiring an

int– a double cannot be passed into a method requiring

a float

Page 9: CS 106 Introduction to Computer Science I 02 / 28 / 2007 Instructor: Michael Eckmann.

a method call with the wrong typepublic int test_value(double input_val){

if (input_val < 0)return –1;

elsereturn 1;

}Example call in some other method:

float length = (float) 3.45;int returned_val;returned_val = test_value(length); // call is allowed: float length is promoted to double;

Page 10: CS 106 Introduction to Computer Science I 02 / 28 / 2007 Instructor: Michael Eckmann.

let's write another method• Let's write a method that will

– print Fizz if the integer passed in is a multiple of 3

– print Buzz if the integer is a multiple of 5

– print FizzBuzz if the integer is a multiple of both 3 and 5.

– print the integer otherwise

• What parameters will it have?

• What return type will it have?

• What name should we give it?

Page 11: CS 106 Introduction to Computer Science I 02 / 28 / 2007 Instructor: Michael Eckmann.

Sorting

• Sorting is a large topic in some computer science courses. • We'll discuss what sorting is and at least two

algorithms for doing it.• There are a myriad of ways to sort.• Arrays of data lend themselves easily to

being sorted. • Sorting can be done in ascending or

descending order.

Page 12: CS 106 Introduction to Computer Science I 02 / 28 / 2007 Instructor: Michael Eckmann.

Sorting

• Original unsorted: 38, 41, 22, 12, 67

• We want the list to be: 12, 22, 38, 41, 67• To accomplish this, one way would be to:– Go through the whole list once and find the lowest value.

– Take it out and put it in a new list.

– Go through the remaining list of numbers and find the lowest and take it out

– Take that one out and put it at the end of the new list.

– And so on ... until there's nothing left in the list.

• Does everyone agree that this will achieve a sorted list.

Page 13: CS 106 Introduction to Computer Science I 02 / 28 / 2007 Instructor: Michael Eckmann.

Sorting

• Another way would be to:– compare the number in the first position to all the other

numbers in the list and swap them in place if the number in the first position is greater than the number in the other position.

– the second pass does the same thing but with the number in the second position.

– third pass does the same but with the 3rd number.

– and so on, until the last position is reached.

• Why would this work or not work?

Page 14: CS 106 Introduction to Computer Science I 02 / 28 / 2007 Instructor: Michael Eckmann.

Sorting

• This algorithm doesn’t require a new list like the first one mentioned. All the sorting is done within the one list.

• After the first pass, what is guaranteed about the list?

• After the second pass, what is guaranteed about the list?

• Etc.

Page 15: CS 106 Introduction to Computer Science I 02 / 28 / 2007 Instructor: Michael Eckmann.

Sorting

• Original unsorted: 38, 41, 22, 12, 67

• compare 38 to 41. 38 is not > 41, so leave them in place.

• compare 38 to 22. Swap them. So, now list is 22, 41, 38, 12, 67

• compare 22 to 12. Swap them. So, now list is 12, 41, 38, 22, 67

• compare 12 to 67. 12 is not > 67, so leave them in place.

• After first pass: 12, 41, 38, 22, 67

Page 16: CS 106 Introduction to Computer Science I 02 / 28 / 2007 Instructor: Michael Eckmann.

Sorting

• After first pass: 12, 41, 38, 22, 67• Now start at second position.

• compare 41 to 38. Swap them. So, now list is 12, 38, 41, 22, 67

• compare 38 to 22. Swap them. So, now list is 12, 22, 41, 38, 67

• compare 22 to 67. Leave them.

• After second pass: 12, 22, 41, 38, 67

Page 17: CS 106 Introduction to Computer Science I 02 / 28 / 2007 Instructor: Michael Eckmann.

Sorting

• After second pass: 12, 22, 41, 38, 67

• Now start at third position.

• compare 41 to 38. Swap them. So, now list is 12, 22, 38, 41, 67

• compare 38 to 67. Leave them.

• After third pass: 12, 22, 38, 41, 67

Page 18: CS 106 Introduction to Computer Science I 02 / 28 / 2007 Instructor: Michael Eckmann.

Sorting

• After third pass: 12, 22, 38, 41, 67

• Now start at fourth position.

• compare 41 to 67. Leave them.

• After fourth pass: 12, 22, 38, 41, 67

• Done now.

Page 19: CS 106 Introduction to Computer Science I 02 / 28 / 2007 Instructor: Michael Eckmann.

Sorting (BubbleSort)

• Another algorithm for sorting is the BubbleSort:

–Compares consecutive numbers in the list

–Swaps them if the two numbers are not in ascending order

–Compare each pair of numbers in the first pass.

–Next pass, start at first again but go only up until the next to last element, and so on…

–Do n – 1 passes, where n is length of the list

Page 20: CS 106 Introduction to Computer Science I 02 / 28 / 2007 Instructor: Michael Eckmann.

Sorting (BubbleSort)

• Original unsorted: 38, 41, 22, 12, 67

• compare 38 to 41. 38 is not > 41, so leave them in place.

• compare 41 to 22. Swap them. So, now list is 38, 22, 41, 12, 67

• compare 41 to 12. Swap them. So, now list is 38, 22, 12, 41, 67

• compare 41 to 67. Leave them.

• After first pass: 38, 22, 12, 41, 67

Page 21: CS 106 Introduction to Computer Science I 02 / 28 / 2007 Instructor: Michael Eckmann.

Sorting (BubbleSort)

• After first pass: 38, 22, 12, 41, 67

• Now start at first position again.• compare 38 to 22. Swap them. So, now list is 22, 38,

12, 41, 67• compare 38 to 12. Swap them. So, now list is 22, 12,

38, 41, 67• compare 38 to 41. Leave them.• (don't need to compare the 4th and 5th positions) –

why???

• After second pass: 22, 12, 38, 41, 67

Page 22: CS 106 Introduction to Computer Science I 02 / 28 / 2007 Instructor: Michael Eckmann.

Sorting (BubbleSort)

• After second pass: 22, 12, 38, 41, 67

• Now start at first position again.

• compare 22 to 12. Swap them. So, now list is 12, 22, 38, 41, 67

• compare 22 to 38. Leave them.

• After third pass: 12, 22, 38, 41, 67

Page 23: CS 106 Introduction to Computer Science I 02 / 28 / 2007 Instructor: Michael Eckmann.

Sorting (BubbleSort)

• After third pass: 12, 22, 38, 41, 67

• Now start at first position again.

• compare 12 to 22. Leave them.

• Done.

• After fourth pass: 12, 22, 38, 41, 67

Page 24: CS 106 Introduction to Computer Science I 02 / 28 / 2007 Instructor: Michael Eckmann.

Sorting (BubbleSort)

• How might we write this in Java?

• We assume the list is stored in an array.

• When we said we’d swap two elements of the list, how could we do this?

• What about how to do the correct number of passes?

Page 25: CS 106 Introduction to Computer Science I 02 / 28 / 2007 Instructor: Michael Eckmann.

Sorting (BubbleSort)

• Here's a graphical bubble sort algorithm.

• http://math.hws.edu/TMCM/java/xSortLab/

• There are many, many more sorting algorithms that are more efficient in that they work by making fewer comparisons. But these should give you a basic idea of sorting and how it could be accomplished.

• Now, let's take a look at the code in the book to sort an array using a bubble sort algorithm.

Page 26: CS 106 Introduction to Computer Science I 02 / 28 / 2007 Instructor: Michael Eckmann.

Sorting (BubbleSort)

public void bubbleSort( int array2[] ){ for ( int pass = 1; pass < array2.length; pass++ ) { for ( int element = 0; element < (array2.length - 1); element++ ) { if ( array2[ element ] > array2[ element + 1 ] ) { swap( array2, element, element + 1 ); } // end if } // end inner for loop (element) } // end outer for loop (pass)} // end method bubbleSort

Page 27: CS 106 Introduction to Computer Science I 02 / 28 / 2007 Instructor: Michael Eckmann.

Sorting (BubbleSort)

public void swap( int array3[], int first, int second )

{

int hold;

hold = array3[ first ];

array3[ first ] = array3[ second ];

array3[ second ] = hold;

}

Page 28: CS 106 Introduction to Computer Science I 02 / 28 / 2007 Instructor: Michael Eckmann.

Sorting (BubbleSort)

• An improvement to make this sorting algorithm not check the highest positions that are already sorted during each pass, could be made.

• That is, the first pass goes through the whole array but after this happens the last position of the array is guaranteed to hold the highest value, so it doesn't need to be compared on the second pass (or any future pass.)

• After the second pass, the highest two positions contain the highest two values so they both don't need to be compared on the third pass and so on...

• Why is this an improvement?• What code could we change to implement this improvement?