Question

You are going to write an object that takes a word (or short phrase) as an...

You are going to write an object that takes a word (or short phrase) as an input for the Constructor and then will reverse the letters in the word.

The object to implement this is referred to as **ReverseWord** and consists of **only** the following public methods:

**public void setWord(String word)** - Sets the word to be processed.

**public String getWord()** - Returns the word that is set with **setWord**. Note that if this is called before setWord then an empty String is returned.

**public String getReversedWord()** - Returns the word set by setWord with the letters in reverse order. Note that if this is called *before* **setWord** then an empty string is returned.

Note that you **do not** implement a Constructor. We will simply use the default constructor that Java creates for us. Your **Program.java** should contain code to test your **ReverseWord** object. Load multiple values and check to make sure that the values match the expected values.

Requirements:

-  You must implement this manually using a loop. While there are Java capabilities that can do this in a single line of code, you may NOT use them. Even though this is less efficient, the objective is to apply the use of loops.

## Getting Started

Create the following files

1. Program.java

2. ReverseWord.java

**You'll have to add the proper Javadoc documentation** as well as the proper code to solve the problem. You will have to add instance variables, constants, method stubs, and code to get this class defined properly. Read the comments (and the problem) to understand what the problem is and how you will solve it.  

You will need to add test code to **Program.java** to test the **ReverseWord.java** source code.

Once you've written your code run the code by right clicking on **Program.java** in the file explorer and selecting **Run** from the context menu or using the debug tool from the **Activity Bar**. Examine the output. Does it do what you want? If not, how can you modify the code to do what you want?

## Testing Your Code

Your **Main.java** should contain code to test your **ReverseWord** object. Ask the user for a word and then output the original word and its reversed version. A sample run is shown below:

```

Input word to be reversed: catalog

Input word: catalog

Reversed word: golatac

```

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The solution is provided below

ReverseWord.java

public class ReverseWord {
   String word;
   String reversedWord;
   public ReverseWord() {
       word="";
       reversedWord="";
   }
   public String getWord() {
       return word;
   }
   public void setWord(String word) {
       this.word = word;
   }
   public String getReversedWord() {
       if(word.compareTo("")==0)
               return "";
       else {
           reversedWord="";
           for(int i=0;i<word.length();i++)
               reversedWord=word.charAt(i)+reversedWord;
       }  
       return reversedWord;
   }
}

--------------------------------------------------------------------

Program.java

import java.util.Scanner;

public class Program {

   public static void main(String[] args) {
       ReverseWord test=new ReverseWord();
       System.out.println("Input word: "+test.getWord());
       System.out.println("Reversed word: "+test.getReversedWord());
       Scanner sc=new Scanner(System.in);
       System.out.print("Input word to be reversed:");
       String input=sc.nextLine();
       test.setWord(input);
       System.out.println("Input word: "+test.getWord());
       System.out.println("Reversed word: "+test.getReversedWord());
       System.out.print("Input word to be reversed:");
       input=sc.nextLine();
       test.setWord(input);
       System.out.println("Input word: "+test.getWord());
       System.out.println("Reversed word: "+test.getReversedWord());
       System.out.print("Input word to be reversed:");
       input=sc.nextLine();
       test.setWord(input);
       System.out.println("Input word: "+test.getWord());
       System.out.println("Reversed word: "+test.getReversedWord());

   }

}

I have attached the screenhot of the test.Observe words are reversed.First time called without setWord get empty string.

Add a comment
Know the answer?
Add Answer to:
You are going to write an object that takes a word (or short phrase) as an...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • A palindrome is a word, phrase, or sequence that reads the same backward as forward, e.g.,...

    A palindrome is a word, phrase, or sequence that reads the same backward as forward, e.g., madam or nurses run. In this program, ask the user to input some text and print out whether or not that text is a palindrome. Create the Boolean method isPalindrome which determines if a String is a palindrome, which means it is the same forwards and backwards. It should return a boolean of whether or not it was a palindrome. Create the method reverse...

  • For this assignment, you will use your knowledge of arrays and ArrayLists to write a Java...

    For this assignment, you will use your knowledge of arrays and ArrayLists to write a Java program that will input a file of sentences and output a report showing the tokens and shingles (defined below) for each sentence. Templates are provided below for implementing the program as two separate files: a test driver class containing the main() method, and a sentence utilities class that computes the tokens and shingles, and reports their values. The test driver template already implements accepting...

  • You will write three static methods to manipulate an input String in different ways using various...

    You will write three static methods to manipulate an input String in different ways using various String methods. You need to provide the code for each of the three static methods in class StringPlay (requirements for each listed below). You will also change the control statement in the test harness to allow for variations in the sentinel value. You need to modify the loop control condition in Lab09.java so that user inputs of ‘finish’, “FINISH”, “FiniSH”, “fINISH”, etc. will end...

  • Background An anagram is a word phrase, or name formed by rearranging the letters of another,...

    Background An anagram is a word phrase, or name formed by rearranging the letters of another, such as the word "cinema", formed from 'iceman". Problem Statement Given two strings s and t, write a function to determine ift is an anagram of S. A sample main function is provided so that you may test your code on sample inputs. Inputs can be provided in the box below your code, please format your input as follows: Example input nose soen Requirements...

  • Write the Java code for the class WordCruncher. Include the following members:

    **IN JAVAAssignment 10.1 [95 points]The WordCruncher classWrite the Java code for the class WordCruncher. Include the following members:A default constructor that sets the instance variable 'word' to the string "default".A parameterized constructor that accepts one String object as a parameter and stores it in the instance variable. The String must consist only of letters: no whitespace, digits, or punctuation. If the String parameter does not consist only of letters, set the instance variable to "default" instead. (This restriction will make...

  • Palindrome is a word or a phrase when taken in reverse order, gives the same word...

    Palindrome is a word or a phrase when taken in reverse order, gives the same word or phrase. For example, the word “radar” is a palindrome. The phrase "Do geese see God?" is also a palindrome after the removal of the question mark and all the spaces and the conversion of the remaining alphabets to either upper or lower case. Write a program that uses an STL stack to check if an arbitrary string containing multiple words separated by spaces...

  • (9) Complete the below Java method recursively, which takes in a String and returns if it's...

    (9) Complete the below Java method recursively, which takes in a String and returns if it's a palindrome. Remember a palindrome means a string that is the same reversed, like "deed", "racecar" or "tacocat". The empty string "" is also a palindrome. public boolean isPalindrome (String word) { (8) (a) (8 points) Implement merging two sorted arrays into one sorted ar- ray. These arrays are sorted in descending order. For example, (5, 4, 2). Return an array with all the...

  • Hi I really need help with the methods for this lab for a computer science class....

    Hi I really need help with the methods for this lab for a computer science class. Thanks Main (WordTester - the application) is complete and only requires uncommenting to test the Word and Words classes as they are completed. The needed imports, class headings, method headings, and block comments are provided for the remaining classes. Javadocs are also provided for the Word and Words classes. Word The Word class represents a single word. It is immutable. You have been provided...

  • create a new Java application called "CheckString" (without the quotation marks) according to the following guidelines....

    create a new Java application called "CheckString" (without the quotation marks) according to the following guidelines. ** Each method below, including main, should handle (catch) any Exceptions that are thrown. ** ** If an Exception is thrown and caught, print the Exception's message to the command line. ** Write a complete Java method called checkWord that takes a String parameter called word, returns nothing, and is declared to throw an Exception of type Exception. In the method, check if the...

  • Given code: /** * Provides some methods to manipulate text * */ public class LoopyText {...

    Given code: /** * Provides some methods to manipulate text * */ public class LoopyText { private String text;    /** * Creates a LoopyText object with the given text * @param theText the text for this LoopyText */ public LoopyText(String theText) { text = theText; }    //Your methods here } Given tester code: /** * Tests the methods of LoopyText. * @author Kathleen O'Brien */ public class LoopyTextTester { public static void main(String[] args) { LoopyText loopy =...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT