Chapter 10 Applications of Arrays and Strings Java

  • Slides: 27
Download presentation
Chapter 10: Applications of Arrays and Strings Java Programming: From Problem Analysis to Program

Chapter 10: Applications of Arrays and Strings Java Programming: From Problem Analysis to Program Design, Second Edition

Chapter Objectives s Learn how to implement the sequential search algorithm. s Explore how

Chapter Objectives s Learn how to implement the sequential search algorithm. s Explore how to sort an array using bubble sort, selection sort, and insertion sort algorithms. s Learn how to implement the binary search algorithm. s Become aware of the class Vector. s Learn more about manipulating strings using the class String. Java Programming: From Problem Analysis to Program Design, Second Edition 2

List Processing s List: A set of values of the same type. s Basic

List Processing s List: A set of values of the same type. s Basic operations performed on a list: s s Search list for given item. Sort list. Insert item in list. Delete item from list. Java Programming: From Problem Analysis to Program Design, Second Edition 3

Search s Necessary components to search a list: s Array containing the list. s

Search s Necessary components to search a list: s Array containing the list. s Length of the list. s Item for which you are searching. s After search completed: s If item found, report “success” and return location in array. s If item not found, report “failure. ” Java Programming: From Problem Analysis to Program Design, Second Edition 4

Search public static int seq. Search(int[] list, int list. Length, int search. Item) {

Search public static int seq. Search(int[] list, int list. Length, int search. Item) { int loc; boolean found = false; for (loc = 0; loc < list. Length; loc++) if (list[loc] == search. Item) { found = true; break; } if (found) return loc; else return -1; } Java Programming: From Problem Analysis to Program Design, Second Edition 5

Selection Sort s List is sorted by selecting list element and moving it to

Selection Sort s List is sorted by selecting list element and moving it to its proper position. s Algorithm finds position of smallest element and moves it to top of unsorted portion of list. s Repeats process above until entire list is sorted. Java Programming: From Problem Analysis to Program Design, Second Edition 6

Selection Sort Java Programming: From Problem Analysis to Program Design, Second Edition 7

Selection Sort Java Programming: From Problem Analysis to Program Design, Second Edition 7

Selection Sort Java Programming: From Problem Analysis to Program Design, Second Edition 8

Selection Sort Java Programming: From Problem Analysis to Program Design, Second Edition 8

Selection Sort public static void selection. Sort(int[] list, int list. Length) { int index;

Selection Sort public static void selection. Sort(int[] list, int list. Length) { int index; int smallest. Index; int min. Index; int temp; for (index = 0; index < list. Length – 1; index++) { smallest. Index = index; for (min. Index = index + 1; min. Index < list. Length; min. Index++) if (list[min. Index] < list[smallest. Index]) smallest. Index = min. Index; temp = list[smallest. Index]; list[smallest. Index] = list[index]; list[index] = temp; } } Java Programming: From Problem Analysis to Program Design, Second Edition 9

Selection Sort s For a list of length n, an average selection sort makes

Selection Sort s For a list of length n, an average selection sort makes n(n – 1) / 2 key comparisons and 3(n – 1) item assignments. s Therefore, if n = 1000, selection sort makes about 500, 000 key comparisons and about 3000 item assignments to sort the list. Java Programming: From Problem Analysis to Program Design, Second Edition 10

Vectors s The class Vector can be used to implement a list. s Unlike

Vectors s The class Vector can be used to implement a list. s Unlike an array, the size of a Vector object can grow/shrink during program execution. s You do not need to worry about the number of data elements in a vector. Java Programming: From Problem Analysis to Program Design, Second Edition 11

Members of the class Vector Java Programming: From Problem Analysis to Program Design, Second

Members of the class Vector Java Programming: From Problem Analysis to Program Design, Second Edition 12

Members of the class Vector Java Programming: From Problem Analysis to Program Design, Second

Members of the class Vector Java Programming: From Problem Analysis to Program Design, Second Edition 13

Members of the class Vector Java Programming: From Problem Analysis to Program Design, Second

Members of the class Vector Java Programming: From Problem Analysis to Program Design, Second Edition 14

Members of the class Vector Java Programming: From Problem Analysis to Program Design, Second

Members of the class Vector Java Programming: From Problem Analysis to Program Design, Second Edition 15

Vectors s Every element of a Vector object is a reference variable of the

Vectors s Every element of a Vector object is a reference variable of the type Object. s To add an element into a Vector object: s Create appropriate object. s Store data into object. s Store address of object holding data into Vector object element. Java Programming: From Problem Analysis to Program Design, Second Edition 16

Vectors Vector<String> string. List = new Vector<String>(); string. List. add. Element("Spring"); string. List. add.

Vectors Vector<String> string. List = new Vector<String>(); string. List. add. Element("Spring"); string. List. add. Element("Summer"); string. List. add. Element("Fall"); string. List. add. Element("Winter"); Java Programming: From Problem Analysis to Program Design, Second Edition 17

Additional String Methods Java Programming: From Problem Analysis to Program Design, Second Edition 18

Additional String Methods Java Programming: From Problem Analysis to Program Design, Second Edition 18

Additional String Methods Java Programming: From Problem Analysis to Program Design, Second Edition 19

Additional String Methods Java Programming: From Problem Analysis to Program Design, Second Edition 19

Additional String Methods Java Programming: From Problem Analysis to Program Design, Second Edition 20

Additional String Methods Java Programming: From Problem Analysis to Program Design, Second Edition 20

Additional String Methods Java Programming: From Problem Analysis to Program Design, Second Edition 21

Additional String Methods Java Programming: From Problem Analysis to Program Design, Second Edition 21

Effects of Some String Methods Java Programming: From Problem Analysis to Program Design, Second

Effects of Some String Methods Java Programming: From Problem Analysis to Program Design, Second Edition 22

Programming Example: Pig Latin Strings s If string begins with a vowel, “-way” is

Programming Example: Pig Latin Strings s If string begins with a vowel, “-way” is appended to it. s If first character is not a vowel: s Add “-” to end. s Rotate characters until the first character is a vowel. s Append “ay. ” s Input: String s Output: String in pig Latin Java Programming: From Problem Analysis to Program Design, Second Edition 23

Programming Example: Pig Latin Strings (Solution) s Methods: is. Vowel, rotate, pig. Latin. String

Programming Example: Pig Latin Strings (Solution) s Methods: is. Vowel, rotate, pig. Latin. String s Use methods to: s Get the string (str). s Find the pig Latin form of str by using the method pig. Latin. String. s Output the pig Latin form of str. Java Programming: From Problem Analysis to Program Design, Second Edition 24

Programming Example: Pig Latin Strings (Sample Runs) Java Programming: From Problem Analysis to Program

Programming Example: Pig Latin Strings (Sample Runs) Java Programming: From Problem Analysis to Program Design, Second Edition 25

Chapter Summary s Lists s Searching lists: s Sequential searching on an order list

Chapter Summary s Lists s Searching lists: s Sequential searching on an order list s Binary search s Sorting lists: s Bubble sort s Selection sort s Insertion sort Java Programming: From Problem Analysis to Program Design, Second Edition 26

Chapter Summary s Programming examples s The class Vector s Members of the class

Chapter Summary s Programming examples s The class Vector s Members of the class Vector s The class String s Additional methods of the class String Java Programming: From Problem Analysis to Program Design, Second Edition 27