Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Exercises Alter the code: Have it respond Tell me more about your pets when the statement contains the word dog or cat. For example, a

Exercises Alter the code: Have it respond Tell me more about your pets when the statement contains the word dog or cat. For example, a possible statement and response would be: Statement: I like my cat Mittens. Response: Tell me more about your pets. There are 2 codes, how do you change the code to get the response?

public class Magpie2 { /** * Get a default greeting * @return a greeting */ public String getGreeting() { return "Hello, let's talk."; } /** * Gives a response to a user statement * * @param statement * the user statement * @return a response based on the rules given */ public String getResponse(String statement) { String response = ""; if (statement.indexOf("no") >= 0) { response = "Why so negative?"; } else if (statement.indexOf("mother") >= 0 || statement.indexOf("father") >= 0 || statement.indexOf("sister") >= 0 || statement.indexOf("brother") >= 0) { response = "Tell me more about your family."; } else { response = getRandomResponse(); } return response; } /** * Pick a default response to use if nothing else fits. * @return a non-committal string */ private String getRandomResponse() { final int NUMBER_OF_RESPONSES = 4; double r = Math.random(); int whichResponse = (int)(r * NUMBER_OF_RESPONSES); String response = ""; if (whichResponse == 0) { response = "Interesting, tell me more."; } else if (whichResponse == 1) { response = "Hmmm."; } else if (whichResponse == 2) { response = "Do you really think so?"; } else if (whichResponse == 3) { response = "You don't say."; } return response; } }
import java.util.Scanner; /** * A simple class to run the Magpie class. * @author Laurie White * @version April 2012 */ public class MagpieRunner2 { /** * Create a Magpie, give it user input, and print its replies. */ public static void main(String[] args) { Magpie2 maggie = new Magpie2(); System.out.println (maggie.getGreeting()); Scanner in = new Scanner (System.in); String statement = in.nextLine(); while (!statement.equals("Bye")) { System.out.println (maggie.getResponse(statement)); statement = in.nextLine(); } } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Lab Manual For Database Development

Authors: Rachelle Reese

1st Custom Edition

1256741736, 978-1256741732

More Books

Students also viewed these Databases questions

Question

What is meant by planning or define planning?

Answered: 1 week ago

Question

Define span of management or define span of control ?

Answered: 1 week ago

Question

What is meant by formal organisation ?

Answered: 1 week ago

Question

d. How will lack of trust be handled?

Answered: 1 week ago

Question

Are the rules readily available?

Answered: 1 week ago