We will go through few programs on array in java . I think it would help students of ICSE and ISC in their computer application paper.
Suppose we want to enter ages of n number of persons in an array. Here the user will decide the array size.
import java.io.*;
class Arr
{
int arr [ ], n;
BufferedReader br=new BufferedReader(new InputStreamReader ( System.in));
void takeAge() throws IOException
{
System.out.println(“ How many Persons:”);
n=Integer.parseInt(br.readLine());
arr=new int[n];
for (i=0;i< n;i++)
{
System.out.println(“ How many persons ");
arr[i]=Integer.parseInt(br.readLine());
}
System.out.println(“Entered Ages are as follows “);
for (i=0;i< n;i++)
{
System.out.println(“ “+ arr[i]);
}
}
}
From the above java program, it is clear that the user decides the size of the array and it is possible as java array is dynamic. But consider another case. Suppose the user don’t know how many values he has to store at the start of the program. As he continues entering values, at some point he has to stop.
In such case we will create an array with maximum size. For example if we want to ages of students of a particular class of a school, the number of students in a class can’t be more than 500 in any case. So user can go on entering values without any risk.
import java.io.*;
class Arr
{
int arr [ ], [500], n;
BufferedReader br=new BufferedReader(new InputStreamReader ( System.in));
arr=new int[500];
char ch=’y’;
void takeAge() throws IOException
{
while (ch !=’n’)
{
System.out.println(“ Enter age:”)
arr[n++]=Integer.parseInt(br.readLine());
System.out.println(“Any more student (y/n):”);
ch=(char)br.readLine();
}
System.out.println(“Entered Ages are “);
for (i=0;i< n;i++)
{
System.out.println(“ “+ arr[i]);
}
}
}
Here is another program on array in java. Take 'n' number of integer values from the user; display the value of 'n' and the average of the numbers.
import java.io.*;
class Arr
{
int number [];
char ch='y';
number=new int[500];
void takedata() throws IOException
{
int i=0, sum=0;
while (ch!='n')
{
System.out.println ("Enter the number:");
number[i++]=Integer.parseInt(br.readLine());
sum=sum+number [i];
System.out.println ("Any more? (y/n)");
ch=(char)br.readLine();
}
System.out.println ("The total is :”+ sum);
System.out.println ("Average is :"+(float) sum/counter);
}
}
Our next java program on array is an interesting one. The user will enter the month number (1 to 12), date and year. Our program will display the total number days from the start of the year.
import java.io.*;
class MyDay
{
int month,day,tday,year,i,lip=0;
boolean a=true,b=true;
int dpm[12]={31,28,31,30,31,30,31,31,30,31,30,31};
void take() throws IOException
{
while(a)
{
System.out.println ("enter the month(1to12):-");
month=Integer.parseInt(br.readLine());
if(month>12)
{
System.out.println ("Enter the proper month:-");
continue;
}
a=false;
}
while(b)
{
System.out.println ("Enter the day(1to31):-\n");
day=Integer.parseInt(br.readLine());
if(day >31)
{
System.out.println ("enter the proper day:-\n");
continue;
}
b=false;
}
tday=day;
System.out.println ("Enter the year:-");
year=Integer.parseInt(br.readLine());
for(i=0;i< month-1;i++)
{
tday=tday+dpm[i];
}
for(i=0;i< 2;i++)
{
if(year%10!=0)
break;
year=year/10;
}if(i==2)
{
if(year%400==0)
lip=1;
}
else if(year%4==0)lip=1;if((lip==1)&&(month>2))
tday=tday+1;
System.out.println("total days from start of the year is:”+tday);
}
}
This site is on icse java programs. If you have any doubt in any program, pl. feel free to put your comments. I am here to clear your doubts.
Related Post: BlueJ Programs on Numeric Array
Related Post: BlueJ Programs on Numeric Array
i think i need step by step understanding of the program.
ReplyDelete