Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a class, SentenceReversal that meets the following criteria: Instance var

ID: 3595323 • Letter: C

Question

Create a class, SentenceReversal that meets the following criteria:

Instance variable that stores a sentence. (Choose appropriate data type)

No argument constructors that sets the default value to anything that you like.

1 argument constructor that initializes the instance variable with formal parameter.

Getter and setter for instance variable.

isReverseSentenceSame() method that does not take any parameters and returns a value of true or false depending on whether the sentence and its reverse are the same. In determining this, the method should do the following:

Ignore case sensitivity (Hint: Look up the String class methods in Java API for help)

Ignore spaces and other punctuation marks (Hint: Look up the Character class methods in Java API for help)

Create a class SentenceReversalTester that does the following:

Ask the user to a sentence (single or multiple words are acceptable).

Create an object of SentenceReversal class using the values provided by the user.

Invoke the isReverseSentenceSame() method to find out whether the sentence and its reverse are equal.

SOP the following:

Original sentence without any spaces or punctuation marks (only letters and digits)

Reverse sentence without any spaces or punctuation marks (only letters and digits)

Meaningful message depending on the value returned by the method.

(Add appropriate comments (class, methods and variables). Poinst are allocated for comments)

Explanation / Answer

package stopwatch;

public class SentenceReversal {

private String sentence;

// No argument constructor, setting the sentence value to fixed string

public SentenceReversal() {

this.sentence = "abcba";

}

// 1 argument constructor, setting the sentence value to the parameter

public SentenceReversal(String sentence) {

this.sentence = sentence;

}

// getter method to get the sentence value

public String getSentence() {

return sentence;

}

// setter method to set the sentence value using the parameter

public void setSentence(String sentence) {

this.sentence = sentence;

}

// verifies the given string is palindrome or not

public boolean isReverseSentenceSame() {

char[] char_sentence,char_formatted_sentence;

char_sentence = sentence.toLowerCase().toCharArray();

char_formatted_sentence = new char[char_sentence.length];

// using isLetterOrDigit in order to removed other than digits and chars

for (int i = 0,j =0; i < char_sentence.length; i++) {

if(Character.isLetterOrDigit(char_sentence[i])) {

char_formatted_sentence[j]=char_sentence[i];

j++;

}

}

String normal = new String(char_formatted_sentence);

normal = normal.toLowerCase().trim();

String reverse = new StringBuilder(normal).reverse().toString();

System.out.println("Original sentence without any spaces or punctuation marks : "+normal);

System.out.println("Reverse sentence without any spaces or punctuation marks : "+reverse);

return normal.equalsIgnoreCase(reverse);

}

}

package stopwatch;

import java.util.Scanner;

public class SentenceReversalTester {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Please enter a sentence");

String input_sentence = sc.nextLine();

SentenceReversal sr = new SentenceReversal(input_sentence);

SentenceReversal sr1 = new SentenceReversal();

if(sr.isReverseSentenceSame()) {

System.out.println(sr.getSentence() + " is a polindrom");

} else {

System.out.println(sr.getSentence() + " is not a polindrom");

}

if(sr1.isReverseSentenceSame()) {

System.out.println(sr1.getSentence() + " is a polindrom");

} else {

System.out.println(sr1.getSentence() + " is not a polindrom");

}

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote