• Post Reply Bookmark Topic Watch Topic
  • New Topic

converting arrayList to array  RSS feed

 
nick Mercado
Greenhorn
Posts: 12
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,
This is my first programming class and having some hard time understanding writing code for classes. For my school project we are suppose to write a program for poker and blackjack.
Bottom quote is from my project description. 1st code is what I wrote in my project java file and in 2nd code, I wanted to test it if I did it right so I made new java class file and tested it to see if I did it right by printing size and elements of Array and ArrayList.
I believe when the parameter is 4 I must copy first 4 cards from cards ArrayList and added into newly created Card[] dealArray variable. But when I submit to my school server I get error which you can see at the bottom.
Any feedback and help is appreciated. I would rather get help by pointing me to a right direction than doing it for me because I really want to learn Java.
Thanks!

public Card[] deal(int numCards)
This method will remove the specified number of cards from the top of the deck and return them as an array. For example, if the parameter is 4, then the first four cards in the deck will be returned as an array of size 4. This is the one place in this class where an array is used. Important: The cards will be removed from the front of the "cards" ArrayList, not the back. As with the cut method, drawing some pictures and thinking about the add/remove options the ArrayList object provides will help make this a smoother section of code to design and implement. For this project you can assume only legal requests will be made.





Result
1 2 3 4 5 6

6

4

4
[1, 2, 3, 4]


java.lang.AssertionError: expected:<4> but was:<45>

at org.junit.Assert.fail(Assert.java:88)

at org.junit.Assert.failNotEquals(Assert.java:834)

at org.junit.Assert.assertEquals(Assert.java:645)

at org.junit.Assert.assertEquals(Assert.java:631)

at p6Testing.PublicTests.testDeckDealCards(PublicTests.java:43)
 
Vahida Havaldar
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why are you using same name for method and object that is deal?
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch both of you

Have you done what the instructions tell you? Not writing code, but drawing pictures. What do your diagrams look like? That is very good advice; you cannot write code until you have a concept of what you are going to do.
When you are getting [1,2,3,4] printed, is there anything wrong with that? Doesn't that mean you have got your technique working?
I presume you are only using a big main method to test out a little bit of code to see whether it works. That sort of divide‑and‑conquer development is a good idea
Now you need a way to shuffle your List, so you can deal randomised cards.
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nick Mercado wrote:. . .. . .
You appear to know a lot more than you think if you know about the toArray method, but let's have a look at the documentation. This will tell you about the toArray method. It lets you calculate how large the array will be. Why are you using a loop rather than a method of the same List? Why are you using add(i, card) rather than simply add(card)? The add(int, T) method is intended for adding at the beginning of the List or in its middle; you simply want to add things at the end.

I still can't see where you are getting that assertion error. I can envisage all sorts of improvements to that deal method, but I can't see it returning the wrong size array. Add a line 10 to it:-
System.out.printf("In deal() method: %s%n", Arrays.toString(dealArray));
I think you will find the problem is elsewhere.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nick Mercado wrote:Any feedback and help is appreciated. I would rather get help by pointing me to a right direction than doing it for me because I really want to learn Java.

Good for you. Have a cow. It also happens to be our philosophy on this site.

You seem to have made a good start, and Campbell has already said much the same as I would; but I have a few additional tips for you:

1. You're usually better off using and passing around interfaces rather than concrete classes - especially with Java collections - so line 2 of your main() method is better written as:
  List<Integer> cards = new ArrayList<Integer>();
Notice that it doesn't affect the rest of your code one bit, but it makes your program much more generic; because if, for some reason, you discover later on that ArrayList isn't the best type of List for your task, you only have to change that one line. viz:
  List<Integer> cards = new LinkedList<Integer>();

2. List.toArray(array) is an odd beast, because it doesn't require that the array you pass be the correct size.
Specifically, from the docs: "If the list fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this list". Therefore:
  someList.toArray(new someType[0])
will always return an array of the correct size. And you don't even need to supply a new array every time. For example, plugging it into your code, you could do:

3. Lists have tons of useful features (which is why we generally recommend using them over arrays), just one of which is that they can be subdivided.
For example:
  cards.subList(0, 4)
returns the first 4 elements in 'cards' as a List. Do you see how that might be useful to you?

Don't worry too much if you don't right now, but just remember for the future that you can return any subsection of a List that you like, and that subsection is ALSO a List.

HIH

Winston
 
nick Mercado
Greenhorn
Posts: 12
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Now you need a way to shuffle your List, so you can deal randomised cards.


Hello Campbell Ritchie, Winston Gutkowski & Vahida Havaldar !

Thank you all for the comments. First off, I will not use variable name as same as Class name .
For my school project we can not use new Java 8 features (I do not know what is new features tho )and I don't think we can use subList and List<T> yet.
I usually try to draw out first like Campbell said and write a method in main to printout if my logic is correct and then write on my project java file with different variable names thats more suitable for my project.
This project was challenging because my professor added one more special suit (total of 5 instead of clubs, spades, diamonds and hearts).
I am pretty sure I did something wrong in my deck class which is to make a deck constructor and copy constructor, deal, cut and shuffle cards methods.
Anyway I was trying to figure out what I did wrong even though this project is past due date because its killing not to know what I did wrong.
Bottom code is shuffle method that I wrote for testing my logic.
Like always, any feedback and help is appreciated 




When Card is Odd
Expected when even # cards:
1 7 2 8 3 9 4 10 5 11 6 12


Expected when odd # cards:
1 7 2 8 3 9 4 10 5 11 6 12 13


Result: 1 7 2 8 3 9 4 10 5 11 6 12 13

Original Cards size: 13

Size of the card after top and bottom shuffle: 13

top deck size: 13
bottom deck size: 7


When Card is Odd
Expected when even # cards:
1 7 2 8 3 9 4 10 5 11 6 12


Expected when odd # cards:
1 7 2 8 3 9 4 10 5 11 6 12 13


Result: 1 7 2 8 3 9 4 10 5 11 6 12

Original Cards size: 12

Size of the card after top and bottom shuffle: 12

top deck size: 6
bottom deck size: 6
 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you tell us what you expect the output to be or what the error is?
 
nick Mercado
Greenhorn
Posts: 12
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:Can you tell us what you expect the output to be or what the error is?


The error message that I get from school server is

java.lang.AssertionError: expected:<4> but was:<45>

at org.junit.Assert.fail(Assert.java:88)

at org.junit.Assert.failNotEquals(Assert.java:834)

at org.junit.Assert.assertEquals(Assert.java:645)

at org.junit.Assert.assertEquals(Assert.java:631)

at p6Testing.PublicTests.testDeckDealCards(PublicTests.java:43)


I think of the project description as like this;

I have a 9 cards,
[ 1 2 3 4 5 6 7 8 9 ] <- deck
Number of cards that I need to deal is 4 (numCards)
[ 1 2 3 4] <- deal
[ 5 6 7 8 9 ] < deck
Then return that deal cards in an Array

I think that is what the project wants...
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't appear to have shown us the line with that assertion.
Please put some print statements into your code; you can then see what you are getting from the List. As somebody else already said, please look at the sublist method, which will probably prove useful to you.
 
nick Mercado
Greenhorn
Posts: 12
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,
I am not able to see assertion error line because I can not see JUnit testing files from the server. Our school submit project server just show us the error message. Sadly we did not learned what sublist method therefore I can not use it (and I do not know what sublist do as well, but will look for it). My semester is about to finish and I was not able to finish this project. However, I am interested in buying Cattle Drive from this forum and wondering how are the projects like? I learned some basic from school but I feel like I am just plug in some code to make my project compile. I did not understanding why this statement or syntex used in particular case. Will this Cattle drive help me understand the concept?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!