Question

Consider the following sample program: import java.util.Scanner; public class Palindrome { public static void main(String[] args){...

Consider the following sample program:

import java.util.Scanner;

public class Palindrome
{
public static void main(String[] args){
Scanner kb = new Scanner(System.in);
System.out.println("Enter a word:");
String word = kb.next();
  
String reverse = "";
for (int i=word.length()-1; i>=0; i--)
reverse += word.charAt(i);
boolean result = reverse.equalsIgnoreCase(word);
  
if (result)
System.out.println("The word " +word+ " is a Palindrome.");
else
System.out.println("The word " +word+ " is not a Palindrome.");
}
}

Rewrite the program so that the main method is:

public static void main(String[] args){

String word = getWordFromUser();

boolean result = checkPalindrome(word);

displayResult(result, word);

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.Scanner;

public class Palindrome {

    public static void main(String[] args) {
        String word = getWordFromUser();
        boolean result = checkPalindrome(word);
        displayResult(result, word);
    }

    public static void displayResult(boolean result, String word) {
        if (result)
            System.out.println("The word " + word + " is a Palindrome.");
        else
            System.out.println("The word " + word + " is not a Palindrome.");
    }

    public static boolean checkPalindrome(String word) {
        String reverse = "";
        for (int i = word.length() - 1; i >= 0; i--)
            reverse += word.charAt(i);
        return reverse.equalsIgnoreCase(word);
    }

    public static String getWordFromUser() {
        Scanner kb = new Scanner(System.in);
        System.out.println("Enter a word:");
        return kb.next();
    }
}
Add a comment
Know the answer?
Add Answer to:
Consider the following sample program: import java.util.Scanner; public class Palindrome { public static void main(String[] args){...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • import java.util.Scanner; public class TriangleMaker {    public static void main(String[] args) {        //...

    import java.util.Scanner; public class TriangleMaker {    public static void main(String[] args) {        // TODO Auto-generated method stub        System.out.println("Welcome to the Triangle Maker! Enter the size of the triangle.");        Scanner keyboard = new Scanner(System.in);    int size = keyboard.nextInt();    for (int i = 1; i <= size; i++)    {    for (int j = 0; j < i; j++)    {    System.out.print("*");    }    System.out.println();    }    for (int...

  • import java.util.Scanner; public class Age { public static void main(String args[]) { Scanner sc = new...

    import java.util.Scanner; public class Age { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int a; System.out.println("Input your age"); a = sc.nextInt(); boolean mess = isAllowed(a); String mess2 = ?isAllowed(a)return"You are allowed to vote";:"You arent allowed"; String age = personAge(a); personAge(a); } public static String personAge(int age) { String mess = ""; if(age<18) return mess = "You are minor"; else if(age>=18 && age<=22) return mess = "You are legal you can vote"; else if(age>=22 && age<=60) return...

  • import java.util.Scanner; public class SieveOfEratosthenes {    public static void main(String args[]) {       Scanner sc...

    import java.util.Scanner; public class SieveOfEratosthenes {    public static void main(String args[]) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter a number");       int num = sc.nextInt();       boolean[] bool = new boolean[num];            for (int i = 0; i< bool.length; i++) {          bool[i] = true;       }       for (int i = 2; i< Math.sqrt(num); i++) {          if(bool[i] == true) {             for(int j = (i*i); j<num; j = j+i) {                bool[j] = false;...

  • import java.util.Scanner; public class LabProgram { public static void main(String[] args) { Scanner scnr = new...

    import java.util.Scanner; public class LabProgram { public static void main(String[] args) { Scanner scnr = new Scanner(System.in); String inputMonth; int inputDay; inputMonth = scnr.nextString(); inputDay = scnr.nextInt(); String season; if(inputMonth == "April","May","June"){ String season = "Spring"; }else if(inputMonth == "July","August","September"){ String season = "Summer"; }else if(inputMonth == "October","November","December"){ String season = "Autumn"; }else if(inputMonth == "January","February","March"){ System.out.println("Winter"); }else{ System.out.println("Invalid"); }    if((inputMonth == "March") && (inputDay >= 20)){ String season = "Spring"; }else if((inputMonth == "June") && (inputDay >= 21)){...

  • import java.util.Scanner; public class SCAN { public static void main(String[ ] args) { int x, y,...

    import java.util.Scanner; public class SCAN { public static void main(String[ ] args) { int x, y, z; double average; Scanner scan = new Scanner(System.in); System.out.println("Enter an integer value"); x = scan.nextInt( ); System.out.println("Enter another integer value"); y = scan.nextInt( ); System.out.println("Enter a third integer value"); z = scan.nextInt( ); average = (x + y + z) / 3; System.out.println("The result of my calculation is " + average); } } What is output if x = 0, y = 1 and...

  • import java.util.Scanner; import java.util.ArrayList; public class P3A2_BRANDT_4005916 {    public static void main(String[] args)    {...

    import java.util.Scanner; import java.util.ArrayList; public class P3A2_BRANDT_4005916 {    public static void main(String[] args)    {        String name;        String answer;        int correct = 0;        int incorrect = 0;        Scanner phantom = new Scanner(System.in);        System.out.println("Hello, What is your name?");        name = phantom.nextLine();        System.out.println("Welcome " + name + "!\n");        System.out.println("My name is Danielle Brandt. "            +"This is a quiz program that...

  • make this program run import java.util.Scanner; public class PetDemo { public static void main (String []...

    make this program run import java.util.Scanner; public class PetDemo { public static void main (String [] args) { Pet yourPet = new Pet ("Jane Doe"); System.out.println ("My records on your pet are inaccurate."); System.out.println ("Here is what they currently say:"); yourPet.writeOutput (); Scanner keyboard = new Scanner (System.in); System.out.println ("Please enter the correct pet name:"); String correctName = keyboard.nextLine (); yourPet.setName (correctName); System.out.println ("Please enter the correct pet age:"); int correctAge = keyboard.nextInt (); yourPet.setAge (correctAge); System.out.println ("Please enter the...

  • import java.util.Scanner; public class Client{ public static void main(String args[]){    Coin quarter = new Coin(25);...

    import java.util.Scanner; public class Client{ public static void main(String args[]){    Coin quarter = new Coin(25); Coin dime = new Coin(10); Coin nickel = new Coin(5);    Scanner keyboard = new Scanner(System.in);    int i = 0; int total = 0;    while(true){    i++; System.out.println("Round " + i + ": "); quarter.toss(); System.out.println("Quarter is " + quarter.getSideUp()); if(quarter.getSideUp() == "HEADS") total = total + quarter.getValue();    dime.toss(); System.out.println("Dime is " + dime.getSideUp()); if(dime.getSideUp() == "HEADS") total = total +...

  • import java.util.Scanner; public class TempConvert { public static void main(String[] args) { Scanner scnr = new...

    import java.util.Scanner; public class TempConvert { public static void main(String[] args) { Scanner scnr = new Scanner(System.in); //ask the user for a temperature System.out.println("Enter a temperature:"); double temp = scnr.nextDouble(); //ask the user for the scale of the temperature System.out.println("Is that Fahrenheit (F) or Celsius (C)?"); char choice = scnr.next().charAt(0); if(choice == 'F') { //convert to Celsius if given temperature was Fahrenheit System.out.println(temp + " degrees Fahrenheit is " + ((5.0/9) * (temp-32)) + " degrees Celsius"); } else {...

  • //Your programs should not crash under any circumstances. import java.util.Scanner; public class LetterCount { public static...

    //Your programs should not crash under any circumstances. import java.util.Scanner; public class LetterCount { public static int countBs(String word) { for (int i = 0; i < word.length(); i++) { int counter = 0; if ((word.charAt(i) == 'b') || (word.charAt(i) == 'B')) { counter++; } } return counter; } public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Please enter a word: "); String str = in.next(); int result = countBs(str); System.out.printf("%s contains letter B %d times.\n", str,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT