Java Telephone and Student Directory
Java Telephone and Student Directory
The Student program implements a linear search for both searching by student number and name. In a linear search, each element in the array is checked sequentially until the desired element is found or all elements are checked. The time complexity of this approach is O(n), where n is the number of elements in the array. This is because, in the worst case, every element must be examined once. While linear search is simple to implement, it is not the most efficient for large lists, due to its linear time complexity. However, given that the program deals with only 20 elements, the performance impact is negligible .
The Student program iterates through the array of student names and utilizes the String class's 'startsWith' method to check if a name begins with the letter 'A'. For each name in the array, it calls 'student[i].startsWith("A")'. If this method returns true, indicating that the name starts with 'A', it prints the name. This operation is straightforward due to the efficiency of the 'startsWith' method in its direct comparison logic with the beginning characters of a string .
The line 'in.nextLine();' is used to consume the newline character left in the input buffer after using 'nextInt()' or 'nextLong()' when reading integers or long data types. Without this line, the Scanner may skip prompts for string input following an integer input. When 'nextInt()' or 'nextLong()' reads an integer, it does not consume the entire line; it stops at the first whitespace or newline. The subsequent 'nextLine()' call removes the newline at that instance, clearing the input buffer for future reads, ensuring correct subsequent user input processing .
The 'min' variable is used to track the index of the minimum element found during each pass of the selection sort algorithm. During each iteration of the outer loop, the algorithm assumes the first unsorted element is the minimum and stores its index in 'min'. It then iterates through the remaining unsorted elements to find a smaller element. If such an element is found, it updates 'min' with the new index. This allows the program to swap the actual minimum element into its correct position, ensuring that the array is sorted by the end of all iterations .
The KboatTelephoneBook program maintains the association between names and their corresponding telephone numbers by using parallel arrays. When a name is swapped during the selection sort process, it also swaps the corresponding telephone number in the parallel array of telephone numbers. This is done by storing the index of the minimum element, performing the element swap in the names array, and simultaneously swapping the telephone numbers using the same indices. This approach ensures that each name remains paired with its correct telephone number throughout the sorting process .
The Student program distinguishes between searching by student number and student name using a switch-case structure based on user input. After prompting the user to enter a choice (1 for number search, 2 for name search), it reads the choice with 'in.nextInt()'. In the switch-case, case '1' corresponds to searching by number, prompting the user to enter the desired student number, which is then searched within the 'number' array. Conversely, case '2' is for name-based searching, where the user inputs the student's name, and the program searches within the 'student' array. This structural distinction ensures that the program processes and directs the search logic according to user intentions accurately .
The program uses the selection sort algorithm to sort the array of names and their corresponding telephone numbers. Selection sort operates by iteratively selecting the minimum element from an unsorted subarray and swapping it with the first unsorted element. This process is repeated for all elements in the array except the last one. The time complexity of selection sort is O(n^2), where n is the number of elements to sort. This is due to the nested loop structure: for each of the n elements, another n elements may need to be checked. In the context of the KboatTelephoneBook program, the array size is fixed at 20, so while the theoretical time complexity remains O(n^2), the actual run time for 20 elements is manageable .
To search for a student by either name or number, the Student program uses a simple linear search with separate cases in a switch statement. For searching by number, the program iterates over the 'number' array and compares each element to the input number. Similarly, when searching by name, it iterates over the 'student' array, comparing each element to the input name using the 'equals' method. If a match is found, it updates the variable 'pos' to the current index. After each search, it checks if 'pos' has been changed from its initial value (-1). If 'pos' has a valid index, it prints the student's name; otherwise, it indicates that the element was not found .
The use of fixed-size arrays in both the KboatTelephoneBook and Student programs imposes significant limitations on scalability and flexibility. Fixed-size arrays require predetermined size allocation, in this case, 20 elements, which restricts the maximum data capacity and can lead to inefficient memory usage if fewer elements are used. Moreover, these arrays cannot dynamically expand or shrink based on input size, limiting the programs' adaptability to varying amounts of data. To handle more extensive or variable data efficiently, dynamic data structures such as ArrayLists or linked lists would be more appropriate, offering automatic resizing and more efficient memory use .
The Student program manages input validation by using a switch-case structure to handle different user choices for searching by number or name. Before the switch-case block, the program prompts the user to enter a choice and reads the integer input. In the switch-case, it checks if the user's input corresponds to the numbers 1 or 2 (for number search or name search, respectively). If the choice is not 1 or 2, it goes to the default case, printing 'Wrong choice' to inform the user of invalid input. This simple input validation does not prevent non-integer input errors, thus limited to handling only correct integer inputs .