Java Full Stack Assignment Overview
Java Full Stack Assignment Overview
The `NumberCheck` program outputs 'The number is zero' when the input integer is precisely zero. It uses a conditional statement to evaluate the input: if the number is greater than zero, it outputs 'The number is positive'; if less than zero, 'The number is negative', otherwise it defaults to 'zero' when neither condition is met .
To modify the `Sum` program for input verification, it could include a try-catch block to handle `NumberFormatException`. This block would wrap the parsing of the input strings to integers. If a string cannot be parsed appropriately (i.e., if it's not a valid number), the exception would be caught, and an error message displayed, avoiding program termination on invalid input .
The Java program determines the largest and smallest numbers in an integer array by using two separate methods: `findMax` and `findMin`, which iterate through the array. `findMax` initializes the maximum value with the first element and then iterates through the array, updating the maximum value whenever it finds a larger element. Similarly, `findMin` initializes the minimum value with the first element and updates it whenever it finds a smaller element in the array .
The `Rearrange` program separates even and odd numbers in an array by creating a new array and iterating over the original array twice. In the first pass, it appends all even numbers to the new array. In the second iteration, it appends all odd numbers. This reordering ensures all even numbers are positioned before odd numbers, effectively 'rearranging' the array into segregated groups of even and odd numbers .
The `PrimeNumber` program optimizes checking if a number is prime by iterating only up to the square root of the number rather than to itself. This approach reduces the number of iterations required because factors of a number above its square root would have complementary factors below the square root. Thus, if a divisor is not found by the time it reaches the square root, the number is prime .
The `ColorCode` program is case-sensitive; thus, inputting lowercase color codes would result in the default case executing. Specifically, lowercased inputs would not match any specified case labels (like 'R', 'B', 'G', etc.), leading the program to output 'Invalid Code' since lowercase letters are not among the explicitly checked switches .
The `SumOfDigits` program calculates the sum of digits of a given number by repeatedly extracting the last digit (using modulus 10) and adding it to a running total called `sum`. After adding, it updates the number by removing the last digit (using division by 10). This process continues until the number is reduced to zero, at which point the sum of all digits is printed out .
To enhance the `Message` program for scenarios where no arguments are passed, it can include a more specific message indicating that a user-friendly error occurred. This could involve checking `args.length` and outputting a message like 'Error: Please provide a name as an argument', improving clarity by explicitly stating the requirement for an argument .
The `Duplicates` program removes duplicates from an array using a combination of sorting and temporary storage. It first sorts the array to bring duplicates together, then uses a temporary array to store unique elements, copying only the first occurrence of each element. Finally, it creates a new array containing only these unique values, effectively filtering out duplicates .
The `lastDigit` function determines if two integers have the same last digit by computing the remainder of each integer when divided by 10. It then compares these remainders; if they are equal, it returns `true`, indicating that the last digits are the same. Otherwise, it returns `false` .