|
Ah, ok, I did use ArrayLists for my Blackjack game: each hand (player and dealer) is an ArrayList of Cards, so you can just add one on each time someone hits. I'll read up on that part of the trail. Thank you!
|
# ? Oct 21, 2016 19:53 |
|
|
# ? Jun 11, 2024 01:21 |
|
Have you worked with generics before? Because it'll come up in collections. Its perfectly okay for now to use a List myList = new ArrayList(); and just put whatever Objects you want in there, since everything in Java is an Object. But the collections classes are 'generic' and so you can create flavors of them using templates that are specifically typed for certian objects, which helps re-inforce type safety and good clean code. So instead of List myList = new ArrayList(); //Takes any Object myList.add(Object); // legal myList.add(Die); // legal myList.add("Hello"); // legal you can do List<Die> myList = new ArrayList<Die>(); //Only accepts Dice, if you try to .add(Object) it'll complain. myList.add(Object); // compile error myList.add(Die); // legal myList.add("Hello"); // compile error The <?> is a wildcard which allows you to supply a particular Class, and then the 'generic' object, such as a List, can modify all its methods so that instead of accepting Object they accept the wildcard. So List has a method public void add(T newObject) where the class type of the parameter isn't actually defined. Instead its a variable. When you say new ArrayList<Die>(); you're essentially plugging in Die.class for T, and suddenly the parameter becomes public void add(Die newObject) So now you have a custom die continer without having to write any code at all! fancy stuff. Of course since Die is an Object, the standard Object container works too; but like I said its good practice to be specific. Keeps you from doing something you didn't mean to do. Zaphod42 fucked around with this message at 20:10 on Oct 21, 2016 |
# ? Oct 21, 2016 20:06 |
|
I'm running a report and filling a JTable with data--is there a way to redraw a selection of any cell as a selection of the entire row? As in if you have six columns if you click A3 it will highlight A1-6? I've searched around and the only thing I've seen is setRowSelectionAllowed(true) which doesn't appear to do much for me.
|
# ? Oct 21, 2016 20:37 |
|
Zaphod42 posted:
How about PDiceHand Extends DiceCup? Or is it better for PDiceHand to just have a DiceCup as a field?
|
# ? Oct 21, 2016 20:38 |
|
I have a job interview monday, and I'll be finishing my CS bachelor in June(We only learn Java at uni). What should I practice and know beforehand?
|
# ? Oct 21, 2016 20:41 |
|
Squashy Nipples posted:How about PDiceHand Extends DiceCup? Definitely the latter. A hand of dice doesn't seem interchangeable to a cup of dice.
|
# ? Oct 21, 2016 20:41 |
|
Squashy Nipples posted:How about PDiceHand Extends DiceCup? Yeah I would either put the code IN DiceUp or if you want a seperate class, you want something that has a DiceCup field reference. PDiceHand extends DiceCup would mean that PDiceHand is a special kind of DiceCup, a sub-type... but that's not really the case.
|
# ? Oct 21, 2016 20:44 |
|
meatbag posted:I have a job interview monday, and I'll be finishing my CS bachelor in June(We only learn Java at uni). What should I practice and know beforehand? Get this if you can: https://www.amazon.com/Cracking-Coding-Interview-Programming-Questions/dp/0984782850/ref=dp_ob_title_bk Its very thorough and kinda overkill but it'll really make you feel prepared instead of going in nervous. Go over all the java keywords and make sure you're comfortable explaining what exactly they mean. Public vs private, static vs dynamic, interfaces, abstract, final, etc. Practice sorting algorithms and Big-O performance of basic operations on common collections. Do't go overkill on sorting, but you should probably be able to write QuickSort on a whiteboard if you have to. Beyond that, just have a lot of things you've worked on that you feel comfortable talking about. Maybe refresh yourself with any libraries you've used in the past so you can speak about experience with them. Probalby a good idea to sit down and talk to yourself (silly as that is) or talk to a friend and go through some projects you've worked on, things you've learned, challenges you've faced, etc. If you suddenly have to start remembering some project you worked on 2 years ago in the interview, you'll do a lot of "uhhhh.... lemme think... errr................" but if you just spend a few minutes going over things like that before hand, every minute of prep saves you several minutes of time during the interview and makes you look way more professional and knowledgeable. I think there's a thread specifically for job interview questions, too. Zaphod42 fucked around with this message at 20:51 on Oct 21, 2016 |
# ? Oct 21, 2016 20:47 |
|
If I'm reading this right, a DiceCup is basically just a collection of dice with certain values. Basically just an object that roll()s its Die objects and tells you what you got So to actually play Poker Dice (I'm too lazy to look up if it's what I'm guessing it is!) you take your DiceCup, but then you need to apply some logic to the result it produces. It's sort of like a rule set, where you work out what a certain collection of dice means, what they represent, how they score or beat another set of dice... That's basically what a Hand is. You take five dice, use them to create a PokerDiceHand (which might be a better name if you're reusing these dice classes for other games), and then you have that higher-level representation of a game state, where the actual dice are sort of abstracted away That's sort of where the object-oriented stuff comes in, you're creating this other representation, with a bunch of data (e.g. ideas about Pairs and Ranks) and also the ability to compare itself to other Hands. From the outside you don't need to know about this - you can just take your dice, automatically turn it into a game Hand, and then compare your Hand to other players' directly, and find out which is better. All of the logic about what a Hand is and which one wins (and how it represents itself as a string etc) is all encapsulated in the Hand class. From the outside you just use simple methods and constructors to play a game, and standard functionality like Comparable lets you do things like throw them all in Collection and call max() to get the winner, or sort() to rank all the players That's hard to do with a bunch of arrays that you always need to look at the right way to decode what they represent!
|
# ? Oct 21, 2016 20:48 |
|
(I swear, last one for today)Zaphod42 posted:PDiceHand extends DiceCup would mean that PDiceHand is a special kind of DiceCup, a sub-type... but that's not really the case. But... isn't it? In the trail they use the example of: class MountainBike extends Bicycle { Because MountainBike includes everything in Bicycle, plus a few custom items (Adjustable Seat Height, more gears) And that sounds exactly the case with DiceCup and PokerDiceHand? It's just a DiceCup with 5 dice, with a few extra fields added in relating to playing PokerDice. (note that I'm not arguing, I'm trying to understand. I get that adding an internal DiceCup field to PokerDiceHand is easy, and a valid way to do it.)
|
# ? Oct 21, 2016 22:07 |
|
PierreTheMime posted:I'm running a report and filling a JTable with data--is there a way to redraw a selection of any cell as a selection of the entire row? As in if you have six columns if you click A3 it will highlight A1-6? I've searched around and the only thing I've seen is setRowSelectionAllowed(true) which doesn't appear to do much for me. You can do that by implementing a custom renderer for your table. A good stackoverflow response can be found here: http://stackoverflow.com/questions/5503171/how-can-i-focus-on-a-whole-row-of-a-jtable In the update to the question the person actually implements what looks to be a sensible renderer that does what's needed. The accepted answer even points to the tutorial where they have examples of such a thing: http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#renderer
|
# ? Oct 22, 2016 00:50 |
|
Squashy Nipples posted:(I swear, last one for today) Hmmmm, I suppose that makes sense too. Its all just different ways of organizing it. The question there is are you ever gonna have a situation where you need to handle both PokerDiceHand DiceCups as well as other DiceCups, but it doesn't really matter. That works too. (Feel free and argue, as long as you're not a dick about it I like argument. That's how you learn )
|
# ? Oct 22, 2016 01:06 |
|
I think this is getting into the contentious area of whether inheritance is good or the worst! The short answer is probably that either way is fine, and for your program it might be completely sensible to treat a Hand that way. As you start implementing it, you might start to run into issues with inheriting from a single class, or creating an instance of the PokerDiceHand instead of its parent class, or just generally coupling two distinct components too closely. The best way to learn is to get into it I reckon - if it starts giving you headaches, look at how you can refine it
|
# ? Oct 22, 2016 01:23 |
|
meatbag posted:I have a job interview monday, and I'll be finishing my CS bachelor in June(We only learn Java at uni). What should I practice and know beforehand? Whiteboard coding. Do it with a friend, and do it a lot this weekend. A. Lot.
|
# ? Oct 22, 2016 14:42 |
I'm trying to copy a previous Netbeans J2EE Glassfish project and rename it, so that I can make a new version of the code. However, after renaming the application, the context path no longer works. The initial directory opens properly ie localhost/ContextPath/index.html but after clicking any links, they just go to localhost/link.html rather than localhost/ContextPath/link.html. Any suggestions? It seems like it's just a bug but I've tried everything including restarting server, redeploying, changing context path to something different in properties/run. Also tried creating glassfish-web.xml and specifying context path there and it hasn't fixed the issue. e: Very bizarre thing. On my laptop, this issue happens if I create new projects but not with my old, working project. On my desktop computer, the old project doesn't work with context path but new projects do. denzelcurrypower fucked around with this message at 23:34 on Oct 23, 2016 |
|
# ? Oct 23, 2016 22:55 |
|
I'm trying to deepen my Java knowledge, so I'm reading through some of the more esoteric (to me) sections of the Java Trails. I've come across lambdas, which are throwing me a bit. Specifically, why is it that in Approach 6, a lambda can be used instead of a Predicate<T> interface? I thought an interface (sort of like a pure abstract class in C++) and a lambda (a function without a name) were two very different things.
|
# ? Oct 24, 2016 02:56 |
|
hooah posted:I'm trying to deepen my Java knowledge, so I'm reading through some of the more esoteric (to me) sections of the Java Trails. I've come across lambdas, which are throwing me a bit. Specifically, why is it that in Approach 6, a lambda can be used instead of a Predicate<T> interface? I thought an interface (sort of like a pure abstract class in C++) and a lambda (a function without a name) were two very different things. I actually just read this last night. I hope I'm wording this correctly, but lambdas / functional programing are inferring the type of object for you without the defining them ala generics. Since Predicates return a Boolean, the interpreter translates what you're doing as that. quote:Type inference Predicate < Integer > greaterThan5 = x > x > 5 This book overall is pretty good read so far. We just started using Spark, so between this and learning Scala, lambdas and functional programming are the new norm for us.
|
# ? Oct 24, 2016 03:08 |
|
Yeah, the end of the page kind of explained it with target values, but it still seems kind of , like all of functional programming so far (I tried working through Elegant JavaScript this summer and that was a trip!).
|
# ? Oct 24, 2016 03:12 |
|
hooah posted:I'm trying to deepen my Java knowledge, so I'm reading through some of the more esoteric (to me) sections of the Java Trails. I've come across lambdas, which are throwing me a bit. Specifically, why is it that in Approach 6, a lambda can be used instead of a Predicate<T> interface? I thought an interface (sort of like a pure abstract class in C++) and a lambda (a function without a name) were two very different things. A lambda can be used to implement an interface. For example, look at replaceAll on List. It's used to replace each element in the list with a transformed version of that item. For example, you could replace each String in a list with a lowercase, trimmed version of that String. One way would be to define a new Class that implements UnaryOperator: Java code:
Java code:
Java code:
* - One non-default method. Default methods are also new to Java 8, and kind of muddle the difference between an interface and an abstract class, and don't come up very often. Gravity Pike fucked around with this message at 06:04 on Oct 24, 2016 |
# ? Oct 24, 2016 05:54 |
|
Yeah it only works for certain cases of simple interfaces, but its basically just super fancy syntactic sugar. Its like an in-line method literal except a lambda.
|
# ? Oct 24, 2016 06:50 |
|
They do clean up the code a bit when you can use them. I'm still hoping we move to Java 8 so I can rewrite my SWT listeners as lambdas.
|
# ? Oct 24, 2016 16:13 |
|
lambdas were one of the features I really liked about C# and so I'm glad to see them make their way into Java too. There's a few cases where its just way simpler to write as p -> p.method() == thing. Not crazy about functional programming languages like Haskell, but allowing a tiny bit of that functionality into declarative langauges is pretty cool.
|
# ? Oct 24, 2016 16:30 |
|
Ok, this is a more nuts-and-bolts question. I'm trying to work through this exercise, but IDEA complains that it can't resolve "Function" in java.util.Function. What gives? In Eclipse, I at least have an idea how I would go about fixing this problem (search for the class, add it to the classpath), but I haven't worked with IDEA enough to go about doing that.
|
# ? Oct 25, 2016 03:31 |
|
hooah posted:Ok, this is a more nuts-and-bolts question. I'm trying to work through this exercise, but IDEA complains that it can't resolve "Function" in java.util.Function. What gives? In Eclipse, I at least have an idea how I would go about fixing this problem (search for the class, add it to the classpath), but I haven't worked with IDEA enough to go about doing that. java.util.Function is a Java 8 thing. You need to either point to a newer JDK or configure the Java language level. They're both under the menu File -> Project Structure.
|
# ? Oct 25, 2016 03:37 |
|
Sedro posted:java.util.Function is a Java 8 thing. You need to either point to a newer JDK or configure the Java language level. They're both under the menu File -> Project Structure. 1.8.0_112 is the only JDK I have installed on this machine.
|
# ? Oct 25, 2016 03:38 |
|
Sedro posted:java.util.Function is a Java 8 thing. You need to either point to a newer JDK or configure the Java language level. They're both under the menu File -> Project Structure. Edit: wait, is it java.util.function.Function ? Other alternative to open project settings is CMD + ; on Mac Not sure on Windows. Pretty sure it's CTRL + ; geeves fucked around with this message at 03:48 on Oct 25, 2016 |
# ? Oct 25, 2016 03:43 |
|
hooah posted:1.8.0_112 is the only JDK I have installed on this machine. There's still a setting for the Java level, it could be enforcing 1.7 or below even if 1.8 is the only thing you have installed.
|
# ? Oct 25, 2016 03:53 |
|
geeves posted:Edit: wait, is it java.util.function.Function ? Awkwardly, yes import java.util.function.Function;
|
# ? Oct 25, 2016 03:57 |
|
geeves posted:Edit: wait, is it java.util.function.Function ? Naturally. function.Function function = new function.Function
|
# ? Oct 25, 2016 03:59 |
|
Oh, so I can't trust Oracle. Surprise, surprise.
|
# ? Oct 25, 2016 04:01 |
|
Sedro posted:Naturally. function.Function function = new function.Function proof that Java is the most functional language
|
# ? Oct 25, 2016 08:08 |
|
Sedro posted:Naturally. function.Function function = new function.Function function.Function<function.Function, function.Function> function = new function.Function<function.Function, function.Function>
|
# ? Oct 25, 2016 15:55 |
|
I have another question. On this page about overriding and hiding methods, near the bottom there's an example with the class FlyingCar. In the startEngine method, they do FlyCar.super.startEngine(key). Why is this not just FlyCar.startEngine(key)? It seems that FlyCar.super would refer to whatever FlyCar's super-interface is (is there even a default super-interface?).
|
# ? Oct 30, 2016 02:39 |
|
Yeah, that's not a great use of the super keyword. Probably it's because normally a call to FlyCar.startEngine(int) would look like calling a static method on a class... but that's a question for the JSR implementers, I suppose. If it helps, you're not the only one a little puzzled over the syntax: http://zeroturnaround.com/rebellabs/java-8-explained-default-methods/ quote:But what if we would like to call the default implementation of method foo() from interface A instead of implementing our own. It is possible to refer to refer to A#foo() as follows: carry on then fucked around with this message at 03:19 on Oct 30, 2016 |
# ? Oct 30, 2016 03:16 |
|
Yeah that's an odd situation. I can see the desire to differentiate it, but at that point I'd prefer it look like super(A).foo(); rather than A.super.foo(); which does seem to imply A's superclass, not A. But hey, Oracle's in charge of that now.
|
# ? Oct 30, 2016 05:43 |
|
The syntax is the same when you want to qualify 'this' (as in ClassName.this). It's been part of Java since ancient times.
|
# ? Oct 30, 2016 08:36 |
|
So I'm back with another question. So I have an Array of Student Objects I need to sort. Each object has an Int UID and a String Name. I need to create a method to sort them by UID. So what I thought what I'd do was a bubble sort. So I Initially did this. Which seemed to have replaced most of the Array with one entry except the biggest entry.code:
code:
|
# ? Nov 3, 2016 16:00 |
|
They're almost identical except you aren't doing the swap right in the first case. There's no need to create an array just to use the 0 index, but you should use a student object reference, not just the int and string values. int and string are plain data types so in java are pass by value. Everything else is pass by reference. When you set: temp = array[j-1].getid(); temp2 = array[j-1].getName(); array[j-1] = array[j]; array[j].setid(temp); array[j].setName(temp2); You haven't actually changed what array[j] points to. So array[j] and array[j-1] are now pointing to the same object. Then when you call array[j].setid(temp); that means you're assigning the setid of both array[j] and array[j-1], which is NOT what you want. So instead the best solution would be code:
|
# ? Nov 3, 2016 16:17 |
|
In the second one you're swapping the position of the two objects. In the first one you're copying the same object into another slot in the array, then setting the name and ID on the object in the other slot, which is the same object. So both slots point to the same object with the properties you just set on it Basically you're working with references that point to an object, so when you copy a reference, it's just another arrow pointing to the same object, any changes you make will be seen by anyone looking at that object. You generally have to go out of your way to specifically make a completely separate copy of an object in Java, calling some special method to do it e- beaten again!
|
# ? Nov 3, 2016 16:28 |
|
|
# ? Jun 11, 2024 01:21 |
|
Unless implementing bubble sort was part of the assigment, you should just use Arrays.sort quote:https://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#sort(T[],%20java.util.Comparator)
|
# ? Nov 4, 2016 14:21 |