Advertisements
Advertisements
प्रश्न
Design a class NoRepeat which checks whether a word has no repeated alphabets in it.
Example: COMP.UTER has no repeated alphabets but SCIENCE has repeated alphabets.
The details of the class are given below:
| Class name: | NoRepeat |
| Data members/instance variables: | |
| word: | to store a word |
| len: | to store the length of the word |
| Methods/Member functions: | |
| NoRepeat (String wd): | parameterized constructor to initialize word=wd |
| boolean check( ): | checks whether a word has no repeated alphabets and returns true else returns false. |
| void prn( ): | displays the word along with an appropriate message |
Specify the class NoRepeat, giving details of the constructor(String), boolean check() and void prn( ). Define the main() function to create an object and call the functions accordingly to enable the task.
कोड लेखन
Advertisements
उत्तर
import java.util*;
public class NoRepeat
{
String word;
int len;
public NoRepeat(String wd)
{
word=wd;
len=word.length();
}
public boolean check()
{
char ch,c;
int found=0;
for(int i=0;i<len;i++)
{
c=word.charAt(i);
for(int j=i+1;j<len;j++)
{
ch=word.charAt(j);
if (c==ch)
{
found=1;
break;
}
}
}
if (found==1)
return true;
else
return false;
}
public void prn()
{
boolean r=check();
if (r=true)
System.out.println(“Repeating letters”);
else
System.out.println(“No Repeating letters”);
}
public static void main(String [] args)
{
String t;
Scanner sc=new Scanner(System.in);
System.out.println(“Enter the word”);
t=sc.nextLine().toUpperCase();
NoRepeat obj=new NoRepeat(t);
obj.prn();
}
}shaalaa.com
क्या इस प्रश्न या उत्तर में कोई त्रुटि है?
