Suppose the entered word is ‘Abz’, the output will be ‘bCA”. Each character in the word is increased by one character and if it exceeds the upper limit of letters, the starting character will be set. At the end of each character cases is to be changed. The upper case character will be changed to lower case and vice versa.

Here are the codes of the program.

import java.io.*;
class Smith
{
String str;
int i,len;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
public void take() throws Exception
{
System.out.println(“Enter the word:”);
str=br.readLine();
len=str.length();
str=move();
str=changeCase();
System.out.println(“Modified string=”+str);
}
private String move()
{
char ch;
String str1=””;

for(i=0;i=65 && ch=97 && ch<=122))
ch=(char)(ch+1);
if(ch==91)
ch='A';
if(ch==123)
ch='a';
str1=str1+ch;
}
return str1;
}
private String changeCase()
{
char ch;
String str1="";
for(i=0;i=65 && ch=97 && ch< =122)
ch=(char)(ch-32);
str1=str1+ch;
}
return str1;
}
public static void main(String args[])throws Exception
{
Smith ob=new Smith();
ob.take();
}
}

Technical analysis of the circular decoding program

I have used three functions in this program. Function ‘public void take()’ takes the word from user and calls two other functions ‘private String move()’ and ‘private String changeCase()’. The first called function moves each letter by one and if the modified letter crosses the upper range, it is set back to starting letter and the second called function is used to change the case of each letters.

Sample input and output of the circular decoding program

Enter the word:
Abc
Modified string=bCD

Enter the word:
AcX
Modified string=bDy

In this BlueJ program user has to enter two strings and the common characters from the strings will be eliminated.

Codes of the program

import java.io.*;

class Word
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String s1,s2,s3=””,s4=””;
int a[],b[],x=0,y=0;
int len1,len2;
char ch;
int i,j;
public void take() throws Exception
{
System.out.println(“Enter the first sentence:”);
s1=br.readLine().trim();
System.out.println(“Enter the second sentence:”);
s2=br.readLine().trim();
len1=s1.length();
len2=s2.length();
a=new int[len1];
b=new int[len2];
for(i= 0;i< len1;i++)
{
ch=s1.charAt(i);
for(j=0;j< len2;j++)
{
if(ch==s2.charAt(j))
break;
}
if(j!=len2)
{
a[x++]=i;
b[y++]=j;
}
}
for(i=0;i< len1;i++)
{
for(j=0;j< x;j++)
{
if(i==a[j])
break;
}
if(j==x)
s3=s3+s1.charAt(i);
}
for(i=0;i< len2;i++)
{
for(j=0;j< y;j++)
{
if(i==b[j])
break;
}
if(j==y)
s4=s4+s2.charAt(i);
}
System.out.println("Original string1="+s1+" Modified string1="+s3);
System.out.println("Original string2="+s2+" Modified string2="+s4);
}
public static void main(String args[]) throws Exception
{
Word ob=new Word();
ob.take();
}
}

Technical analysis of this program

In this string modification program, two strings are entered and initially stored in string objects. The first nested loop loop is used to check for common characters in the strings and the common charater locations are stored in two different arrays. The second nested loop is used to search the common charater locations from the first string and those characters are skipped, remaining characters are appended on another string object. The same process is carried out on the second string object to eliminate the common characters from it.

In this BlueJ program user will enter any decimal value greater than 0 but less than 4000 and the equivalent roman number will be displayed.

import java.io.*;
class Name
{

String roman[] = {“M”, “CM”, “D”, “CD”, “C”, “XC”, “L”, “XL”, “X”, “IX”, “V”, “IV”, “I”};
int decimal[] = {1000,900,500,400,100,90,50,40,10,9,5,4,1};
int b;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

public void take() throws Exception
{
String str=””;
while(true)
{
System.out.println(“Enter the decomal number;”);
b=Integer.parseInt(br.readLine());
if(b>0 && b<4000)
break;
}
for (int i = 0; i = decimal[i]) {
b = b-decimal[i];
str = str+roman[i];
}
}
System.out.println(“Equivalent roman=”+str);
}
public static void main(String args[])throws Exception
{
new Name().take();
}
}

Technical analysis of the decimal to roman number conversion program

Both the decimal numbers having unique roman symbols and the respective roman numbers are stored in two separate arrays. While taking the number from user, it is ensured that the number is more than 0 and at the same time less than 4000. Using the nested loop, the roman numbers are accessed by the outer loop in decending order and the inner loop checks whether the entered value is greater or equal to the equivalent decimanl number. If the number the greater than the equivalent decimanl number, the entered number is reduced by the equivalent decimal and the matching roman number is concatenated on a string object. This process is carried on until all the roman numbers are accessed. Depending upon the entered decimal value, the inner loop will execute but the outer loop continues until all the roman numbers are accessed.

Here is a string program on which changes the position of the words but not the alphabets. Details of the program are given below.

The input here will consists of a number of lines of English text consisting of the letters of the English alphabet, the punctuation marks (‘) apostrophe, (.) full stop, (,) comma, (;) semicolon, (:) colon and white space characters (blank, new line). Your task is to print the words of the text in reverse order without a punctuation mark other than blanks. For example consider the following input text:
‘This is a sample piece of text to illustrate this problem. If you are smart you will solve this right’.
The corresponding output would read as:
‘right this solve will you smart are you If problem this illustrate to text of piece sample a is This’.
That is, the lines are printed in reverse order. Note: Individual words are not reversed. The first line of input contains a single integer N( =65 && s1.charAt(s1.length()-1)=97 && s1.charAt(s1.length()-1)=0;i–)
{
str=str+ arr[i]+” “;
}
str=str.trim();
str=str+”.”;
}
public void display()
{
System.out.println(“Output=”+str);
}
public static void main(String args[]) throws IOException
{
ReverseSentence ss=new ReverseSentence();
ss.take();
ss.display();
}
}

Variable description of the program

String str – Initial text is stored in this string objecty.
BufferedReader br – Used to take input from user.
StringTokenizer stk – break the input text into tokens.
String arr[] – The words of the text are stored here.
int index= – index of the string array.

Brief study of the program

This program is little bit simple one. The input text is broken into words using StringTokenizer class. The punctuations of the text are eliminated from the words and ultimately they are stored in a string array.
The values are accessed from the end location of the array and are concatenated in another string object and displayed as a text with the words reversed.

This program deals with changing the case of the characters of an entered string. Upper case characters will be changed to lower case characters and vice versa. We can do this program using different techniques.

How to proceed on this character case changing program

We have to extract each characters of the string. To perform this job, a loop is required and then the status of character is to be checked. This checking can be performed in different ways. We can use static functions of Charater class to check status of any character, whether it is in lower case or upper case. The second approach of checking is ASCII values of each character. Either of these techniques can be used to check the case of the characters and required changes can be done.

Here is the program which checks the character case using static function

import java.io.*;
class ChangeCase
{
String str1,str2=””;
char ch;
int i,len;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
public void show()throws Exception
{
System.out.println(“Enter the sentence:”);
str1=br.readLine();
len=str1.length();
for(i=0;i< len;i++)
{
ch=str1.charAt(i);
if(Character.isUpperCase(ch))
ch=(char)(ch+32);
else if(Character.isLowerCase(ch))
ch=(char)(ch-32);
str2=str2+ch;
}
System.out.println("Modified string="+str2);
}
public static void main(String args[])throws Exception
{
new ChangeCase ().show();
}
}

Two unfamiliar functions are used in this program, ‘isUpperCase()’ and ‘isLowerCase()’. These are static functions of Character class. Character is a predefined wrapper class of java.lang package. Prototype of these two functions are ‘public boolean isUpperCase(char)’ and ‘public boolean isLowerCase(char)’. If the argument char value in ‘public boolean isUpperCase(char)’ is in upper case it return true otherwise false. The other function is just the reversed.

Here is the second program which checks the character case using ASCII values

import java.io.*;
class Pattern
{
String str1,str2="";
char ch;
int i,len;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
public void show()throws Exception
{
System.out.println("Enter the sentence:");
str1=br.readLine();
len=str1.length();
for(i=0;i=65 && ch=97 && ch< =122)
ch=(char)(ch-32);
str2=str2+ch;
}
System.out.println("Modified string="+str2);
}
public static void main(String args[])throws Exception
{
new Pattern().show();
}
}

Today we’ll deal with isc computer applications sample papers.

This is a program on Queue. Maximum 5 printing jobs can be kept on pending here. Execution of printing will be in First in First out order.

import java.io.*;
class print
{
int job[];
int newjob;
int capacity;
int front;
int rear;
print()
{
capacity=5;
front=rear=-1;
createjob();
}
void createjob()
{
job=new int[capacity];
}
void addjob(int j)
{
if(rear==capacity)
System.out.println(“printjob is full,cant add any more printing job”);
else
{
job[++rear]=j;
}
if(front==-1)
front=0;
}
void removejob()
{
if(front==rear && front==-1)
System.out.println(“no print job”);
else if(front==rear)
{
System.out.println(“last print job is executed”+job[front]);
front=-1;rear=-1;
}
else
System.out.println(“print job is executed”+job[front++]);
}
}
class printjob
{
public static void main(String args[])throws IOException
{
print obj=new print();
int i=0;
BufferedReader r=new BufferedReader(new InputStreamReader(System.in));
for(int i=0;in)
break;
i=i+1;
if(i >11)
{
year++;
i=0;
}
}
b=b-a[i];
n=n-b;
i=i+1;
System.out.print(n+”,”);
switch(i)
{
case 1:
System.out.print(“January”);
break;
case 2:
System.out.print(“February”);
break;
case 3:
System.out.print(“March”);
break;
case 4:
System.out.print(“April”);
break;
case 5:
System.out.print(“May”);
break;
case 6:
System.out.print(“June”);
break;
case 7:
System.out.print(“July”);
break;
case 8:
System.out.print(“August”);
break;
case 9:
System.out.print(“September”);
break;
case 10:
System.out.print(“October”);
break;
case 11:
System.out.print(“November”);
break;
case 12:
System.out.print(“December”);
break;
}
System.out.println(” “+year);
}
}

Sample output

Enter the Year
2010
Enter the number of days
34
3, February 2010

About the above program on numeric array

A numeric array is the pivot of this program. Firstly all the number of days of the 12 months are stored in the array. An infinite for loop is used here to add the days of each month and is checked with the total number of days entered by the user. During addition of days, 29 days for the month of February is considered in case of leap year. Year is also incremented with the complete of one year (when the month number becomes greater than 11). At one point, the sum of the number of days calculated days would be more than the number of days entered by user and at this point the infinite loop is terminated.

From this point date is displayed by calculating the difference. Month name is displayed using a switch statement and year is displayed.

This program will take a string from user and will display the unique words in ths sentence.

import java.util.*;
import java.io.*;
class wordunique
{
public static void main(String args[])throws IOException
{
BufferedReader r=new BufferedReader(new InputStreamReader(System.in));
int x=0;int i,j;
String temp=””;
String s;
System.out.println(“Enter a sentence”);
s=r.readLine();
String word[]=new String[10];
StringTokenizer st=new StringTokenizer(s);
while(st.hasMoreTokens())
{
temp=st.nextToken();
for(j=0;j< 10;j++)
{
if(temp.compareTo(word[j])==0)
break;
}
if(j==x)
word[x++]=temp;
}
for(i=0;i< x;i++)
System.out.println(word[i]);
}
}

Sample Output:
Enter a sentence:This is is burdwan
This is burdwan

In this program enter any sentence and the words of the sentence will be sorted in ascending order.

import java.util.*;
import java.io.*;
class wordsort
{
public static void main(String args[])throws IOException
{
BufferedReader r=new BufferedReader(new InputStreamReader(System.in));
int x=0;int i,j;
String temp="";
String s;
System.out.println("Enter a sentence");
s=r.readLine();
String word[]=new String[10];
StringTokenizer st=new StringTokenizer(s);
while(st.hasMoreTokens())
{
word[x++]=st.nextToken();
}
for(i=0;i< x-1;i++)
{
for(j=i+1;j 0)
{
temp=word[i];
word[i]=word[j];
word[j]=temp;
}
}
}
for(i=0;i=0;i–)
System.out.println(arr[i]);
}
boolean pop()
{
if(index==-1)
{
System.out.println(“Stack is empty”);
return false;
}
else
{
System.out.println(“Deleted element”+arr[index–]);
return true;
}
}
}
class test
{
public static void main(String args[])throws IOException
{
array ob=new array();
boolean bool;
bool=ob.push();
if(bool==true)
ob.display();
bool=ob.push();
if(bool==true)
ob.display();
bool=ob.pop();
if(bool==true)
ob.display();

}
}

This page is on isc sample papers. If you have any doubt in any program, pl. feel free to put your comments. I am here to clear your doubts.

About the program on Stack

This is a program on implementation of array as a stack. Unlike queue, stack works from one end only – the front end. Again in this program like queue, insertions have a limit as this is basically an array. Deletion from stack as usual is not possible when there is no element in the stack.

How to proceed in the binary addition program

This BlueJ program is on addition of two binary numbers. In this program, we will take two binary numbers in two string objects. The smaller string object will be stored on ‘bin1’ and the greater or same string object will be stored on ‘bin2’. Here length of the strings are considered for smaller or greater strings.

Then characters from the end of both string objects are extracted and numeric addition is done on both of them. If the result exceeds 1, it will be reset on 0,1 will be carried forward and the result will be concatenated with another string object ‘str’. Ultimately ‘str’ will be displayed from reverse direction..

import java.io.*;
class Blue
{
public static void main(String args[])throws IOException
{
Blue ob=new Blue();
ob.take();
}
public void take()throws IOException
{
String bin1,bin2;
int i,a,b,c,len1,len2,flag=0;
String str=” “,str1;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println(“Enter 1st binary Number:”);
bin1=br.readLine().trim();
System.out.println(“Enter 2nd binary Number:”);
bin2=br.readLine().trim();
if(bin1.length()>bin2.length())
{
str1=bin1;
bin1=bin2;
bin2=str1;
}
len1=bin1.length()-1;
len2=bin2.length()-1;
for(i=len1;i>=0;i–)
{
a=bin1.charAt(i)-48;
b=bin2.charAt(len2)-48;
len2–;
c=a+b+flag;
flag=0;
if(c>1)
{
c=0;
flag=1;
}
str=str+c;
}
if(len2==-1 && flag==1)
str=str+flag;
else
{
for(i=len2;i>=0;i–)
{
a=bin2.charAt(i)-48;
c=a+flag;
flag=0;
str=str+c;
}
}
str=str.trim();
len1=str.length()-1;
System.out.println(“Sum of the binary numbers are:”);
for(i=len1;i>=0;i–)
System.out.print(str.charAt(i));
}
}

This post is mainly for ISC students as StringTokenizer class is in their syllabus. StringTokenizer is a predefined class of java.util package which can be used to break a string into tokens.

Constructors of StringTokenizer class

StringTokenizer class has three overloaded version of constructors and each version works with slight difference.
StringTokenizer(String s): This constructor of StringTokenizer class taken a string object as argument and break the string into tokens considering blank space as delimiters which means this constructor breaks the argument string into words.

Suppose the argument string is “This is a sample test”, the tokens will be “This”, “is”, “a”, “sample” and “test”.
In several programs we need to break a sentence into words and in such case we mostly use a combination of ‘indexOf ()’ and ‘substring ()’ function to perform the job. Using StringTokenizer class constructor, these type of jobs can be easily performed.

Second version of StringTokenizer class constructor is StringTokenizer( String s, String delim): Here the first argument is the string object to be broken into tokens and the second argument is the delimiter according to which the first argument will be broken into tokens. Here the default delimiter blank space, as in first version of constructor will be ignored.

Suppose the first argument is “This is a sample test, You have to wait for @another test” and the second argument is “,@” then the first argument string will be broken into tokens where the delimiter characters appears means the first token will be “This is a sample test”, the second token will be “You have to wait for” and the third token will be “another test”.

Third version of StringTokenizer class constructor is StringTokenizer( String s, String delim, boolean bool), where ‘bool’ is set to true: This version of constructor acts as the second version of constructor and associate the delimiter with the token. If we use the above sentence as first argument like StringTokenizer(This is a sample test, You have to wait for @another test”, “,@”, true); the tokens will be as follows “This is a sample test,”, “You have to wait for @” and “another test”.

StringTokenizer class has a function ‘ int countTokens()’ which the returns the number of tokens associated with a StringTokenizer class object.

Another function of StringTokenizer class is ‘boolean hasMoreTokens ()’ which retuens true if there is token with the StringTokenizer class object otherwise returns false.

The next most useful StringTokenizer class function is ‘ String nextToken()’ which returns the tokens from the StringTokenizer class object.

Here is a sample program on StringTokenizer class

import java.io.*;
import java.util.*;
class Str
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String str,s;
StringTokenizer stk;
int i;
public void show() throws Exception
{
System.out.println(“Enter the sentence:”);
str=br.readLine();
stk=new StringTokenizer(str);
i=stk.countTokens();
System.out.println(“Number of tokens in the sentence=”+i);
System.out.println(“The tokens are as follows\n”);
while(stk.hasMoreTokens())
{
s=stk.nextToken();
System.out.println(s);
}
}
public static void main(String args[])throws Exception
{
Stk ob=new Stk();
ob.show();
}
}

Today I will discuss about a string modification program. String class has a function ‘String trim ()’ which removes leading and trailing spaces from a string value. But what happens if there are more than one blank space between two words in a text? The above ‘trim ()’ function can not remove such blank spaces from a string.

How to proceed on this string modification program

Firstly the leading and trailing space from the string is to be removed, if any. Function ‘String trim ()’ will perform this job. Next action is to access every character and check if there is consecutive blank spaces in the string. The characters and only one blank space following any character is to be concatenated in another string object. If there are multiple blank spaces, they should not be concatenated in the second string object which will hold the modified string.

Codes of removing extra blank spaces from a string object

import java.io.*;
class Words
{
int w;
BufferedReader br;
String text,modified=””;
public static void main(String args[])throws IOException
{
Words ob=new Words();
ob.accept();
ob.result();
}
Words()
{
br=new BufferedReader(new InputStreamReader(System.in));
text=””;
w=0;
}
public void accept()throws IOException
{
System.out.println(“Enter the Sentence:”);
text=br.readLine().trim();
}
public void result()
{
int i,len;
char ch;
System.out.println(“The entered sentence =”+text);
len=text.length();
for(i=0;i< len;i++)
{
ch=text.charAt(i);
if(ch==' ' && w==1)
{
w=0;
modified=modified+ch;
}
else if(ch!=' ')
{
modified=modified+ch;
w=1;
}
}
System.out.println("The modified sentence ="+modified);
}
}

Technical analysis of the above string modification program

The job of removing extra blank spaces from the string is done within the ‘public void result ()’ function. Using a for loop each character of the string is accessed. An int type variable ‘w’ is used to notify if consecutive blank spaces are there in the string. Initial value of ‘w’ is set to ‘0’. Whenever any alphaber is found in the string, the value of the variable ‘w’ is set to ‘1’. On the process of checking each characters, if any blank space is found and the value of ‘w’ is ‘1’ at that time, the blank space is concatenated in the second string object. At the same time the value of ‘w’ is reset to ‘0’ so that consecutive two blank spaces are not taken in the modified string object.

Today’s program is on string manipulation. This type of program is very much essential for ISC students. In practical examination of ISC Computer Science every year, programs on string manipulation using string array object is a common feature.

Here is the program on string array

Accept a paragraph of text consisting of sentences that are terminated
by either “.”, “,”, “!” or a “?” followed by a space. Assume that there can be a maximum of 05 sentences in a paragraph.
Design a program to perform the following :
(a) Arrange the sentences in alphabetical order of words, sentence by sentence.
(b) Separate the words which begin with a vowel.

Sample data 1:
INPUT: HELLO ! HOW ARE YOU ? WHEN ARE YOU COMING ? HOPE TO SEE YOU SOON.
OUTPUT: HELLO ! ARE HOW YOU ? ARE COMING WHEN YOU ? HOPE SEE SOON TO YOU.
VOWELS: ARE
Sample data 2 :
INPUT : THE QUICK BROWN FOX JUMPED OVER THE LAZY DOG.
OUTPUT : BROWN DOG FOX JUMPED LAZY OVER QUICK THE THE.
VOWELS: OVER

Here are the codes of the program

import java.io.*;
import java.util.*;
class String1
{
String str,str3;
BufferedReader br;
StringTokenizer stk;
String str1[];
String str2[];
String vowel[];
int i,j,k,len,x=0,y=0,z=0;
String1()
{
str1=new String [10];
str2=new String [10];
vowel=new String [200];
br=new BufferedReader(new InputStreamReader(System.in));
}
public void take()throws IOException
{
System.out.println(“Enter the paragraph of sentences:”);
str=br.readLine();
str=str.substring(0,str.length()-1);
stk=new StringTokenizer(str,”!?”,true);
while(stk.hasMoreTokens())
{
str1[x++]=stk.nextToken();
}
for(i=0;i< x;i++)
{
str=str1[i];
stk=new StringTokenizer(str," ");
y=0;
while(stk.hasMoreTokens())
{
str2[y++]=stk.nextToken();
}

for(j=0;j< y-1;j++)
{
for(k=j+1;k0)
{
str3=str2[j];
str2[j]=str2[k];
str2[k]=str3;
}
}
}
for(j=0;j< y;j++)
{
vowel[z++]=str2[j];
System.out.print(" "+str2[j]);
}

y=0;
}
System.out.println(".");
System.out.println("\nVowels:");
for(j=0;j< z;j++)
{
str=vowel[j].trim().toUpperCase();
if(str.charAt(0)=='A' ||str.charAt(0)=='E' ||str.charAt(0)=='I' ||str.charAt(0)=='O' ||str.charAt(0)=='U')
System.out.print(vowel[j]+" ");
}
}
public static void main(String args[])throws IOException
{
String1 ob=new String1();
ob.take();
}
}

Brief study of the program

The paragraph is entered in a string object firstly. StringTokenizer class is used in this program to break the text into tokens in respect of punctuations as it is given in the program that paragraph is to be broken into sentences in respect of punctuation and stored in a string array object ‘str1’. This step ensures that all sentences are stored in the string array. Next step is to break the sentences again in words and stored in another string array ‘str2’. These words are then sorted in ascending order and displayed. Another very interesting job is performed here, all the words of ‘str2’ are stored in the third string array ‘vowel’ from which words starting with vowels will be searched. This step is required as values of the string array ‘str2’ will be changed every time a new sentence from the string array ‘str1’ is broken into words.