Question

In: Computer Science

Write a Java program that prompts the user to input a word (String). The program must...

Write a Java program that prompts the user to input a word (String).

The program must print the reversed word with all consecutive duplicate characters removed.

The program must contain the following classes: -

The StackX class (you can use the Java Stack class). - The Reverse class which must contain a private data field called “word” of type string, a constructor, and a void method called revNoDup(). The revNoDup() method must reverse the word and remove the consecutive duplicate characters. The revNoDup() method must print the new word (i.e. reversed with consecutive duplicate characters removed). The revNoDup() method must use a Stack to reverse the word and remove the duplicate characters. - The Test class which must contain the main() method. In the main() method, you must prompt the user to input a word (String), create an object from class Reverse, and call the revNoDup() method.

Here is a sample run:

Enter a word:

Tomorrow

woromoT

Solutions

Expert Solution

Greetings!!!!!!!!!!!!!!!

Please find the required program and few sample run screen shots as below.

import java.io.*; 
import java.util.*; 

// class StackX
class StackX
{
    // word data field as private
    String word;
    
    //Constructor of StackX
    public StackX()
    {
    
    }
    
    /* revNoDup function, which takes word as argument and print the word in 
    reverse order with removal of consecutive character */
    
    void revNoDup(String str)
    {
        //Stack class of java is used.
        // created object of class Stack.
        Stack <Character> stack =new Stack<>();
        
        //Prevchar is used to check for consecutive character.
        char prevchar=' ';
        
        //looping entire string character by character
        for(int i = 0; i < str.length(); i++)
        {
           //Taking current character in currentchr variable.
           char currentchr = str.charAt(i);
           
           /* Checking current character with previous one and ignoring if
           matching occur */
            if(currentchr!=prevchar)
                stack.push(currentchr); 
                
            //Keeping track of previous character for comparison.    
            prevchar=currentchr;
        }
        
        //popping element from the stack and displaing reverse word.
        while(!stack.empty())
        {
            Character reverseChar = (Character) stack.pop(); 
            System.out.print(reverseChar); 
        }
        
    }
    
}


public class Test
{
        public static void main(String[] args) {
                
        //System.in is used for standard input stream
        Scanner scannerObj= new Scanner(System.in); 
        
        //take input word from the user
    System.out.print("Enter a word: ");  
    String strWord= scannerObj.nextLine();                
    
    //Create object of the StackX class created as above.           
        StackX stkObj = new StackX();
        
        //call revNoDup function, passing word as parameter.
    stkObj.revNoDup(strWord);
        }
}


Thank You


Related Solutions

Write a Java program that prompts the user to input a string and prints whether it...
Write a Java program that prompts the user to input a string and prints whether it is a palindrome. A palindrome is a string which reads the same backward as forward, such as Madam (disregarding punctuation and the distinction between uppercase and lowercase letters). The program must use the stack data structure. The program must include the following classes: The StackX class (or you can use the Java Stack class). The Palindrome class which must contain a method named palindrome()...
Write a program that prompts the user to input a string. The program then uses the...
Write a program that prompts the user to input a string. The program then uses the function substr to remove all the vowels from the string. For example, if str=”There”, then after removing all the vowels, str=”Thr”. After removing all the vowels, output the string. Your program must contain a function to remove all the vowels and a function to determine whether a character is a vowel. You must insert the following comments at the beginning of your program and...
Write a C++ Program Write a program that prompts the user to input a string. The...
Write a C++ Program Write a program that prompts the user to input a string. The program then uses the function substr to remove all the vowels from the string. For example, if str=”There”, then after removing all the vowels, str=”Thr”. After removing all the vowels, output the string. Your program must contain a function to remove all the vowels and a function to determine whether a character is a vowel. You must insert the following comments at the beginning...
Write a Python program which prompts the user to input a string. Then, print the string...
Write a Python program which prompts the user to input a string. Then, print the string in reverse to the terminal Sample output Please enter a word: "zeus" The reverse of zeus is suez Hint: There are several ways to accomplish this. Recall that a string is an itterable object and therefore can be used with a for loop
JAVA: Write a program that prompts the user to input a type of medication and the...
JAVA: Write a program that prompts the user to input a type of medication and the output will be a list of side effects that can occur from that medication.
(JAVA) Create a program that prompts the user for an age input. The program defines the...
(JAVA) Create a program that prompts the user for an age input. The program defines the age group of the user. Follow the table below to construct the output for your program. Age Age Group 0 Baby 1-3 Toddler 4-11 Child 12-17 Teenager 18-21 Young Adult 22-64 Adult 65+ Senior Negative Number Invalid Input Sample Input Enter an age: 18 Sample Output: You are a young adult. Sample Input Enter an age: 29 Sample Output: You are an adult. Sample...
⦁   Write a Bash script that prompts for user input and reads a string of text...
⦁   Write a Bash script that prompts for user input and reads a string of text from the user. If the user enters a no null (no empty) string, the script should prompt the user to re-enter again the string; otherwise should display the string entered “. ⦁   Given an array of the following four integers (3, 5, 13, 14); write a Bash script to display the second and all elements in the array.    ⦁   Write a short Bas...
Write a MIPS assembly program that prompts the user first for a string, then for a...
Write a MIPS assembly program that prompts the user first for a string, then for a character. The program should then search the string for the character and print out the number of times the character appears in the string. You can use as many restrictions as you want, just be sure to list them. You must allocate space in the .data segment of your program for the user string.
Write Java program that asks a user to input a letter, converts the user input to...
Write Java program that asks a user to input a letter, converts the user input to uppercase if the user types the letter in lowercase, and based on the letter the user the user enters, display a message showing the number that matches the letter entered. For letters A or B or C display 2 For letter D or E or F display 3 For letter G or H or I display 4 For letter J or K or L...
Write a Java application with a JavaFXGUI that takes a String input by the user and...
Write a Java application with a JavaFXGUI that takes a String input by the user and shows whether the string contains all 26 letters of the (English version of the Latin) alphabet. For example, "Pack my box with five dozen liquor jugs" contains all 26 letters, but "The quick frown box jumps over the hazy log" does not contain a d. It does not matter whether one or more letters appear more than once. The GUI needs, at minimum, a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT