Java Practical Programs Overview
Java Practical Programs Overview
The 'for' loop is used for a known number of iterations determined by initialization, condition, and update in a single expression, ideal for collections or ranges. The 'while' loop, seen in Source 2, depends on continuous condition evaluation and repeats until the condition evaluates false and is suitable for cases where iterations depend on dynamic conditions potentially unknown beforehand. Evaluative choice between them relies on readability and operational context .
Source 1 uses the Scanner class to handle user inputs in two separate ways: as integers with `nextInt()` and as strings with `nextLine()`. The program prompts for three integers, sums them, and displays the total in one instance, while another example reads a string from the user, showcasing input handling for both numerical and textual data types using System.in .
The program in Source 1 prints odd numbers from 1 to 9 using a 'for' loop that starts at 1, increments by 2, and continues as long as the loop counter is less than or equal to 10. This ensures only odd numbers (1, 3, 5, 7, 9) are printed. The termination condition of the loop is set by 'i<=10', ensuring no even numbers are included .
The 'do-while' loop in Source 2 guarantees execution of the loop body at least once before checking the continuation condition. This contrasts with the standard 'while' loop, where the condition is checked before the body can execute. This feature of 'do-while' supports conditions where initial iteration is necessary prior to evaluation and continued loops are contingent on post-run verification .
The switch statement in Source 2 directs program execution based on the value of the variable 'number'. Different cases correspond to specific values (10, 20, 30), executing the associated block of code. If none match, the default case is executed, demonstrating a clear alternative execution path when none of the specified cases apply. This illustrates a structured approach to branching logic beyond simple if statements .
The program uses if-else if statements to compare two integers, num1 and num2. If num1 is greater than num2, it prints that num1 is greater; otherwise, if num1 is less, it prints num1 is less. If neither condition is true, then the numbers are equal. The structure allows for clear, conditional checking of each possible relationship between the two numbers .
The program stores double values in an array named 'marks'. It initializes the array with specific values (346, 144, 103, 256.5, 387.5) and uses a for-loop to iterate through the array's elements, printing each value to the console in sequence, demonstrating array initialization and iteration in Java .
In the Java program, random numbers are generated by Math.random(), which returns a double value greater than or equal to 0.0 and less than 1.0. The numbers are then multiplied by 100 to scale up to the range 1 to 100, and an explicit cast to int (int)(Math.random()*100) is performed to truncate any decimal portion, ensuring whole numbers .
The 'rectangle_area' method serves as a user-defined function to calculate the area of a rectangle given its length and breadth. It takes two parameters, performs the area calculation via the formula (length * breadth), and returns the computed area. This demonstrates encapsulation, reusability, and abstraction in Java programming by allowing rectangle area calculations to be defined in a single place and called with different input values .
The Book class includes a constructor that initializes the object's String attributes to empty values and the double attribute ‘price’ to 0. It also contains a display method to print these properties. The program demonstrates object instantiation and initial property setting via the constructor, illustrating encapsulation. Following object creation, the Book's attributes are set to specific values and printed, leveraging the constructor for default configuration and subsequent attribute customization .