icse march: 2014: java programs using bluej

56
JAVA FOR ICSE BlueJ (Computer Applications) By Dheeraj Mehrotra (President Awardee) http://dheerajmehrotra.tripod.com Buy online at www.flipkart.com

Upload: dr-dheeraj-mehrotra

Post on 24-Oct-2014

4.966 views

Category:

Documents


3 download

DESCRIPTION

Java, BlueJ, Dheeraj Mehrotra, ICSE Computer Applications, Solution for ICSE Computer Applications by Dr. Dheeraj Mehrotra (National Awardee)

TRANSCRIPT

Page 1: ICSE MARCH: 2014: JAVA PROGRAMS USING BLUEJ

JAVA FOR ICSE

BlueJ(Computer Applications)

By Dheeraj Mehrotra(President Awardee)

http://dheerajmehrotra.tripod.com

Buy online at www.flipkart.com

// Program to print first ten multiples of the number.

import java.io.*;

Page 2: ICSE MARCH: 2014: JAVA PROGRAMS USING BLUEJ

class hg{

public static void main(String args[]){

hg obj=new hg();obj.multiple(5);

}void multiple(int n){

if(n<=50){

System.out.println(n);multiple(n+5);}

}}

// Program to input a string and decode the same.

import java.io.*;class matrix6{

public static void main (String args[])throws IOException{

int b,c,p=0,m=0,l,i,j,n,d=0,r=0;String s;InputStreamReader x=new InputStreamReader(System.in);BufferedReader y= new BufferedReader (x);System.out.println("ENTER THE LENGTH OF THE CODE");n=Integer.parseInt(y.readLine());

if(n>6) { p=-1; System.out.println("INVALID_length should not exceed 6

characters"); } if(n<=6) { System.out.println("ENTER THE CODE"); s=y.readLine(); l=s.length(); if(n!=l) { d=-1; System.out.println("INVALID_length not same as

specified"); } for(i=0;i<l-1;i++) { for(j=i+1;j<l;j++) { b=(int)s.charAt(i);

Page 3: ICSE MARCH: 2014: JAVA PROGRAMS USING BLUEJ

c=(int)s.charAt(j); if(b>=97 && b<=122) { p=-1; } if(p==-1 && d==0) { System.out.println("INVALID_only uppercase

letters permitted"); d=-1; } if(b==c) { r=-1; if(r==-1 && d==0) { System.out.println("INVALID_repetition of

characters not allowed"); d=-1; } } } } if(d!=-1) { for(i=0;i<l-1;i++) { b=(int)s.charAt(i); c=(int)s.charAt(i+1); if(c!=(b+2)) { m=-1; } } if(m==0) { System.out.println("VALID"); } else { System.out.println("INVALID_only alternate letters valid"); } } }

}}

// Program to input a string and print the same in alphabetical order.

import java.io.*;class jkhg

Page 4: ICSE MARCH: 2014: JAVA PROGRAMS USING BLUEJ

{public static void main(String args[])throws IOException{

String s;char b,ch;int d=0,c=0,i,l,p=0,k;InputStreamReader x=new InputStreamReader(System.in);BufferedReader y=new BufferedReader(x);System.out.println("ENTER A STRING");s=y.readLine();s=s.toLowerCase();s=s.concat(" ");l=s.length();for(k=97;k<122;k++){for(i=0;i<l;i++){

b=s.charAt(i);if(b!=' '){

d=d+1;}else{

ch=s.charAt(i-d);c=(int)ch;if(c==k){

p=i-d;System.out.print(s.substring(p,i)+" ");

}d=0;

}}}}}

// Program to perform Column Arrangement of an array.

import java.io.*;class xy{

public static void main(String args[])throws IOException{int temp=0,i=0,j=0;InputStreamReader x=new InputStreamReader(System.in);BufferedReader y=new BufferedReader(x);int a[][]=new int[3][3];System.out.println("enter no.");for(i=0;i<3;i++)

Page 5: ICSE MARCH: 2014: JAVA PROGRAMS USING BLUEJ

{for(j=0;j<3;j++){a[i][j]=Integer.parseInt(y.readLine());}}for(i=0;i<3;i++){for(j=0;j<3;j++){System.out.print(a[i][j]);}System.out.println();}int p=0;for(i=0;i<3;i++){p=0;

for(j=0;j<2;j++){

if(a[p][i]<a[p+1][i]){

temp=a[p+1][i];a[p+1][i]=a[p][i];a[p][i]=temp;

}p=p+1;}

}for(i=0;i<3;i++){for(j=0;j<3;j++){System.out.print(a[i][j]);}System.out.println();}}}

// Program to add two Angles as objects.

import java.io.*;class angle{

int deg,min;angle(){

deg=0;

Page 6: ICSE MARCH: 2014: JAVA PROGRAMS USING BLUEJ

min=0;}

angle(int d,int m){

deg=d;min=m;

}void display(){

System.out.println("ANGLE:"+deg+"."+min+"'");}int angletomin(){

int a=(deg*60)+min;return a;

}angle sumangle(angle A,angle B){

angle C=new angle();int p=A.angletomin();int q=B.angletomin();int r=p+q;C=A.mintoangle(r);return C;

}angle mintoangle(int m){

angle M=new angle();M.deg=m/60;M.min=m%60;return M;

}public static void main(String args[])throws IOException{

int d1,d2,m1,m2;InputStreamReader x=new InputStreamReader(System.in);BufferedReader y=new BufferedReader(x);System.out.println("ENTER DEGREES AND MINUTES OF OBJECT 1");d1=Integer.parseInt(y.readLine());m1=Integer.parseInt(y.readLine());System.out.println("ENTER DEGREES AND MINUTES OF OBJECT 2");d2=Integer.parseInt(y.readLine());m2=Integer.parseInt(y.readLine());angle B=new angle(d1,m1);angle A=new angle(d2,m2);B.display();A.display();angle C=new angle();C=A.sumangle(A,B);System.out.println("SUM OF THE ANGLES IS:");C.display();

}}

// Sample Program using a constructor in Java

Page 7: ICSE MARCH: 2014: JAVA PROGRAMS USING BLUEJ

import java.io.*;class abc{

int a,b;abc(int x,int y){

a=x;b=y;

}}class xyz extends abc{

int p,q;xyz(int x,int y,int l,int m){

super(x,y);p=l;q=m;

}void display(){

System.out.println(a+" "+b+" "+p+" "+q);}public static void main(String args[]){

xyz k=new xyz(77,56,566,556);k.display();

}}

// Example Program using TWO INPUT Angles with Degrees and Minutes and return the SUM of Angles using Objects as arguments to functions.

import java.io.*;class angle1{

int deg,min;angle1(){

deg=2;min=30;

}angle1(int d,int m){

deg=d;min=m;

}void display(){

System.out.print(deg+" "+min);

}int angletomin()

Page 8: ICSE MARCH: 2014: JAVA PROGRAMS USING BLUEJ

{int m;int d=(d*2)+m;return d;

}angle sumangle(angle a,angle b){

angle1 o=new angle1();int p=a.angletomin();int q=b.angletomin();int r=p+q;

}angle mintoangle(int m){

angle b=new angle();b.deg=m/60;b.min=m/60;return b;

}public static void main(String args[])throws IOException{

angle a=new angle(5,40);angle b=new angle(6,20);angle c=new angle();InputStreamReader d=new InputStreamReader(System.in);deg=Integer.parseInt(br.readLine());min=Integer.parseInt(br.readLine());a.display();b.display();c=a.sumangle();c.display();}

}

// Program to return the date as per the input date.

import java.io.*;class date1{

public static void main(String args[])throws IOException{

int dd,mm,yyyy,i,p;InputStreamReader x=new InputStreamReader(System.in);BufferedReader y= new BufferedReader(x);System.out.println("ENTER DATE IN dd mm yyyy FORMAT");dd=Integer.parseInt(y.readLine());mm=Integer.parseInt(y.readLine());yyyy=Integer.parseInt(y.readLine());String str,s1;

Page 9: ICSE MARCH: 2014: JAVA PROGRAMS USING BLUEJ

String s[]={"January","February","March","April","May","June","July","August","September","October","November","December"};

str=s[mm-1];s1="th";if(dd==1 || dd==21 || dd==31){

s1="st";}if(dd==2 || dd==22){

s1="nd";}System.out.println(dd+s1+" "+str+" "+yyyy);}}

// Program to display the format:

5555555555 etc.

import java.io.*;class series{

public static void main(String args[]){

int n=5;int i,j;for(i=1;i<=5;i++){

for(j=1;j<=i;j++){

System.out.print(n+" ");}System.out.println(" ");n=n-1;

}}

}

// Program to print ARMSTRONG numbers.

import java.io.*;

Page 10: ICSE MARCH: 2014: JAVA PROGRAMS USING BLUEJ

class armstrong{

int power(int n,int p){

if(p==0){

return 1;}elsereturn(n*power(n,p-1));

}int sumdigit(int n){

if(n==0){

return 0;}elsereturn(power((n%10),3)+sumdigit(n/10));

}void display(){int i;

for(i=1;i<1000;i++){

int b=sumdigit(i);if(b==i){

System.out.println(i);}

}}public static void main(String args[]){

armstrong obj=new armstrong();obj.display();

}}

// Program to print any string in ASCENDING ORDER OF CHARACTERS.

import java.io.*;class ascend {

public static void main(String args[])throws IOException{InputStreamReader Inp=new InputStreamReader(System.in);BufferedReader Br=new BufferedReader(Inp);String s=Br.readLine();int k;int i;String s1="";String s2="";int l=s.length();String g=s.toLowerCase();for(k=1;k<20;++k){

Page 11: ICSE MARCH: 2014: JAVA PROGRAMS USING BLUEJ

for(i=0;i<l;i++){

char b=s.charAt(i);if(b!=' '&& b!='.'){

s1=s1+b;}

else{

int l1=s1.length();if(l1==k){s2=s2+s1+" ";}s1="";}}}l=s2.length();

char c=s2.charAt(0);int x=(int)c-32;char d=(char)x;System.out.print(d);

String cc=s2.substring(1,l-1);System.out.print(cc);}}

// Program to input elements in an Array and disply the list after deleting a particular element.

import java.io.*;class delete{

public static void main(String args[])throws IOException{

int i,j,p=0;InputStreamReader x=new InputStreamReader(System.in);BufferedReader y=new BufferedReader(x);int a[]=new int[5];System.out.println("ENTER 5 NUMBERS");for(i=0;i<5;i++){

a[i]=Integer.parseInt(y.readLine());}System.out.println("CONTENTS OF THE ARRAY ARE ");for(i=0;i<5;i++){

System.out.println(a[i]);}int n=5,max=0;for(i=0;i<n;i++){

Page 12: ICSE MARCH: 2014: JAVA PROGRAMS USING BLUEJ

if(a[i]>max){

max=a[i];p=i;

}}

for(j=p;j<n-1;j++){

a[j]=a[j+1];}n=n-1;

System.out.println("ARRAY AFTER DELETION:");for(i=0;i<n;i++){

System.out.println(a[i]);}

}}

// Program to input a string and print the string as per the ascending order length of characters (Length of each word).

import java.io.*;class jkhg{

public static void main(String args[])throws IOException{

String s,str;char b;int c=0,i,l,p=0,k;InputStreamReader x=new InputStreamReader(System.in);BufferedReader y=new BufferedReader(x);System.out.println("ENTER A STRING");s=y.readLine();s=s.concat(" ");l=s.length();for(k=0;k<l;k++){for(i=0;i<l;i++){

b=s.charAt(i);if(b!=' '){

c=c+1;}else{

if(c==k){

p=i-c;System.out.print(s.substring(p,i)+" ");

}

Page 13: ICSE MARCH: 2014: JAVA PROGRAMS USING BLUEJ

c=0;}

}}}}

// Sample Program to print the sum of TWO Diagnols of the array (Left) and (Right) Diagnol of the double dimensional array.

import java.io.*;class klhjf{

public static void main(String args[])throws IOException{

int i,j,sl=0,sr=0;InputStreamReader x=new InputStreamReader(System.in);BufferedReader y=new BufferedReader(x);int a[][]=new int[3][3];

System.out.println("ENTER NOS IN AN ARRAY");for(i=0;i<3;i++){

for(j=0;j<3;j++){

a[i][j]=Integer.parseInt(y.readLine());}

}System.out.println("CONTENTS OF ARRAY ARE");

for(i=0;i<3;i++){

for(j=0;j<3;j++){

System.out.print(a[i][j]+" ");}System.out.println(" ");

}j=2;for(i=0;i<3;i++){

sl=sl+a[i][i];sr=sr+a[i][j];j--;

}System.out.println("SUM OF LEFT DIAGONAL="+sl);System.out.println("SUM OF RIGHT DIAGNOL="+sr);

}}

Page 14: ICSE MARCH: 2014: JAVA PROGRAMS USING BLUEJ

// Example Program to perform BINARY SEARCH within an array of numbers.

import java.io.*;class binary_search{

public static void main(String args[])throws IOException{

int i;InputStreamReader x=new InputStreamReader(System.in);BufferedReader y=new BufferedReader(x);int a[]=new int[10];System.out.println("ENTER THE NUMBER TO BE SEARCHED");int n=Integer.parseInt(y.readLine());System.out.println("ENTER 10 NUMBERS FOR THE ARRAY");for(i=0;i<10;i++){a[i]=Integer.parseInt(y.readLine());

} System.out.println("CONTENTS OF ARRAY ARE"); for(i=0;i<10;i++) { System.out.println(a[i]); } System.out.println("NUMBER TO BE SEARCHED IS "+n); int p=-1,mid,l=0,u=9; while(l<=u) { mid=(l+u)/2; if(a[mid]==n) { p=mid; break; } else if(n> a[mid]) { l=mid+1; } else if(n<a[mid]) { u=mid-1; } } if(p==-1) { System.out.println("NUMBER DOES NOT EXIST IN THE ARRAY"); } else { System.out.println("NUMBER EXISTS AT THE INDEX "+p); }

}}

Page 15: ICSE MARCH: 2014: JAVA PROGRAMS USING BLUEJ

// Example Program to ………………

import java.io.*;class xy{

public static void main(String args[])throws IOException{InputStreamReader x=new InputStreamReader(System.in);BufferedReader y=new BufferedReader(x);

System.out.println("enter text"); String s=y.readLine(); int l=s.length(); char a[][]=new char[l][l];int i,j=0;int p=0,t=0; for(i=0;i<l;i++) { for(j=0;j<l;j++) { a[i][j]=' ';}} for(i=0;i<l;i++) { for(j=0;j<l;j++) { if(i==j) { a[i][j]=s.charAt(p); p++; } if(i+j==l-1) { a[i][j]=s.charAt(t); t=t+1; } }} for(i=0;i<l;i++) { for(j=0;j<l;j++) { System.out.print(a[i][j]); } System.out.println();}}}

// Program to SORT numbers using BUBBLE SORTING.

Page 16: ICSE MARCH: 2014: JAVA PROGRAMS USING BLUEJ

import java.io.*;class hjgg{

public static void main(String args[])throws IOException{

int i,j;InputStreamReader x=new InputStreamReader(System.in);BufferedReader y=new BufferedReader(x);int a[]=new int[10];System.out.println("ENTER 10 NUMBERS");for(i=0;i<10;i++){

a[i]=Integer.parseInt(y.readLine());}System.out.println("CONTENTS OF THE ARRAY ARE");for(i=0;i<10;i++){

System.out.println(a[i]);}for(i=0;i<10;i++){

int c=0,p;for(j=0;j<9;j++){if(a[j]>a[j+1]){

p=a[j];a[j]=a[j+1];a[j+1]=p;

}}

}System.out.println("NUMBERS IN ASCENDING ORDER ");for(i=0;i<10;i++){

System.out.println(a[i]);}

}}

// Example Program to print the calendar of any year.

import java.io.*;class calendar{

public static void main(String args[])throws IOException{

int i,j,k,m,p=0,r,q,yy,i1,g;String ss;InputStreamReader x=new InputStreamReader(System.in);

Page 17: ICSE MARCH: 2014: JAVA PROGRAMS USING BLUEJ

BufferedReader y= new BufferedReader(x);String s[]={"

","January","February","March","April","May","June","July","August","September","October","November","December"};

String str[]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};

int b[]={0,31,28,31,30,31,30,31,31,30,31,30,31};System.out.println("ENTER THE FIRST DAY OF THE YEAR,YEAR AND

MONTH");ss=y.readLine();yy=Integer.parseInt(y.readLine());m=Integer.parseInt(y.readLine());if(yy%4==0){

b[2]=29;}int ar[][]=new int[6][7];

for(i=1;i<=m;i++){

System.out.println("\t\t"+s[i]);for(i1=0;i1<6;i1++)

{for(j=0;j<7;j++){

ar[i1][j]=0;}}for(j=0;j<7;j++){

System.out.print(str[j]+" ");}System.out.println();for(g=0;g<7;g++)

{if(str[g].equals(ss)){ p=g;}}

r=1;for(k=p;k<7;k++){

ar[0][k]=r;r=r+1;

}for(k=1;k<6;k++){

for(j=0;j<7;j++){

ar[k][j]=r;r=r+1;if(r>b[i]){

break;}

}if(r>b[i]){

Page 18: ICSE MARCH: 2014: JAVA PROGRAMS USING BLUEJ

break;}

}for(k=0;k<6;k++){

for(j=0;j<7;j++){

if(ar[k][j]==0){

System.out.print(" \t ");}else{

System.out.print(ar[k][j]+" \t ");

}}System.out.println();

}q=b[i]%7+p;if(q<7){

p=q;}else{

p=q-7;}}

}}

// Example Program : …………………….

import java.io.*;class employee{

int empno;String empname,empdes;employee(){

empno=0;empname="";empdes="";

}employee(int a,String b,String c){

empno=a;

Page 19: ICSE MARCH: 2014: JAVA PROGRAMS USING BLUEJ

empname=b;empdes=c;

}void display(){

System.out.println("EMPLOYEE NUMBER:"+empno);System.out.println("NAME:"+empname);System.out.println("DESIGNATION:"+empdes);

}}class salary extends employee{

int basic;salary(int a,String b,String c,int d){

super(a,b,c); basic=d;

}void calculate(){

double t=basic+(0.1*basic)+(0.15*basic);double s=basic-(0.08*basic);display();System.out.println("TOTAL:"+t);System.out.println("NET SALARY:"+s);

}public static void main(String args[])throws IOException{

int xx,yy;String s,str;InputStreamReader x=new InputStreamReader(System.in);BufferedReader y=new BufferedReader(x);System.out.println("ENTER NAME,DESIGNATION,EMPLOYEE

NUMBER,BASIC SALARY:");s=y.readLine();str=y.readLine();xx=Integer.parseInt(y.readLine());yy=Integer.parseInt(y.readLine());salary z=new salary(xx,s,str,yy);z.calculate();

}}

// Example Program to print a CIRCULAR Matrix.

import java.io.*;class xr{

public static void main(String args[])throws IOException{

int i,j,c=1;;InputStreamReader x=new InputStreamReader(System.in);BufferedReader y=new BufferedReader(x);System.out.println("enter the dimension of your array");int n;n=Integer.parseInt(y.readLine());

Page 20: ICSE MARCH: 2014: JAVA PROGRAMS USING BLUEJ

int a[][]=new int[n][n];int r=0,s=n-1;for(i=0;i<n;i++){

for(j=r;j<s;j++){

a[i][j]=c;c++;

}for(j=r;j<=s-1;j++){

a[j][s]=c;c++;

}for(j=s;j>=r;j--){

a[s][j]=c;c++;

}for(j=(s-1);j>r;j--){

a[j][i]=c;c++;

}r++;s--;

}for(i=0;i<n;i++){

for(j=0;j<n;j++){

System.out.print(a[i][j]+" ");}System.out.println();

}}

}

// Program to Enter a number and print in WORDS.

import java.io.*;class jkhg{

public static void main(String args[])throws IOException{

String s;char b;char h;int d=0,c=0,i,j,l,k,n;InputStreamReader x=new InputStreamReader(System.in);BufferedReader y=new BufferedReader(x);System.out.println("ENTER A NUMBER");n=Integer.parseInt(y.readLine());

Page 21: ICSE MARCH: 2014: JAVA PROGRAMS USING BLUEJ

String a[]={"one","two","three","four","five","six","seven","eight","nine"};

String b[]={"eleven","twelve","thirteen"}}}

// Program to print the frequency of each number in a list of numbers.

import java.io.*;class frequency{

public static void main(String args[])throws IOException{

int i,j,k;InputStreamReader x=new InputStreamReader(System.in);BufferedReader y=new BufferedReader(x);int a[]=new int[10];System.out.println("ENTER 10 NUMBERS");for(i=0;i<10;i++){

a[i]=Integer.parseInt(y.readLine());}System.out.println("CONTENTS OF THE ARRAY ARE ");for(i=0;i<10;i++){

System.out.println(a[i]);}for(i=0;i<10;i++){

int c=0,d=0;for(j=0;j<10;j++){

if(a[j]==a[i]){

c=c+1;}}for(k=0;k<i;k++){

if(a[i]==a[k]){

d=d+1;}}

if(d==0){

System.out.println("FREQUENCY OF "+a[i]+"="+c);

} }

}

Page 22: ICSE MARCH: 2014: JAVA PROGRAMS USING BLUEJ

}

// Program to display HAPPY- HAPPY format (ISC Practical)

import java.io.*;class jkhg{

public static void main(String args[])throws IOException{

int a,l,i,j,n,b;String s;InputStreamReader x=new InputStreamReader(System.in);BufferedReader y=new BufferedReader(x);System.out.println("ENTER A STRING");s=y.readLine();l=s.length();a=l/2;b=l/2;System.out.println();System.out.println(s);for(i=0;i<l/2;i++){

for(j=0;j<l;j++){if(j<a){

System.out.print(s.charAt(j));}if(j>b){

System.out.print(s.charAt(j));}if(j>=a && j<=b){

System.out.print(" ");}}

System.out.println();--a;++b;}a=2;b=l-3;

for(i=l/2;i>1;i--){

for(j=0;j<l;j++){if(j<a){

System.out.print(s.charAt(j));}if(j>b)

Page 23: ICSE MARCH: 2014: JAVA PROGRAMS USING BLUEJ

{System.out.print(s.charAt(j));

}if(j>=a && j<=b){

System.out.print(" ");}}System.out.println();++a;--b;}System.out.println(s);

}}

// Program to input ……………..

import java.io.*;class hours{

int hrs,mins;hours(){

hrs=0;mins=0;

}hours(int h,int m){

hrs=h;mins=m;

}void display(){

System.out.println(hrs+" "+mins);}hours sumhours(hours A,hours P){

hours C=new hours();int X=A.hrs*60+A.mins;int Y=P.hrs*60+P.mins;int Z=X+Y;C.hrs=Z/60;C.mins=Z%60;return C;

}public static void main(String args[])throws IOException{

hours A=new hours(6,20);hours P=new hours(8,90);hours C=new hours();System.out.print("First Object");A.display();System.out.print("Second Object");P.display();

Page 24: ICSE MARCH: 2014: JAVA PROGRAMS USING BLUEJ

System.out.print("Sum of time is");C=A.sumhours(P); C.display();

} }

// Program to perform INSERTION SORTING in Java

import java.io.*;class insertion_sorting {

public static void main(String args[])throws IOException{

int i,j,num;InputStreamReader x=new InputStreamReader(System.in);BufferedReader y=new BufferedReader(x);int a[]=new int[10];System.out.println("ENTER 10 NUMBERS");for(i=0;i<10;i++){

a[i]=Integer.parseInt(y.readLine());}System.out.println("CONTENTS OF THE ARRAY ARE");for(i=0;i<10;i++){

System.out.print(a[i]+",");}for(i=1;i<10;i++){

j=i-1;num=a[i];while(j>=0 && a[j]>num){

a[j+1]=a[j];j=j-1;

}a[j+1]=num;

}System.out.println("CONTENTS IN ASCENDING ORDER");for(i=0;i<10;i++){

System.out.print(a[i]+",");}

}}

// Example Program to perform Linear Search in JAVA

import java.io.*;class linear_search

Page 25: ICSE MARCH: 2014: JAVA PROGRAMS USING BLUEJ

{public static void main(String args[])throws IOException{

int n,i;InputStreamReader x=new InputStreamReader(System.in);BufferedReader y=new BufferedReader(x);int a[]=new int[5];System.out.println("ENTER THE NUMBER TO BE SEARCHED"); n=Integer.parseInt(y.readLine());System.out.println("ENTER 5 NUMBERS FOR THE ARRAY");for(i=0;i<5;i++){

a[i]=Integer.parseInt(y.readLine());}System.out.println("CONTENTS OF THE ARRAY ARE");for(i=0;i<5;i++){

System.out.println(a[i]);}System.out.println("NUMBER TO BE SEARCHED IS "+n);int p=-1;for(i=0;i<5;i++){

if(n==a[i]){

p=i;break;

}}if(p!=-1){

System.out.println("NUMBER EXISTS AT THE INDEX "+p);

}else{

System.out.println("NUMBER DOES NOT EXISTS IN THE ARRAY");

}}

}

// Example Program to find the LONGEST word in an entered String.

import java.io.*;class jkhg{

public static void main(String args[])throws IOException{

String s,str;char b;int c=0,i,l,p=0,max=0;;InputStreamReader x=new InputStreamReader(System.in);BufferedReader y=new BufferedReader(x);

Page 26: ICSE MARCH: 2014: JAVA PROGRAMS USING BLUEJ

System.out.println("ENTER A STRING");s=y.readLine();s=s.concat(" ");l=s.length();for(i=0;i<l;i++){

b=s.charAt(i);if(b!=' '){

c=c+1;}else{

if(c>max){

max=c;p=i;

}c=0;

}}str=s.substring(p-max,p);System.out.println(str+" "+max);}}

// Program to print the SORTED Double Dimensional ARRAY.

import java.io.*;class jkhg{

public static void main(String args[])throws IOException{

int i,j,k,l,q=0,w=0,q1=0,w1=0,n,p;

InputStreamReader x=new InputStreamReader(System.in);BufferedReader y=new BufferedReader(x);System.out.println("ENTER DIMENSION OF ARRAY");n=Integer.parseInt(y.readLine());int a[][]=new int[n][n];System.out.println("ENTER NOS IN AN ARRAY");for(i=0;i<n;i++){

for(j=0;j<n;j++){

a[i][j]=Integer.parseInt(y.readLine());}

}System.out.println("CONTENTS OF ARRAY ARE");

for(i=0;i<n;i++){

Page 27: ICSE MARCH: 2014: JAVA PROGRAMS USING BLUEJ

for(j=0;j<n;j++){

System.out.print(a[i][j]+" ");}System.out.println(" ");

}int max=0,m=0;for(i=0;i<n;i++){

for(j=0;j<n;j++){

if(a[i][j]>max){

max=a[i][j];q=i;w=j;

}}

}System.out.println("LARGEST NO. IS "+max+" AT ROW "+q+" AND

COLOUMN "+w);

for(i=0;i<n;i++){

for(j=0;j<n;j++){

if(a[i][j]>m && a[i][j]!=max){

m=a[i][j];q1=i;w1=j;

}}

}System.out.println("SECOND LARGEST NO. IS "+m+" AT ROW

"+q1+" AND COLOUMN "+w1);for(i=0;i<n;i++)

{for(j=0;j<n;j++){

for(k=0;k<n;k++){

for(l=0;l<n;l++){

if(a[i][j]<a[k][l]){

p=a[i][j];a[i][j]=a[k][l];a[k][l]=p;

}}

}}

}System.out.println("SORTED ARRAY");for(i=0;i<n;i++)

{for(j=0;j<n;j++)

Page 28: ICSE MARCH: 2014: JAVA PROGRAMS USING BLUEJ

{System.out.print(a[i][j]+" ");

}System.out.println(" ");

}}}

// Program to print the SUM OF ELEMENTS in a double dimensional array.

import java.io.*;class matrix{

int ar[][]=new int[5][5];int i,j,y;void input()throws IOException{

InputStreamReader x=new InputStreamReader(System.in);BufferedReader y=new BufferedReader(x);System.out.println("ENTER NOS IN AN ARRAY");for(i=0;i<5;i++){

for(j=0;j<5;j++){

ar[i][j]=Integer.parseInt(y.readLine());}

}}int sum(int i){

int s=0;for(j=0;j<5;j++){

s=s+ar[i][j];}return s;

}void print(){

for(i=0;i<5;i++){

for(j=0;j<5;j++){

System.out.print(ar[i][j]+" ");}System.out.println();

}}void sumrows(){

for(i=0;i<5;i++){

y=sum(i);

Page 29: ICSE MARCH: 2014: JAVA PROGRAMS USING BLUEJ

System.out.println("SUM OF ROW "+(i+1)+" IS "+y);

}}public static void main(String args[])throws IOException{

matrix a=new matrix();a.input();a.print();a.sumrows();

}}

// Example Program to ………..

import java.io.*;class multiple{

public static void main(String args[])throws IOException{

int i,j,b;InputStreamReader x=new InputStreamReader(System.in);BufferedReader y=new BufferedReader(x);int a[]=new int[5];System.out.println("ENTER 5 NUMBERS");for(i=0;i<5;i++){ a[i]=Integer.parseInt(y.readLine());}System.out.println("CONTENTS OF THE ARRAY ARE");for(i=0;i<5;i++){

System.out.println(a[i]);for(j=1;j<=10;j++){

b=a[i]*j;System.out.println(a[i]+"*"+j+"="+b);

}}

}}

// Example Program Name…….

import java.io.*;class ghf{

public static void main(String args[])throws IOException

Page 30: ICSE MARCH: 2014: JAVA PROGRAMS USING BLUEJ

{int i,j,n,l;char b;String s;InputStreamReader x=new InputStreamReader(System.in);BufferedReader y=new BufferedReader(x);System.out.println("ENTER A NAME");s=y.readLine();l=s.length();char a[][]=new char[l][l];for(i=0;i<l;i++){

for(j=0;j<l;j++){

a[i][j]=' ';}

}for(i=0;i<l;i++){n=l-i-1;

b=s.charAt(i);a[i][i]=b;a[i][n]=b;

}for(i=0;i<l;i++){

for(j=0;j<l;j++){

System.out.print(a[i][j]);}System.out.println();

}}

}

// Example Program printing SUM of ODD numbers and EVEN numbers all the elements.

import java.io.*;class array{

int i,sum1=0,sum2=0,sum3=0;void input(){

for(i=2;i<=20;i=i+2){

sum1=sum1+i;}System.out.println("SUM OF 10 EVEN NOS.="+sum1);}void display(){

for(i=1;i<=10;i++){

Page 31: ICSE MARCH: 2014: JAVA PROGRAMS USING BLUEJ

sum2=sum2+i;}System.out.println("SUM OF 10 NATURAL NOS.="+sum2);

}void sumno(){

for(i=1;i<=19;i=i+1){

sum3=sum3+i;}System.out.println("SUM OF 10 ODD NOS.="+sum3);

}public static void main(String args[])throws IOException{

array z=new array();z.input();z.display();z.sumno();}

}

// Example Program …………….

import java.io.*;class output{

public static void main(String args[]){

int i,j=1;int divident=2;int n=30;int ar[]={2,0,0,0,0,0,0,0,0,0};while(divident<=n){

for(i=2;i<divident;i++){

if(divident%i==0){

break;}if(i==divident-1){

ar[j++]=divident;}

} divident++;

}for(i=0;i<10;i++){System.out.println(ar[i]);}

}}

Page 32: ICSE MARCH: 2014: JAVA PROGRAMS USING BLUEJ

// To Add POINTS using Objects.

import java.io.*;class point2D{

float x,y;point2D(int nx,int ny){

x=nx;y=ny;

}float distance2D(point2D A){

float x2=(A.x-x)*(A.x-x);float y2=(A.y-y)*(A.y-y);float d=(float)Math.sqrt(x2+y2);return d;

}}class point3D extends point2D{

float z;point3D(int nx,int ny,int nz){

super(nx,ny);z=nz;

}void show(){

System.out.println("POINT :"+"("+x+","+y+","+z+")");}float distance3D(point3D A){float x3=(A.x-x)*(A.x-x);

float y3=(A.y-y)*(A.y-y);float z3=(A.z-z)*(A.z-z);float d=(float)Math.sqrt(x3+y3+z3);return d;

}public static void main(String args[])throws IOException{

int X,Y,Z,XX,YY,ZZ;InputStreamReader xx=new InputStreamReader(System.in);BufferedReader yy=new BufferedReader(xx);System.out.println("ENTER X,Y,Z CO-ORDINATES OF 1st

OBJECT");X=Integer.parseInt(yy.readLine());Y=Integer.parseInt(yy.readLine());Z=Integer.parseInt(yy.readLine());point3D o=new point3D(X,Y,Z);System.out.println("ENTER X,Y,Z CO-ORDINATES OF 2nd

OBJECT");XX=Integer.parseInt(yy.readLine());YY=Integer.parseInt(yy.readLine());

Page 33: ICSE MARCH: 2014: JAVA PROGRAMS USING BLUEJ

ZZ=Integer.parseInt(yy.readLine());point3D oo=new point3D(XX,YY,ZZ);o.show();oo.show();float d2=o.distance2D(oo);float d3=o.distance3D(oo);System.out.println("DISTANCE IN 2D:"+d2);System.out.println("DISTANCE IN 3D:"+d3);

}}/*ENTER X,Y,Z CO-ORDINATES OF 1st OBJECT5552ENTER X,Y,Z CO-ORDINATES OF 2nd OBJECT8152POINT :(5.0,5.0,52.0)POINT :(8.0,1.0,52.0)DISTANCE IN 2D:5.0DISTANCE IN 3D:5.0Press any key to continue...

// Program to print PRIME FACTORS on a given number.

import java.io.*;class prime_factors{

public static void main(String args[])throws IOException{int n,i,j,c=0,m=0;InputStreamReader x=new InputStreamReader(System.in);

BufferedReader y=new BufferedReader(x);System.out.println("ENTER THE NUMBER");n=Integer.parseInt(y.readLine());int a=n;for(i=2;i<=n;i++){

c=0;m=0;if(n%i==0)

{for(j=1;j<=i;j++){

if(i%j==0){

c=c+1;}

}if(c==2){

while(n%i==0)

Page 34: ICSE MARCH: 2014: JAVA PROGRAMS USING BLUEJ

{m=m+1;n=n/i;}n=a;

System.out.println("FREQUENCY OF FACTOR "+i+"="+m);

}}

}}}

// Printing REVERSE of a STRING.

import java.io.*;class jkhg{

public static void main(String args[])throws IOException{

String s; char b;int c=0,i,l,p=0,k;InputStreamReader x=new InputStreamReader(System.in);BufferedReader y=new BufferedReader(x);System.out.println("ENTER A STRING");s=y.readLine();s=s.concat(" ");l=s.length();for(i=0;i<l;i++){

b=s.charAt(i);if(b!=' '){

c=c+1;}else{p=i-c;for(k=i;k>=p;k--){

System.out.print(s.charAt(k));}

c=0;}

}}}

// Printing Reverse of a STRING.

Page 35: ICSE MARCH: 2014: JAVA PROGRAMS USING BLUEJ

import java.io.*;class jkhg{

public static void main(String args[])throws IOException{

String s; char b;int c=0,i,l,p=0,k;InputStreamReader x=new InputStreamReader(System.in);BufferedReader y=new BufferedReader(x);System.out.println("ENTER A STRING");s=y.readLine();s=" ".concat(s);l=s.length();for(i=l-1;i>=0;i--){

b=s.charAt(i);if(b!=' '){

c=c+1;}else{p=c+i;for(k=i;k<=p;k++){

System.out.print(s.charAt(k));}

c=0;}

}}}

// Program to get the SADDLE POINT Of an ARRAY using Java

import java.io.*;class jkhg{

public static void main(String args[])throws IOException{

int i,j,k,l,q=0,w=0,q1=0,w1=0,n,p;

InputStreamReader x=new InputStreamReader(System.in);BufferedReader y=new BufferedReader(x);System.out.println("ENTER DIMENSION OF ARRAY");n=Integer.parseInt(y.readLine());int a[][]=new int[n][n];System.out.println("ENTER NOS IN AN ARRAY");for(i=0;i<n;i++){

Page 36: ICSE MARCH: 2014: JAVA PROGRAMS USING BLUEJ

for(j=0;j<n;j++){

a[i][j]=Integer.parseInt(y.readLine());}

}System.out.println("CONTENTS OF ARRAY ARE");

for(i=0;i<n;i++){

for(j=0;j<n;j++){

System.out.print(a[i][j]+" ");}System.out.println(" ");

}for(j=0;j<n;j++){

i=0;if(a[i][j]<min){

min=a[i][j];}for(k=0;k<n;k++){

l=0;if(a[k][l]>max){

max=a[k][l];}

}++i;++l;

}I)System.out.println("SADDLE PT )}}

// Program to print ……

import java.io.*;class jgffc{public static void main (String args[])throws IOException

{int i,j,p,k,l,n;InputStreamReader x=new InputStreamReader(System.in);

BufferedReader y=new BufferedReader(x);System.out.println("ENTER THE DIMENSION OF ARRAY");n=Integer.parseInt(y.readLine());int a[][]=new int[n][n];System.out.println("ENTER NOS IN AN ARRAY");for(i=0;i<n;i++)

Page 37: ICSE MARCH: 2014: JAVA PROGRAMS USING BLUEJ

{for(j=0;j<n;j++){

a[i][j]=Integer.parseInt(y.readLine());}

}System.out.println("CONTENTS OF ARRAY ARE");

for(i=0;i<n;i++){

for(j=0;j<n;j++){

System.out.print(a[i][j]+" ");}System.out.println(" ");

}for(i=0;i<n;i++){

for(j=0;j<(n-1);j++){

sm=a[i][j];

}}}}

// Program to perform SORTING using SELECTION SORTING on numbers.

import java.io.*;class jfdf{

public static void main(String args[])throws IOException{

int n,i,j;InputStreamReader x=new InputStreamReader(System.in);BufferedReader y=new BufferedReader(x);int a[]=new int[10];System.out.println("ENTER 10 NUMBERS");for(i=0;i<10;i++){ a[i]=Integer.parseInt(y.readLine());}System.out.println("CONTENTS OF THE ARRAY ARE");for(i=0;i<10;i++){

System.out.println(a[i]);}int sm,p; for(i=0;i<9;i++){

sm=a[i];p=i;for(j=i+1;j<10;j++)

Page 38: ICSE MARCH: 2014: JAVA PROGRAMS USING BLUEJ

{if(a[j]<sm){

sm=a[j];p=j;

}}a[p]=a[i];a[i]=sm;

}System.out.println("NUMBERS IN ASCENDING ORDER");for(i=0;i<10;i++){

System.out.println(a[i]);}}

}

// Performing SUM of the series: …………………….

import java.io.*;class series6{

public static void main(String args[]){

int i,j,k,l,m,n,sp,a;sp=10;for(j=6;j>=1;j=j-2){for(i=1;i<=sp;i=i+1){

System.out.print(" ");}sp=sp+2;for(k=1;k<=j;k++){

System.out.print("* ");}System.out.println();}for(a=1;a<=sp-1;a++){

System.out.print(" ");}System.out.println("*");

sp=sp-2;for(l=2;l<=6;l=l+2){

for(m=sp;m>=1;m=m-1){

System.out.print(" ");

Page 39: ICSE MARCH: 2014: JAVA PROGRAMS USING BLUEJ

}sp=sp-2;for(n=1;n<=l;n++){

System.out.print("* ");}System.out.println();

}}

}

// Printing SUM of Series……….

import java.io.*;class series1{

public static void main(String args[]){

int i,j,k;for(i=1;i<=5;i++){

for(j=1;j<=i;j++){

System.out.print(j);}for(k=(i-1);k>=1;k--){

System.out.print(k);}System.out.println();

}}

}

// Program to print SPECIAL NUMBERS within a given range.

import java.io.*;class abc{

int b,c,i,a;

int factorial(int n){

if(n==0)return 1;

else{

return(n*factorial(n-1));}

}int sum(int m){

Page 40: ICSE MARCH: 2014: JAVA PROGRAMS USING BLUEJ

if(m==0){

return 0;}else{

b=m%10; c=factorial(b);return(c+sum(m/10));

}}void display(){System.out.println("SPECIAL NUMBERS BETWEEN 1 AND 1000");for(i=1;i<=1000;i++){

a=0; a=sum(i);if(a==i){

System.out.println(i);}}}public static void main(String args[]){

abc z=new abc();z.display();

}}

// Program to print a STRING PALINDROME

import java.io.*;class jkhg{

public static void main(String args[])throws IOException{

String s,str;char b,c;int i,l,p=0,k;InputStreamReader x=new InputStreamReader(System.in);BufferedReader y=new BufferedReader(x);System.out.println("ENTER A STRING");s=y.readLine();s=s.concat(" ");l=s.length();k=l-1;for(i=0;i<l;i++){

b=s.charAt(i);c=s.charAt(k);if(b==c){

Page 41: ICSE MARCH: 2014: JAVA PROGRAMS USING BLUEJ

p=-1;}k=k-1;

}if(p==-1){

System.out.println("NOT A PALINDROME");}else{

System.out.println("PALINDROME");}

}}

// Program to have STRING Array.

import java.io.*;class jkhg{

public static void main(String args[])throws IOException{

String s;char b;char h;int d=0,c=0,i,j,l,k,n;InputStreamReader x=new InputStreamReader(System.in);BufferedReader y=new BufferedReader(x);System.out.println("ENTER THE NO. OF SENTENCES(upto 20)");n=Integer.parseInt(y.readLine());String st[]=new String[n];for(i=0;i<n;i++){System.out.println("ENTER A STRING");st[i]=y.readLine();}for(i=n-1;i>=0;i--){

s=" "+st[i];l=s.length();for(j=l-1;j>=0;j--){

b=s.charAt(j);if(b!=' ')

{c=c+1;

}else{

for(k=j;k<=j+c;k++){

Page 42: ICSE MARCH: 2014: JAVA PROGRAMS USING BLUEJ

h=s.charAt(k);d=(int)h;if((d>=97 && d<=122) || (d>=65 && d<=91) || h=='

'){

System.out.print(h);}}

c=0;}

}}}}

// Program to ……………………

import java.io.*;class jkhg{

public static void main(String args[])throws IOException{

String s;char b;int c=0,i,l;InputStreamReader x=new InputStreamReader(System.in);BufferedReader y=new BufferedReader(x);System.out.println("ENTER A STRING");s=y.readLine();s=s.concat(" ");l=s.length();for(i=0;i<l;i++){

b=s.charAt(i);if(b!=' '){

System.out.print(b);c=c+1;

}else{

System.out.println(" "+c);c=0;

}}}}

// Program to print a SYMMETRIC Array.

Page 43: ICSE MARCH: 2014: JAVA PROGRAMS USING BLUEJ

import java.io.*;class jgffc{public static void main (String args[])throws IOException

{int i,j,c=0,n;InputStreamReader x=new InputStreamReader(System.in);

BufferedReader y=new BufferedReader(x);System.out.println("ENTER THE DIMENSION OF ARRAY");n=Integer.parseInt(y.readLine());int a[][]=new int[n][n];int b[][]=new int [n][n];System.out.println("ENTER NOS IN AN ARRAY");for(i=0;i<n;i++){

for(j=0;j<n;j++){

a[i][j]=Integer.parseInt(y.readLine());}

}System.out.println("CONTENTS OF ARRAY ARE");

for(i=0;i<n;i++){

for(j=0;j<n;j++){

System.out.print(a[i][j]+" ");}System.out.println(" ");

}for(i=0;i<n;i++)

{for(j=0;j<n;j++){

b[i][j]=a[j][i];}

}for(i=0;i<n;i++){

for(j=0;j<n;j++){

if(a[i][j]==b[i][j]){

c=c+1;}

}}if(c==(n*n)){

System.out.println("IT IS SYMMETRIC ARRAY");}else{

System.out.println("IT IS NOT A SYMMETRIC ARRAY");}}}

Page 44: ICSE MARCH: 2014: JAVA PROGRAMS USING BLUEJ

// Printing TRANSPOSE of an Array.

import java.io.*;class jkhg{

public static void main(String args[])throws IOException{

int i,j;InputStreamReader x=new InputStreamReader(System.in);BufferedReader y=new BufferedReader(x);int a[][]=new int[3][3];int b[][]=new int[3][3];System.out.println("ENTER NOS IN AN ARRAY");for(i=0;i<3;i++){

for(j=0;j<3;j++){

a[i][j]=Integer.parseInt(y.readLine());}

}System.out.println("CONTENTS OF ARRAY ARE");

for(i=0;i<3;i++){

for(j=0;j<3;j++){

System.out.print(a[i][j]+" ");}System.out.println(" ");

}for(i=0;i<3;i++){

for(j=0;j<3;j++){

b[i][j]=a[j][i];}

}for(i=0;i<3;i++){

for(j=0;j<3;j++){

System.out.print(b[i][j]+" ");}System.out.println(" ");

}}

}

// Printing Sentence as Entered, Number of Words and Vowels.

import java.io.*;class jkhg{

Page 45: ICSE MARCH: 2014: JAVA PROGRAMS USING BLUEJ

public static void main(String args[])throws IOException{

String s;char b;int c=1,i,j=0,l,v=0,w=0,k;InputStreamReader x=new InputStreamReader(System.in);BufferedReader y=new BufferedReader(x);System.out.println("ENTER THE TEXT");s=y.readLine();s=s.toUpperCase();l=s.length();int a[]=new int[l];int b1[]=new int [l];for(i=0;i<l;i++){

b=s.charAt(i);if(b=='!' || b=='.' || b=='?'){

a[j]=v;b1[j]=w+1;v=0;w=0;++j;

}else{if(b==' '){

w=w+1;}if(b=='A' || b=='E' || b=='I' || b=='O' || b=='U'){

v=v+1;}}

}System.out.println("SENTENCE \t NO. OF VOWELS \t NO. OF

WORDS");for(i=0;i<j;i++){

System.out.println(c+" \t\t "+a[i]+" \t\t "+b1[i]);c++;

}c=1;System.out.println("SENTENCE \t NO. OF WORDS/VOWELS");for(i=0;i<j;i++){

System.out.print(c+" \t\t ");for(k=0;k<a[i];k++){

System.out.print("VVV");}

System.out.println();System.out.print("\t\t ");for(k=0;k<b1[i];k++){

Page 46: ICSE MARCH: 2014: JAVA PROGRAMS USING BLUEJ

System.out.print("WWW");}System.out.println();c=c+1;

}}}

// Program to print a WONDEROUS Square.

import java.io.*;class jgffc{public static void main (String args[])throws IOException

{int i,j,s=0,s1,s2,n;InputStreamReader x=new InputStreamReader(System.in);

BufferedReader y=new BufferedReader(x);System.out.println("ENTER THE DIMENSION OF ARRAY");n=Integer.parseInt(y.readLine());int a[][]=new int[n][n];System.out.println("ENTER NOS IN AN ARRAY");for(i=0;i<n;i++){

for(j=0;j<n;j++){

a[i][j]=Integer.parseInt(y.readLine());}

}System.out.println("CONTENTS OF ARRAY ARE");

for(i=0;i<n;i++){

for(j=0;j<n;j++){

System.out.print(a[i][j]+" ");}System.out.println(" ");

}

double b=0.5*n*((n*n)+1);

for(i=0;i<n;i++){

s1=0;s2=0;for(j=0;j<n;j++){

s1=s1+a[i][j];s2=s2+a[j][i];

} if(s1==b && s2==b){

s=s+1;}

}if(s==n){

Page 47: ICSE MARCH: 2014: JAVA PROGRAMS USING BLUEJ

System.out.println("IT IS A WONDROUS SQUARE");}else{

System.out.println("IT IS NOT A WONDROUS SQUARE");}}}