This week's book giveaway is in the Programmer Certification forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 11 Programmer I Study Guide: Exam 1Z0-815 and have Jeanne Boyarsky & Scott Selikoff on-line!
See this thread for details.
Win a copy of OCP Oracle Certified Professional Java SE 11 Programmer I Study Guide: Exam 1Z0-815 this week in the Programmer Certification 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
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

Deck Shuffle JUnit Help

 
Marshal
Posts: 14373
239
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is another argument for the practice of Test-Driven development, or at least for the practice of always making sure your test fails before you make changes to your production code that makes it pass. If you do that, you can be fairly sure you have a valid test. If not, as in this case, your test could be invalid and you just happily accept its failure to blow up as a sign that all is well. This is kind of like thinking the baby is securely buckled in its car seat just because you don't hear him or her crying.
 
Junilu Lacar
Marshal
Posts: 14373
239
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Junilu Lacar wrote:. . . half of the cards are not in the original order.

As I said yesterday that is a weak test which is almost certain to pass.

Rather, your test requires that NONE of the cards are in their original order.

That is a very strong test, probably far too strong; it is likely that shuffling will replace one card in its original location.


Both are valid points. Which goes to show how unimaginative at best and behind the times at worst the instruction on testing practice OP is getting. I don't mind saying that even though it may seem harsh. Instructors need to be called out on this, in my opinion. If the instructor later on shows a better way to test logic tha involves random numbers, I'll gladly take the comment back.
 
Junilu Lacar
Marshal
Posts: 14373
239
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@OP: once you've seen your test fail to fail appropriately, can you find the bug in your test code?
 
Marshal
Posts: 7271
492
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jon Fil wrote:


I didn't test this code on my IDE, but please clarify what you are testing at line 8; 9?

String unshuffled = deck.get(i); gives back a card name - a String technically speaking.
deck1.equals(unshuffled); while this one checks whether Deck is equal to card (String).

That's wrong obviously.
 
Marshal
Posts: 66236
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:. . . Instructors need to be called out on this, in my opinion. . . .

Most lecturers aren't up to speed with modern testing, never having used it. What worries me about the instructions is that they seem over‑specified to me. I would prefer to see candidates design their own class, rather than being told which methods to use and whether those methods should have a return value or void.
 
Bartender
Posts: 3611
151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To me, this exercise seems very reasonable for a course that is two months underway. That a student has problems with it is no surprise. No doubt there are students who do not have much problems, but these do not write here.

And about the 50% test: it is a weak one, but it is apparently not that easy to implement. A better test would have been: the shuffle method is sufficient if in at least 99% of many shuffles it is the case that at least 90% of the cards have moved. But ehh..

P.S: the chance that no card is in its original place after shuffling is a well-known one, with a limiting chance of 1/e when the size of the population goes to infinity.
 
Campbell Ritchie
Marshal
Posts: 66236
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:. . . in at least 99% of many shuffles it is the case that at least 90% of the cards have moved. . . .

But you would have to run the test hundreds of times to verify it is satisfied in 99% of cases.

limiting chance of 1/e when the size of the population goes to infinity.

Is e Euler's number? That would mean there is an (e − 1) ÷ e chance of at least one card being returned to its original location. That's over 63%.
 
Piet Souris
Bartender
Posts: 3611
151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Piet Souris wrote:. . . in at least 99% of many shuffles it is the case that at least 90% of the cards have moved. . . .

But you would have to run the test hundreds of times to verify it is satisfied in 99% of cases.


Indeed, I would expect many more students writing here...    

Campbell wrote:Is e Euler's number? That would mean there is an (e − 1) ÷ e chance of at least one card being returned to its original location. That's over 63%.


Yes, if you calculate the chances of no unmoved cards for sizes 1, 2, 3, 4, ... you get an alternating sequence, with limit 1/e. Just watch: if your family draws lots at Christmas, notice how many times the procedure must be repeated before no one has drawn his/her own name.
 
Liutauras Vilda
Marshal
Posts: 7271
492
Mac OS X VI Editor BSD Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


@OP

This first test to me it seems ok and along the lines of instructions. So if it passes, move on. Well, that doesn't mean there isn't anything add, but don't want to confuse you. Well, I'll add anyway trying not to confuse you. (leave code as you have)

Again, test method name "testLocation()" - it is just semi-clear what it is going to test. Let's assume this method name is correct, in which case, I'd expect such implementation:

Do you see the difference? Very subtle actually. A small detail. But rather important. Very famous European basketball ex-player now basketball coach Sarunas Jasikevicius once during press conference said, rephrasing: "Basketball is a game where big guys play with small details. Those who understand their importance - win".

So in the former case, method name says testLocation(), but actually testing if two cards are equal. While in the latter, we test what we say we are going to test.

So how you could fix that? Simply by renaming the test method. One better name I could think of could be:

Think if you could come up with more general name to test all 4 cards at once.
 
Junilu Lacar
Marshal
Posts: 14373
239
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:To me, this exercise seems very reasonable for a course that is two months underway. That a student has problems with it is no surprise. No doubt there are students who do not have much problems, but these do not write here.


Valid points, for sure. I should at least be happy that this particular instructor is introducing testing with JUnit at all. That's probably better than most.
 
Junilu Lacar
Marshal
Posts: 14373
239
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:One better name I could think of could be:


Or

BTW, this won't compile:

You probably meant to declare that variable as an int.
 
Junilu Lacar
Marshal
Posts: 14373
239
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aside from the poor semantics of the testLocation() method, I don't think it's going pass. When initially populated, your Deck doesn't start with 2s and ends with Aces, it starts with Aces and ends with Kings.
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:Aside from the poor semantics of the testLocation() method, I don't think it's going pass. When initially populated, your Deck doesn't start with 2s and ends with Aces, it starts with Aces and ends with Kings.



Yup. I noticed that and spent hours trying to figure out why when I print the deck in Deck.java, the deck starts with the four Ace's.
And when I print the deck in JUnit test, the deck starts with Two's and ends with the four Ace's.

What's causing the deck, in JUnit when I do deck.print(), to have the four Ace's at the end?
 
Liutauras Vilda
Marshal
Posts: 7271
492
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jon Fil wrote:I noticed that and spent hours trying to figure out why when I print the deck in Deck.java, the deck starts with the four Ace's.


So and how you are/were trying to figure out why it prints Aces at first?

Isn't logical to check how your arrays are initialized? If you'd compare those arrays initializations from your very first post with more recent code posting you'd see it.

Jon Fil wrote:And when I print the deck in JUnit test, the deck starts with Two's and ends with the four Ace's.


Sorry but it doesn't make sense to print anythin in JUnit, at least not in this situation.

The question you need to ask yourself, does it make sense that in Deck class's main method when you printing cards they are in the order they are now? Maybe it does make sense, maybe it doesn't - for you to decide based on what you are programming.

Jon Fil wrote:What's causing the deck, in JUnit when I do deck.print(), to have the four Ace's at the end?


Don't know. And I don't think it is important to know.


From your initial post:


From your more recent post:


 
Liutauras Vilda
Marshal
Posts: 7271
492
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, based on your latest code, by executing Deck's main method, I get printed (showing only very first 8 prints):

Makes sense, right, because you have:

Now, in unit tests, you have:

So again, makes sense that unit test fail, because by printing (deck from main method) order is seen as that Aces go first, and in tests you are claiming that should be 2s as first cards in a deck.
 
Jon Fil
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:So, based on your latest code, by executing Deck's main method, I get printed (showing only very first 8 prints):

Makes sense, right, because you have:

Now, in unit tests, you have:

So again, makes sense that unit test fail, because by printing (deck from main method) order is seen as that Aces go first, and in tests you are claiming that should be 2s as first cards in a deck.




Ugh... I'm so silly... Somehow there were TWO Deck.java copies in my eclipse workspace but in two different folders.
My JUnit test was referring to my earlier version of Deck instead of my newly edited one!
 
Jon Fil
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just got this project back and graded.
Surprisingly, I received a grade of 7.25 / 8 .

For the actual Deck class, I got a -0.25 for my shuffle method.
Please read the comments.




For my Junit Test, I got a -0.5 .
Surprisingly(no, not really), this is the only block where he took points off.


No errors/comments in this section:

 
Master Rancher
Posts: 3399
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jon Fil wrote:


Probably "no comments" - you didn't include any documentation on the method or explanation of what you were doing.
 
Jon Fil
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mike Simmons wrote:

Jon Fil wrote:


Probably "no comments" - you didn't include any documentation on the method or explanation of what you were doing.



I didn't need to include comments to explain.  I didn't include any comments to the rest of the codes and my instructor didn't take points off.
 
Sheriff
Posts: 6374
172
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why don't you ask your instructor?
 
Jon Fil
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Knute Snortum wrote:Why don't you ask your instructor?




I have emailed him plus it's the weekend.  He'll most likely not check his email til Monday.  Sorry for asking
 
Uh oh, we're definitely being carded. Here, show him this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!