C Programs Tutorials | IT Developer
IT Developer

Java Programs - Advanced



Share with a Friend

Unique Word Program - Java Program

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 a 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.

import java.util.Scanner; class Unique{ String word; int len; public Unique(){ word = ""; len = 0; } public void acceptword(){ Scanner in = new Scanner(System.in); System.out.print("ENTER THE WORD: "); word = in.nextLine().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(){ System.out.println("WORD ENTERED: " + word); if(checkUnique()) System.out.println("IT IS A UNIQUE WORD"); else System.out.println("IT IS NOT A UNIQUE WORD"); } public static void main(String[] args) { Unique obj = new Unique(); obj.acceptword(); obj.display(); } }

Output

 
 
 OUTPUT 1: 
ENTER THE WORD: APPLE
WORD ENTERED: APPLE
IT IS A UNIQUE WORD

 OUTPUT 2: 
ENTER THE WORD: ARORA
WORD ENTERED: ARORA
IT IS A UNIQUE WORD