Here is the program:
A new advanced Operating System, incorporating the latest hi-tech features has been designed by Opera Computer System. The task of generating copy protection codes to prevent software piracy has been entrusted to the Security Department. The security department has decided to have codes containing a jumbled combination of alternate uppercase letters of the alphabet starting from A upto K (namely among A,C,E,G,I,K). The codes may or may not be in the consecutive series of alphabets.
Develop a program to input a code and its length. At the first instance of an error display “Invalid!” stating the appropriate reason. In case of no error, display the message “Valid!”
Codes of the program:
import java.io.*;
class Sentence
{
String s1="ACEGIK";
boolean bool;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
public void disp()throws Exception
{
int n;
String sentence;
System.out.println("Enter the length:");
n=Integer.parseInt(br.readLine());
System.out.println("Enter the code:");
sentence=br.readLine();
bool=checkLength(sentence,n);
if(!bool)
return;
bool=checkChar(sentence);
if(!bool)
return;
bool=checkSequence(sentence);
if(!bool)
return;
bool=checkRepeat(sentence);
if(!bool)
return;
System.out.println("Valid Input...");
}
private boolean checkLength(String str, int n)
{
boolean b=true;
if(n!=str.length())
{
b=false;
System.out.println("Invalid! String length not the same as specified");
}
else if(n>6)
{
b=false;
System.out.println("Error! Length of string should not exceed 6 characters!");
}
return b;
{
int i,len;
len=str.length();
for (i=0;i< len;i++)
{
if(str.charAt(i)< 65 || str.charAt(i) >90)
break;
}
if(i==len)
return true;
else
{
System.out.println("Invalid! Only upper case characters are permitted.");
return false;
}
}
private boolean checkSequence(String str)
{
int i,len;
char ch;
len=str.length();
for (i=0;i< len;i++)
{
ch=str.charAt(i);
if(s1.indexOf(ch)< 0)
break;
}
if(i==len)
return true;
else
{
System.out.println("Invalid! Only alternate letters are permitted.");
return false;
}
}
private boolean checkRepeat(String str)
{
int i,j,len;
char ch;
len=str.length();
for (i=0;i< len-1;i++)
{
ch=str.charAt(i);
for(j=i+1;j< len;j++)
{
if(ch==str.charAt(j))
break;
}
if(j!=len)
break;
}
if(i==len-1)
return true;
else
{
System.out.println("Invalid! Repetition of letters is not permitted.");
return false;
}
}
{
Sentence ob=new Sentence();
ob.disp();
}
}
Technical analysis of the program
In this program, I have used number of functions to check the validity of the generated code. It is stated in the program that the characters in the code should be within specific letters. The combination of the letters are stored in a string object which will help to check if there is any duplicate letter in the generated code. The function ‘void disp()’ is called first and from this function the entire job of the program is controlled. All the other functions defined in this program are return type and they return ‘true’ if the function finds the generated code is correct in respect of that particular function. These checking functions are called one after another from the body of the function and if any one of them returns ‘false’ means the code is not correct, the program is terminated by using a return statement. If all the functions return true, the last statement of the function ‘void disp()’ will be executed and it prints ‘valid input…’.
Sample input and output
Enter the length:
4
4
Enter the code:
ABCE
Invalid! Only alternate letters are permitted!
ABCE
Invalid! Only alternate letters are permitted!
Enter the length:
4
4
Enter the code:
AcIK
Invalid! Only upper case characters are permitted
AcIK
Invalid! Only upper case characters are permitted
Enter the length:
4
4
Enter the code:
AAKE
Invalid! Repetition of letters is not permitted
AAKE
Invalid! Repetition of letters is not permitted
Enter the length:
7
Enter the code:
AAKEYYG
Error! Length of string should not exceed 6 characters!
AAKEYYG
Error! Length of string should not exceed 6 characters!
Enter the length:
4
4
Enter the code:
ACEGI
ACEGI
Invalid! String length not the same as specified
Enter the length:
3
Enter the code:
ACE
ACE
Valid Input...
Related Post: BlueJ Programs on String/Sentence
No comments:
Post a Comment