Koustubh Java Practicle Assignment 2
Koustubh Java Practicle Assignment 2
The logic involves iterating through each array element, checking if it's odd or even using the modulus operator, and accumulating corresponding sums in separate variables `sumEven` and `sumOdd`. This technique highlights patterns such as parity, revealing insights like the relative distribution of even vs. odd numbers or cumulative parity effects in sequences.
Utilizing packages in Java organizes code into manageable sections, preventing namespace collisions by providing separate scopes for classes. This enhances maintainability by grouping related classes and enabling easier navigation, fostering better scalability and collaboration across larger projects due to clear demarcation of code responsibilities.
The constructor in the Student class within the College package allows for direct initialization of the object's fields when an instance is created. It provides a way to assign initial values to 'rollno' and 'percent', ensuring that the object has a valid state upon creation, which enhances code reliability and readability.
Reversing a string using a for loop with a `StringBuilder` offers simplicity and straightforward implementation with linear time complexity O(n). Alternatives, like using recursion, may introduce additional overhead and complexity without performance gains. Methods using built-in functions like `Collections.reverse()` for lists or Python-like slices in other languages can offer similar efficiency with potentially cleaner code.
The Java program sorts an ArrayList in descending order using the `Collections.sort()` method with `Collections.reverseOrder()` as the comparator. The final outcome is the ArrayList sorted in descending order, which is then printed.
The method to remove duplicate characters involves iterating over the input string and checking each character against a `StringBuilder` that accumulates unique characters. If a character is not present in the `StringBuilder`, it is appended. This approach has a complexity of O(n^2) due to the `indexOf` method used in each iteration, which itself has a complexity of O(n)
The program iterates over the array with a loop, comparing each element to the target number. For each match, a counter is incremented. This strategy is efficient for small arrays but has a linear complexity of O(n), making it less efficient for large arrays due to the need to inspect each element individually.
Encapsulating the `Books`, `Issue`, and `Returns` classes within the Library package ensures modularity and organization, allowing each class to manage specific aspects of library functionality: book data, issue transactions, and returns. This design principle facilitates code maintenance and scalability by separating concerns and promoting reusable code.
The methodology involves iterating through each word split by spaces, and toggling the case of each character using `Character.isUpperCase` and `Character.toLowerCase` methods to flip the character's case. Non-alphabetic characters remain unchanged as the methods only affect alphabetic characters, ensuring other characters do not break the string format.
The program iterates over each matrix row and utilizes the index equality of `matrix[i][i]` to sum elements along the principal diagonal. A limitation is the assumption that the matrix is square (N*N), which is necessary for accessing indices `i,i` safely. There is also an implicit assumption that the matrix is populated with valid integer values.