Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Mooc.fi Part 6 Exercise 9 SimpleDictionary

 
Greenhorn
Posts: 12
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone, first time posting here. I've been learning Java using mooc.fi and have so far been able to figure out on my own why my code doesn't pass the test in TMCBeans. This one has me stumped though. My code seems to work as intended and I get the desired output but TMC says it's wrong. The error I am getting is, "Make sure the command 'search' returns the correct string."  This method, when search is entered by the user as the command, is supposed to output the translation of the first word added by the user with the add command. It does this but mooc.fi is saying the output is incorrect. Is this a bug with mooc.fi or am I missing something? Thanks for anyone that looks at this for me.

Here is a link to the web page with some more information for anyone trying to solve this.

https://java-programming.mooc.fi/part-6/2-separating-user-interface-from-program-logic




Here is the expected output according to mooc.fi

Command: something
Unknown command
Command: add
Word: pike
Translation: hauki
Command: change
Unknown command
Command: search
To be translated: pike
Translation: hauki
Command: search
To be translated: carrot
Word carrot was not found
Command: end
Bye bye!
 
Bartender
Posts: 4067
156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kelly,

welcome to the Ranch and enjoy the stay!

At first glance: when the command is 'search' and the search word is present, do you print this search word (you call that word: translate)?
 
Kel Taylor
Greenhorn
Posts: 12
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So this is what i think the code does, and the output seems to suggest that I'm correct: when the user inputs the command 'add', it asks for two words and stores them in a list and also it creates a SimpleDictionary(first word, second word). When the command is 'search' it asks for a word. if word the user inputs is the first word in the SimpleDictionary, it prints the second word in the SimpleDictionary. This is the desired output, but the TMCBeans is saying it's not working and i do not get credit on mooc.fi for that part of the exercise.
 
Kel Taylor
Greenhorn
Posts: 12
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the code of the testmethod that i am failing for some reason. To me it looks like as long as the string i output contains the second word of the SimpleDictionary object it should pass.
 
Rancher
Posts: 179
7
Mac OS X IntelliJ IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We cannot see the exercise without creating an account, could you post it here so we can see?
 
Kel Taylor
Greenhorn
Posts: 12
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, sorry about that, here is the exercise.
part1.PNG
[Thumbnail for part1.PNG]
part2.PNG
[Thumbnail for part2.PNG]
part3.PNG
[Thumbnail for part3.PNG]
 
Kel Taylor
Greenhorn
Posts: 12
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rest of the exercise.
part4.PNG
[Thumbnail for part4.PNG]
part5.PNG
[Thumbnail for part5.PNG]
 
Kel Taylor
Greenhorn
Posts: 12
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The part that is failing the test in TMCBeans is Part 3: Translating a word
 
Kel Taylor
Greenhorn
Posts: 12
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here are the other classes for the exercise.


And

 
Piet Souris
Bartender
Posts: 4067
156
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmm... just ran your program, it works indeed like it should.

The only thing that is different in your output is that the translated word is displayed at the same line as "Translation", while all the other combinations have their answer put on a new line. For instance I get this:

Command:
add
Word:
carrot
Translation:
peen
Command:
search
To be translated:
carrot
Translation: peen
Command:
end
Bye bye!
 
Kel Taylor
Greenhorn
Posts: 12
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Piet, I have tried every combination of print and println nothing seems to make this thing pass the test. I'm very confused at this point. If I am reading the test method correctly, it should pass the test if the output contains the second argument of the SimpleDictionary object anywhere in the output.
 
Piet Souris
Bartender
Posts: 4067
156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, beats me too. Sure you haven't missed one line/newline combination?        
 
Kel Taylor
Greenhorn
Posts: 12
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Haha well it is possible. Thanks for looking!
 
Marshal
Posts: 15884
265
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is your use of the added ArrayList in your start() method. Your code makes the assumption that the dictionary entries are always going be added to the dictionary through lines 10-19. It's not. If you examine the test code carefully, it has these lines:

Line 2 determines the input that your code will get from the test, namely, "search", "carrot", and "end". You'll notice that the "add" command will never be sent to your program. Line 3 appears to create a dictionary that already has the entry for "carrot" -> "porkkana". Since this code is independent of your code that adds an entry to the dictionary, the entry added to the dictionary is never added to the added ArrayList in your code. The added list isn't actually necessary but because you have it, you created a loophole in your program, hence the bug. That test should fail.

To fix the bug, you need to eliminate the duplication you created with the added ArrayList. This is actually a great example of a subtle and difficult to discern violation of the DRY Principle (Don't Repeat Yourself). The DRY principle dictates that there should be one and only one representation of any bit of knowledge in your code. The added ArrayList represents duplicated knowledge - it "knows" what's been added to the dictionary, which is knowledge that the dictionary itself knows. The bug exists because the knowledge in the added ArrayList is out of synch with the knowledge that's in the dictionary because adding entries to the dictionary does not happen exclusive through the code in the start() method.
 
Junilu Lacar
Marshal
Posts: 15884
265
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm just adding this comment while I have it in my head. It's pretty interesting how wanting to eliminate an unnecessary temporary variable like added can lead to fixing a bug. I was stumped by this bug as well but my gut reaction in reading the code was "Why do you need that added array list?" At first I thought I wouldn't comment on it as it didn't seem relevant to the bug and I didn't want to seem too picky. As it turned out, after carefully considering what the test code was doing, I realized that the added variable that I had such a negative reaction to was in fact the root of the problem. I guess a couple of lessons learned here:

1. Listen to that little voice in your head that tells you to refactor something.

2. Look for subtle violations of the DRY principle, like this one.
 
Kel Taylor
Greenhorn
Posts: 12
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow, thank you very much! This was driving me crazy! I've learned a lot from your reply, thank you.
 
Junilu Lacar
Marshal
Posts: 15884
265
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now that we've address the problem with the failing test, I'd like to get picky.

The structure of your code could use some improvement. A method that long becomes very unwieldy to read and maintain. Having so many different things going in one place also makes it more difficult to test and debug. To make this better, you can extract blocks of code that do one thing into their own separate method. Then you can eliminate all the continue statements and use a switch statement or a series of if-else-if statements. Either way will be better than what you have now.

We had a guest author here recently who advocated for limiting methods to no more than 5 lines of code. It will be tough in this case but you can certainly whittle it down quite a bit from 40+ lines of code.


The above code examples bear out what C.A.R. Hoare wrote: "There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult."
 
Kel Taylor
Greenhorn
Posts: 12
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you again. You solved my problem and taught me how to avoid it in the future. I couldn't be more grateful. I'm glad this forum was mentioned in the Head First Java book!
 
Marshal
Posts: 25818
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:The above code examples bear out what C.A.R. Hoare wrote: "There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult."



He said that in 1980, according to my quick web research, and after 40 years it's still true. Probably more true, in fact, since designs are much larger in scope than they were back then.
 
Junilu Lacar
Marshal
Posts: 15884
265
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:He said that in 1980... and after 40 years it's still true. Probably more true, in fact, since designs are much larger in scope than they were back then.


Yes, it was in his lecture in accepting the 1980 Turing Award. There are quite a few nuggets of wisdom in that lecture, actually. Another passage in there that I really like that remains true today:

C.A.R. Hoare wrote:There is nothing a mere scientist can say that will stand against the flood of a hundred million dollars. But there is one quality that cannot be purchased in this way—and that is reliability. The price of reliability is the pursuit of the utmost simplicity. It is a price which the very rich find most hard to pay.

 
Piet Souris
Bartender
Posts: 4067
156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done, Junilu!

Where are these lines coming from?
 
Junilu Lacar
Marshal
Posts: 15884
265
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:Where are these lines coming from?


From the test code OP posted here: https://coderanch.com/t/733961/java/Mooc-fi-Part-Exercise-SimpleDictionary#3413754

Line 2 that instantiates Scanner with a String was what helped me see what the problem was.
 
Piet Souris
Bartender
Posts: 4067
156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks! Completely missed that post    
 
Junilu Lacar
Marshal
Posts: 15884
265
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The bug had me stumped, too. I looked at the main code and didn't see any glaring logic error; the only thing I saw that was off was that added list but it didn't look like it would have caused a bug. I looked at the test code and concurred with OP on her analysis that as long as the word was in the output, print vs println shouldn't matter. That turned out to be a red herring though. Only when I came back a couple of hours later and talked through (yes, I talked my dummy Star Wars programming partners through) the test code did I realize what the bug was.
 
Piet Souris
Bartender
Posts: 4067
156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Enjoy a cow!
 
Piet Souris
Bartender
Posts: 4067
156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And a cow for Kelly, for the efforts and the misery of a hard to spot bug!
 
Kel Taylor
Greenhorn
Posts: 12
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I know the answer to this but I'll ask just in case. The best place for all the new methods that you suggested would be in the same class as the start() method right? And since they're in the same class, that's why you're able to just make add(); it's own statement without any dots?
 
Junilu Lacar
Marshal
Posts: 15884
265
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. And they would be private to that class as well, so as not to pollute the public interface.

An invocation of an unqualified method has an implicit this. in front of it:
 
Sheriff
Posts: 7708
534
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cowgratulations, your post has been published in CodeRanch's August 2020 journal.
Staff note (Liutauras Vilda):

@OP

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I like to take these exercises and try them myself. Once done, to what I think is correct, I then follow the discussion. I ended up with the end product except for the add() and search() methods. The sample output shows:

Command: something
command unknown
add
word: pike
translation: hauki
.

.

My output shows:

Command:
something
command unknown
add
word:
pike
translation:
hauki
.
.
.

I cannot get ie "Command: something" on the same line.  I've tried:


This does not print "Command:" and hangs on input.

What am I missing?

Second question. How is the test:



implemented?
 
Junilu Lacar
Marshal
Posts: 15884
265
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We don't know how the test is implemented. I'd guess that the io object captures the standard output, System.out, and the getSystemOut() method returns whatever output has been captured so far as a String.

We can't say why your program hangs on input until you post your code. Without the code, we'd only be guessing.
 
Did Steve tell you that? Fuh - Steve. Just look at this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic