Question

Assignment goals • Build experience solving problems by implementing branch and loop algorithms. • Become able...

Assignment goals • Build experience solving problems by implementing branch and loop algorithms. • Become able to program using control structures. • Understand and implement string comparison in Java. • Develop skills required to write and debug Java programs. Description You program must start and keep dialog with the user. Please create the first prompt for the dialog. After that your program must accept the user’s response and echo it (output to screen) in upper case and adding the question mark at the end. The program must run endlessly until the user say “stop” in any combination of cases. Then you program must say “GOODBYE!” and quit. Example: HELLO, I AM THE PROGRAM Hi, I am Michael HI, I AM MICHAEL? Are you kidding? ARE YOU KIDDING?? I prefer to speak to somebody smarter I PREFER TO SPEAK TO SOMEBODY SMARTER? Stupid STUPID? You YOU? sToP! STOP!? stop GOODBYE! Recommendations It is a good practice again to start with clear understanding what your input and output are. Create several examples and write them down on the paper. Later you will use them to test your program when it is ready. Before you start programming use the lecture slides and the textbook to refresh the following concepts: • Constants and literals (how to inline the stop phrase in the program), • Decision structures or branching (what possible scenarios are), • Strings and string comparing (how to evaluate the input), • Flag variable in programming, • Loop structures (how to quit your program). You have three attempts to turn your work in. Use them the following way: 1. Start with pseudocode, then write and debug your program. Once you have working program that passes your test and is well documented – turn it in. Back up your submission. 2. Take your time to think about your program and what can be done better: user interface, error checking, additional features, etc. Improve and polish the program and its documenting. If you can come to the working version which is better than the first submission – turn the second version in. 3. Use the third submission only if something went wrong, otherwise keep it unused.

Homework Answers

Answer #1

The Java program to take a user input as String and to convert it to uppercase and add "?" at the end and to print it is:


//Importing Scanner class from java.util package to create a Scanner object
//Scanner object is used to take input from user
import java.util.Scanner;
public class StringBot {
        
        public static void main(String[] args) {
                //Creating a string variable to store user input and to manipulate it.
                String user_input;
                //Creating a integer variable flag to control do-while loop
                int flag=0;
                //Creating a Scanner object scnr
                Scanner scnr = new Scanner(System.in);
                
                System.out.printf("HI, I AM THE PROGRAM\n");
                //Using do-while to repeat the process till user enter "stop"
                do{
                        //Taking String input from user, using scnr.nextLine()
                        //nextLine() takes a satement in a line as input
                        user_input=scnr.nextLine();
                        
                        //Using toUpperCase() function, converting user entered string to uppercase
                    user_input = user_input.toUpperCase();
                    
                    //Checking if user entered "stop", if yes we make flag=-1 to end do-while loop
                    if (user_input.equalsIgnoreCase("STOP")) {
                        System.out.printf("GOODBYE!");
                            flag=-1;
                    }
                        
                    else
                            System.out.printf("%s",user_input+"?");
                
                    System.out.printf("\n");
                    }while(flag!=-1);
                
                //After use, Scanner object has to be closed using close() function
                scnr.close();
        }
}

This program repeats itself till user enter "stop" . I have used a do-while loop to repeat the same process again and again till a condition is triggered by user. And that condition is user entering "stop".

I have used simple functions to complete this code like nextLine() to take String as a input, toUpperCase() to convert the string to uppercase. I have provided comments in the program explaining the lines of code.

I have tested the code and it is working good. I am sharing a output screenshot for your reference.

Hope this code helps you.

Thank you :)

Know the answer?
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
Similar Questions