// Hunter Schafer, CSE 143 // This class shows the example I did at the end of lecture to improve the code public class Style { // Version 1 public int helper(int a) { ListNode b = front; int a_ = 0; int b_ = 0; int c = 0; while (b != null) { if (b.data == a) { a_ = b_; c = 1; } b_++; b = b.next; } if (c == 1) { return a_; } else { return -1; } } // Version 2 // Changed: VARIABLE NAMES ARE IMPORTANT public int indexOf(int value) { ListNode current = front; int indexFound = 0; int index = 0; int hasFound = 0; while (current != null) { if (current.data == value) { indexFound = index; hasFound = 1; } index++; current = current.next; } if (hasFound == 1) { return indexFound; } else { return -1; } } // Version 3 // Changed: Made hasFound a boolean to better represent what it is doing public int indexOf(int value) { ListNode current = front; int indexFound = 0; int index = 0; boolean hasFound = false; while (current != null) { if (current.data == value) { indexFound = index; hasFound = true; } index++; current = current.next; } if (hasFound) { return indexFound; } else { return -1; } } // Version 4 (Final) // Changed: Initialized indexFound to be -1 so we don't have to check if we found something public int indexOf(int value) { ListNode current = front; int indexFound = -1; int index = 0; while (current != null) { if (current.data == value) { indexFound = index; } index++; current = current.next; } return indexFound; } }