Monday, July 5, 2010

BlueJ numeric array programs


This is a continuation of array in BlueJ for ICSE Schools.  Previously we have seen some programs on array in java. Few program common in ICSE computer applications sample papers are given below.
  
 An election is contested by 5 candidates numbered from 1 to 5 and the voting is done by marking the candidate number on the ballot paper. Write a program to read the ballots and count the votes cast for each candidate using an array variable count. In case the number read is outside the range 1 to 5 , the ballot should be considered as 'spoilt ballot' and the program should also count the number of spoilt ballots

import java.io.*;
class Arr
{
int count[] =new int[1000];
int  one,two,three,four,five,spoilt,n,i;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
public void takeDate() throws IOException
{
 one=two=three=four=five=spoilt=0;
 System.out.println("Enter the number of voters\t");
n=Integer.parseInt(br.readLine());
  for(i=0;i< n;i++)
 {
System.out.println ("Choice\t");
count[i]=Integer.parseInt(br.readLine());
 }
  for(i=0;i< n;i++)
  {
   if(count[i]==1)
   one++;
   else if(count[i]==2)
   two++;
   else if(count[i]==3)
   three++;
   else if(count[i]==4)
   four++;
   else if(count[i]==5)
   five++;
   else
   spoilt++;
   }
System.out.println ("\nNumber of votes for first candidate\t%d",one);
   System.out.println ("\nNumber of votes for second candidate\t%d",two);
   System.out.println ("\nNumber of votes for third candidate\t%d",three);
System.out.println ("\nNumber of votes for 4rth candidate\t%d",four);
   System.out.println ("\nNumber of votes for 5th candidate\t%d",five);
System.out.println ("\nNumber of spoilt ballot\t%d",spoilt);
}
}

No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner