Java Programs for 2D Arrays and Classes
Java Programs for 2D Arrays and Classes
A Java program computes and displays tax on annual taxable income by utilizing if-else statements to apply specific tax rates to income ranges. The tax rules are: no tax up to Rs. 1,00,000; 10% on income between Rs. 1,00,001 and Rs. 1,50,000; 20% on income between Rs. 1,50,001 and Rs. 2,50,000 plus Rs. 5000; and 30% on income above Rs. 2,50,000 plus Rs. 25,000. Tax is calculated incrementally based on these brackets .
To implement a method that checks if a 2D array contains only even numbers and then prints those elements, iterate through each element of the array using nested loops, check if each element is even by using the modulus operator (element%2==0), and print those elements conditionally. This approach requires careful handling of indices to traverse the array correctly and ensure accurate condition checking and output .
Linear searching in an array of double datatype in Java involves sequentially checking each element of the array until the desired value is found or the end of the array is reached. If the value is found, the program displays the message 'Found' along with its position; if not, it displays the message 'Not found'. This method involves iterating over the array and using conditional checks for the match .
In a Java class, the discount on book prices is calculated based on price brackets. If the price is less than or equal to Rs. 1000, a 5% discount is applied; if the price is more than Rs. 1000 but less than or equal to Rs. 3000, a 10% discount is applied; and if the price exceeds Rs. 3000, a 20% discount is applied. The discounted price is calculated by multiplying the price with the applicable discount percentage and subtracting it from the original price .
To identify a lead number in a Java program, the program must compute the sum of the even digits and the sum of the odd digits of a given number. A number is considered a lead number if the sum of its even digits equals the sum of its odd digits. For example, for the number 3669, the sum of odd digits (3 and 9) equals 12, and the sum of even digits (6 and 6) also equals 12, making 3669 a lead number .
A Java class can compute and display the net salary of an employee by employing member methods like void calculate() and void print(). The calculate() method computes the net salary using formulas: Net = basic + hra + da - pf, where hra is 18.5% of basic salary, da is 17.45% of basic salary, and pf is 8.10% of basic salary. Employees above 50 years receive an additional Rs.5000. The print() method then displays the employee's details including the net salary .
A Java program determines the range of values in an integer array by computing the difference between the largest and the smallest elements in the array. This requires scanning through the array to identify the maximum and minimum values, then calculating their difference. For example, if the smallest number is 3 and the largest is 15 in the array, the range is 15 - 3 = 12 .
An EvenPal number is one whose number is a palindrome and whose sum of digits is an even number. The algorithm involves checking if a number reads the same forwards and backwards to verify it's a palindrome, and calculating the sum of all its digits to confirm this sum is even. For instance, the number 505 is an EvenPal because reverse(505) = 505 and the sum 5+0+5 = 10 is even .
The allocation of a stream to a student in Java is performed by the method void allocation(), which assigns streams based on the student's marks. If marks are greater than or equal to 300, the student is allocated to Science and Computer; if marks are between 200 and less than 300, they are allocated to Commerce and Computer; if marks are between 75 and less than 200, they get Arts and Animation; and if marks are less than 75, they need to 'Try again' .
For a 4x4 integer array to be classified as a diagonal array in Java, the sums of its left and right diagonals must be equal. This involves calculating the sum of elements from the top-left to bottom-right corner and comparing it with the sum of elements from the top-right to bottom-left corner. For instance, if both diagonal sums equal 11, as in the example provided, then the array is classified as a diagonal array .