Help coding in java

Post » Sat Feb 19, 2011 8:24 am

I'm learning to program in Java using BlueJ and am making an exercise. However at the moment I'm completely stuck.

This is the class I'm working on with the relevant methods:





public String generateResponse(HashSet words)
{
Iterator it = words.iterator();
while(it.hasNext()) {
String word = it.next();

String response = responseMap.get(word);
if(response != null) {
return response;
}
}
return pickDefaultResponse();
}

private void fillResponseMap()
{
responseMap.put(crash,
"Well, it never crashes on our system. It must have something\n" +
"to do with your system. Tell me more about your configuration.");
responseMap.put("crashes",
"Well, it never crashes on our system. It must have something\n" +
"to do with your system. Tell me more about your configuration.");
responseMap.put("slow",
"I think this has to do with your hardware. Upgrading your processor\n" +
"should solve all performance problems. Have you got a problem with\n" +
"our software?");
responseMap.put("performance",
"Performance was quite adequate in all our tests. Are you running\n" +
"any other processes in the background?");
responseMap.put("bug",
"Well, you know, all software has some bugs. But our software engineers\n" +
"are working very hard to fix them. Can you describe the problem a bit\n" +
"further?");
responseMap.put("buggy",
"Well, you know, all software has some bugs. But our software engineers\n" +
"are working very hard to fix them. Can you describe the problem a bit\n" +
"further?");
responseMap.put("windows",
"This is a known bug to do with the Windows operating system. Please\n" +
"report it to Microsoft. There is nothing we can do about this.");
responseMap.put("macintosh",
"This is a known bug to do with the Mac operating system. Please\n" +
"report it to Apple. There is nothing we can do about this.");
responseMap.put("expensive",
"The cost of our product is quite competitive. Have you looked around\n" +
"and really compared our features?");
responseMap.put("installation",
"The installation is really quite straight forward. We have tons of\n" +
"wizards that do all the work for you. Have you read the installation\n" +
"instructions?");
responseMap.put("memory",
"If you read the system requirements carefully, you will see that the\n" +
"specified memory requirements are 1.5 giga byte. You really should\n" +
"upgrade your memory. Anything else you want to know?");
responseMap.put("linux",
"We take Linux support very seriously. But there are some problems.\n" +
"Most have to do with incompatible glibc versions. Can you be a bit\n" +
"more precise?");
responseMap.put("bluej",
"Ahhh, BlueJ, yes. We tried to buy out those guys long ago, but\n" +
"they simply won't sell... Stubborn people they are. Nothing we can\n" +
"do about it, I'm afraid.");
}

private void fillDefaultResponses()
{
defaultResponses.add("That sounds odd. Could you describe that problem in more detail?");
defaultResponses.add("No other customer has ever complained about this before. \n" +
"What is your system configuration?");
defaultResponses.add("That sounds interesting. Tell me more...");
defaultResponses.add("I need a bit more information on that.");
defaultResponses.add("Have you checked that you do not have a dll conflict?");
defaultResponses.add("That is explained in the manual. Have you read the manual?");
defaultResponses.add("Your description is a bit wishy-washy. Have you got an expert\n" +
"there with you who could describe this more precisely?");
defaultResponses.add("That's not a bug, it's a feature!");
defaultResponses.add("Could you elaborate on that?");
}

/**
* Randomly select and return one of the default responses.
* @return A random default response
*/
private String pickDefaultResponse()
{
// Pick a random number for the index in the default response list.
// The number will be between 0 (inclusive) and the size of the list (exclusive).
int index = randomGenerator.nextInt(defaultResponses.size());
return defaultResponses.get(index);
}
}


So basically, I type a sentence, it automatically controls if any of the words in the sentence correspond to one of the responseMaps and then it prints that one.
Now the exercise is that I have to make it so that it also detects synonyms or similar words without making extra response maps. Basically, I should be able to merge the Crash and Crashes response map. I have to do this by assigning multiple values to a String I think.
So I've made this:
String[] crash = { "crash", "crashes", "freeze" };
I don't know what to do with it, or even how to use it correctly. Does anybody have any suggestions?
User avatar
Auguste Bartholdi
 
Posts: 3521
Joined: Tue Jun 13, 2006 11:20 am

Post » Fri Feb 18, 2011 9:46 pm

Formatted and highlighted version of the code to help other people solve the issue:
public String generateResponse(HashSet words) {    Iterator it = words.iterator();    while (it.hasNext()) {        String word = it.next();        String response = responseMap.get(word);        if (response != null) {            return response;        }    }    return pickDefaultResponse();}private void fillResponseMap() {    responseMap.put(crash,            "Well, it never crashes on our system. It must have something\n"            + "to do with your system. Tell me more about your configuration.");    responseMap.put("crashes",            "Well, it never crashes on our system. It must have something\n"            + "to do with your system. Tell me more about your configuration.");    responseMap.put("slow",            "I think this has to do with your hardware. Upgrading your processor\n"            + "should solve all performance problems. Have you got a problem with\n"            + "our software?");    responseMap.put("performance",            "Performance was quite adequate in all our tests. Are you running\n"            + "any other processes in the background?");    responseMap.put("bug",            "Well, you know, all software has some bugs. But our software engineers\n"            + "are working very hard to fix them. Can you describe the problem a bit\n"            + "further?");    responseMap.put("buggy",            "Well, you know, all software has some bugs. But our software engineers\n"            + "are working very hard to fix them. Can you describe the problem a bit\n"            + "further?");    responseMap.put("windows",            "This is a known bug to do with the Windows operating system. Please\n"            + "report it to Microsoft. There is nothing we can do about this.");    responseMap.put("macintosh",            "This is a known bug to do with the Mac operating system. Please\n"            + "report it to Apple. There is nothing we can do about this.");    responseMap.put("expensive",            "The cost of our product is quite competitive. Have you looked around\n"            + "and really compared our features?");    responseMap.put("installation",            "The installation is really quite straight forward. We have tons of\n"            + "wizards that do all the work for you. Have you read the installation\n"            + "instructions?");    responseMap.put("memory",            "If you read the system requirements carefully, you will see that the\n"            + "specified memory requirements are 1.5 giga byte. You really should\n"            + "upgrade your memory. Anything else you want to know?");    responseMap.put("linux",            "We take Linux support very seriously. But there are some problems.\n"            + "Most have to do with incompatible glibc versions. Can you be a bit\n"            + "more precise?");    responseMap.put("bluej",            "Ahhh, BlueJ, yes. We tried to buy out those guys long ago, but\n"            + "they simply won't sell... Stubborn people they are. Nothing we can\n"            + "do about it, I'm afraid.");}private void fillDefaultResponses() {    defaultResponses.add("That sounds odd. Could you describe that problem in more detail?");    defaultResponses.add("No other customer has ever complained about this before. \n"            + "What is your system configuration?");    defaultResponses.add("That sounds interesting. Tell me more...");    defaultResponses.add("I need a bit more information on that.");    defaultResponses.add("Have you checked that you do not have a dll conflict?");    defaultResponses.add("That is explained in the manual. Have you read the manual?");    defaultResponses.add("Your description is a bit wishy-washy. Have you got an expert\n"            + "there with you who could describe this more precisely?");    defaultResponses.add("That's not a bug, it's a feature!");    defaultResponses.add("Could you elaborate on that?");}/** * Randomly select and return one of the default responses. * @return A random default response */private String pickDefaultResponse() {// Pick a random number for the index in the default response list.// The number will be between 0 (inclusive) and the size of the list (exclusive).    int index = randomGenerator.nextInt(defaultResponses.size());    return defaultResponses.get(index);}


Minor Irk:
You are using the wrong Collection type in generateResponse. You shouldn't ever been looking through a HashSet like that. The idea of a HashSet is you can find something in a single operation O(1) time. Change it to a Collection and everything is fine though. Plus, it is then possible to pass in things like ArrayLists


Now the exercise is that I have to make it so that it also detects synonyms or similar words without making extra response maps. Basically, I should be able to merge the Crash and Crashes response map. I have to do this by assigning multiple values to a String I think.

Given your restrictions, I would fill a map that maps a word from one form to the form stored in the responseMap.

So for example, you could do:

normalizer.put("crashy", "crash");normalizer.put("freeze", "crash");

That when, when you recive a word from your collection of words, you see if you can convert it into a standard from using the above map.

public String generateResponse(Collection words) {    for (String word : words ) {        String standardWord = normalizer.get(word);        if ( standardWord != null ){            world = standardWord;        }        String response = responseMap.get(word);        if (response != null) {            return response;        }    }    return pickDefaultResponse();}

User avatar
Ashley Hill
 
Posts: 3516
Joined: Tue Jul 04, 2006 5:27 am

Post » Sat Feb 19, 2011 5:04 am

Thanks! Will try it out when I get the chance.
User avatar
John N
 
Posts: 3458
Joined: Sun Aug 26, 2007 5:11 pm

Post » Sat Feb 19, 2011 8:58 am

Unfortunately, it doesn't seem to work. I post all the changes I've done to the original code in red and every code that isn't relevant isn't posted.
I've tried freeze and crashes but it just responds with a default response (not posted).
Edit: And before I forget, everything I posted is BlueJ's own code, but I'll keep in mind what you said about HashSet and colletions.


* Construct a Responder
*/
public Responder()
{
responseMap = new HashMap();
defaultResponses = new ArrayList();
fillResponseMap();
fillDefaultResponses();
randomGenerator = new Random();
Synonyms = new HashMap();
fillSynonyms();


}


public String generateResponse(HashSet words)
{
for (String word : words) {
String standardWord = Synonyms.get(word);
if( standardWord != null ){
word = standardWord;

}
String response = responseMap.get(word);
if(response != null) {
return response;
}
}
// If we get here, none of the words from the input line was recognized.
// In this case we pick one of our default responses (what we say when
// we cannot think of anything else to say...)
return pickDefaultResponse();
}


/**
* Enter all the known keywords and their associated responses
* into our response map.
*/
private void fillResponseMap()
{
responseMap.put("crash",
"Well, it never crashes on our system. It must have something\n" +
"to do with your system. Tell me more about your configuration.");
responseMap.put("slow",
"I think this has to do with your hardware. Upgrading your processor\n" +
"should solve all performance problems. Have you got a problem with\n" +
"our software?");
responseMap.put("performance",
"Performance was quite adequate in all our tests. Are you running\n" +
"any other processes in the background?");
responseMap.put("bug",
"Well, you know, all software has some bugs. But our software engineers\n" +
"are working very hard to fix them. Can you describe the problem a bit\n" +
"further?");
responseMap.put("buggy",
"Well, you know, all software has some bugs. But our software engineers\n" +
"are working very hard to fix them. Can you describe the problem a bit\n" +
"further?");
responseMap.put("windows",
"This is a known bug to do with the Windows operating system. Please\n" +
"report it to Microsoft. There is nothing we can do about this.");
responseMap.put("macintosh",
"This is a known bug to do with the Mac operating system. Please\n" +
"report it to Apple. There is nothing we can do about this.");
responseMap.put("expensive",
"The cost of our product is quite competitive. Have you looked around\n" +
"and really compared our features?");
responseMap.put("installation",
"The installation is really quite straight forward. We have tons of\n" +
"wizards that do all the work for you. Have you read the installation\n" +
"instructions?");
responseMap.put("memory",
"If you read the system requirements carefully, you will see that the\n" +
"specified memory requirements are 1.5 giga byte. You really should\n" +
"upgrade your memory. Anything else you want to know?");
responseMap.put("linux",
"We take Linux support very seriously. But there are some problems.\n" +
"Most have to do with incompatible glibc versions. Can you be a bit\n" +
"more precise?");
responseMap.put("bluej",
"Ahhh, BlueJ, yes. We tried to buy out those guys long ago, but\n" +
"they simply won't sell... Stubborn people they are. Nothing we can\n" +
"do about it, I'm afraid.");
}


private void fillSynonyms()
{
Synonyms.put("crashes", "crash");
Synonyms.put("freeze", "crash");
}

}
User avatar
Ella Loapaga
 
Posts: 3376
Joined: Fri Mar 09, 2007 2:45 pm

Post » Fri Feb 18, 2011 8:25 pm

Unfortunately, it doesn't seem to work. I post all the changes I've done to the original code in red and every code that isn't relevant isn't posted.
I've tried freeze and crashes but it just responds with a default response (not posted).

It seems to work fine though.

http://ideone.com/0ue8Y
User avatar
phillip crookes
 
Posts: 3420
Joined: Wed Jun 27, 2007 1:39 pm

Post » Fri Feb 18, 2011 8:04 pm

Sorry, the reason I posted it didn't work was because I had another synonym I tested first and that was a 2 word one( messes up, but I wrote it less polite). Stupid of me!

Thanks a lot for the help, this helps me along a lot.
User avatar
jessica sonny
 
Posts: 3531
Joined: Thu Nov 02, 2006 6:27 pm


Return to Othor Games