|
I'm currently learning Java as part of a degree on the Open University in the UK. It's my first programming language (Sense, an OU variant of Scratch, doesn't count) and I'm coming late to it. While I'm enjoying the course, there's a load of course-specific terminology (messages instead of methods etc) and I'm concerned that I'm learning Java in a weird way. Can anyone recommend a definitive beginners guide to Java and OOP programming that reflects how the language is used in real applications?
|
# ¿ Jan 15, 2014 15:58 |
|
|
# ¿ Apr 27, 2024 07:15 |
|
Volmarias posted:Have you seen the Oracle Java tutorials yet? There's probably a way of turning them into PDFs or something, though.
|
# ¿ Jan 20, 2014 10:50 |
|
I'm really confused about how to show a sequence in a UML diagram where an object is returning results from one of its instance variables without using any methods to get those results. That probably isn't the best explanation, so here's a bit of code:code:
I'm still learning Java and UML and I'm finding it tough to represent processes in the latter. Here are three diagrams I've tried: ^Pretty sure this isn't right. It seemed like a good way to represent things until I actually drew it out. ^This is simplified, just showing the keys and values being accessed by the class. It still doesn't look right to me due to the lack of messages to those objects. ^This is the simplest and looks the most like other UML diagrams I've seen, but I also don't think it shows the process very well. Any ideas are appreciated. I got myself in a bit of tizzy trying to figure this out earlier and I'm probably overthinking it massively.
|
# ¿ Feb 25, 2014 00:11 |
|
Is Swing worth checking out or is there a better alternative for simple UI stuff? I've been making a text based game as a project and I'd like to move beyond the console so I can have buttons to click on etc. I'm not planning on adding actual graphics or animation at the moment. Alternatively, I'm also thinking of trying out Eclipse and the Android dev stuff, which I assume has its own UI bits that I could use. Chas McGill fucked around with this message at 10:21 on Apr 8, 2014 |
# ¿ Apr 8, 2014 10:10 |
|
supermikhail posted:https://netbeans.org/‎ though. (I mean, yes, I recommend Swing.) New project -> new swing form -> text area and button -> right click on button -> events -> action -> action performed -> textArea.setText("This is an interactive program where you can click a button.\nAnd you did.\nHappy now?"); rhag posted:They kinda let Swing rot. JavaFX is the "new" shiny UI library for java. Included in jdk 1.8. Cheers. I'll have a look at both and go with whatever seems easier. My needs are extremely basic.
|
# ¿ Apr 8, 2014 15:20 |
|
BirdOfPlay posted:What's a good resource on learning how exceptions are to be used and added? I'm going to read through Oracle's lessons about them but am thinking that something meatier would be helpful to.
|
# ¿ Apr 11, 2014 13:16 |
|
Gravity Pike posted:Exceptions are a "something wrong!" ...
|
# ¿ Apr 14, 2014 17:42 |
|
I've been banging my head against this all afternoon. code:
code:
Chas McGill fucked around with this message at 14:58 on Apr 21, 2014 |
# ¿ Apr 21, 2014 14:54 |
|
Volmarias posted:Edit: misread.
|
# ¿ Apr 21, 2014 15:22 |
|
Volmarias posted:Arguably, you could make the method throw a checked exception instead of returning null, but that's probably not appropriate for this case. Exceptions are meant for exceptional behavior, not flow control. This seems like bad programming though, so I've changed it to simply set 'again' to false, which breaks the loop and causes the NumberFormatException to display once. Not perfect, but at least the user can actually cancel out and they'll know why the other method hasn't continued. Chas McGill fucked around with this message at 13:21 on Apr 23, 2014 |
# ¿ Apr 23, 2014 13:14 |
|
Zaphod42 posted:That's why his version returns an Integer, not an int. Integer is a class which wraps int and can be null. Since they're so darn similar, java will even automatically convert between Integer and int for you, which is known as autoboxing. It just means that At the moment the loop causes the dialogue box display an alert and then reappear if the user enters a number outside the range. That's fine in the case of an incorrect input. What I was after is the 'cancel' behaviour you see in most applications - hit cancel and the box closes without anything else happening. I think your idea of handling an invalid return in the calling method is the best way to do it. The way I had things set up previously was that getNumber() would return 0 (an invalid number) to the calling method, which would then take that 0 and perform its action 0 times. That seems a bit superfluous, even if it's getting me the result I want. So I could instead do?: code:
Chas McGill fucked around with this message at 18:24 on Apr 23, 2014 |
# ¿ Apr 23, 2014 18:20 |
|
code:
I'm trying to create a map with a String as a key and and ArrayList of Book objects as a value. It is a complete ballache.
|
# ¿ Apr 25, 2014 16:08 |
|
Sedro posted:Variance is strange in Java. You have to explicitly say that the value in your map can be a List<Book> or any subtype of List<Book>. Ugh, right. I'll just go with ArrayList for everything in that case, but I thought it was good practice to use a more generic interface in case things change later on - ie: using Map and then HashMap. I think I've figured this out, although I'm concerned that my code is shite: code:
Output = code:
Zaphod42 posted:Is there a reason you can't do: Chas McGill fucked around with this message at 16:40 on Apr 25, 2014 |
# ¿ Apr 25, 2014 16:31 |
|
Zaphod42 posted:You're calling bibliography = new ArrayList<Book>(); twice, so get rid of one of them. I started out intending to make a Map<String, String> and ended up thinking "well, wouldn't it be more interesting to have a list of stuff for each key?" and I've regretted it. Thanks for your help.
|
# ¿ Apr 25, 2014 17:12 |
|
Kilson posted:You are correct, but it's only *important* to do that when you're returning a collection (list/map/etc.), so that the caller can just treat it as a generic list/map/whatever, without worrying about whether it's an ArrayList or a LinkedList or some other thing. Now I'm trying to figure out how to add an arraylist for a specific key rather than running through all of them in the map. I did say that Java isn't coming easily, but I do tend to remember concepts once I've actually created code using them. M31 posted:So, what you're wondering about is: Chas McGill fucked around with this message at 14:04 on Apr 26, 2014 |
# ¿ Apr 26, 2014 13:55 |
|
Can anyone recommend something similar to CodingBat for online, interactive Java problems? I like CodingBat a lot, but it doesn't cover a wide spread of the language. I have an exam next week and I find I tend to remember concepts more easily when I've actually coded something with them.
|
# ¿ May 27, 2014 10:40 |
|
Got a question about composition vs inheritance. Example: An outdoor centre has bikes and boats available for rent. Bike: make model idNumber ageGroup Boat: make model idNumber capacity Those are just examples of attributes the classes might have. My inclination would be to have an abstract Vehicle class to capture their commonalities, but I've recently been learning about composition and also see its benefits. However, inheritance seems to 'make more sense' to me here since there's such an obvious is-a relationship. A Boat is a vehicle. On the other hand, I feel like I'm probably ascribing too much real world meaning to the system. It feels weird to say that a boat references an instance of vehicle, rather than just being a vehicle. But it doesn't seem as weird to say that the role of Teacher has-a Person who fulfils it.
|
# ¿ Jun 26, 2014 14:07 |
|
FateFree posted:In your example inheritance seems like a fine solution. But you shouldn't think of composition as a boat referencing an instance of a vehicle. The boat is still a vehicle, it just maybe internally has an object that helps store some of its state like some sort of Model(make, model, id) class. Gravity Pike posted:I mean, the right answer is "whatever makes your code work." Do you have a situation where you have something that could be a boat or a bicycle, and you want to deal with whatever-it-is as a vehicle? Or do you have a Person, and want them to act as a Doctor in one situation, and a Mother in another? If you're not in either of these situations, gently caress it, don't bother making unnecessary hierarchical structures for the sake of having a hierarchy. It isn't terribly important that your code is an accurate description of The World, because you're not trying to model the world; you're usually trying to solve a very specific problem. Thanks for the answers. I find structural stuff like this interesting, as well as the psychology of how the way I think code should work reflects how I think the world works, even if that doesn't necessarily translate to an efficient solution. It's tough to separate what a class symbolises to me and how it should function. Going to read about dependency injection etc now. pigdog posted:It would be okay to use inheritance in this case, though most of the time, you use inheritance if you wish to inherit behavior, the code that's in the parent class. Chas McGill fucked around with this message at 14:09 on Jun 27, 2014 |
# ¿ Jun 27, 2014 14:04 |
|
Thick question incoming: How do I check if a collection only contains a certain value or object? I know about aCollection.contains(whatever) but I'm not sure how I'd make sure the collection doesn't contain anything else. I've been googling but haven't found anything useful. (This is for a test method)
|
# ¿ Jul 2, 2014 13:56 |
|
baquerd posted:
Ugh what a dope I am. Thanks.
|
# ¿ Jul 2, 2014 14:26 |
|
Either work for my purposes. Thanks. Having brain problems today. It's just for a test to make sure a collection only contains a specific object after a method is invoked.
|
# ¿ Jul 2, 2014 15:24 |
|
Java is my first programming language (not counting the Duplo that is Scratch) and I don't mind its verbosity either. I'd rather be able to read through a process on 4 lines and understand it than have it on 1 line made up of abbreviations and obscure symbols. I like the structure that the syntax forces at the moment. Maybe when I'm more experienced I'll chafe against it though.
|
# ¿ Jul 7, 2014 11:11 |
|
emanresu tnuocca posted:Guys I have a stupid question related to my previous question a few posts back. code:
Chas McGill fucked around with this message at 12:58 on Jul 14, 2014 |
# ¿ Jul 14, 2014 12:56 |
|
Following on from this - marketing at work wants to create a bit of modelling software to visualise the effect of spend on oil platforms. Basically it'd be a picture that changes when the user moves sliders or inputs figures. Are there any good Java libraries for something like this, or should I be looking at another tool/language? Seems like flash would be the traditional thing, but I only have (limited) experience with Java.
|
# ¿ Sep 3, 2014 08:58 |
|
I'm trying to use the new LocalDate class in Java 8 and for some reason I'm getting an error in IntelliJ that its constructor is private, despite the javadoc listing it as public. I expect I'm missing something fairly obvious here. I've imported the correct package. I just want a toString() friendly date format without any time information.
|
# ¿ Sep 26, 2014 14:09 |
|
HFX posted:You linked the javadoc for Joda time. The LocalDate class in Java 8 has no public constructors and instead supplies statics methods to create a new local date. I got the result I wanted with this: LocalDate dob = LocalDate.of(1950,10,14); Chas McGill fucked around with this message at 15:17 on Sep 26, 2014 |
# ¿ Sep 26, 2014 15:14 |
|
HFX posted:It's okay. At least you get to play with cool features. Work just crushes me by forcing me to stay in the bounds of Java 6 if I'm lucky. Well, I just finished a couple of Java modules so this is just for a personal project to stop everything I learnt from leaking out of my brain now that they're over. I remember worrying that what I was being taught would be obsolete within a year, but that was before I understood how much legacy work there is.
|
# ¿ Sep 26, 2014 16:01 |
|
thegasman2000 posted:The modules are: Mate, I just did those two modules. Don't worry too much about it. If you have any programming experience you'll be in a better position than I was to start with. M250 is baby's first java and M256 is jargon memorisation. Feel free to ask me any questions about the modules. I got a distinction for M250 and I'm anxiously waiting on the M256 results. for (Frog eachFrog : frogTribe) { eachFrog.croak(); } if (kermit.isGreen()) { kermit.matingDance(); } edit: Feels weird to see someone posting about the OU on here. Chas McGill fucked around with this message at 23:55 on Sep 30, 2014 |
# ¿ Sep 30, 2014 23:50 |
|
TheresaJayne posted:I started those at one time about 10 years ago, they started easy and then got mega hard even for a seasoned pro, Both exams were balls hard though.
|
# ¿ Oct 1, 2014 08:48 |
|
thegasman2000 posted:Looks like I am going to have fun then! I take it the exam is the vast majority of the grade too :/ The most advanced thing you'll do in M250 is probably reading from and writing to CSV files, or iterating through collections. M256 has very little programming since it's mostly theoretical. The course materials for both modules is extremely wordy, so if you're struggling with any concept you can probably just google it and find a much more concise explanation on stack overflow/youtube/javadocs. I will say that I feel like M250 gave me a decent grounding in Java basics and M256 got me interested in design patterns etc.
|
# ¿ Oct 1, 2014 10:01 |
|
Is this a weird way to use exceptions (this is taken from a bit of homework I did earlier in the year):code:
|
# ¿ Oct 9, 2014 14:10 |
|
|
# ¿ Apr 27, 2024 07:15 |
|
The Laplace Demon posted:That's what people are complaining about. There's nothing exceptional about those exceptions. Check if the book's been loaned or whatever your preconditions are before you try to loan it to the member. It's fine in a small, one-off project, but it's a pain in the rear end to try and debug why poo poo's broke years down the line. Volmarias posted:This. Don't rely on exceptions to be your flow control. Exceptions should be an uncommon, unexpected event that is still possible to occur. Think IOExceptions for an unexpected network disconnection.
|
# ¿ Oct 10, 2014 09:31 |