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

Java coding exercise. Hint: User will enter certain number of random words, you

ID: 3828553 • Letter: J

Question

Java coding exercise.

Hint: User will enter certain number of random words, you need to read the input as string and evaluate based on the following instructions.

Write a void method called palindromeCheck that takes NO argument. The method should have functionality to check whether or not the word is a palindrome and print to the screen all of the palindromes, one per line. Also, the last line of the output should have the message: “There are x palindromes out of y words provided by user” (where x is the number of palindrome words detected, and y is the total number of words entered by user).

Hint: for this exercise you will need the following methods on String objects:
length() gives the length of a string (that is, the number of characters it contains) and charAt(i) - gives the character at position i. For example:
int sizeOfString = "bob".length() //should be 3 char
firstChar = "bob".charAt(0) //should be ’b’

Here is a templet that has been provided to code this exercise:

public static void palindromeCheck() {

String someWord = ""; //Stores words read from user input

int count = 0; //keeps track of Palindrome words

int total = 0; //Counts the total number of lines read from the given text file

System.out.println(" Enter some words separated by white space");

Scanner keyboard = new Scanner(System.in);

//hint 1: Using keybord.next() will only return what comes before a space.

//hint 2: Using keybord.nextLine() automatically reads the entire current line.

//for each word user enters

while (keyboard.hasNext()) {

someWord = keyboard.next(); // store each word in a String variable then operate

total++; //increment number of words as you read each one

//Code your logic for how to determine if a word is Palindrome first, then complete # 2

System.out.println(" " + total + " " + someWord);

}

//if encountered ENTER then close scanner stream and terminate

keyboard.close();

//x is a variable for count and y is variable total

//#2. print

}

Explanation / Answer

JAVA :

import java.io.*;
import java.util.*;

public class Sample1 {

public static boolean isPalindrome(String str){
for(int i=0;i<=str.length()/2;i++){
if(str.charAt(i)!=str.charAt(str.length()-i-1)){
return false;
}
}
return true;
}
public static void palindromeCheck() {
String someWord = ""; //Stores words read from user input
int count = 0; //keeps track of Palindrome words
int total = 0; //Counts the total number of lines read from the given text file
System.out.println(" Enter some words separated by white space");
Scanner keyboard = new Scanner(System.in);
//hint 1: Using keybord.next() will only return what comes before a space.
//hint 2: Using keybord.nextLine() automatically reads the entire current line.
//for each word user enters
int pal = 0;
String line = keyboard.nextLine().trim();
keyboard = new Scanner(line);
while (keyboard.hasNext()) {
someWord = keyboard.next(); // store each word in a String variable then operate
if(isPalindrome(someWord)){
System.out.println(someWord);
pal++;
}
total++; //increment number of words as you read each one
//Code your logic for how to determine if a word is Palindrome first, then complete # 2
// System.out.println(" " + total + " " + someWord);
}
//if encountered ENTER then close scanner stream and terminate
keyboard.close();
//x is a variable for count and y is variable total
//#2. print
  
System.out.println("There are "+pal+" palindromes out of "+total+" provided by the user");
}

public static void main(String args[]) throws IOException {

palindromeCheck();
}
}

OUTPUT :

Enter some words separated by white space
aba man mine noon pro
aba
noon
There are 2 palindromes out of 5 provided by the user

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