0% found this document useful (0 votes)
17 views6 pages

Java Programs for 2D Arrays and Classes

Uploaded by

mayanksmohite
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views6 pages

Java Programs for 2D Arrays and Classes

Uploaded by

mayanksmohite
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Important Java programs

1)Write a program to fill double dimensional array of order 4 X 5. Print the even elements of
the array.
2)Fill a 2D array of order 4 x 4 print the left and right diagonal elements of the array.
3) Fill a double dimensional array of order 4 X 4 print the largest element of the left diagonal.
4) Define a class with the following specifications:
Class name student
Member variables name-name of student
age-age of student
mks-marks abtained
stream-stream allocated
(Declare the variables using appropriate data types)
Member methods:
void accept() – accept the details using scanner class
void allocation() – Allocate the stream as per following criteria

mks stream
>=300 Science and Computer
>=200 and < 300 Commerce and Computer
>= 75 and < 200 Art and Animation
< 75 Try again

void print() – Display Student name,age,mks and stream allocated


void main() – to create an object of the class and invoke the methods.

5) Define a class to overload the method print as follows:


void print() – to print the format
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4
5 5 5 5 5

void print (int n) To check whether the number is a lead number. A lead
number is the one whose sum of even digits are equal to
sum of odd digits

eg. 3669 odd digits sum= 3 + 9 =12

even digits sum= 6 + 6 =12

3669 is a lead number.

6)Using the switch statement, write a menu driven program for the following:
1)5 4 3 2 1 2) I
5432 IC
543 ICS
54 ICSE
5
For the incorrect option, an appropriate error message should be displayed.
7) Define a class to accept values into an integer array of size 10 and print range of the
array, range is the difference between the largest and the smallest elements of array.
[15]

8) Define a class to accept values in an integer array of size [Link] sum of one digit number
and sum of two digit numbers entered. Display them separately
Eg. Input- a[]={2,12,4,9,18,25,3,32,20,1}
Output- Sum of one digit numbers=2+4+9+3+1=19
Sum of two digit numbers= 12+18+25+32+10=107.
9)Compute the tax according to the Annual taxable income, given the following conditions
and print it.
Taxable Income (TI) in Rs Income Tax
Up to Rs. 1,00,000 Nil
From Rs. 1,00,001 – 1,50,000 10% of the income exceeding Rs. 1,00,000

From Rs. 1,50,001 – 2,50,000 20% of the income exceeding Rs. 1,50,000 + Rs.
5000
Above Rs. 2,50,000 30% of the income exceeding Rs. 2,50,000 +
Rs.25,000

10)Define a class to accept values into an array of double datatype of size [Link] a
double value from user and search in the array using linear search [Link] value si
found display the message “Found” with its position where it is present in the array
otherwise display message “Not found”.
Define a class with the following specifications:
Class name employee
Member variables eno – employee number
Name – name of the employee
Age- age of the employee
Basic – basic salary
(Declare the variables using appropriate data types)
Member methods:
void accept() – accept the details using scanner class
void calculate() – to calculate the net salary as per the given specifications:
Net = basic+hra+da-pf
Hra = 18.5% of basic
Da = 17.45% of basic
Pf= 8.10% of basic
If the age of the employee is above 50, he/she gets an additional allowance of rs.5000.
void print() – to print the details as per the following format
Eno ename age basic net
void main() – to create an object of the class and invoke the methods.
11)Define a class named book fair with the following description
Instance variable/data members:
String Bname – stores the name of the book.
Double price – stores the price of the book.
Member methods :
i). BookFair() – Default constructor to initialize data members.
ii). void Input -To input and store the name and price of the book.
iii) void calculate()- To calculate the price after discount. Discount is calculated based on
the following criteria.
Price Discount
Less than equal to 1000 5 % of price
More than 1000 and less than or equal to 10 % of price
3000
More than 3000 20 % of price

iii). Void display() – to display the name and price of the book after discount.
12)Define a class to accept a number from user and check if it is an EvenPal number or
not.( The number is said to be EvenPal when number is palindrome number(reverse of a
number) and sum of the digit is an even numbers.
Eg-505 is a palindrome numbers and
Sum of digits= 5 +0 +5 = 10
13) Define a class to accept values into integer array of order 4 X 4 and check whether it is
DIAGONAL array or [Link] array is DIAGONAL array if sum of left diagonal is equal to
sum of right diagonal. Print the message.
Example=
3 4 2 5
2 5 2 3
5 3 2 7
1 3 7 1
Sum of left diagonal= 3+5+2+1=11
Sum of right diagonal=5+2+3+1=11

solution

import [Link].*;
class digit
{
public static void main(String args[])
{
int a[]=new int[10];
int i,digit1=0,digit2=0;
Scanner sc=new Scanner([Link]);
[Link]("Enter elements in array");
for(i=0;i<=9;i++)
{
[Link]("Enter element in"+i+"Location");
a[i]=[Link]();
}
//Checking for single or double digit number
for(i=0;i<=9;i++)
{
if (a[i]>=0&&a[i]<=10)
{
digit1=digit1+a[i];
}
else if(a[i]>=11&&a[i]<=100)
{
digit2=digit2+a[i];
}
else
{
[Link]("Enter one or two digits number only");
}
}
[Link]("Sum of single digit number="+digit1);
[Link]("Sum of double digit number="+digit2);
}
}

Common questions

Powered by AI

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 .

You might also like