Java Programming Practice for IGCSE
Java Programming Practice for IGCSE
The bubble sort algorithm repeatedly steps through a list, compares adjacent elements, and swaps them if they are in the wrong order. This process continues until the list is sorted. For sorting product records by product name, the algorithm compares the alphabetical order of product names and rearranges the records in ascending order. Although simple to implement, bubble sort has a time complexity of O(n^2), making it less efficient for large lists .
Bubble sort is less efficient due to its quadratic time complexity (O(n^2)), which requires multiple passes through the data for large lists. More efficient alternatives include quicksort, mergesort, and heapsort, which have better average-case time complexities (O(n log n)) and perform faster due to fewer comparisons and efficient data partitioning or merging techniques .
A linear search algorithm in Java iterates through each element in the EmployeeRecords array to find a specified employee ID. If the ID is found, the algorithm returns its index. If the algorithm traverses the entire array without finding the ID, it informs the user that the record was not found. One limitation of linear search is its inefficiency with large datasets, as it potentially requires checking each element sequentially until a match is found .
To handle file operations in Java, use classes from java.io package. For writing a line of text to a file, create a FileWriter object and use it to output text to the file. To read the text back, use a FileReader or BufferedReader object to stream the file's contents back into the program. These operations must handle exceptions like IOException to ensure errors are managed properly during file access .
When validating score inputs, ensure all entries fall within the specified range (0 to 100 for scores) to maintain data integrity. Implement checks to reject invalid entries and prompt for re-entry until a valid score is provided. This validation prevents calculation errors, such as incorrect averages, and ensures reliable performance of statistical analyses within the program .
Nested loops can be employed in Java to repeat outputs by placing one loop inside another. For instance, a nested FOR loop can output numbers 1 to 5 twenty times by having an outer loop execute the inner loop's sequence repeatedly. The inner loop runs five iterations from 1 to 5, while the outer loop controls the number of times this sequence is printed .
To calculate and display the highest, lowest, and average scores for multiple students in Java, a program should prompt users to input student names, subjects, and scores for each test. Each subject consists of five tests scored out of 100. The program should aggregate the scores to identify the highest and lowest scores, then compute the average by summing the scores and dividing by the number of tests. Input validation is crucial, as it ensures scores fall within the 0 to 100 range to prevent erroneous data from affecting the calculations .
A Java program can use a while loop to continuously prompt the user to input data, such as the weight of sacks of rice, and keep a tally of the number of entries made. The loop will terminate when a specific sentinel value, '-1' in this case, is entered. This approach allows the program to accumulate both the total number of inputs and the cumulative sum of the entered weights, which can then be displayed to the user once the loop exits .
In Java, you can obtain the length of a string using the length() method, extract a substring using substring(), and change string format using toUpperCase() and toLowerCase() methods. For example, inputting a name into a variable, you can find its length, extract specific portions, such as the first three characters, and convert the string to uppercase or lowercase .
In Java, the modulus of two integers 'a' MOD 'b' can be calculated using the '%' operator, while integer division 'a' DIV 'b' utilizes the '/' operator. To generate a random integer within a specified range, such as between 100 and 300, use the Random class or Math.random(), scaling the output to match the desired range, e.g., Random.nextInt(201) + 100 .