Unique Word ISC Computer Science 2022 Semester 2 Theory
Design a class Unique, which checks whether a word begins and ends with a vowel.
Example: APPLE, ARORA, etc.
The details of the members of the class are given below:
Class name: Unique
Data members/instance variables:
word: stores the word
len: to store the length of the word
Methods/Member functions:
Unique(): default constructor
void acceptWord(): to accept the word in uppercase
boolean checkUnique() checks and returns 'true' if the word starts and ends with a vowel otherwise returns 'false'
void display(): displays the word along with an appropriate message
Specify the class Unique giving details of the constructor, void acceptWord(), boolean checkUnique() and void display(). Define the main() function to create an object and call the functions accordingly to enable the task.
class Unique{
String word;
int len;
public void acceptWord(){
Scanner in = new Scanner(System.in);
System.out.print("Enter the word: ");
word = in.next().toUpperCase();
len = word.length();
}
public boolean checkUnique(){
char first = word.charAt(0);
char last = word.charAt(len - 1);
if("AEIOU".indexOf(first) >= 0 && "AEIOU".indexOf(last) >= 0)
return true;
return false;
}
public void display(){
if(checkUnique())
System.out.println(word + " is unique");
else
System.out.println(word + " is not unique");
}
public static void main(String[] args){
Unique obj = new Unique();
obj.acceptWord();
obj.display();
}
}
Comments
Post a Comment