Question

In: Computer Science

Part I - Reading input in a loop Before you start doing any coding, brainstorm between...

Part I - Reading input in a loop

Before you start doing any coding, brainstorm between the two of you on how to go about starting this program. You need to get your code to repeatedly prompt for a new String until the user enters an empty line, and then end with a goodbye message. What is the algorithm that you need to use? Write your algorithm into comments in the main method of the Palindrome class (as shown in the class notes). The code you write needs to reproduce the following transcript (user entries are in BOLD):

Enter a string: 1331

Enter a string: racecar

Enter a string: blue

Enter a string:
Empty line read - Goodbye!
Part II - Checking for a palindrome

Now brainstorm with your partner again - once the user has entered a String you need to think about how to tell if it is a palindrome. One way to do this is to compare the characters that are supposed to match - if all of these characters match, then it is a palindrome. If any pair does not match, then it is not a palindrome. Think about the algorithm that does this kind of "pairwise character" comparison - how would you implement that with a loop? Work out the algorithm with your partner and put it into your code, then fill in the code around your algorithm to complete the task. A sample transcript of what your code should do once both parts are completed is below. User entries are shown in BOLD.

Enter a string: 1331
1331 is a palindrome.

Enter a string: racecar
racecar is a palindrome.

Enter a string: blue
blue is NOT a palindrome.

Enter a string:
Empty line read - Goodbye!
Do not worry about differences in case, punctuation, spacing or other issues. Your code should only check whether or not the string of characters is exactly the same forwards as it is backwards.

Enter a string: Racecar
Racecar is NOT a palindrome

Enter a string: RACECAR
RACECAR is a palindrome.

Enter a string: A man, a plan, a canal, Panama.
A man, a plan, a canal, Panama. is NOT a palindrome.

Enter a string: y123321y
y123321y is a palindrome.

Enter a string:
Empty line read - Goodbye!
Use the following template for your code:

public class Palindrome {
public static void main(String[] args) {
// TODO - fill in with your code
}
}

NOTE: You MUST use a nested loop for this assignment. There are other ways to solve this problem, but for this assignment you must have a loop nested inside another loop. Think about using the outer loop to read the String and the inner loop to test for the palindrome.

NOTE 2: For this assignment you need to test whether or not a String is empty (a blank line). Do NOT compare the String with an empty String. Instead, test the length of the String - if it's empty, the length will be 0. You can also use the isEmpty instance method of String if you know how to use it or would like to look it up. Comparing the String with an empty String is less efficient than just checking the length. (Note that to read an empty line on the console, you must use the Scanner's nextLine() method - the next() method skips blank space and so will not read an empty line).

Solutions

Expert Solution

import java.util.Scanner;

public class Palindrome {

public static void main(String[] args) {
  

/*

  • While(true):
    • Read input from user
    • If empty string
      • Display mesage
      • Exit While Loop   
    • Else
      • Check if the string is a palindrome or not.
      • Display result

*/


//Scanner variable to get user input
Scanner sc = new Scanner(System.in);
String str;
  
//Loop to get user input
while(true)
{
  
System.out.printf("Enter a string: ");
str=sc.nextLine(); //Getting input from user
  
int slen = str.length(); //Variable to store string length
int flag=0; //Flag variable
  
//If the input is empty
if((slen == 0)){
System.out.println("Empty line read - Goodbye!");
break;
}
else{ //If the user input is not empty
  
//Loop to check if the string is a palindrome or not.
for(int i=0; i<=slen/2 ; i++){
  
//Checking if the character and its counter character from the end are same or not.
if(str.charAt(i)!=str.charAt(slen-1-i))
flag=1; //Flag is set if it is not a palindrome.
}
  
// Printing result
if(flag==0)
System.out.println(str+" is a palindrome.");
else
System.out.println(str+" is NOT a palindrome.");
}
  
  
}
  
}
  
}

Sample Runs:


Related Solutions

Here is the start of the SAS code to address this question: DATA READING; INPUT GROUP...
Here is the start of the SAS code to address this question: DATA READING; INPUT GROUP $ WORDS @@; DATALINES; X 700 X 850 X 820 X 640 X 920 Y 480 Y 460 Y 500 Y 570 Y 580 Z 500 Z 550 Z 480 Z 600 Z 610 ; Is there a significant difference between the mean reading score among the groups? Yes No Assuming you answer "yes" in the previous question, between what groups was there a...
The sequence below represents the coding DNA strand of the start of a gene, including part...
The sequence below represents the coding DNA strand of the start of a gene, including part of the 5' untranslated region. (5')...TCGGAAGGAGGTAGCGGCAATGGGGAAAAGTATTGCTT...(3') In studying variants of this gene in disease, a nucleotide mutation of A→T was detected in the first base of the third codon. What effect would this mutation have? (When considering your answer, assume standard initiation of translation is numbered as codon 1.) Select one: a. Terminates translation b. No effect c. Disrupts ionic interactions at physiological pH...
Before you start this exercise, I want you to write down the answers to these questions.  ...
Before you start this exercise, I want you to write down the answers to these questions.   Really, write them down BEFORE you read the discussion question. 1)  Are you for or against any forms of discrimination?  (Seems like a fairly ridiculous question, doesn't it?) 2) How do you define discrimination? If you had to describe discrimination what would it be?  How would you know if you were being discriminated against? STOP!  Before you read anything else, did you write down the answer to...
Is there any difference between doing what is morally right and doing what will make one...
Is there any difference between doing what is morally right and doing what will make one happy?
Part A: Here is the start to SAS: DATA CHISQ; INPUT GROUP $ OUTCOME $ COUNT;...
Part A: Here is the start to SAS: DATA CHISQ; INPUT GROUP $ OUTCOME $ COUNT; DATALINES; DRUG ALIVE 90 DRUG DEAD 10 PLACEBO ALIVE 80 PLACEBO DEAD 20 ; Add the necessary code to evaluate the chi-square. What is the continuity-adjusted chi-square a. 0.0477 b. 0.0459 c. 0.0747 d. 0.0482 Part B. For the data in the above question, what is the 95% confidence interval for the odds ratio?​​​​​​​ 0.99-5.09 1.00-1.27 0.97-5.51 1.01-1.25
Hello. I have a statement that I´m having difficulties to answer. Do you have any input...
Hello. I have a statement that I´m having difficulties to answer. Do you have any input on this? "The only objective companies need to follow to achieve stakeholder goals is shareholder wealth maximisation." Thank you
1. After doing the textbook (Rachels) reading, and reading John Searle’s article, how would you, in...
1. After doing the textbook (Rachels) reading, and reading John Searle’s article, how would you, in your own words, summarize the basic arguments of the Mind-Brain Identity Theory? What are the basic arguments here (and make specific reference to the readings in your answer)? 2. In general, as you described it in Question #1 above, do you find yourself agreeing or disagreeing with “Mind-Brain Identity Theory” as an explanation for subjective experiences? Explain why (and remember that when assessing arguments,...
Hello, I stuck doing c++ program The program will be recieved from the user as input...
Hello, I stuck doing c++ program The program will be recieved from the user as input name of an input file and and output.file. then read in a list of name, id# and score from input file (inputFile.txt) and initilized the array of struct. find the student with the higher score and output the students info in the output file obtain the sum of all score and output the resurl in output file. prompt the user for a name to...
Part I The input to the program will be a text file containing the information for...
Part I The input to the program will be a text file containing the information for a tolerance table. An example follows using the values from the first lecture on tolerance analysis. These values will be stored in a text file. The data is comma delimited, which means that each data field is separated by a comma. If the first word is ‘PART’ the following values are the nominal size, +/- impact, tolerance, and fixed/variable. If the first word is...
Choose an issue you would like to model with a feedback loop. Remember to start with...
Choose an issue you would like to model with a feedback loop. Remember to start with the “action” and predict the effect and type of feedback that might result.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT