Page 2 of 2 FirstFirst 12
Results 21 to 33 of 33
  1. #21
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    167
    Rep Power
    4

    Default

    Quote Originally Posted by milovan View Post
    Yes it is Array.
    You can use it like:
    Java Code:
    int [] newArray = new Combination(/* ints for constructor*/).getCombo();
    int number = newArray[0]; // will return first value in the array, in your case, value of [I]count[/I] variable...
    Thanks. I didn't know you could return arrays for instance. I thought that only simple variable types could be returned...from another class, if that makes any sense....

    Quote Originally Posted by pbrockway2 View Post
    The total in the Roller class represents the total that the roller will "select" for. Ie only dice combos with this total will be included in the combos that the roller generates.

    There is an instance of Roller, but it doesn't appear until the main() method - and it could in practice be in some other class altogether. That instance is generating the combos with a total of 11.




    It's not necessary to split the problem into two classes. But, it seemed to me, you had two different sorts of things: combinations, and combination generating processes. My thinking - such as it was - was a kneejerk reaction along the lines of "2 types of thing: two classes".




    Simple is good! A combination is a simple sort of thing. The behaviour is limited to returning the information that is needed to decide on inclusion in a bunch of combos (which can be counted etc), and some String information that can be used to textually describe the combination.

    (My motivation here was your question where you seemed to be doing those two things: creating a bunch of combos, and wanting to print stuff about them.)



    As I said above your original question referred to the need to print the combinations at some point. This meant I was going to have the DiceCombo class have a method returning a String to be printed. toString() just seemed like a good idea, but there is nothing special about that. The important point is that I converted your demand for a method that would (in part) print a whole lot of combos into a method that would just return a String.

    String.format() is just a personal preference.

    ------------------------

    By the way, I haven't tried to figure out what your code is doing or you describe it. So there is something a bit general about my points. I just see a method doing multiple things (calculating, printing, returning) and think there should be multiple methods. Likewise I see combinations and the generation of possible combinations subject to a constraint, and I think there should be multiple classes (combinations and generators).
    Thank you Pbrockway2. Everything you said was very clear and made sense and fit precisely what I am trying to do. I am mostly trying to understand the specific syntax. For instance, at times you used <CLASSNAME> or even LIST and those are all new to me, so I was trying to figure it out, but otherwise everything you wrote was very useful and clear conceptually.

    The code is supposed to do exactly what you said for now, which is to simply return all of the combinations and print them to the screen. Pretty simple. Thank you!

  2. #22
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Regarding "List<DiceCombo> ret = new ArrayList<DiceCombo>();"

    A List is like an array, but it has useful methods. I only used the add() method which does the obvious thing. The list instance which is created (actually an ArrayList instance which is a sort of List) plays the role of the "bunch" which is created and returned.

    Unlike an array a list grows as required which is a nice property.

    Collections as a whole are dealt with in Oracle's Tutorial. But conceptually a list is so like an array that you can dive in and use it guided by the API docs linked to above.

    The angle brackets are commonly used with collections like list. They can be read "of" so the line in question can be read as "Let ret be a List of DiceCombo and assign it a new ArrayList of DiceCombo".

  3. #23
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    167
    Rep Power
    4

    Default

    Quote Originally Posted by pbrockway2 View Post
    Regarding "List<DiceCombo> ret = new ArrayList<DiceCombo>();"

    A List is like an array, but it has useful methods. I only used the add() method which does the obvious thing. The list instance which is created (actually an ArrayList instance which is a sort of List) plays the role of the "bunch" which is created and returned.

    Unlike an array a list grows as required which is a nice property.

    Collections as a whole are dealt with in Oracle's Tutorial. But conceptually a list is so like an array that you can dive in and use it guided by the API docs linked to above.

    The angle brackets are commonly used with collections like list. They can be read "of" so the line in question can be read as "Let ret be a List of DiceCombo and assign it a new ArrayList of DiceCombo".

    So I am looking at the API and I have another question. Does the <E> mean that you write the name of the list inside of the angle brackets or is it the name of the class? In other words, did you call it <DiceCombo> because that's the name of the DiceCombo class or is it just coincidental?

  4. #24
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    No it not coincidental. What goes in the anglke brackets is what the list is a list of. In this case the list is declared to be a list of combinations.

  5. #25
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    167
    Rep Power
    4

    Default

    Okay, so then why not just do?

    ArrayList Name = new ArrayList();

    I don't understand how and why List is used with ArrayList. Also, I still don't think I get the brackets. So do you always need an object to use List?

  6. #26
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    You can write "ArrayList name = new ArrayList()" but generally, nowadays, people would use the generic form and say what the list is a list of. The situation is somewhat like having the option of declaring everything as Object. (As an experiment try writing a program where everything is declared as Object - some languages allow this sort of approach but it is painful in a strongly typed language like Java)

    In the code I posted the only thing I did with the things from the list was print them. And println() will accept any Object argument. Other times we may want to do things that are specific to a particular type (like call their methods): in those cases, unless we have said what the list is a list of, the compiler will not know that such method calls are legal.

    Again name can be declared as specifically an ArrayList, or more generally as an instance of List. I chose the more general approach since the only thing I was going to do with the list was to add things to it. Of more importance here is that getCombos() is declared to return a List and not an ArrayList. This gives me the freedom to change the type of ret to other types that implement List (like LinkedList) without having to change any other class that uses this method. Other classes can continue to ask for and get a List without knowing or caring what sort of list it is that they are receiving.
    Last edited by pbrockway2; 03-20-2011 at 11:38 PM.

  7. #27
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    167
    Rep Power
    4

    Default

    Oh okay. Thank you pbrockway2. This was very useful! I appreciate you taking the time to answer my questions.

  8. #28
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    167
    Rep Power
    4

    Default

    So how do I change the status of this thread to solved?
    Last edited by bigsonny; 03-20-2011 at 11:42 PM.

  9. #29
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    You're welcome.

    I'm not sure how you change the thread: check under "thread tools" at the top.

  10. #30
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    167
    Rep Power
    4

    Default

    Got it. Thanks!

    btw, how long have you been programming in Java? Is this your main language, or do you use multiple languages?

  11. #31
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    I've been using Java since 1.0.2b (which is quite a while ago!). I'm just a hobbyist and currently I'm working with JavaScript/Python (a google apps web site).

  12. #32
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    167
    Rep Power
    4

    Default

    Thanks. I hope to be as fluent as you are with Java one day. So While I am familiar with Javascript (in principle), what is the advantage of using Python? Why not use PHP or Perl? What advantages do you finding using this specific programming language?

  13. #33
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    There was no reason at for for Python - except that Google apps supports it. What I am writing is pretty modest, so PHP would have done (but then I would have had to pay to host it...). I'm enjoying Python but - and equally so with JavaScript - I'm rather out of my element. It's a case of googling about every time I hit a snag. The result so far isn't very pretty, but it does seem to work!

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Problem with my return method
    By braddy in forum New To Java
    Replies: 4
    Last Post: 10-19-2010, 07:34 AM
  2. Not able to return the method value
    By dmakshay2002 in forum Advanced Java
    Replies: 11
    Last Post: 05-28-2010, 03:07 PM
  3. Method won't return value
    By footyvino in forum New To Java
    Replies: 2
    Last Post: 03-26-2010, 11:49 AM
  4. Method return type problem
    By McChill in forum New To Java
    Replies: 7
    Last Post: 05-05-2009, 10:21 PM
  5. Return value of method
    By cachi in forum New To Java
    Replies: 1
    Last Post: 08-01-2007, 09:23 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •