Java Lab Experiments in Eclipse
Java Lab Experiments in Eclipse
The Java program creates a pyramid pattern using nested loops to control the spacing and number of asterisks per row . The outer 'while' loop manages the number of pyramid levels as defined by 'n', where 'i' represents the current pyramid level from 1 to 'n'. The first inner 'while' loop introduces spaces, this loop iterates 'n-i' times each level to format the left-aligned edges and center the pyramid shape. The second inner 'while' loop then prints asterisks ('*') for each level, generating '2*i-1' asterisks at each level, incrementing with each iteration of 'i'. Hence, the nested loops collaboratively render the rows of the pyramid, with the total structure reflecting a symmetric increase of space and asterisk formation .
The Java calculator program uses a switch-case structure to determine and perform the desired arithmetic operation based on user input . Once the user inputs two operands and an operation character (e.g., '+', '-', '*', '/'), the program evaluates the operation character within the switch statement. Each case corresponds to a particular arithmetic operation: addition, subtraction, multiplication, or division. Depending on the input character, the control transfers to the associated case block, where the specific operation is computed on the two input numbers, resulting in the output being stored in a variable 'result'. If an invalid operation character is input, a default case handles it, printing an error message for unsupported operations, thereby ensuring robust handling of inputs .
The Java program identifies whether a character is an alphabet using conditional logic involving character range checks . It prompts the user to enter a character, then extracts the first character of the input string. The program performs a check to see if the character falls within the ranges of 'a' to 'z' or 'A' to 'Z'. These ranges check whether the character is a lowercase or uppercase alphabet, respectively. If the character satisfies either condition, it is deemed an alphabet; otherwise, it is categorized as a non-alphabet character .
In the Java program for generating the Fibonacci series, a loop-based structure is employed . The program first initializes two integer variables, a and b, to 0 and 1 respectively, which represent the first two numbers in the Fibonacci sequence. Using a for-loop, it iterates from 0 to n-2, where 'n' is the number of terms desired, as input by the user. Within the loop, a new Fibonacci number 'c' is computed as the sum of the preceding two numbers, 'a' and 'b'. After printing 'c', the values of 'a' and 'b' are updated to 'b' and 'c', respectively, for use in the next iteration. This process continues, printing each subsequent Fibonacci number, until the loop completes, thus generating the Fibonacci sequence up to 'n' terms .
The Java program counts the number of digits in an integer by repeatedly dividing the number by 10 until the integer becomes zero . This approach operates on the principle that dividing an integer by 10 truncates its last digit, and hence, reduces the number by one digit. A counter variable keeps track of the number of times the division operation is performed. This method is effective because it works universally for any positive integer, utilizing simple arithmetic operations and loop constructs to accurately count and print the total number of digits present in the given integer .