Java Arithmetic and Logic Operations
Java Arithmetic and Logic Operations
The `fact` method calculates the factorial of a positive integer recursively. For any integer `a` greater than 0, it multiplies `a` by the factorial of `a-1`. The recursion base case occurs when `a` is 1, where the method returns 1, allowing the recursion to unwind .
The main method in the program handles invalid input scenarios by wrapping calls to methods in try-catch blocks. Each method call that can potentially throw an ArithmeticException (such as operations involving negative numbers) is enclosed in a try block. If an exception is thrown, it is caught in the catch block, where the specified error message is then displayed, maintaining program flow .
The `palindrome` function checks if a string reads the same forwards and backwards by comparing characters sequentially from the start and end towards the center. If all character pairs match, it confirms the string as a palindrome. Otherwise, it determines it is not. This check is case-sensitive and assumes no structural or whitespace alterations in comparison .
The `arith` function manages negative inputs by throwing an ArithmeticException if any of the input integers `a`, `b`, or `c` is negative. This exception message instructs the user to enter non-negative numbers only. This error handling ensures that operations such as addition, subtraction, multiplication, and division are performed only with valid non-negative numbers .
The `fib` function iteratively computes the Fibonacci series starting with two initial terms `0` and `1`. It then calculates the subsequent terms by summing the last two recorded values. This computation runs in a loop, producing and printing each term until reaching the specified count, generating the series progressively .
The `three` function uses conditional logic to evaluate which of the three numbers `a`, `b`, or `c` is the greatest. First, it checks if `a` is greater than both `b` and `c`. If true, `a` is the greatest. If not, it checks if `b` is greater than `c`; if so, `b` is the greatest. Otherwise, it concludes that `c` is the greatest .
The `prime` function verifies a number's primality by checking if it is divisible by any number from 2 up to half of the number (effectively excluding even multiples). If it finds any divisors, the count is incremented, indicating the number is not prime. If no divisors are found, the function concludes the number is a prime .
The number 371 is determined to be an Armstrong number because the sum of the cubes of its digits equals the number itself. The `armstrong` method computes this by extracting each digit of `371`, cubing it, summing the results (3³ + 7³ + 1³ = 371), and comparing it to the original number. Since they are equal, 371 qualifies as an Armstrong number .
Each function handling negative inputs—`two`, `three`, and arithmetic—starts with a check for negative values among their parameters. When a negative input is detected, an ArithmeticException is thrown, halting the function's execution and propagating control back to the caller, which should handle it. The consistent use of exception handling across these functions showcases a robust strategy to prevent invalid computations and enforces the invariant of using only non-negative numbers, ensuring logical integrity in program operations .
The `swap` function exchanges the values of two numbers `a` and `b` without using a temporary variable. It achieves this by first adding both numbers (`a = a + b`), then assigning the new value of `a` minus `b` to `b` (`b = a - b`), and finally assigning the new value of `a` minus `b` back to `a` (`a = a - b`). This series of operations effectively swaps the values .