A new
advance operating system, incorporating the latest hi-tech features has been
designed by Opera Computer Systems. The task of generation copy protected 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 upper-case letters of the
alphabet starting from A up to K (namely among A,C,E,G,I,K). The code may or
may not be in the consecutive series of alphabets. Each code should not exceed
6 characters, display an appropriate error message.
Write
a program to input a code and its length. At the first instance of error
display “invalid” Stating the appropriate reason. In case of no error, display
the message “valid!”
Test
your program for the following data and some random data:-
Input N=4
ABCK
Output Invalid! Only alternate letters are permitted.
Input N=4
ACIK
Output Invalid! Only upper case letters are permitted.
Input N=4
AAKE
Output Invalid! Repetition of characters not permitted.
Input N=7
Output Error
Length of String should not exceed 6 characters.
Input N=4
AEGIK
Output Invalid! String
length not the same as specified.
Input N=ACE
Output Valid!
Input N=5
GEAIK
Output Valid!
Solution:-
import java.util.*;
class AdvanceOS
{
String
s1="ACEGIK";
boolean
bool;
void
disp()
{
Scanner
sc=new Scanner(System.in);
int
n;
String
sentence;
System.out.println("Enter
the length:");
n=sc.nextInt();
System.out.println("Enter
the code:");
sentence=sc.next();
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...");
}
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;
}
boolean
checkChar(String str)
{
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;
}
}
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;
}
}
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;
}
}
public static void main(String args[])
{
AdvanceOS ob=new AdvanceOS();
ob.disp();
}
}
No comments:
Post a Comment