Question

In: Computer Science

For this week’s assignment, you will write a program class that has two subroutines and a...

For this week’s assignment, you will write a program class that has two subroutines and a main routine. The program should be a part of the ‘Firstsubroutines’ class and you should name your project Firstsubroutines if you are using Netbeans.

Your program must prompt the user to enter a string. The program must then test the string entered by the user to determine whether it is a palindrome. A palindrome is a string that reads the same backwards and forwards, such as "radar", "racecar", and "able was I ere I saw elba". It is customary to ignore spaces, punctuation, and capitalization when looking for palindromes. For example, "A man, a plan, a canal. Panama!" is considered to be a palindrome.

To determine whether a string is a palindrome, you can: convert the string to lower case; remove any non-letter characters from the string; and compare the resulting string with the reverse of the same string. If they are equal, then the original string is considered to be a palindrome.

Here’s the code for computing the reverse of str:

  String reverse;

  int i;

  reverse = "";

  for (i = str.length() - 1; i >= 0; i--) {

        reverse = reverse + str.charAt(i);

  }
  

As part of your assignment, you must write a static subroutine that finds the reverse of a string. The subroutine should have one parameter of type String and a return value of type String.

You must also write a second subroutine that takes a String as a parameter and returns a String. This subroutine should make a new string that is a copy of the parameter string, except that all the non-letters have been omitted. The new string should be returned as the value of the subroutine. You can tell that a character, ch is a lower-case letter by testing if (ch >= 'a' && ch <= 'z')

(Note that for the operation of converting str to lower case, you can simply use the built-in toLowerCase subroutine by saying: str = str.toLowerCase();)

You should write a descriptive comment before each subroutine, saying what it does.

Finally, write a main() routine that will read in a string from the user and determine whether or not it is a palindrome. The main routine should use The program should print the string converted to lower case and stripped of any non-letter characters. Then it should print the reverse string. Finally, it should say whether the string is a palindrome. (Use the two subroutines to process the user's string.) For example, if the user's input is "Hello World!", then the output might be:

    stripped: helloworld
    
    reversed: dlrowolleh
    
    This is NOT a palindrome.
    
    and if the input is "Campus motto: Bottoms up, Mac!", the output might be:
    
    stripped: campusmottobottomsupmac
    
    reversed: campusmottobottomsupmac  

    This IS a palindrome.
  

You must complete your program, test, debug, and execute it. You should test two different cases, one that is a palindrome and another one that is not a palindrome. You must submit your java code file (preferably by cut and paste the code into the assignment dialog box). The output of your program must be captured by either copying the content in the output window and pasting it into the assignment dialog box or by capturing the image of the screen which contains the output of your java program or the output and submitting this with your assignment as an attachment. In windows you can capture a screen shot with the Ctrl Alt and Print Screen key sequence. This image can then be pasted into a Word, WordPad, or OpenOffice document which can be submitted with your assignment.

Solutions

Expert Solution

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

// Firstsubroutines.java

import java.util.Scanner;

public class Firstsubroutines {

      // method to reverse a String

      static String reverse(String str) {

            String reverse;

            int i;

            reverse = "";

            // looping and prepending all characters to reverse

            for (i = str.length() - 1; i >= 0; i--) {

                  reverse = reverse + str.charAt(i);

            }

            return reverse;

      }

      // method to strip all non alphabetic characters from str

      static String strip(String str) {

            // converting str to lower case

            str = str.toLowerCase();

            String stripped = "";

            // looping and appending all alphabets to stripped

            for (int i = 0; i < str.length(); i++) {

                  char c = str.charAt(i);

                  if (c >= 'a' && c <= 'z') {

                        // alphabet

                        stripped += c;

                  }

            }

            return stripped;

      }

      public static void main(String[] args) {

            // scanner for reading input

            Scanner scanner = new Scanner(System.in);

            System.out.print("Enter a String: ");

            // reading text

            String text = scanner.nextLine();

            // stripping and displaying it

            text = strip(text);

            System.out.println("\nstripped: " + text);

            // reversing and displaying it

            String rev = reverse(text);

            System.out.println("\nreversed: " + rev);

            // checking if text is palindrome and displaying the result

            if (text.equals(rev)) {

                  System.out.println("\nThis IS a palindrome.");

            } else {

                  System.out.println("\nThis is NOT a palindrome.");

            }

      }

}

/*OUTPUT 1*/

Enter a String: Hello World!

stripped: helloworld

reversed: dlrowolleh

This is NOT a palindrome.

/*OUTPUT 2*/

Enter a String: race car

stripped: racecar

reversed: racecar

This IS a palindrome.


Related Solutions

For this week’s lab assignment, you will write a program called lab9.c. You will write a...
For this week’s lab assignment, you will write a program called lab9.c. You will write a program so that it contains two functions, one for each conversion. The program will work the same way and will produce the same exact output. The two prototypes should be the following: int btod(int size, char inputBin[size]); int dtob(int inputDec); The algorithm for the main() function should be the following: 1. Declare needed variables 2. Prompt user to enter a binary number 3. Use...
For this computer assignment, you are to write a C++ program to implement a class for...
For this computer assignment, you are to write a C++ program to implement a class for binary trees. To deal with variety of data types, implement this class as a template. Most of the public member functions of the BinaryTree class call private member functions of the class (with the same name). These private member functions can be implemented as either recursive or non-recursive, but clearly, recursive versions of these functions are preferable because of their short and simple implementations...
For this computer assignment, you are to write a C++ program to implement a class for...
For this computer assignment, you are to write a C++ program to implement a class for binary trees. To deal with variety of data types, implement this class as a template. Most of the public member functions of the BinaryTree class call private member functions of the class (with the same name). These private member functions can be implemented as either recursive or non-recursive, but clearly, recursive versions of these functions are preferable because of their short and simple implementations...
This week’s assignment is a position paper. Below are two statements of which you will select...
This week’s assignment is a position paper. Below are two statements of which you will select one to be the basis of the assignment. You are then asked to determine if you will take a pro (agree) or con (disagree) position on the statement. You must then (in 1 page) argue your position of pro or con. Remember, you are to select one statement only. You should restate the quote at the beginning of your paper (center it), and then...
Write a program that creates a Singleton class to connect to two databases. The program must...
Write a program that creates a Singleton class to connect to two databases. The program must provide two instances of this class. One instance for connecting to MySQL database and the other for connecting to Oracle database.
in Java please For this assignment you are to write a class that supports the addition...
in Java please For this assignment you are to write a class that supports the addition of extra long integers, by using linked-lists. Longer than what is supported by Java's built-in data type, called long. Your program will take in two strings, consisting of only digits, covert each of them to a linked-list that represents an integer version on that string. Then it will create a third linked-list that represents the sum of both of the linked lists. Lastly, it...
For this assignment you will write a class that transforms a Postfix expression (interpreted as a...
For this assignment you will write a class that transforms a Postfix expression (interpreted as a sequence of method calls) into an expression tree, and provides methods that process the tree in different ways. We will test this using our own program that instantiates your class and calls the expected methods. Do not use another class besides the tester and the ExpressionTree class. All work must be done in the class ExpressionTree. Your class must be called ExpressionTree and have...
Write a java program that has a class named Octagon that extends the class Circ and...
Write a java program that has a class named Octagon that extends the class Circ and implements Comparable (compare the object's area) and Cloneable interfaces. Assume that all the 8 sides of the octagon are of equal size. Your class Octagon, therefore, must represent an octagon inscribed into a circle of a given radius (inherited from Circle) and not introduce any new class variables. Provide constructors for clas Octagon with no parameters and with 1 parameter radius. Create a method...
Write a java program using the following information Design a LandTract class that has two fields...
Write a java program using the following information Design a LandTract class that has two fields (i.e. instance variables): one for the tract’s length(a double), and one for the width (a double). The class should have:  a constructor that accepts arguments for the two fields  a method that returns the tract’s area(i.e. length * width)  an equals method that accepts a LandTract object as an argument. If the argument object holds the same data (i.e. length and...
1. You are to write a simple program with two classes. One controller class and a class to hold your object definition.
1. You are to write a simple program with two classes. One controller class and a class to hold your object definition. (Similar to what we used in class) 2. Use a package in your project, the package name should be xxxprojectname. xxx is your initials taken from the first three characters of your Cal Poly email address. 3. Read in the following from the JOptionPane input window: a. Customer First Name b. Customer Last Name c. Customer Phone Number...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT