Java Lab Programs for B.Com Students
Java Lab Programs for B.Com Students
The 'if-else' structure in Java demonstrates decision-making by evaluating a condition and executing a set of statements based on the condition's truth value. Specifically, an 'if' statement checks if a condition, such as whether a number is even (e.g., number%2==0), holds true. If the condition is true, it executes the 'if' block; otherwise, it executes the 'else' block to handle false conditions. This mechanism allows for branching logic in programs .
A 'do-while' loop in Java executes the loop body at least once before checking its condition, ensuring that actions within the body occur prior to condition evaluation. In the example, "Hello World" is printed five times as the body is executed before checking if 'i' is less than 6. This loop structure guarantees that the content within the loop runs no matter the initial condition, differing from a 'while' loop which checks conditions before the initial execution .
Inheritance in Java allows a new class to inherit properties and behaviors from an existing class, promoting code reuse and the creation of hierarchical classifications. In the provided example, the 'MountainBike' class extends the 'Bicycle' class, inheriting its fields (gear and speed) and methods (applyBrake and speedUp). 'MountainBike' can then add new features, like seatHeight, and override methods, which allows it to customize behavior. This structure eliminates code duplication and enhances maintainability by avoiding rewriting code for similar functionalities .
The 'switch' statement in Java is used for executing one among multiple code blocks based on the value of a single variable. In the example provided, the switch statement evaluates the integer variable 'month'. It checks against case labels (integers representing months) and executes the accompanying block for the matching case. If 'month' is 7, the 'case 7' block runs, assigning "7 - July" to 'monthString'. This feature efficiently handles multiple conditional branches compared to several 'if-else' statements .
The method `largest()` demonstrates the concept of iteration and comparison within arrays. It ensures finding the highest number by using a for-loop that traverses each element beyond the initial element assumed as the maximum. During iteration, it compares the 'max' with each element of the array, and whenever an element is found to be greater, 'max' is updated. This guarantees that by the loop's completion, 'max' contains the largest array element .
The Java program demonstrates basic arithmetic operations: addition, subtraction, multiplication, division, and modulo with two integers (12 and 5). It shows that the addition sums values, subtraction computes the difference, multiplication yields the product, division gives the quotient, and modulo finds the remainder. Understanding these operations enhances knowledge of operator precedence, integer division behavior, and remainder computation, pivotal for algorithm development and problem-solving tasks .
String manipulation in the Java example is achieved by creating a new string that iterates over each character of the original string. At a specified index, the program inserts the substring by appending it to the new string. This approach essentially builds a new string from the original up to the insert point, adds the new content, and continues with the remaining original string portion. It effectively demonstrates basic string dynamism and manipulation without altering the original string directly .
The Java program determines the maximum value in an array by initializing a variable 'max' with the first element of the array. It then iterates through each element of the array starting from the second position, comparing each to 'max'. If an element exceeds the current 'max', it updates 'max' with this element's value. This iteration ensures that 'max' holds the greatest value by the loop's end, effectively finding the largest element .
The provided single inheritance example simplifies inheritance by creating a simple parent-child relationship between classes. The class 'Two' extends 'One', inheriting the 'print_geek()' method. 'Two' introduces an additional method, 'print_for()', illustrating how a derived class can extend functionality. The example tests inheritance by instantiating 'Two', calling inherited and newly-defined methods, thus showing code reuse's practical benefit without needing to replicate method definitions in multiple classes .
The Java program for matrix addition handles operations by iterating over each element in the two 4x4 matrices (A and B) using nested for-loops. For each corresponding element of A and B, it computes the sum and stores the result in a new matrix C. The program then outputs matrix C, showing the sum of corresponding elements from A and B. This results in the resultant matrix C having double the values of original matrices since each element in the matrices being summed are identical .