Page 668 - Computer science 868 Class 12
P. 668
void disp( ) : Displays the original word along with the encrypted word. Also calls
method add() to display the sum of the ascii codes of the alphabets that
constitute variable ‘word’
Specify the class Encrypt giving details of the constructor( ), void acceptword( ), int add() , void changeCons( ) and void disp( ).
Define a main( ) function to create an object and call the functions accordingly to enable the task.
Ans. import java.util.*;
class Encrypt
{ String word,wordn;
int len;
Encrypt()
{ word=wordn="";
len=0;
}
void acceptword()
{ Scanner sc=new Scanner(System.in);
System.out.println("Enter word");
word=sc.next().toUpperCase();
len=word.length();
}
int add()
{ int s=0;
for(int i=0;i<len;i++)
{ s+=(int)word.charAt(i);}//ascii code sum
return s;
}
void changeCons()
{ for(int i=0;i<len;i++)
{ char ch=word.charAt(i);
if(Character.isLetter(ch) && "AEIOU".indexOf(ch)!=-1)//consonant
{ int a=(int)ch-1;
if(a<65)// if A
a+=26;
wordn=wordn+(char)a;// encrypted word
}
else
wordn=wordn+ch;
}}
void display() // output
{ System.out.println("Original word "+word);
System.out.println("Encrypted word "+wordn);
System.out.println("Sum of ascii characters "+add());
}
public static void main()
{ Encrypt ob=new Encrypt();
ob.acceptword();
ob.changeCons();
ob.display();
}}
SECTION – C
Answer any two questions.
Each program should be written in such a way that it clearly depicts the logic of the problem step wise.
This can be achieved by using comments in the program and mnemonic names or pseudo codes for algorithms.
The programs must be written in Java and the algorithms must be written in standard form, wherever required/specified.
Question 9. [5]
Define a class Repeat which allows the user to add elements from one end (rear) and remove elements from the other end (front)
only. The following details of the class Repeat are given below:
Class name : Repeat
Data Members
st[] : an array to hold a maximum of 100 integer elements
666666 Touchpad Computer Science-XII

