Programming 2 Lab Exercises
Programming 2 Lab Exercises
The isprime(n) method determines if an integer is prime by checking divisibility only by 1 and itself. For example, with input 11, the method would attempt to divide 11 by every integer from 2 up to the square root of 11. Since none of these divisions result in an integer, 11 is concluded to be a prime number. The method returns true since the only factors of 11 are 1 and 11 itself .
The method examines each digit of the input number, checking if it is even by using the modulus operation (digit % 2 == 0). If it finds any digit that is odd, it immediately returns false. For the input 8642, all digits are even, so the method iterates through each and returns true, confirming that every digit is even .
The method calculates word count by splitting the string on spaces. Based on the test data, it accurately counts 9 words. However, its effectiveness in handling edge cases like multiple spaces between words could vary. A robust implementation should trim spaces and ensure continuous spaces don't affect the true word count. It's crucial for correctness in diverse input cases .
In the first homework question, the method set(int x) demonstrates the concept of immutability by not altering the original variables in the main method. When set is called with x and y, it only modifies the local copy of the variable x within the method's scope, therefore not affecting the original x and y in the main method. Immutability is shown as the primitive types are passed by value, hence the outputs remain as X= 10 ,Y= 11 .
The method compares three numbers using conditional statements to determine the smallest. For inputs 25, 37, and 29, it first checks if 25 is less than both 37 and 29; it is, so it identifies 25 as the smallest. The logic ensures each number gets compared appropriately, identifying the minimum of the set .
In the Test1 class, method overloading is demonstrated by defining two methods with the same name, func, but with different parameter types. The first func accepts an int and a double, while the second takes a double and an int. This allows different operations depending on the order and type of arguments provided. The JVM determines which method to call based on the arguments. Therefore, func(a, b) invokes the first version, adding x with y/2, yielding 4.5, and func(b, a) calls the second version, resulting in (5 - 2*2) or 1.0 .
The Power() method can be implemented using a loop to multiply x by itself y times, or alternatively using recursion where y equals zero returns one, and otherwise, it returns x multiplied by the Power() method called with x and y-1. This iteration or recursion accumulates the result of x raised to the power of y without relying on Java's pow() function .
The swap1 method effectively alters the contents of the array due to Java's pass-by-reference nature for arrays. When the method is called with an array, the method accesses and modifies the original array elements directly, swapping the first and second elements. This is reflected in the change from '2,6' to '6,2' before and after the call. This demonstrates successful in-place modification of the array contents .
The isPalindrome method checks if a string reads the same forwards and backwards by comparing characters from both ends moving toward the center. For instance, for input 'racecar', the method successively checks if 'r' equals 'r', 'a' equals 'a', and so on. If any characters don't match, it returns false; otherwise, true when all matched .
Encapsulation in the swap1 method is exemplified by passing the entire array reference, allowing the method to encapsulate the sorting logic internally without exposing internal state manipulation outside. This aligns with encapsulation's principle of detail hiding and interface exposing, making the method effective as it modifies data structure through direct reference .