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 plus MyProgrammingLab with Pearson eText -- Access Card Package (7th 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)
- B A E H Figure 1 K Questions 1. List the shortest paths between all node pairs. Indicate the number of shortest paths that pass through each edge. Explain how this information helps determine edge betweenness. 2. Compute the edge betweenness for each configuration of DFS. 3. Remove the edge(s) with the highest betweenness and redraw the graph. Recompute the edge betweenness centrality for the new graph. Explain how the network structure changes after removing the edge. 4. Iteratively remove edges until at least two communities form. Provide step-by-step calculations for each removal. Explain how edge betweenness changes dynamically during the process. 5. How many communities do you detect in the final step? Compare the detected communities with the original graph structure. Discuss whether the Girvan- Newman algorithm successfully captures meaningful subgroups. 6. If you were to use degree centrality instead of edge betweenness for community detection, how would the results change?arrow_forwardUnit 1 Assignment 1 – Loops and Methods (25 points) Task: You are working for Kean University and given the task of building an Email Registration System. Your objective is to generate a Kean email ID and temporary password for every new user. The system will prompt for user information and generate corresponding credentials. You will develop a complete Java program that consists of the following modules: Instructions: 1. Main Method: ○ The main method should include a loop (of your choice) that asks for input from five users. For each user, you will prompt for their first name and last name and generate the email and password by calling two separate methods. Example о Enter your first name: Joe Enter your last name: Rowling 2.generateEmail() Method: This method will take the user's first and last name as parameters and return the corresponding Kean University email address. The format of the email is: • First letter of the first name (lowercase) + Full last name (lowercase) +…arrow_forwardI have attached my code, under I want you to show me how to enhance it and make it more cooler and better in graphics with following the instructions.arrow_forward
- After our initial deployment for our ML home based security system, the first steps we took to contribute further to the project, we conducted load testing, tested and optimize for low latency, and automated user onboarding. What should be next?arrow_forwardWhy investing in skills and technology is a critical factor in the financial management aspect of system projects.arrow_forwardwhy investing in skills and technology is a critical factor in the financial management aspect of systems projects.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