Download presentation
Presentation is loading. Please wait.
Published byDorthy Nicholson Modified over 9 years ago
1
Chapter 10: Class Vector and String, and Enumeration Types Java Programming: Program Design Including Data Structures Program Design Including Data Structures
2
Java Programming: Program Design Including Data Structures2 Chapter Objectives Learn how to implement the sequential search algorithm Explore how to sort an array using bubble sort, selection sort, and insertion sort algorithms Learn how to implement the binary search algorithm Become aware of the class Vector Learn more about manipulating strings using the class String
3
Java Programming: Program Design Including Data Structures3 List Processing List: A set of values of the same type Basic operations performed on a list: Search list for given item Sort list Insert item in list Delete item from list
4
Java Programming: Program Design Including Data Structures4 Search Necessary components to search a list: Array containing the list Length of the list Item for which you are searching After search completed: If item found, report “success” and return location in array If item not found, report “failure”
5
Java Programming: Program Design Including Data Structures5 Search (continued) public static int seqSearch(int[] list, int listLength, int searchItem) { int loc; boolean found = false; for (loc = 0; loc < listLength; loc++) if (list[loc] == searchItem) { found = true; break; } if (found) return loc; else return -1; }
6
Java Programming: Program Design Including Data Structures6 Bubble Sort Suppose list[0...n - 1] is a list of n elements We want to rearrange (sort) the elements of list The bubble sort algorithm works as follows: In a series of n - 1 iterations, the successive elements, list[index] and list[index + 1], of list are compared If list[index] is greater than list[index + 1], then the elements list[index] and list[index + 1] are swapped (interchanged)
7
Java Programming: Program Design Including Data Structures7 Bubble Sort
8
Java Programming: Program Design Including Data Structures8 Bubble Sort (continued)
9
Java Programming: Program Design Including Data Structures9 public static void bubbleSort(int list[], int listLength) { int temp; int counter, index; for (counter = 0; counter < listLength - 1; counter++) { for (index = 0; index < listLength - 1 – counter; index++) if (list[index] > list[index + 1]) { temp = list[index]; list[index] = list[index + 1]; list[index + 1] = temp; } Bubble Sort (continued)
10
Java Programming: Program Design Including Data Structures10 Selection Sort List is sorted by selecting list element and moving it to its proper position Algorithm finds position of smallest element and moves it to top of unsorted portion of list Repeats process above until entire list is sorted
11
Java Programming: Program Design Including Data Structures11 Selection Sort (continued)
12
Java Programming: Program Design Including Data Structures12 Selection Sort (continued)
13
Java Programming: Program Design Including Data Structures13 public static void selectionSort(int[] list, int listLength) { int index; int smallestIndex; int minIndex; int temp; for (index = 0; index < listLength – 1; index++) { smallestIndex = index; for (minIndex = index + 1; minIndex < listLength; minIndex++) if (list[minIndex] < list[smallestIndex]) smallestIndex = minIndex; temp = list[smallestIndex]; list[smallestIndex] = list[index]; list[index] = temp; } Selection Sort (continued)
14
Java Programming: Program Design Including Data Structures14 Insertion Sort The insertion sort algorithm sorts the list by moving each element to its proper place
15
Java Programming: Program Design Including Data Structures15 Insertion Sort (continued)
16
Java Programming: Program Design Including Data Structures16 Insertion Sort (continued)
17
Java Programming: Program Design Including Data Structures17 Insertion Sort (continued)
18
Java Programming: Program Design Including Data Structures18 public static void insertionSort(int[] list,int noOfElements) { int firstOutOfOrder, location; int temp; for (firstOutOfOrder = 1; firstOutOfOrder < noOfElements; firstOutOfOrder++) { if (list[firstOutOfOrder] < list[firstOutOfOrder - 1]) { temp = list[firstOutOfOrder]; location = firstOutOfOrder; do { list[location] = list[location - 1]; location--; } while(location > 0 && list[location - 1] > temp); list[location] = temp; } } //end insertionSort Insertion Sort (continued)
19
Java Programming: Program Design Including Data Structures19 Sequential Ordered Search public static int seqOrderedSearch(int[] list, int listLength, int searchItem) { int loc; //Line 1 boolean found = false; //Line 2 for (loc = 0; loc < listLength; loc++) { //Line 3 if (list[loc] >= searchItem) { //Line 4 found = true; //Line 5 break; //Line 6 } if (found) //Line 7 if (list[loc] == searchItem) //Line 8 return loc; //Line 9 else //Line 10 return -1; //Line 11 else //Line 12 return -1; //Line 13 } // In Line 11, return –(loc+1);
20
Java Programming: Program Design Including Data Structures20 Binary Search Algorithm Search item is compared with middle element of list If search item < middle element of list, search is restricted to first half of the list If search item > middle element of list, search is restricted to second half of the list If search item = middle element, search is complete
21
Java Programming: Program Design Including Data Structures21 Binary Search Algorithm Determine whether 75 is in the list.
22
Java Programming: Program Design Including Data Structures22 public static int binarySearch(int[] list, int listLength, int searchItem) { int first = 0; int last = listLength - 1; int mid; boolean found = false; while (first <= last && !found) { mid = (first + last) / 2; if (list[mid] == searchItem) found = true; else if (list[mid] > searchItem) last = mid - 1; else first = mid + 1; } if (found) return mid; else return –1; } //end binarySearch Binary Search Algorithm
23
Java Programming: Program Design Including Data Structures23 Vectors The class Vector can be used to implement a list Unlike an array, the size of a Vector object can grow/shrink during program execution You do not need to worry about the number of data elements in a vector
24
Java Programming: Program Design Including Data Structures24 Members of the class Vector
25
Java Programming: Program Design Including Data Structures25 Members of the class Vector (continued)
26
Java Programming: Program Design Including Data Structures26 Members of the class Vector (continued)
27
Java Programming: Program Design Including Data Structures27 Members of the class Vector (continued)
28
Java Programming: Program Design Including Data Structures28 Vectors (continued) Every element of a Vector object is a reference variable of the type Object To add an element into a Vector object: Create appropriate object Store data into object Store address of object holding data into Vector object element
29
Java Programming: Program Design Including Data Structures29 Vector stringList = new Vector (); stringList.addElement("Spring"); stringList.addElement("Summer"); stringList.addElement("Fall"); stringList.addElement("Winter"); Vectors (continued)
30
Java Programming: Program Design Including Data Structures30 String The class String is a very important class in Java. Additional String methods are discussed.
31
Java Programming: Program Design Including Data Structures31 Additional String Methods
32
Java Programming: Program Design Including Data Structures32 Additional String Methods (continued)
33
Java Programming: Program Design Including Data Structures33 Additional String Methods (continued)
34
Java Programming: Program Design Including Data Structures34 Additional String Methods (continued)
35
Executing String Methods Consider the following statements: String sentence; String str1; String str2; String str3; String str4; sentence = “It is sunny and warm.”; str1 = “warm.”; str2 = “Programming with Java”; str3 = “sunny”; str4 = “Learning Java Programming is exciting”; Java Programming: Program Design Including Data Structures35
36
Java Programming: Program Design Including Data Structures36 Effects of Some String Methods
37
Java Programming: Program Design Including Data Structures37 Enumeration Types Java allows programmers to create their own data types by specifying the values of that data type. The values belonging to primitive data types are predefined. enumeration of enum types using the key word enum The values of the enum data type are identifiers. Used as a public static reference variables to objects of the enum type enum Grades {A, B, C, D, F}; where A, B, …, F are enum constants (unique values) Grades an enum type Grades myGrade; myGrade = Grades.B;
38
Java Programming: Program Design Including Data Structures38 Enumeration Methods Methods in enumeration of enum types ordinal() returns the ordinal value of an enum constant name() returns the name of the enum value values() returns the values of an enum type as a list
39
Java Programming: Program Design Including Data Structures39 Chapter Summary Lists Searching lists: Sequential searching Sequential searching on an order list Binary search Sorting lists: Bubble sort Selection sort Insertion sort
40
Java Programming: Program Design Including Data Structures40 Chapter Summary (continued) Programming examples The class Vector Members of the class Vector The class String Additional methods of the class String Enumeration enum Types Members of the class Vector
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.