‹ Back to the paper
A class Flipgram has been defined to flip the letters of the left and right halves of a…
A class Flipgram has been defined to flip the letters of the left and right halves of a non-heterogram word. If the word has odd number of characters, then the middle letter remains at its own position.
A heterogram is a word where no letter appears more than once.
Example 1: INPUT : BETTER
OUTPUT: TERBET
Example 2: INPUT : NEVER
OUTPUT: ERVNE
Example 3: INPUT : THAN
OUTPUT: HETEROGRAM
The details of the members of the class are given below:
Class name : Flipgram
Data member/instance variable:
word : to store a word
Methods/Member functions:
Flipgram(String s) : parameterised constructor to assign word = s
boolean ishetero( ) : to return true if word is a heterogram else return false
String flip( ) : to interchange the left and right sides of a non-heterogram word and return the resultant word
void display( ) : to print the flipped word for a non-heterogram word by invoking the method flip( ). An appropriate message should be printed for a heterogram word
Specify the class Flipgram giving the details of the constructor(String), boolean ishetero( ), String flip( ) and void display( ). Define a main( ) function to create an object and call the functions accordingly to enable the task.
Answer
Answer
AIclass Flipgram
{
String word;
Flipgram(String s)
{
word = s;
}
boolean ishetero()
{
for (int i = 0; i < word.length(); i++)
{
for (int j = i + 1; j < word.length(); j++)
{
if (word.charAt(i) == word.charAt(j))
return false;
}
}
return true;
}
String flip()
{
int len = word.length();
int half = len / 2;
String left = word.substring(0, half);
String right = (len % 2 == 0) ? word.substring(half) : word.substring(half + 1);
String mid = (len % 2 == 0) ? "" : word.substring(half, half + 1);
return right + mid + left;
}
void display()
{
if (ishetero())
System.out.println("HETEROGRAM");
else
System.out.println(flip());
}
public static void main(String[] args)
{
Flipgram f1 = new Flipgram("BETTER");
f1.display();
Flipgram f2 = new Flipgram("NEVER");
f2.display();
Flipgram f3 = new Flipgram("THAN");
f3.display();
}
}Explanation: ishetero() checks every pair of characters for a repeat; if none repeats, the word is a heterogram. flip() splits the word into left and right halves (keeping the middle letter fixed for odd-length words) and returns right+mid+left. display() prints "HETEROGRAM" for a heterogram, otherwise the flipped word. Tested: BETTER -> TERBET, NEVER -> ERVNE, THAN -> HETEROGRAM (all letters distinct), matching the question's examples.From ISC 2025 Computer Science Paper 1, question 8.