Thursday, December 31, 2009

ICSE sample papers on BlueJ (2)


Today again I will discuss on some questions on icse computer application paper.

1. Show the use of logical operator && with an example.

public void show()
{
int
a=5,b=6,c=7,d;
if(a< c &&  a< b)
{
d=a;
}
}

Logical && operator means a number of conditions can be combined using && operator and is all the conditions are true then the block will be executed.

2. Explain the term – “pass by reference”.


In java function can be called using two techniques, one of them is pass by  reference or call by reference. Here objects are passed to a function as argument and the actual argument object in the function definition refers the passed object. Any modification done on the data members of the actual argument object causes a change in the original object.

Differentiate between indexOf() and valueOf() functions.

Both indexOf() and valueOf() are static functions of String class.
indexOf() function takes a string or character as argument and returns it’s first occurance location . So the return type of this function is int value. This function has several overloaded versions.
valueOf()  function takes any non string value as argument and returns it’ string version.

 Program to find the frequency of each digit in a number and the sum of the digits
  
A class Number has been defined to find the frequency of each digit present in it and the sum of the digits.

 Some of the members of the class Number are given below:
Class name   Number
Data member   num – long integer type
Member functions:
Number( )   constructor to assign 0 to num
Number(long a)  constructor to assign a to num
void digitFrequency( )  to find the frequency of each digit and to display it.
int sumDigits( )  to returns the sum of the digits of the number.
Specify the class Number giving the details of the two constructors and functions void digitFrequency( ) and 
int sumDigits( ). You do not need to write the main function.


class Number
{
private  long num;
public Number()
{
num=0;
}
public Number(long a)
{
num=a;
}
public void digitFrequency()
{
int one,two,three,four,five,six,seven,eight,nine,zero;
one=0;
two=0;
three=0;
four=0;
five=0;
six=0;
seven=0;
eight=0;
nine=0;
zero=0;
for(long i=num;i > 0;i=i/10)
{
if(i%10==0)
zero++;
else if(i%10==1)
one++;
else if(i%10==2)
two++;
else if(i%10==3)
three++;
else if(i%10==4)
four++;
else if(i%10==5)
five++;
else if(i%10==6)
six++;
else if(i%10==7)
seven++;
else if(i%10==8)
eight++;
else if(i%10==9)
nine++;
 }
 System.out.println("Zero Digit="+zero);
System.out.println("one Digit="+one);
System.out.println("Two Digit="+two);
System.out.println("Three Digit="+three);
System.out.println("Four Digit="+four);
System.out.println("Five Digit="+five);
System.out.println("Six Digit="+six);
System.out.println("Seven Digit="+seven);
System.out.println("Eight Digit="+eight);
System.out.println("Nine Digit="+nine);
}
public int sumDigits()
{
int sum=0;
for(long i=num;i > 0;i=i/10)
{
sum+=i%10;
}
return sum;
}
}


Program on merging two ascending lists

There is class Ascending that contains integers that are already arranged in ascending order. Some of the member functions of Ascending are given below:
Class name    Ascending
Data member
int a[ ]     an array of integers sorted in ascending order.
int size     number of integers in the array

Member function

Ascending( int n)   constructor to create an Ascending list of size n
void displayList( )    to display the list of integers
Ascending merge(Ascending a1) to merge the ascending list a1 with the current Ascending list object and return a third Ascending list which is also sorted in ascending order.

Important: While generating the final Ascending list both the original Ascending lists must be scanned only once. Elements common to the two list should appear only once in the third Ascending list.

Specify the class Ascending giving details of the function void displayList( ) and Ascending merge(Ascending  a1) only.

import java.io.*;
class Ascending
{
private int size,a[];
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
public Ascending(int n)throws Exception
{
size=n;
a=new int[size];

for(int i=0;i< size;i++)
{
System.out.println("Value=");
a[i]=Integer.parseInt(br.readLine());
}
}
public Ascending()
{

}
public void displayList()
{
for(int i=0;i< size;i++)
{
System.out.println(a[i]+ " ");
}
}
public Ascending merge(Ascending a1)throws Exception
{
Ascending temp=new Ascending();
temp.size=size+a1.size;
temp.a=new int[temp.size];
int i=0,j=0,x=0;
while(i< size && j< a1.size)
{
if(a[i]< a1.a[j])
{
temp.a[x++]=a[i++];

}
else if(a1.a[j] < a[i])
{
temp.a[x++]=a1.a[j++];
}
else
{
temp.a[x++]=a1.a[j];
j++;
i++;
}
}

if(i < size)
{
for(;i < size;i++)
{
temp.a[x++]=a[i];
}
}
else
{
for(;j< a1.size;j++)
{
temp.a[x++]=a1.a[j];
}
}
return temp;
}
}

4 comments:

  1. I'm using BlueJ to test a Bluetooth App.
    I imported these classes:
    import java.io.BufferedReader;
    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.ObjectOutputStream;
    import java.io.OutputStream;
    ...

    when I compile it complains given an error "cannot find sysmbol - class BufferedReader"
    What must I do to fix this?

    ReplyDelete
  2. Can you send me the code so that I will check it? From the import statement it seems o.k.

    ReplyDelete
  3. sir, could you explain me what is happening in the main() of Ascending merge(Ascending mat)

    ReplyDelete
  4. The main () function will be like
    public static void main(String args[]) throws Exception
    {
    Ascending obj1,obj2,obj3;
    BuffredReader r=new BufferedReader(new InputStreamReader(System.in));
    int n;
    System.out.println(“Number of elements for the 1st object:”);
    n=Intger.parseInt(r.readLine());
    obj1=new Ascending(n);
    System.out.println(“Number of elements for the 2nd object:”);
    n=Intger.parseInt(r.readLine());
    obj2=new Ascnding(n);
    obj3=obj1.merge(obj2);
    System.out.println(“Merged elements as follows:”);
    obj3.displayList();
    }

    ReplyDelete

Subscribe via email

Enter your email address:

Delivered by FeedBurner