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

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...
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...
JAVA programming - please answer all prompts as apart of 1 java assignment. Part A Create...
JAVA programming - please answer all prompts as apart of 1 java assignment. Part A Create a java class InventoryItem which has a String description a double price an int howMany Provide a copy constructor in addition to other constructors. The copy constructor should copy description and price but not howMany, which defaults to 1 instead. In all inheriting classes, also provide copy constructors which chain to this one. Write a clone method that uses the copy constructor to create...
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...
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...
PROGRAMMING LANGUAGE : JAVA Problem specification. In this assignment, you will create a simulation for a...
PROGRAMMING LANGUAGE : JAVA Problem specification. In this assignment, you will create a simulation for a CPU scheduler. The number of CPU’s and the list of processes and their info will be read from a text file. The output, of your simulator will display the execution of the processes on the different available CPU’s. The simulator should also display: -   The given info of each process -   CPU utilization - The average wait time - Turnaround time for each process...
Problem in chapter 15 of Java book. Need a solution please! Problem: Standard telephone keypads contain...
Problem in chapter 15 of Java book. Need a solution please! Problem: Standard telephone keypads contain the digits zero through nine. The numbers two through nine each have three letters associated with them (as seen below). Many people find it difficult to memorize phone numbers, so they use the correspondence between digits and letters to develop seven-letter words that correspond to their phone numbers. For example, a person whose telephone number is 686-2377 might use this tool to develop the...
This question has two parts. Please answer both, thank you! Part A.) On the first day...
This question has two parts. Please answer both, thank you! Part A.) On the first day of the fiscal year, a company issues a $8,200,000, 9%, 9-year bond that pays semiannual interest of $369,000 ($8,200,000 × 9% × ½), receiving cash of $6,868,206. Journalize the first interest payment and the amortization of the related bond discount. Round to the nearest dollar. If an amount box does not require an entry, leave it blank. Interest Expense Discount on Bonds Payable Cash...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT