Question

In: Computer Science

This problem is about the java programming and contain two parts First part: a word that...

This problem is about the java programming and contain two parts

First part:

a word that reads the same forward and backward is called a palindrome, e.g., "mom", "dad", "racecar", "madam", and "Radar" (case-insensitive). Write a program called TestPalindrome, that asks the user for a word and prints whether the word is a palindrome or not.
Hint: for a string called inStr, you can use inStr. toLowerCase() which returns a new string which is all in lower case letters. Use two indexes forwardIndex and backwardIndex to scan the string forward and backward at the same time.

Give the code of the first part.

The second part is :

copy-paste the TestPalindrome program,Then modify the program to check a whole sentence (not just one word) to see whether it is a palindrome or not. Punctuation, spaces, and capitalization must be ignored.
Here are examples of palindromic sentences:
•Madam, I'm Adam
•A man, a plan, a canal - Panama!
Hint: to read a whole sentence, create a Scanner object called input and then use input.nextLine(). For a character variable called c, you can use Character.isLetter(c) to compute a boolean result indicating whether the character is a letter or not.
The

Solutions

Expert Solution

Solution for the above program is provided below. If any doubt please comment below.

First Part:

TestPalindrome.java


import java.util.Scanner;
public class TestPalindrome {
public static void main(String[] args) {
  
// New scanner object
Scanner input=new Scanner(System.in);
String inStr;
  
// Input the word
System.out.print("Enter the word to test it is palindrome: ");
inStr=input.next();
inStr=inStr.toLowerCase();
int backwardIndex=inStr.length()-1;
boolean palindrome=true;
  
// Check it is palindrome or not
for(int forwardIndex=0;forwardIndex<inStr.length();forwardIndex++)
{
// If not palindrome
if(inStr.charAt(forwardIndex)!=inStr.charAt(backwardIndex))
{
palindrome=false;
break;
}
backwardIndex--;
}
  
// Print result
if(palindrome)
{
System.out.println("The given word is palindrome");
}
else
{
System.out.println("The given word is not palindrome");
}
}
  
}

Output:

Second Part:

TestPalindrome.java


import java.util.Scanner;
public class TestPalindrome {
public static void main(String[] args) {
  
// New scanner object
Scanner input=new Scanner(System.in);
String inStr;
  
// Input the line
System.out.print("Enter the line to test it is palindrome: ");
inStr=input.nextLine();
inStr=inStr.toLowerCase();
int backwardIndex=inStr.length()-1;
boolean palindrome=true;
// Check it is palindrome or not
for(int forwardIndex=0;forwardIndex<inStr.length();forwardIndex++)
{
// If entire line parsed
if(backwardIndex<0)
break;
  
// Read each character
char c1=inStr.charAt(forwardIndex);
char c2=inStr.charAt(backwardIndex);
  
// If both are letters
if (Character.isLetter(c1)&&Character.isLetter(c2))
{
// If not palindrome
if(c1!=c2)
{
palindrome=false;
break;
}
backwardIndex--;
}
  
// If c2 is not letter
if(!Character.isLetter(c2))
{
// Decrement indexes
backwardIndex--;
forwardIndex--;
}
}
  
// Print result
if(palindrome)
{
System.out.println("The given line is palindrome");
}
else
{
System.out.println("The given line is not palindrome");
}
}
  
}

Output:


Related Solutions

Programming language to be used: Java Exercises Part 1) The Dog Class In the first part...
Programming language to be used: Java Exercises Part 1) The Dog Class In the first part of the lab, we are writing a class to represent a Dog. It should not have a main method. Dog needs fields for price (to purchase the dog), breed, name, and age. Use appropriate data types The class should have the following two kinds of Constructors: Constructor 1: Write a constructor that accepts a value for each of the fields Constructor 2: Write a...
JAVA 1 PROGRAMMING QUESTION In this program you will be writing a class that will contain...
JAVA 1 PROGRAMMING QUESTION In this program you will be writing a class that will contain some methods. These will be regular methods (not static methods), so the class will have to be instantiated in order to use them. The class containing the main method will be provided and you will write the required methods and run the supplied class in order to test those methods. ? Specifications There are two separate files in this program. The class containing the...
JAVA programming Classwork- please answer all prompts as apart of one java programming project Part A...
JAVA programming Classwork- please answer all prompts as apart of one java programming project Part A Add to your project this class Position, which has x and y coordinates. Create an abstract class GameElt, which has a String name, an int health (keep it in the range 0 to 100) and a Position pos. For GameElt, include a default constructor that starts pos at (0, 0), and a parameterized constructor that takes x and y coordinates and a name. health...
Word Building Build the following terms using word parts. 1. pus in the uterus: word part...
Word Building Build the following terms using word parts. 1. pus in the uterus: word part for pus ________________________ word part for uterus ________________________ term for pus in the uterus ________________________ 2. near the ovary: word part for near ________________________ word part for ovary ________________________ term for near the ovary ________________________ 3. pertaining to the urinary and reproductive systems: word part for urinary ________________________ word part for reproductive system ________________________ word part for pertaining to ________________________ term for pertaining to...
Using Java This is two-part question, but I have already completed the first part and just...
Using Java This is two-part question, but I have already completed the first part and just need help with the second. Here I will provide both questions and my answer to the first part: Part I Question: Write a class called Dog that contains instance data that represents the dog’s name, breed, weight, birthdate, and medical history. Define the Dog constructor to accept and initialize instance data (begin the medical history with an empty line). Include accessor and mutator methods...
There are two parts to this question first part: Consider a production facility, where the present...
There are two parts to this question first part: Consider a production facility, where the present value of expected future cash inflows from production, V = 80, may fluctuate in line with the random fluctuation in demand (u = 1.4, d = 0.71 per period and the risk-free rate, r = 5%). Suppose management has the option in two years, to contract to half the scale and half the value of the project (c = 50%), and recover $40m (Rc...
JAVA PROGRAMMING Part 1 Create a class Student, with attributes id, first name, last name. (All...
JAVA PROGRAMMING Part 1 Create a class Student, with attributes id, first name, last name. (All the attributes must be String) Create a constructor that accepts first name and last name to create a student object. Create appropriate getters and setters Create another class StudentOperationClient, that contains a main program. This is the place where the student objects are created and other activities are performed. In the main program, create student objects, with the following first and last names. Chris...
This problem consists of two parts Part I A portion of the Stockholders’ Equity section of...
This problem consists of two parts Part I A portion of the Stockholders’ Equity section of Hatten Corporation’s balance sheet as of December 31, 2016, appears below. Dividends have not been paid for the years 2014 and 2015. There has been no change in the number of shares of stock issued and outstanding during these years. Assume that the board of directors of Hatten Corporation declares a dividend of $28,650 after completing operations for the year 2016.      Stockholders’ Equity...
This problem consists of two parts. Part I A portion of the Stockholders’ Equity section of...
This problem consists of two parts. Part I A portion of the Stockholders’ Equity section of Hatten Corporation’s balance sheet as of December 31, 2016, appears below. Dividends have not been paid for the years 2014 and 2015. There has been no change in the number of shares of stock issued and outstanding during these years. Assume that the board of directors of Hatten Corporation declares a dividend of $28,650 after completing operations for the year 2016. Stockholders’ Equity Preferred...
Please note that this assignment consists of two separate parts. The first part gives the cash...
Please note that this assignment consists of two separate parts. The first part gives the cash flows for two mutually exclusive projects and is not related to the second part. The second part is a capital budgeting scenario. Part 1 Please calculate the payback period, IRR, MIRR, NPV, and PI for the following two mutually exclusive projects. The required rate of return is 15% and the target payback is 4 years. Explain which project is preferable under each of the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT