bartleby

Concept explainers

bartleby

Videos

Textbook Question
Book Icon
Chapter 4, Problem 1E

Write a fragment of code that will read words from the keyboard until the word done is entered. For each word except done, report whether its first character is equal to its last character. For the required loop, use a

  1. a. while statement
  2. b. do-while statement
Expert Solution
Check Mark

Explanation of Solution

a.

Using “while” statement:

The code fragment to read words until the user enters “done”. For every word, the program should check whether the first and last characters are match or not. The program using “while” condition is as follows. Code fragment is highlighted.

//Import required package

import java.util.Scanner;

//Define the Main class

public class Main

{

    //Define main method

    public static void main(String[] args)

    {

        //Create an object for scanner class

        Scanner sc = new Scanner(System.in);

        //Declare a variable

        String word = " ";

        //Do till the user enters "done"

        while(!word.equals("done"))

        {

            //Get a word from the user

            System.out.print("\nEnter a word: ");

            word = sc.next();

            //Check if word is equal to "done"

            if(word.equals("done"))

                //Break the loop

                break;

     //Check if 1st and last characters are equal

     if(word.charAt(0) == word.charAt(word.length()-1))

                //Print the message

        System.out.println("The first and last character matches in '"+ word + "'");

            //Else

            else

                //Print the message

        System.out.println("The first and last character does not matches in '"+ word + "'");

        }

    }

}

Explanation:

  • The statements under the condition “while(!word.equals("done"))”  gets executed till the user enters “done”.
    • The program then gets a word from the user and then it checks whether the 1st and last characters are match or not.
    • The program gets halted if the user enters “done”.
Sample Output

Output:

Enter a word: abi

The first and last character does not matches in 'abi'

Enter a word: aruna

The first and last character matches in 'aruna'

Enter a word: done

Expert Solution
Check Mark

Explanation of Solution

b.

Using “do-while” statement:

The code fragment to read words until the user enters “done”. For every word, the program should check whether the first and last character are match or not. The program using “do-while” condition is as follows. Code fragment is highlighted.

//Import required package

import java.util.Scanner;

//Define the Main class

public class Main

{

    //Define main method

    public static void main(String[] args)

    {

        //Create an object for scanner class

        Scanner sc = new Scanner(System.in);

        //Declare a variable

        String word = " ";

        //Do till the user enters "done"

        do

        {

            //Get a word from the user

            System.out.print("\nEnter a word: ");

            word = sc.next();

            //Check if word is equal to "done"

            if(word.equals("done"))

                //Break the loop

                break;

     //Check if 1st and last characters are equal

     if(word.charAt(0) == word.charAt(word.length()-1))

                //Print the message

        System.out.println("The first and last character matches in '"+ word + "'");

            //Else

            else

                //Print the message

        System.out.println("The first and last character does not matches in '"+ word + "'");

        } while(!word.equals("done"));

    }

}

Explanation:

  • The statements inside “do-while” condition gets executed till the user enters “done”.
    • The program then gets a word from the user and then it checks whether the 1st and last characters are match or not.
    • The program gets halted if the user enters “done”.
Sample Output

Output:

Enter a word: charles

The first and last character does not matches in 'charles'

Enter a word: david

The first and last character matches in 'david'

Enter a word: done

Want to see more full solutions like this?

Subscribe now to access step-by-step solutions to millions of textbook problems written by subject matter experts!
Students have asked these similar questions
Write a for loop that prints out the integers 7 through 40, each on a new line using range (). Use while loops to print the multiplication tables of 2,3 and 4. The table must include the first 10 multiplications. (must be separate loops for each one)
Change the program below by using do…while loop
please answer all questions about for loop below: for (i = 1; i <= 5; i++)  {   cout << i << “,  ”<<endl;  }   Questions: What is the name of loop variable, or loop control variable? How many iterations does the loop do? What is initial value of count? What is value of i when the loop exits (fails) What is the output of the code? What is increment in i between each iteration Observe comma in cout When is the variable incremented: Beginning or the end Loop variable must be defined before entering loop This is pretest loop The integer counter, i, controls the number of loop iterations

Chapter 4 Solutions

Java: An Introduction To Problem Solving And Programming Plus Mylab Programming With Pearson Etext -- Access Card Package (8th Edition)

Ch. 4.1 - Prob. 11STQCh. 4.1 - Write a for statement that displays the even...Ch. 4.1 - Prob. 13STQCh. 4.2 - Write a Java loop that will display the phrase One...Ch. 4.2 - Write a Java loop that will set the variable...Ch. 4.2 - Write a Java loop that will read a list of numbers...Ch. 4.2 - What output is produced by the following code? for...Ch. 4.2 - What output is produced by the following code? for...Ch. 4.2 - What output is produced by the following code? for...Ch. 4.2 - Revise the loop shown in Listing 4.6 to use a...Ch. 4.2 - What is the bug in the code in the section Tracing...Ch. 4.2 - Add some suitable output statements to the...Ch. 4.2 - What is the bug in the code in the previous...Ch. 4.2 - Prob. 24STQCh. 4.2 - Suppose that you did not have assertion checking...Ch. 4.3 - Prob. 26STQCh. 4 - Write a fragment of code that will read words from...Ch. 4 - Develop an algorithm for computing the...Ch. 4 - Develop an algorithm for a simple game of guessing...Ch. 4 - Write a fragment of code that will compute the sum...Ch. 4 - Convert the following code so that it uses nested...Ch. 4 - Write a for statement to compute the sum 1 + 22 +...Ch. 4 - (Optional) Repeat the previous question, but use...Ch. 4 - Write a loop that will count the number of blank...Ch. 4 - Write a loop that will create a new string that is...Ch. 4 - Write a program that will compute statistics for...Ch. 4 - Suppose we attend a party. To be sociable, we will...Ch. 4 - Define an enumeration for each of the months in...Ch. 4 - Write a fragment of code that computes the final...Ch. 4 - Suppose that you work for a beverage company. The...Ch. 4 - Suppose that we want to compute the geometric mean...Ch. 4 - Prob. 16ECh. 4 - Create an applet that draws a pattern of circles...Ch. 4 - Prob. 18ECh. 4 - What does the following fragment of code display?...Ch. 4 - Repeat Practice Program 4 of Chapter 3, but use a...Ch. 4 - Write a program that implements your algorithm...Ch. 4 - Repeat Practice Program 5 of Chapter 3, but use a...Ch. 4 - Write a program to read a list of nonnegative...Ch. 4 - Write a program to read a list of exam scores...Ch. 4 - Combine the programs from Programming Projects 5...Ch. 4 - Write a program that simulates the Magic 8 Ball...Ch. 4 - Whats for dinner? Let the computer decide. Write a...Ch. 4 - Write a program that implements your algorithm...Ch. 4 - Prob. 2PPCh. 4 - Write a program that reads a bank account balance...Ch. 4 - Modify Programming Project 5 from Chapter 2 to...Ch. 4 - Write a program that asks the user to enter the...Ch. 4 - Write a program that simulates a bouncing ball by...Ch. 4 - You have three identical prizes to give away and a...Ch. 4 - Prob. 9PPCh. 4 - Holy digits Batman! The Riddler is planning his...Ch. 4 - Your country is at war and your enemies are using...Ch. 4 - Prob. 12PPCh. 4 - Prob. 13PPCh. 4 - Prob. 14PPCh. 4 - (Challenge) Repeat the previous project, but...Ch. 4 - Write a JavaFx application that displays a series...

Additional Engineering Textbook Solutions

Find more solutions based on key concepts
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,
Text book image
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage
Text book image
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
Control Structures - while loop - do-while loop - for loop - Goto - break - continue statements; Author: EzEd Channel;https://www.youtube.com/watch?v=21l11_9Osd0;License: Standard YouTube License, CC-BY