Question

In: Computer Science

You are to name your package assign1 and your file Palindrome.java. Palindrome Class Palindrome - testString...

You are to name your package assign1 and your file Palindrome.java.

Palindrome Class

Palindrome

- testString : String

+ Palindrome (String)

+ isPalindrome (): boolean

The method isPalindrome is to determine if a string is a palindrome. A palindrome, for this assignment, is defined as a word or phrase consisting of alphanumeric characters that reads the same frontwards and backwards while ignoring cases, punctuation and white space. If there are no alphanumeric characters, the string is considered a palindrome. The method should return true if it is a palindrome and false otherwise.

Notice – there is no main, no input and no output for this assignment. You are limited to the following Java library classes.

- String
- Character

Here are the restrictions on this method. Up to 20% penalty if not followed.

1. You may NOT return from the inside of a loop.
2. You may NOT break from the inside of a loop.
3. You may use ONLY ONE loop (either while or do-while).

4. You may NOT copy the String to another String.
5. You may NOT process the String more than one time (only make one pass

through it).
6. You must STOP processing as early as possible (when you find that it is or is not

a palindrome). In other words, using a for loop is not a good solution.

Solutions

Expert Solution

Program code in java:

//declaring package
package assign1;

//class definition
public class Palindrome {
    //variable declaration
    String testString;
    //constructor
    public Palindrome(String text) {
        //assigning value to the variable after removing all the spaces and putting text in lower case
        testString=text.replaceAll("\\s", "").toLowerCase();
    }
    //method to find if the string is palindrome or not
    boolean isPalindrome(){
        System.out.println("Provided text is: "+testString);
        //variable declaration
        boolean flag=true;
        //finding the length of string and storing in variable
        int len=testString.length();
        //running the loop to find if the string is pallindrome
        for(int i=0; i<=len/2;i++){
            //comparing each character in the string and if the are not equal, reset the flag
            if(testString.charAt(i)!=testString.charAt(len-i-1)){
                flag=false;
            }
        }
        //return true if the string is palindrome or false if it is not
        return flag;
    }
    //main method
    public static void main(String[] args) {
        //creating object and providing the string to the constructor
        Palindrome pr=new Palindrome("Hello tmolleh");
        //claling the function to find the palindrome and storing the result in varianble
        boolean val=pr.isPalindrome();
        //checking for the value of variable
        if(val==true){//true means the string is palindrome
            System.out.println("The given text is a palindrome");
        }
        else{//false if string is not palindrome
            System.out.println("The given text is not a palindrome");
        }    
    }
    //end of main method
}
//end of class

Output:


Related Solutions

Write a Java class. The class name must be ShapeMetrics, which means the file name must...
Write a Java class. The class name must be ShapeMetrics, which means the file name must be ShapeMetrics.java. Provide the following functions: 1. getAreaOfRectangle(), with two float arguments, width and height, in that order, returning a float value which is the area of the rectangle with that width and height. 2. getSpaceDiagonalOfRectangularCuboid (), with three float arguments, width, height, and depth, in that order, returning a float value which is the length of the diagonal line which bisects the cuboid....
Create a Java class with the name (identifier) MyProgram and place it in the Questions package....
Create a Java class with the name (identifier) MyProgram and place it in the Questions package. You will need to create a main method for this class and place all of your code inside of that method. Your program should complete the following steps: - Prompt the user to enter a name for a Student. - Use the Scanner class to read in the user input and store the result in a variable. - Use the Random class to generate...
1. Circle: Implement a Java class with the name Circle. It should be in the package...
1. Circle: Implement a Java class with the name Circle. It should be in the package edu.gcccd.csis. The class has two private instance variables: radius (of the type double) and color (of the type String). The class also has a private static variable: numOfCircles (of the type long) which at all times will keep track of the number of Circle objects that were instantiated. Construction: A constructor that constructs a circle with the given color and sets the radius to...
(Using Java) create a class that can identify a palindrome. A palindrome is defined as -...
(Using Java) create a class that can identify a palindrome. A palindrome is defined as - A string of characters that reads the same from left to right as its does from right to left - Example: Anna, Civic, Kayak, Level, Madam - To recognize a palindrome, a queue can be used in conjunction with a stack o A stack can be used to reverse the order of occurrences o A queue can be used to preserve the order of...
What is a package? What package is the Scanner class in? What package is the String class in?
What is a package? What package is the Scanner class in? What package is the String class in? 
C++ Start with your Date class in the Date.cpp file (from Date01B assignment or whatever) Name...
C++ Start with your Date class in the Date.cpp file (from Date01B assignment or whatever) Name the new Date file Date03.cpp Add the following constructors to the date class create a constructor that takes 3 integers in the order month, day, year assigns the month, day, year parameters to the corresponding data items. Use the ‘setter’ to assign the values Add a line cout << “in constructor with 3 ints\n” in the constructor a 'default' constructor - no arguments Add...
// File name: Person.h // Person class declaration. Person is the base class. #pragma once #include...
// File name: Person.h // Person class declaration. Person is the base class. #pragma once #include <iostream> using namespace std; class Person { private:         string fName;         string lName;         int birthYear;         int birthMonth;         int birthDay; public:         Person();         void setName(string, string);         void setBirthDate(int, int, int);         string getFullName();         string getBirthDate(); }; // File name: Person.cpp // Person class definition. Person is the base class. #include "Person.h" Person::Person() { fName = ""; lName =...
Name your file “DMx1_Your last name_Your first name.” You can use MS Word to complete the...
Name your file “DMx1_Your last name_Your first name.” You can use MS Word to complete the matrices (go to Insert, Table, Insert table, fill the table). Save the file as a PDF file. If you draw the matrices on paper, take pictures. Make sure the pictures are clear and readable. Insert the pictures in MS Word document and save it as a PDF file. 1. Align using dot matrix: horizontal sequence – AGGCTCCC, vertical sequence – GCGCTCCG. Trace and explain...
how to create file: vim hello<your name>.py print “Hello <your name>!” on ubuntu
how to create file: vim hello<your name>.py print “Hello <your name>!” on ubuntu
Test Your Understanding Create a JAVA package by name ClassEx2 and in the main() method of...
Test Your Understanding Create a JAVA package by name ClassEx2 and in the main() method of the class, assign your name to a String object and your father name to a different String object and do the following; Concatenate your name and your father name. Get the length of your full name. Find the index of any character in your full name. Replace the occurrence of any lower case character in your full name to capital character. Compare your first...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT