Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I started this assignment but honestly haven't learned much in my class so I'm not sure the right way to go about this/ if what

image text in transcribed I started this assignment but honestly haven't learned much in my class so I'm not sure the right way to go about this/ if what I have is right.

Here is my code:

/**

* A program to carry on conversations with a human user.

* This is the initial version that:

*

  • * Uses indexOf to find strings

    *

  • * Handles responding to simple words and phrases

    *

* This version uses a nested if to handle default responses.

* @author Laurie White

* @version April 2012

*/

public class Magpie2

{

private String [] randomResponse = ("Interesting,tell me more", "Hmmm.", "Do you really think so?", "You don't say.");

/**

* 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 = randomResponse.length;

double r = Math.random();

int whichResponse = (int)(r * NUMBER_OF_RESPONSES);

return randomResponse[whichResponse];

}

}

Magpie Incorporating Arrays Assignment J. Add arrays to the Magpie class: Create a private instance variable array of type String and fill it with the current responses in getRandomResponse. Modify the getRandomResponse method to generate a random number to select an element from the array of responses and return it. Submit your Magpie2.java code into the Magpie Incorporating Arrays dropbox

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

Step: 3

blur-text-image

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

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2019 Wurzburg Germany September 16 20 2019 Proceedings Part 2 Lnai 11907

Authors: Ulf Brefeld ,Elisa Fromont ,Andreas Hotho ,Arno Knobbe ,Marloes Maathuis ,Celine Robardet

1st Edition

3030461467, 978-3030461461

More Books

Students also viewed these Databases questions

Question

What is a controlled foreign corporation? What is Subpart F income?

Answered: 1 week ago

Question

At what level(s) was this OD intervention scoped?

Answered: 1 week ago