Question

In: Computer Science

IN JAVA PLEASE The Palindrome class will have a single constructor that accepts a String argument...

IN JAVA PLEASE

The Palindrome class will have a single constructor that accepts a String argument and checks to see if the String is a palindrome.

If it is a palindrome it will store the palindrome and the original String as state values. If is not a palindrome it will throw a “NotPalindromeError” and not finish the creation of the new Palindrome object.

The constructor will use a single stack to evaluate if the String is a palindrome.

You must use a bounded stack to do the evaluation. You may use the ArrayBoundedStack from the book.

The bounded stack may only hold one half of the target String.

The Strings to be checked will consist of alphanumeric characters, spaces, and punctuation. The spaces and punctuation are to be ignored for the purpose of determining if the Strings are palindromes. Capitalization should not affect the identification of a palindrome.

Solutions

Expert Solution


public class Palindrome {
        private String original;
        public String palindrome;

        public Palindrome(String str) throws Exception {
        
                this.original = str;
                int n=str.length();
                ArrayBoundedStack s =new ArrayBoundedStack(n/2);// Creating array of half of size of string
                for(int i=0;i<n/2;i++)
                {
                        s.push(str.charAt(i));
                }
                for(int i=n-n/2;i<n;i++)
                {
                        if(s.pop()!=str.charAt(i))
                        {
                                throw new Exception("NotPalindromeError");// throwing exception if the string is not string 
                        }
                }
                this.palindrome=this.original;          
        }
        
}

code snap:

ArrayBoundedStack class:


public class ArrayBoundedStack {
        private int size;
        char[] arr;
        int i=0;

        public ArrayBoundedStack(int size) {
                super();
                this.size = size;
                arr=new char[size];     
        }
        public void push(char c)
        {
                arr[i++]=c;
        }
        public char pop()
        {
                return arr[--i];
        }
        public char peek()
        {
                return arr[i-1];
        }
        

}

code snap:

main method:

import java.util.Scanner;

public class Main1 {

        public static void main(String[] args) throws Exception {
                // TODO Auto-generated method stub
                Scanner src=new Scanner(System.in);
                String s=src.nextLine();
                Palindrome p=new Palindrome(s);
                System.out.println("Palindrome is:"+p.palindrome);
        }

}

code snap:

output for a string which is a palindrome:

output for a string which not a palindrome:


Related Solutions

Please write code in java and comment . thanks Item class A constructor, with a String...
Please write code in java and comment . thanks Item class A constructor, with a String parameter representing the name of the item. A name() method and a toString() method, both of which are identical and which return the name of the item. BadAmountException Class It must be a RuntimeException. A RuntimeException is a subclass of Exception which has the special property that we wouldn't need to declare it if we need to use it. It must have a default...
PLEASE DO IN JAVA 4.15 Palindrome A palindrome is a string that is the same spelled...
PLEASE DO IN JAVA 4.15 Palindrome A palindrome is a string that is the same spelled forward as it is spelled backward. So, "abcba" is a palindrome, as are "eye" and "madam". This program takes as input from the user, a string and outputs whether the string is a palindrome. (1) Modify the given program to use a loop to output the string one character at a time. (1 pt) Example output for word = radar: Word Entered: radar r...
In Java, write a recursive function that accepts a string as its argument and prints the...
In Java, write a recursive function that accepts a string as its argument and prints the string in reverse order. Demonstrate the function in a driver program.
JAVA The class will have a constructor BMI(String name, double height, double weight). The class should...
JAVA The class will have a constructor BMI(String name, double height, double weight). The class should have a public instance method, getBMI() that returns a double reflecting the person's BMI (Body Mass Index = weight (kg) / height2 (m2) ). The class should have a public toString() method that returns a String like Fred is 1.9m tall and is 87.0Kg and has a BMI of 24.099722991689752Kg/m^2 (just print the doubles without special formatting). Implement this class (if you wish you...
Java and please have screenshots with source code and output \item[(1)] A palindrome is a string...
Java and please have screenshots with source code and output \item[(1)] A palindrome is a string that reads the same forwards as backwards. Using only a fixed number of stacks and queues, the stack and queue ADT functions, and a fixed number of int and char variables, write an algorithm to determine if a string is a palindrome. Assume that the string is read from standard input one character at a time. The algorithm should output true or false as...
Class Exercise: Constructor using JAVA Let’s define a Class together and have a constructor while at...
Class Exercise: Constructor using JAVA Let’s define a Class together and have a constructor while at it. - What should the Class object represent? (What is the “real life object” to represent)? - What properties should it have? (let’s hold off on the methods/actions for now – unless necessary for the constructor) - What should happen when a new instance of the Class is created? - Question: What are you allowed to do in the constructor? - Let’s test this...
Please write code in java and comment . thanksItem classA constructor, with a String...
Please write code in java and comment . thanksItem classA constructor, with a String parameter representing the name of the item.A name() method and a toString() method, both of which are identical and which return the name of the item.BadAmountException ClassIt must be a RuntimeException. A RuntimeException is a subclass of Exception which has the special property that we wouldn't need to declare it if we need to use it.It must have a default constructor.It must have a constructor which...
Write a function that takes a string as an argument checks whether it is a palindrome....
Write a function that takes a string as an argument checks whether it is a palindrome. A palindrome is a word that is the same spelt forwards or backwards. Use similar naming style e.g. name_pal. E.g. If we call the function as abc_pal(‘jason’) we should get FALSE and if we call it a abc_pal(‘pop’) we should get TRUE. Hint: define your function as abc_pal(str). This indicates that string will be passed. Next create two empty lists L1=[] and L2=[] ....
(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...
Write an interactive Java class which accepts an input argument when the application is executed from...
Write an interactive Java class which accepts an input argument when the application is executed from the command-line. Accept input from the user and compare the value entered to the command-line argument value. If the strings do not equal, display "INVALID VALUE! TRY AGAIN!", otherwise display "PERMISSION GRANTED!" and exit the program.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT