Tuesday, November 5, 2024

BlueJ Program On 2 D Array and Sorting


Details of The Program: CLICK HERE

import java.io.*; 
public class P 
{ 
void sort(int arr[])          //Taking single dimension array as parameter 
{

int i, j, size = arr.length, temp,flag;

/*'size' holds the number of values in the array */ 
for(i=0;i 
{

flag=0; 
/*Sorting using bubble sort technique, so if there is no exchange of values in one pass there is no need to run the loop */ 
for(j=0;j 
{ 
if(arr[j]>arr[j+1])

{
flag=1; temp = arr[j]; 
arr[j] = arr[j+1]; 
arr[j+1] = temp; 
}

} 
if(flag==0)

break;
}

}

void show(int arr[][]) /*Taking 2D array as parameter*/ 
{

int i, j; for(i=0;i

{

for(j=0;j 
/* arr[i] gives one row at a time. arr[i].length gives the number of values in each row */ 
{ 
System.out.print(arr[i][j]+"\t"); 
}

System.out.println();

} 
}
 void sort2D(int arr[][]) 
{ 

/* In java, array is an object, so here the function calling technique is call be reference means any changes made to the formal parameter in this function will be reflected in the original parameter.*/


int i, j; 
for(i=0;i 
{ 
sort(arr[i]); 
} 
}
public static void main(String args[])throws Exception 
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

P obj = new P(); int arr[][], m, n, i, j; 
System.out.println("Enter the number of rows: "); 
m = 
Integer.parseInt(br.readLine()); 
System.out.println("Enter the number of columns: ");
n = Integer.parseInt(br.readLine()); 
if(m< 2 && m >9||n >9 && n< 2) 
{

System.out.println("Matrix out of range"); System.exit(0);

} 
arr = new int[m][n]; 
for(i=0;i< 
{
System.out.println("Enter values for row no "+(i+1) + ":"); for(j=0;j
{

System.out.print("\nColumn No " + (j+1) + ":"); arr[i][j] = Integer.parseInt(br.readLine()); 
} 
}
System.out.println("Original Matrix: ");
obj.show(arr); 
obj.sort2D(arr);

System.out.println("Matrix after sorting rows: "); obj.show(arr);
}
}

Variable Description

Type
Name
Use
BufferedReader
br
Intakes limit from user
int
arr
2 D array to hold the values
Int
m
Holds no of rows in the array
int
n
Holds no of columns in the


array

int
I,j
Loop Control Variable
int
size
Holds the no of elements in


each row the array
Algorithm




Step 1: Create BufferedReader object ‘br’ and int type variables ‘n’ and 'm' to store the column and row size of the 2 D array.

Step 2: Values of ‘n’ and 'm' are checked for validity and if it is invalid, terminate the program otherwise create the 2 D array 'arr'.

Step 3: Stores the values in the two dimensional array 'arr'.

Step 4: Display the original array


Step 5: Pass the two dimensional array to function 'sort2D()' which sends each rows of the 2 d array to 'sort()' function which sorts the elements bubble sort technique.


Back To 2018 Computer Practical Paper: CLICK HERE

Sunday, April 28, 2024

Program To Check Neon number Using BlueJ

What is neon number:


A neon number is that where sum of digits of square of the number is equal to the number.
For example if the input number is 9, it's square is 9*9 = 81 and sum of the digits is 9. 9 is a neon number


Java Program to check neon number


Download eBook on BlueJ 
import java.io.*;
class Neon
{
int n,sq,sum=0;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
public void showNeon() throws Exception
{
 System.out.println("Enter the number:");
 n=Integer.parseInt(br.readLine());
 sq=n*n;
 while(sq>0)
{
 sum=sum+sq%10;
 sq=sq/10;
}
if(n==sum)
 System.out.println(n+ " is a neon number");
else
 System.out.println(n+ " is not a neon number");
}
public static void main(String args[])throws Exception
{
 Neon ob=new Neon();
ob.showNeon();
}
}

BlueJ Program On Neon Numbers Between 1 And 500

import java.io.*;
class Neon
{
int i,sq,sum=0;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
public void showNeon() throws Exception
{
for(i=1;i<=500;i++)
{
sum=0;
 sq=i*i;
 while(sq>=1)
{
 sum=sum+sq%10;
 sq=sq/10;
}
if(i==sum)
 System.out.println(i+ " is a neon number");
}
}
public static void main(String args[])throws Exception
{
 Neon ob=new Neon();
ob.showNeon();
}
}

Related PostBlueJ Programs on Number



Solved ICSE 2010 Computer Application Paper


This is ICSE 2010 Computer Application Paper. All the questions are solved here. This page may be helpful to ICSE students.


SECTION – A (40 Marks)

Question 1.
      (a)  Define the term Byte Code.  [2]
Ans: Byte code is a highly optimized set of instructions designed to be executed by java runtime system.

(    b)  What do you understand by type conversion? [2]
Ans: Type conversion is the technique of one type of data into the other type. There are two type of type conversion – Implicit conversion and Explicit conversion. In implicit conversion, lower type data is automatically converted into upper side and in explicit conversion, one type of data is converted into the other type forcefully.

    (c)  Name two jump statements and their use. [2]
And: break and continue are two jump statements in java. In java, break can be used inside a loop and switch statement to send the control out of the loop body or switch body.
Continue can be used only inside a loop body and it starts premature iteration of the loop.

     (d)  What is Exception ? Name two Exception Handling Blocks. [2]

Ans: Exception is a run time abnormality caused due to many reasons. Two exception handling blocks are try…catch and throws.



(a)  Write two advantages of using functions in a program. [2]
Ans: Two advantages of using functions in a program can be defined as it breaks the program into number of small modules and secondly it makes the program simple.
Question 2.
(a) State the purpose and return data type of the following String functions:           [2]
(i)         indexOf ( ).
(ii)        CompareTo ( )

Ans: Return data type of both the functions are int. The function indexOf() checks the existence of any single character or a string in the invoking string object and returns the first occurrence location if found otherwise it returns -1.
compareTo () checks two strings for equality. If both the strings are same it returns 0. If the invoking string is greater than the argument string it returns greater than 0 otherwise it returns less than 0.


(b) What is the result stored in x, after evaluating the following expression [2]
 int x = 5;
x = x++ *2 + 3 * –x;

Ans:  -8
(a)            Differentiate between static and non-static data members. [2]
Ans: static data members are class members and so only one copy of such members are created for number of objects of the class. The same static data member is shared by all objects.
For each objects of a class, separate copy of non-static data members are created so each object has it’s own copy of non-static data member.
(b)            Write the difference between length and length ( ) functions. [2]
Ans: length is a data member of array and it holds the number of elements stored in the array. length () is a function of String class and return type of the function is int which denotes the number of characters in the string object.

(c)            Differentiate between private and protected visibility modifiers. [2]
Ans:  private members are accessible within the class only where as protected members are accessible within the class as well as by sub classes.
Question 3
(a)  What do you understand by data abstraction? Explain with an example.[2]

Ans: Data abstraction is the mechanism of binding data members and function members together so that data members are safe from unauthorized access. Function members acts on the data members and thus complexity of the program is reduced.

A car is an object which has many physical components which are the data members of the object. Different functions like brake, horn etc are working on the physical components and we are getting the result without knowing details about the mechanism.

(b) What will be the output of the following code?
 (i)    int m=2;                            [2]
  int n=15;
  for(int i = 1; i<5 i="" o:p="">
  m++; --n;
 System.out.println(“m=” +m);
 System.out.println(“n=”+n);


Ans:  m=6
n=14

(ii)  char x = ‘A’ ; int m;                 [2]
 m=(x==’a’) ? ‘A’ : ‘a’;
 System.out.println(“m=”+m);


Ans:  m=97

Programming Section


Question 4.
 Write a program to perform binary search on a list of integers given below, to search for an element input by the user. If it is found display the element along with its position, otherwise display the message “Search element not found”.   [15]
5,7,9,11,15,20,30,45,89,97
  
Ans:

 import java.io.*;
class BSearch
{
 int i,n,low,high,mid;
int arr[]={5,7,9,11,15,20,30,45,89,97};
BufferedReader br=new BufferedReader (new InputStreamReader (System.in));

    public void input() throws Exception
    {
    System.out.println("Enter the value to search:");
    n=Integer.parseInt(br.readLine());
    search();
   }
   void search()
   {
   low=0;high=9;
    while(low<=high)
{
 mid=(low+high)/2;
 if(arr[mid]==n)
break;
else if(arr[mid]>n)
 high=mid-1;
else
low=mid+1;
}
 if(low<=high)
   System.out.println("Element Found.");
  else
   System.out.println("Search element not found.");
  }

public static void main(String args[])throws Exception
{
BSearch ob=new BSearch();
ob.input();
}
}



Question 5
Define a class ‘Student‘ described as below:             [15]
Data members/instance variables :   name,age,m1,m2,m3 (marks in 3 subjects),
maximum, average
 Member methods :
(i) A parameterized constructor to initialize the date members.
(ii) To accept the details of a student.
(iii) To compute the average and the maximum out of three marks.
(iv) To display the name, age, marks in three subjects, maximum and average.
Write a main method to create an object of a class and call the above member methods.


Ans:


import java.io.*;
class Student
{
 String name;
int age,m1,m2,m3,maximum;
double average;
BufferedReader br=new BufferedReader (new InputStreamReader (System.in));
Student(String n)
{
 name=n;
}
public void input() throws Exception
{
  System.out.println("Enter the age of "+name + ":");
  age=Integer.parseInt(br.readLine());
System.out.println("Enter the 1st subject marks of "+name + ":");
  m1=Integer.parseInt(br.readLine());
System.out.println("Enter the 2nd subject marks of "+name + ":");
  m2=Integer.parseInt(br.readLine());
System.out.println("Enter the 3rd subject marks of "+name + ":");
  m3=Integer.parseInt(br.readLine());

}
   void compute()
 {
 average=(double)(m1+m2+m3)/3;
  if(m1>m2 && m1>m3)
  maximum=m1;
  else if(m2>m1 && m2>m3)
  maximum=m2;
else
maximum=m3;
}
 void show()
{
System.out.println("Name:"+name);
System.out.println("Age:"+age);
System.out.println("First Subject:"+m1);
System.out.println("Second Subject:"+m2);
System.out.println("Third Subject:"+m3);
System.out.println("Maximum marks:"+maximum);
System.out.println("Average marks:"+average);
  }

public static void main(String args[])throws Exception
{
String n;
BufferedReader br=new BufferedReader (new InputStreamReader (System.in));
System.out.println("Name of the student:");
n=br.readLine();
Student ob=new Student(n);
ob.input();
ob.compute();
ob.show();
}
}

Question 6
Shasha Travels Pvt. Ltd. gives the following discount to its customers: [15]
Ticket amount                                                           Discount
Above Rs 70000                                                                       18%
Rs 55001 to Rs 70000                                                            16%
Rs 35001 to Rs 55000                                                            12%
Rs 25001 to Rs 35000                                                            10%
less than Rs 25001                                                      2%
Write a program to input the name and ticket amount for the customer and calculate the discount amount and net amount to be paid. Display the output in the following format for each customer :
SL. NO.
Name
Ticket charges
Discount
Net amount
(Assume that there are 15 customers, first customer is given the serial no (SlNo.) 1, next customer 2 ……… and so on.

 import java.io.*;
class Fare
{
 String name[]=new String[5];
 int amount[]=new int[5];
int i;
double discount,net;
BufferedReader br=new BufferedReader (new InputStreamReader (System.in));
public void input() throws Exception
{
for(i=0;i<15 i="" o:p="">
{
  System.out.println("Enter the name:");
  name[i]=br.readLine();
System.out.println("Enter the amount for ticket in Rs.:");
 amount[i]=Integer.parseInt(br.readLine());
}
}
void show()
{

System.out.println("SL. NO. Name    Ticket charges  Discount   Net amount");
for(i=0;i<15 i="" o:p="">
{
   if(amount[i]>70000)
   {
   discount= amount[i]*18.0/100;
   net=amount[i]-discount;
   }
   else if(amount[i]>=55001 && amount[i]<=70000)
   {
   discount= amount[i]*16.0/100;
   net=amount[i]-discount;
   }
   else if(amount[i]>=35001 && amount[i]<=55000)
   {
   discount= amount[i]*12.0/100;
   net=amount[i]-discount;
   }
   else if(amount[i]>=25001 && amount[i]<=35000)
   {
   discount= amount[i]*10.0/100;
   net=amount[i]-discount;
   }
   else
   {
   discount= amount[i]*2.0/100;
   net=amount[i]-discount;
   }

 System.out.println((i+1)+ "\t\t"+name[i]+ " "+amount[i]+ " "+ discount+ " "+net);
 }
  }

public static void main(String args[])throws Exception
{
Fare ob=new Fare ();
ob.input();
ob.show();
}
}

  
Question 7
Write a menu driven program to accept a number and check and display whether it is a Prime Number or not  OR an Automorphic Number or not. (Use switch-case statement).         [15]
(a)   Prime number : A number is said to be a prime number if it is divisible only  by 1 and itself and not by any other number.     Example : 3,5,7,11,13 etc.
(b) Automorphic number : An automorphic number is the number which is contained in the last digit(s) of its square.
Example:  25 is an automorphic number as its square is 625 and 25 is present as the last two digits.


 import java.io.*;
class Library
{
 public void automorphic(int n)
 {
int no,digit=0,sq,rev=0;
 no=n;
do
{
   digit++;
  no=no/10;
 } while(no!=0);
 sq=n*n;
do
  {
   rev=rev*10+sq%10;
   sq=sq/10;
   digit--;
   if(digit==0)
   break;
   }  while(true);
  no=rev;
  rev=0;
   while(no!=0)
   {
    rev=rev*10+no%10;
    no=no/10;
 }
   if(n==rev)
   System.out.println(n+" is an Automorphic number");
   else
   System.out.println(n+" is not an Automorphic number");
  }
public void prime(int n)
{
 int i;
for(i=2;i
{
 if(n%i==0)
break;
}
if (i==n)
System.out.println(n+" is a prime number");
else
System.out.println(n+" is not a prime number");
}
public static void main(String args[])throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int ch,n;
Library ob=new Library();
System.out.println("Enter 1 for automorphic number and 2 for prime number:");
ch=Integer.parseInt(br.readLine());
System.out.println("Enter the number:");
n=Integer.parseInt(br.readLine());
switch(ch)
{
 case 1:
ob.automorphic(n);
break;
case 2:
ob.prime(n);
break;
default:
System.out.println("Wrong Choice:");
}
}
}

Question 8.
Write a program to store 6 element in an array P, and 4 elements in an array Q and produce a third array R, which does not contain common values of array P and Q. Display the resultant array     [15]
 EXAMPLE:
INPUT                                                         OUTPUT
    P[ ]                            Q [ ]                               R [ ]
4                                 19                                   4
6                                 23                                  6
1                                   7                                   1
2                                   8                                   2
3                                                                       3
10                                                                     10
                                                                         19
23
7
                                                                                8


import java.io.*;

class Merge

{

int p[]=new int[6];
int q[]=new int[4];
int r[]=new int[10];
int i,j,x=0,y=0,k;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

public void takeValues() throws Exception
 {
System.out.println("For 1st array:");

for(i=0;i< 6;i++)
{
System.out.println("Value:");
p[i]=Integer.parseInt(br.readLine());
}

System.out.println("For 2nd array:");

for(i=0;i< 4;i++)
{
System.out.println("Value:");
q[i]=Integer.parseInt(br.readLine());
}

System.out.print("First array:");

for(i=0;i< 6;i++)
System.out.print(" "+p[i]);

System.out.println();

System.out.print("Second array:");

for(i=0;i< 4;i++)
System.out.print(" "+q[i]);

 for(i=0;i< 6;i++)
 {
  for(j=0;j< 4;j++)
  {

  if(q[j]==p[i])
  break;
  }

  if(j==4)
  {
  r[x++]=p[i];
  }

  else
  {
  for(k=j;k< 3;k++)
  {
   q[k]=q[k+1];
   }

   y++;

   }

  }

 for(i=0;i< 4;i++)
 {
  for(j=0;j< 10;j++)
  {
  if(q[i]==r[j])
  break;
  }

  if(j==x)
  {
  r[x++]=q[i];
  }
  }
  }

 public void show()

{
System.out.println();
System.out.print("The resultant array:");
for(i=0;i< x;i++)
System.out.print(" "+r[i]);
}

public static void main(String args[])throws Exception
{
Merge ob=new Merge ();
ob.takeValues();
ob.show();
}
}

 Question 9
Write a program to input a string in uppercase and print the frequency of each character.   [15]
INPUT :   COMPUTER HARDWARE
OUTPUT :
 CHARACTERS                   FREQUENCY
 A                                            2
 C                                             1
 D                                             1
 E                                              2
 H                                             1
 M                                             1
 O                                             1
 P                                              1
 R                                             3
T                                              1
U                                             1
W                                            1




import java.io.*;
class Frequency
{

String s;
int i,len,j,c;
char ch[],ch1;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
public void takeString() throws Exception
 {
System.out.println("Enter the sentence:");
s=br.readLine().toUpperCase();
ch=new char[s.length()];
}
 public void show()
{
len=s.length();
for(i=0;i< len;i++)
ch[i]=s.charAt(i);
for(i=0;i< len-1;i++)
{
 for(j=i+1;j< len;j++)
 {
  if(ch[i]>ch[j])
  {
   ch1=ch[i];
   ch[i]=ch[j];
   ch[j]=ch1;
   }
   }
   }
   ch1=ch[0];
   c=1;
  System.out.println("Characters   Frequency");
for(i=1;i< len;i++)
{
 if(ch1==ch[i])
 c++;
else
{
   System.out.println(ch1+ " \t\t\t\t"+c);
   ch1=ch[i];
   c=1;
}
}
}
public static void main(String args[])throws Exception
{
Frequency ob=new Frequency ();
ob.takeString();
ob.show();
}

}

Subscribe via email

Enter your email address:

Delivered by FeedBurner