
Concept explainers
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
- a. while statement
- b. do-while statement

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”.
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

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”.
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?
Chapter 4 Solutions
Java: An Introduction to Problem Solving and Programming (8th Edition)
Additional Engineering Textbook Solutions
Starting Out with Java: From Control Structures through Objects (7th Edition) (What's New in Computer Science)
Mechanics of Materials (10th Edition)
Starting Out With Visual Basic (8th Edition)
Modern Database Management
Degarmo's Materials And Processes In Manufacturing
Computer Science: An Overview (13th Edition) (What's New in Computer Science)
- Provide the full blue terminal commands & output and other things that are usefularrow_forwardHomework You have the row vector (A) that has values from (-100 to 100), write MATLAB codes to: (1) Interchange the first 10 elements with last ten elements. (2) Replace the elements at indices (93,9,17,50) of A with 99. (3) Return the second element and the element before the last of A as a column vector (C1). (4) Extract the first five elements and the last five elements of A and append them as a row vector (R). (5) Set the elements of A with odd indices to zeros(0).arrow_forwardif the error in a closed-loop Servo motor system is zero, why does the motor Contimac ranning instead of stoppingarrow_forward
- When the FCC added Color Television to the Industry Standards, they went with the system developed in the 1940s by Peter Goldman for CBS. Question 15 options: True False Part of the reason that many critics disliked 1950s gameshows was the fact that gameshows offered one of the few opportunities to see unscripted interactions with "real" (average/non-famous) people on television. Question 16 options: True False The Andy Griffith Show is an example of the "rural revival" shows that become enormously popular on 1960s American television. Question 19 options: True False During the Network Era, the hours before primetime each day were exclusively devoted to locally-produced programming, not programming dictated by an affiliate station's parent network. Question 20 options: True Falsearrow_forwardAlthough color television was not added to the industry standard until 1956, CBS had been broadcasting selected special events in color as early as 1950. Question 1 options: True False Two key factors in creating the Network Era of American television were the FCC licensing freeze and ______________. Question 4 options: The Quiz Show Scandals Habitual Viewing Operation Frontal Lobes Drop-In Viewing Least Objectionable Programming was designed to embrace the public service-oriented vision of using television to elevate mass culture and enrich viewers. Question 6 options: True False By the end of the 1950s, all three remaining networks (NBC, CBS, & ABC) were broadcasting their entire nightly programming schedule in full color. Question 9 options: True Falsearrow_forward7. See the code below and solve the following. public class Test { public static void main(String[] args) { int result = 0; } result = fn(2,3); System.out.println("The result is: + result); // fn(x, 1) = x // fn(x, y) = fn(x, y-1) + 2, when y>1 public static int fn(int x, int y) { if (x <= 1) return x; else return fn(x, y-1) + 2; } } 7-1. This program has a bug that leads to infinite recursion. Modify fn(int x, int y) method to fix the problem. (2 point) 7-2. Manually trace the recursive call, fn(2,3) and show the output (step by step). (2 point) 7-3. Can you identify the Base Case in recursive method fn(int x, int y)? (1 point)arrow_forward
- 6. See the code below and solve the following. import java.io.*; public class DataStream { } public static void main(String[] args) } DataOutputStream output = new DataOutputStream(new FileOutputStream("temp.dat")); output.writeUTF("Book1"); output.writeInt(85); output.writeUTF("Book2"); output.writeInt(125); output.writeUTF("Book3"); output.writeInt(70); output.close(); // ToDo: Read all data from temp.dat and print the data to the standard output (monitor) 6-1. This program has a compile error, and the message is “Unhandled exception type FileNotFoundException". How do you fix this error? (1 point) 6-2. Is FileNotFoundException a checked exception or an unchecked exception? (1 point) 6-3. What is the difference between checked exception and unchecked exception? (1 point) 6-4. Please complete the above program by reading all data from temp.dat and print the data to the standard output (monitor) by using System.out.print, System.out.println or System.out.printf method. (2 points)arrow_forwardWrite a program that reads a list of integers from input and determines if the list is a palindrome (values are identical from first to last and last to first). The input begins with an integer indicating the length of the list that follows. Assume the list will contain a maximum of 20 integers. Output "yes" if the list is a palindrome and "no" otherwise. The output ends with a newline. Hints: - use a for loop to populate the array based on the specified size (the first number entered) - use a for loop to check first value with last value, second value with second from end, etc. - if the values do not match, set a Boolean variable to flag which statement to output (yes or no) Ex: If the input is (remember to include spaces between the numbers): 6 1 5 9 9 5 1 the output is: yes Ex: If the input is: 5 1 2 3 4 5 the output is: C++ codingarrow_forwardDesign and draw a high-level "as-is" process diagram that illustrates a current process related to a product or service offered through the SSDCI.gov database.arrow_forward
- C++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology PtrC++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage LearningMicrosoft Visual C#Computer ScienceISBN:9781337102100Author:Joyce, Farrell.Publisher:Cengage Learning,
- Programming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:CengageEBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENTSystems ArchitectureComputer ScienceISBN:9781305080195Author:Stephen D. BurdPublisher:Cengage Learning




