JAVA JAVA JAVA PLEASE INCLUDE COMMENTS. IVE ADDED THE CODE IVE WRITTEN SO FAR AF
ID: 3667312 • Letter: J
Question
JAVA JAVA JAVA
PLEASE INCLUDE COMMENTS.
IVE ADDED THE CODE IVE WRITTEN SO FAR AFTER THE QUESTION.... HELP ME OUT PLS
Computer Friend
Objective:
Write a program ChatBot.java, where the user inputs a one-line sentence and the computer responds accordingly.
Specification:
The computer should continuously ask and respond to questions according to the following rules:
If the sentence ends with a question mark (?) and has an even number of spaces in the sentence, then respond with "yes" (note: 0 is an even number)
If the sentence ends with a question mark (?) and has an odd number of spaces in the sentence, then respond with "no".
If the sentence ends with an exclamation mark, then respond with "Wow!"
If the user enters "quit", then the program exits
In any other case, respond with:
You always say "<user input>"
Make sure the print out includes the quotation marks
Example Dialog:
package chatBot;
import java.util.Scanner;
public class ChatBot {
public String odd = "no";
public String even = "yes";
public static void main(String[] args) {
Scanner keyboard=new Scanner(System.in);
System.out.println("Welcome to Computer Friend! ");
System.out.println(" ");
System.out.println("Hello! Say something to me!");
String userInput= keyboard.next();
int index=userInput.length();
Character end = new Character('?');
if(index >= 0){
for(int i=0; i<index; i++)
{
if(charAt(keyboard.length).equals==" "+'?')
{
System.out.println("yes");
}
}
else
{
System.out.println("no");
}
else if()//TODO ! return
{
System.out.println("wow!");
}
else if()//TODO return user input
{
System.out.println();
}
}
}
}
Explanation / Answer
package chegg;
import java.util.Scanner;
public class ChatBot {
public String odd = "no";
public String even = "yes";
public static void main(String[] args) {
Scanner keyboard=new Scanner(System.in);
System.out.println("Welcome to Computer Friend! ");
System.out.println("Hello! Say something to me!");
String userInput= keyboard.nextLine();// read string that contains spaces also
int index=userInput.length()-1;
while(! "quit".equals(userInput)){
if(userInput.charAt(index) == '?'){
//counting number of spaces
int count = 0;
for(int i=0; i<=index; i++){
if(userInput.charAt(i)==' ')
count++;
}
if(count%2 == 0) // even number of spaces
System.out.println(""yes"");
else
System.out.println(""no"");
}else if(userInput.charAt(index) == '!'){
System.out.println(""Wow!"");
}else{
System.out.println("""+userInput+""");
}
System.out.println("Hello! Say something to me!");
userInput= keyboard.nextLine();// read string that contains spaces also
index=userInput.length()-1;
}
}
}
/*
Output:
Welcome to Computer Friend!
Hello! Say something to me!
Hello! Say something to me!
"Wow!"
Hello! Say something to me!
Hello! Say something to me?
"no"
Hello! Say something to me!
Hello! Say something t?
"yes"
Hello! Say something to me!
quit
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.