A class Magic enables the user to accept a word and checks for Magic word.
A Magic word is one which has consecutive alphabets within itself at any position. -
Example: MYSTERY has consecutive letters ST and is a Magic word.
LAB, PQ, XY, ZA... are some examples of consecutive alphabets
Some of the members of the class are given below:
Class name
Magic
Data members/instance variables:
:
to store the word
str
Member functions/methods:
:
default constructor
Magic()
:
to accept a word in UPPER case
void input()
:
boolean check()
checks and returns true if the word is a Magic
word otherwise returns false
:
void display():
displays the word with an appropriate message
Define the class Magic giving details of the constructor( ), void input().
boolean check() and void display ( ). Define the main() function to create an object and
call the functions accordingly to enable the task.
Answers
Explanation:
Magic enables the user to accept a word and checks for Magic word.
A Magic word is one which has consecutive alphabets within itself at any position. -
Example: MYSTERY has consecutive letters ST and is a Magic word.
LAB, PQ, XY, ZA... are some examples of consecutive alphabets
Some of the members of the class are given below:
Class name
Magic
Data members/instance variables:
:
to store the word
str
Member functions/methods:
:
default constructor
Magic()
:
to accept a word in UPPER case
void input()
:
boolean check()
checks and returns true if the word is a Magic
word otherwise returns false
:
void display():
displays the word with an appropriate message
Define the class Magic giving details of the constructor( ), void input().
boolean check() and void display ( ). Define the main() function to create an object and
call the functions accordingly to enable the task
Program :
import java.util.*;
public class Magic
{
String str;
Magic()
{
str = "";
}
void input()
{
Scanner Sc = new Scanner(System.in);
System.out.print("Enter a word : ");
str = Sc.nextLine();
}
boolean check()
{
boolean count = false;
str = str.toUpperCase();
for(int i = 0 ; i < str.length()-1 ; i++)
{
char a = str.charAt(i);
char b = str.charAt(i+1);
int x = (int)a;
int y = (int)b;
if(y == x+1)
{
count = true;
}
}
return count;
}
void display()
{
System.out.print("Word is : "+str);
if(check()==true)
{
System.out.print("\nMagic Word");
}
else
{
System.out.print("\nNot A Magic Word");
}
}
public static void main(String args[])
{
Magic m = new Magic();
m.input();
m.check();
m.display();
}
}