Tuesday, June 15, 2010

BlueJ Programs on in Displaying Average of Array Elements

We will go through few program 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 BlueJ program on array, 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 . 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);
}
}

Related Post: BlueJ Programs on Numeric Array

14 comments:

  1. can u plzzz solve dis series----

    1-3+5-7+.....+n

    ReplyDelete
  2. Here is the program

    class Pattern
    {
    int i,sum=1;
    public void show(int n)
    {
    for(i=1;i<=n;i++)
    {
    System.out.print(i);
    if(i%2==0 && i!=n)
    {
    System.out.print("+");
    sum=sum+i;
    }
    else if (i%2!=0 && i!=n)
    {
    System.out.print("-");
    sum=sum-i;
    }
    else
    System.out.print("=");
    }
    System.out.print(sum);
    }
    public static void main(String args[])
    {
    Pattern ob=new Pattern();
    ob.show(9);
    }
    }

    ReplyDelete
  3. i want the java program to find Special numbers less than 50 ! i hav also some doubts regarding call by value and call by reference ! so plzz help me out !

    ReplyDelete
  4. Regarding call be value and call by reference, you will get short description on post of 17 May 2010. In future I will post about it in details.

    Special number program will be posted very soon.

    ReplyDelete
  5. Q.Enter the date from the user and a number find out the date after n days

    ReplyDelete
  6. Q. Enter a string from the user and a word find out that frequency of that word in a string and display it.

    ReplyDelete
  7. Have you searched the program in my blog?

    ReplyDelete
  8. Search the program in my blog

    ReplyDelete
  9. Search ' ISC 2011-solved computer application practical paper' in my blog

    ReplyDelete
  10. i want the code for a program where from a given array a random value is obtained.
    for example in A[] = {a,s,d,f,g,h}
    any one of the letters should be displayed.
    in this case either a or s or d or f or g or h should be shown on the terminal window.
    the array is not to be displayed on the terminal screen.
    only one letter is to be shown randomly.

    ReplyDelete
  11. import java.util.*;
    public class Series
    {
    char ch[]={'a','w','t','h','k','d'};
    int i=0,x,len;
    Random rd;
    Series()
    {
    rd=new Random();
    }
    public void show()throws Exception
    {
    while(true)
    {
    len=ch.length;
    x=rd.nextInt(len);
    System.out.println(ch[x]);
    i++;
    try
    {
    Thread.sleep(1000);
    }catch(Exception e){}
    if(i==10)
    break;
    }
    }
    public static void main(String args[]) throws Exception
    {
    Series ob=new Series();
    ob.show();
    }
    }

    ReplyDelete
  12. thanks sir but could you tell me what is random and thread and sleep

    ReplyDelete
  13. Random is a class of java.util package. the method 'nextInt(int x)' of Random class generates random number within 'x'.
    Thread is a class of java.lang package and thread object represent control. The static method 'sleep (int millisecond)' takes an argument in milliseconds and suspends the control for that specified time. That's why you are getting the output after some interval.

    ReplyDelete

Subscribe via email

Enter your email address:

Delivered by FeedBurner