No Repeat Program ISC Computer Science 2022 Semester 2 Theory

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 the 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, 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(){
        for(int i = 0; i < len - 1; i++){
            char ch = word.charAt(i);
            if(word.substring(i + 1).indexOf(ch) >= 0)
                return false;
        }
        return true;
    }
    public void prn(){
        if(check())
            System.out.println(word + " has no repeated characters.");
        else
            System.out.println(word + " has repeated characters.");
    }
    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();
    }
}

Comments

Popular posts from this blog

Encrypt Program ISC Specimen 2023 Theory

Bank Inheritance Program ISC Specimen 2023 Theory