C Programs Tutorials | IT Developer
IT Developer

Java Programs - Advanced



Share with a Friend

No Repeated Alphabets Program - Java Program

Design a class NoRepeat which checks whether a word has no repeated alphabets in it.

Example: COMPUTER 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.

import java.util.Scanner; class NoRepeat{ String word; int len; public NoRepeat(String wd){ word = wd; len = word.length(); } public boolean check(){ String temp = ""; for(int i = 0; i < len; i++){ char ch = word.charAt(i); if(temp.indexOf(ch) == -1) temp += ch; } return temp.equals(word); } public void prn(){ if(check()) System.out.println(word + " has no repeated alphabets!"); else System.out.println(word + " has repeated alphabets."); } public static void main(String[] args){ Scanner in = new Scanner(System.in); System.out.print("Enter the word: "); String w = in.next().toUpperCase(); NoRepeat obj = new NoRepeat(w); obj.prn(); } }

Output

 
 
 OUTPUT 1: 
Enter the word: COMPUTER
COMPUTER has no repeated alphabets!

 OUTPUT 2: 
Enter the word: SCIENCE
SCIENCE has repeated alphabets.