Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
baka kaba
Jul 19, 2003

PLEASE ASK ME, THE SELF-PROFESSED NO #1 PAUL CATTERMOLE FAN IN THE SOMETHING AWFUL S-CLUB 7 MEGATHREAD, TO NAME A SINGLE SONG BY HIS EXCELLENT NU-METAL SIDE PROJECT, SKUA, AND IF I CAN'T PLEASE TELL ME TO
EAT SHIT

Is it possible to declare an abstract method that takes a particular type of object as a parameter, and then in the subclass that implements that method, require another type of object that extends the original one? Something like this:

code:
public abstract class BaseClass {

	...

	/** Get this controller's current output value */
	public abstract <T extends BaseClass.Options> float getData(int socket, T options);

	public class Options{}
}
code:
public class SubClass extends BaseClass {

	...

	public float getData(int socket, SubClass.Options options) {
	    // bla bla
	}

	public class Options extends BaseClass.Options {
	    // things
	}
}
The abstract method is in the BaseClass to ensure all the subclasses implement it, but I want them each to require their own specific inner-class implementation of Options - but doing the above throws an error because the method signature doesn't match.

Is there a way to do what I'm trying to do, or should I just settle for using the same signature and type checking the options object when it's passed in? Sorry if this is full of stupid things, I'm kinda winging it here (especially with generics). Also this is on Android, in case there's any late-game Java magic that would otherwise help

Adbot
ADBOT LOVES YOU

baka kaba
Jul 19, 2003

PLEASE ASK ME, THE SELF-PROFESSED NO #1 PAUL CATTERMOLE FAN IN THE SOMETHING AWFUL S-CLUB 7 MEGATHREAD, TO NAME A SINGLE SONG BY HIS EXCELLENT NU-METAL SIDE PROJECT, SKUA, AND IF I CAN'T PLEASE TELL ME TO
EAT SHIT

Ahhh that's it, you're a star! Looks like I need to do a bit more reading to understand what's going on, but it's accepting it at least

baka kaba
Jul 19, 2003

PLEASE ASK ME, THE SELF-PROFESSED NO #1 PAUL CATTERMOLE FAN IN THE SOMETHING AWFUL S-CLUB 7 MEGATHREAD, TO NAME A SINGLE SONG BY HIS EXCELLENT NU-METAL SIDE PROJECT, SKUA, AND IF I CAN'T PLEASE TELL ME TO
EAT SHIT

^^^ :arghfist::choco:

Try running through your program yourself - pick a base and loop through the code, following your instructions. Try a few exponents starting at 0 and working up, and work out what they should be in advance.

When you have an exponent of 1, total should equal base. You set that, but then you hit your loop. Because you're checking if count is less than or equal to exp, and count starts at 0, you always enter the loop and multiply your total by the base at least twice.

You execute the code in the loop exp + 1 times, but you already set total to base1 before you got there.
So you're actually calculating base1 * baseexp+1, or baseexp+2.

You have to be careful with loops, and the code you always execute before or after them


e: edits for not even noticing what I was pointing out

baka kaba fucked around with this message at 01:38 on Jan 22, 2014

baka kaba
Jul 19, 2003

PLEASE ASK ME, THE SELF-PROFESSED NO #1 PAUL CATTERMOLE FAN IN THE SOMETHING AWFUL S-CLUB 7 MEGATHREAD, TO NAME A SINGLE SONG BY HIS EXCELLENT NU-METAL SIDE PROJECT, SKUA, AND IF I CAN'T PLEASE TELL ME TO
EAT SHIT


I've wrestled with this a bit over the last couple of days (this was pretty decent for covering things), and I'm still having trouble - can someone tell me if it's even possible to do what I'm trying? I get the feeling I'm using things the wrong way.

To recap:
  • I have a superclass with some abstract methods (and some not, which is why it's not an interface) that all subclasses have to implement
  • One of the methods takes a unique Options object for each subclass, and only that object, so they don't actually implement the same method signature
  • I'd like the unique object to be created from an inner class, for organisation and also so I can just refer to it as Subclass.Options
  • So the abstract method signature I'm thinking of is really method(Options options), in the scope of each class that implements it. I want each class to accept an instance of its own Options class

Nothing I've tried with generics has worked so far - I thought the first suggestion got the classes sorted out, but they didn't actually work the way they were supposed to. Eclipse was even throwing out what looked like a weird recursion error, which I assume was something like it going Controller<Controller<Controller<?>.Options>.Options>.Options and so on. So:

Am I making my life hell with inner classes?

Can I even use generics to make an abstract method signature, where the implementing classes' signatures are always more restrictive?

I could just have the method accept everything and do an internal type check and save myself the headaches, but I was wondering if there was a more 'elegant' way to do things ('elegant' in quotes after a horrorshow of angle brackets)

baka kaba
Jul 19, 2003

PLEASE ASK ME, THE SELF-PROFESSED NO #1 PAUL CATTERMOLE FAN IN THE SOMETHING AWFUL S-CLUB 7 MEGATHREAD, TO NAME A SINGLE SONG BY HIS EXCELLENT NU-METAL SIDE PROJECT, SKUA, AND IF I CAN'T PLEASE TELL ME TO
EAT SHIT

FateFree posted:

Can't you just have an AbstractOptions class that your base method uses, and have all your subclasses have inner classes that extend it? Subclass.Options extends AbstractOptions?

Otherwise you'll just have a base class that has generics, BaseClass<? extends AbstractOptions>, and your subclasses SubClass<SubClass.Options>, and Subclass.Options extends AbstractOptions.

This will let you do things like SubClass.Options options = subClass.getOptions();


That's along the lines of what I was trying, I just need every class implementing that method to require a specific subclass of AbstractOptions, whereas the abstract signature requires any subclass of AbstractOptions. It's basically a different signature, but it's logically fixed and consistent, so I thought I might be able to express that relationship with generics.

The trouble (I think) I've been running into is having a generic class like Subclass<Subclass.Options>, which seems to work fine for the definitions - but when it comes to using it that inner Subclass is considered a raw type, and there are obviously complaints. I'll have to take a look at exactly what is going wrong, but I'm just worried that it's a sign that I shouldn't even be trying to do this, at least not this way


FateFree posted:

This will cause the headaches, not save them!

No see I can just fall to the defaults and log an error if I like, it'll be totally sweet, let me gooooo

Sedro posted:

Looking back at the earlier code, you probably need to make the inner classes static.

Now this is interesting. I'm getting a good feeling about this one

baka kaba fucked around with this message at 21:30 on Jan 22, 2014

baka kaba
Jul 19, 2003

PLEASE ASK ME, THE SELF-PROFESSED NO #1 PAUL CATTERMOLE FAN IN THE SOMETHING AWFUL S-CLUB 7 MEGATHREAD, TO NAME A SINGLE SONG BY HIS EXCELLENT NU-METAL SIDE PROJECT, SKUA, AND IF I CAN'T PLEASE TELL ME TO
EAT SHIT

Couldn't you implement a Comparator that compares by date, but if the dates are equal it randomly returns higher or lower? That way you can just throw a sort on there and get random ordering for the equal dates, and just step through your list. You can make all the nulls sink to one end of the list too, if that's what you want.

How many objects are you working with here? And how often does this run?

baka kaba
Jul 19, 2003

PLEASE ASK ME, THE SELF-PROFESSED NO #1 PAUL CATTERMOLE FAN IN THE SOMETHING AWFUL S-CLUB 7 MEGATHREAD, TO NAME A SINGLE SONG BY HIS EXCELLENT NU-METAL SIDE PROJECT, SKUA, AND IF I CAN'T PLEASE TELL ME TO
EAT SHIT

Wouldn't that skew all your choices to the older side of the distribution, and make the newer ones never come up at all? Since they'd always be ignored in favour of the older picks

I'm still not entirely sure what you want to do - like this thing about not manually setting the margin of approximation... what does determine it? It might help to lay out a step by step example of what you want to happen

baka kaba
Jul 19, 2003

PLEASE ASK ME, THE SELF-PROFESSED NO #1 PAUL CATTERMOLE FAN IN THE SOMETHING AWFUL S-CLUB 7 MEGATHREAD, TO NAME A SINGLE SONG BY HIS EXCELLENT NU-METAL SIDE PROJECT, SKUA, AND IF I CAN'T PLEASE TELL ME TO
EAT SHIT

mod sassinator posted:

Is this a good thread for Android development questions? If it is, I'm curious if anyone has opinions on using ADT + Eclipse to develop Android apps vs. the new IntelliJ-based Android Studio. Both are being made by Google and seem well supported. Poking around a bit with them I like the IntelliJ one much more--Eclipse seems really clunky and the UI builder is pretty bad. Am I going to regret it if I dive into using Android Studio to make apps?

You probably want to pop over to the Android thread, but Android Studio is probably going to end up being the default IDE platform, so you may as well get acquainted with it. Do you have any special requirements, like a specific project you need to work on? As far as I know most of the core stuff is working, with a few glitches here and there, but if you're using something specialised it might not be ready for primetime, if you get me. You should mention this in the thread since people there will have a bit more experience!

ADT is well-established and robust, so it's still a good choice at this point, and you can get on with learning about Android itself. It also has the EGit plugin which has completely spoiled me (history/merge/diff and all that good stuff right there in the IDE), plus it deploys a hell of a lot faster than AS does with Gradle - maybe that's a problem with my setup, I don't know. But I can hit go on Eclipse and have the APK installed within 10 seconds, versus a minute or two with AS

ps you have the best avatar on the forums

baka kaba
Jul 19, 2003

PLEASE ASK ME, THE SELF-PROFESSED NO #1 PAUL CATTERMOLE FAN IN THE SOMETHING AWFUL S-CLUB 7 MEGATHREAD, TO NAME A SINGLE SONG BY HIS EXCELLENT NU-METAL SIDE PROJECT, SKUA, AND IF I CAN'T PLEASE TELL ME TO
EAT SHIT

That cat always looks ready to sass the gently caress out of everyone (don't tell me if I'm wrong)

Karl Sharks posted:

I'm doing a project that simulates a forest ecosystem and was curious if there was a way to have Eclipse debugger follow a specific object, as in one Mouse out of many, just to see how it behaves? Or something that would do something similar to it.

Also, sorry if this is the wrong thread, posted after only checking for an IDE thread instead of considering the general questions thread. I can ask there if it's more appropriate.

You can set a breakpoint and make it conditional, so if you drop it in a bit of mouse logic and set it to trigger when mouseId == 321 or whatever it'll pause with all your mouse details to hand. You can set up some expressions to display all the useful info you want too, any values or calculations, so you have an at-a-glance list of data every time your mouse comes up.

I'm on my phone and I can't really give you any links, hopefully that's enough to point you in the right direction but I can find some stuff tomorrow if you need it. It ain't complicated though, once you know it's there

baka kaba
Jul 19, 2003

PLEASE ASK ME, THE SELF-PROFESSED NO #1 PAUL CATTERMOLE FAN IN THE SOMETHING AWFUL S-CLUB 7 MEGATHREAD, TO NAME A SINGLE SONG BY HIS EXCELLENT NU-METAL SIDE PROJECT, SKUA, AND IF I CAN'T PLEASE TELL ME TO
EAT SHIT

Think about what your output is meant to show at the end of your code. You're displaying some values, so if any of them look wrong, walk through your code and look at how they're being manipulated. It's a pretty simple algorithm, so just take your time and tell yourself what's happening to your variables at each line.

As another couple of pointers, make sure you read up on variable scope (the official Java tutorials are pretty great, if you haven't seen them), and always be very careful with for loops and the condition for ending (or outright skipping) them. Think about what should happen if you invest for 1 month, then walk through your code and make sure it does the right thing.

Time for my question - can you do that with the i variable? Is that acceptable practice? I guess it would work but it seems like a weird way to do it. I'd check but I'm phone postin'

e- I don't see a mention of scope in the early Java basics stuff so I guess that's not a good example of their usefulness :v:

baka kaba fucked around with this message at 02:48 on Mar 21, 2014

baka kaba
Jul 19, 2003

PLEASE ASK ME, THE SELF-PROFESSED NO #1 PAUL CATTERMOLE FAN IN THE SOMETHING AWFUL S-CLUB 7 MEGATHREAD, TO NAME A SINGLE SONG BY HIS EXCELLENT NU-METAL SIDE PROJECT, SKUA, AND IF I CAN'T PLEASE TELL ME TO
EAT SHIT

Have you checked your results against some hand calculations, to make sure you're getting the right numbers out? You always need to make sure, that way if something's coming out wrong you can track down any mistakes you've made. That 12 month final return looks a bit high for 5.75%

Also as far as commenting goes, do what you gotta do to please the TA (an introductory 'read user's values' comment at the top might be fine but you know better), but don't skimp on explaining the actual technical stuff you're doing. The idea is that you can come back to the code and see a comment that says 'here's what's going on here', instead of having to work it all out. It'll also help you to lay out exactly what you want to do when you're writing the code. Try it in your for loop - explain exactly what your formula is doing, in English or pseudocode. You can pare it back to a basic summary when you're done, if you like

baka kaba fucked around with this message at 06:06 on Mar 21, 2014

baka kaba
Jul 19, 2003

PLEASE ASK ME, THE SELF-PROFESSED NO #1 PAUL CATTERMOLE FAN IN THE SOMETHING AWFUL S-CLUB 7 MEGATHREAD, TO NAME A SINGLE SONG BY HIS EXCELLENT NU-METAL SIDE PROJECT, SKUA, AND IF I CAN'T PLEASE TELL ME TO
EAT SHIT

Sountox posted:

Doh! You are absolutely correct! Nice catch I almost submitted it like that. I removed the *(i) from the (investBal = investBal + (investBal * ((interestRate/1200) * (i)))) equation and it went through just fine!

:downs:

Thanks for that.

Did you change the for loop logic too after testing your results? You're starting at 0 and looping while it's less than or equal to your number of months - so it executes once for 0 and then once for every month after that, which is probably one more time than you want. It wasn't showing up before because you were multiplying by i, so that didn't add anything on the first, extra i=0 loop.

If I knew you were rushing I'd have hinted harder, but hopefully you did checks after your fix and made sure it was giving the right results!

baka kaba
Jul 19, 2003

PLEASE ASK ME, THE SELF-PROFESSED NO #1 PAUL CATTERMOLE FAN IN THE SOMETHING AWFUL S-CLUB 7 MEGATHREAD, TO NAME A SINGLE SONG BY HIS EXCELLENT NU-METAL SIDE PROJECT, SKUA, AND IF I CAN'T PLEASE TELL ME TO
EAT SHIT

What's your question though? Do you want to know how to compile a Java program or something?

baka kaba
Jul 19, 2003

PLEASE ASK ME, THE SELF-PROFESSED NO #1 PAUL CATTERMOLE FAN IN THE SOMETHING AWFUL S-CLUB 7 MEGATHREAD, TO NAME A SINGLE SONG BY HIS EXCELLENT NU-METAL SIDE PROJECT, SKUA, AND IF I CAN'T PLEASE TELL ME TO
EAT SHIT

If you make sure you indent properly, it should be a lot easier to spot when things are happening within a loop, or outside of it after it's finished

baka kaba
Jul 19, 2003

PLEASE ASK ME, THE SELF-PROFESSED NO #1 PAUL CATTERMOLE FAN IN THE SOMETHING AWFUL S-CLUB 7 MEGATHREAD, TO NAME A SINGLE SONG BY HIS EXCELLENT NU-METAL SIDE PROJECT, SKUA, AND IF I CAN'T PLEASE TELL ME TO
EAT SHIT

This is a total uninformed guess, but are your graphics drivers up to date and working? Can you get any 3D functionality outside of Processing? Might be worth at least running some tests

baka kaba
Jul 19, 2003

PLEASE ASK ME, THE SELF-PROFESSED NO #1 PAUL CATTERMOLE FAN IN THE SOMETHING AWFUL S-CLUB 7 MEGATHREAD, TO NAME A SINGLE SONG BY HIS EXCELLENT NU-METAL SIDE PROJECT, SKUA, AND IF I CAN'T PLEASE TELL ME TO
EAT SHIT

Brad Pitt posted:

That has definitely been the biggest challenge in this class. Just asking him how your pseudo code looks returns a very confusing and poorly worded responses.

Yeah, basically what FdT said - he specifically says you need to create a method named compute which takes your x and y arrays as input parameters. So he's defined the method signature there - it needs to be named compute, and take two input parameters, both of which are 1-dimensional integer arrays (explicitly defined in the second line of your quote).

Take a look at the equation that compute() is meant to... compute. You have three variables in there - x, y and z. The spec says "in both compute methods you will return the total summed value for z" - so basically, your compute() method needs to work out the value of z when you give it two other values x and y.

The key word there is summed, that implies you need to produce a bunch of values for z and return the sum of them, and it makes sense seeing as you're not passing in a single value for x and y, you're passing in several values for each. So your compute() method will have to run that equation several times, once for each pair of x and y in the input arrays you're feeding it, and sum all of the results.

The phrasing "you will create a method named compute" and "in the second compute method" definitely implies both methods are supposed to be called compute(), except in the second one you're passing your data array instead, which is a 2-dimensional array. That means it has a different method signature than the first, and you're overloading it. If that's new to you have a quick look here: http://docs.oracle.com/javase/tutorial/java/javaOO/methods.html
It basically means you can call compute() with different types of parameters and it will just use the appropriate code, instead of having to name everything differently.

If that all makes sense, hopefully you pretty much know how your program is meant to flow, and it's just a case of turning it into the appropriate Java code!

baka kaba
Jul 19, 2003

PLEASE ASK ME, THE SELF-PROFESSED NO #1 PAUL CATTERMOLE FAN IN THE SOMETHING AWFUL S-CLUB 7 MEGATHREAD, TO NAME A SINGLE SONG BY HIS EXCELLENT NU-METAL SIDE PROJECT, SKUA, AND IF I CAN'T PLEASE TELL ME TO
EAT SHIT

A NullPointerException is a sign that you're trying to access an object that doesn't exist. Take a look at the line where you're getting that exception, and see if there are any objects around that look suspicious. Here's some hints in some vague order of hintingness, although someone is probably gonna post before me!


Look at the Arrays bit of the trail, especially the "Creating..." section

Arrays are objects, even when they hold primitive data types (which yours doesn't, but still)

Look at the equivalent line in their solution, and look at the objects they're using. Work back through the code and see what they do with that object

:siren: THE ANSWER :siren: You haven't initialised your array. When you define private PlayingCard[] myCards you're creating a reference to an object. You're saying that myCards is a reference to an array object, that holds PlayingCard objects. You haven't actually created the array object itself, which is the first thing the solution code does.

When your loop goes to create the PlayingCard objects to put into the array, it isn't there. Your reference doesn't point to an object yet. Always gotta create the object before you can use it, or you'll hit a null, and probably get a NullPointerException to point you in the right direction (little joke there to keep my spirits up since I know two people already answered this before me)


e- welp

baka kaba fucked around with this message at 02:21 on May 7, 2014

baka kaba
Jul 19, 2003

PLEASE ASK ME, THE SELF-PROFESSED NO #1 PAUL CATTERMOLE FAN IN THE SOMETHING AWFUL S-CLUB 7 MEGATHREAD, TO NAME A SINGLE SONG BY HIS EXCELLENT NU-METAL SIDE PROJECT, SKUA, AND IF I CAN'T PLEASE TELL ME TO
EAT SHIT

Is it an incredibly bad idea to create a nested class in your test class, which extends the class you're testing and adds public methods which call the private ones? That way you don't need to change any access modifiers and you can keep all the testing gubbins separate from the real code

baka kaba
Jul 19, 2003

PLEASE ASK ME, THE SELF-PROFESSED NO #1 PAUL CATTERMOLE FAN IN THE SOMETHING AWFUL S-CLUB 7 MEGATHREAD, TO NAME A SINGLE SONG BY HIS EXCELLENT NU-METAL SIDE PROJECT, SKUA, AND IF I CAN'T PLEASE TELL ME TO
EAT SHIT

Yeah my mistake, I was thinking of protected methods and variables. Sorry, don't mind me, I've been writing unit tests all week and debugging at the same time, it's leaking out

baka kaba
Jul 19, 2003

PLEASE ASK ME, THE SELF-PROFESSED NO #1 PAUL CATTERMOLE FAN IN THE SOMETHING AWFUL S-CLUB 7 MEGATHREAD, TO NAME A SINGLE SONG BY HIS EXCELLENT NU-METAL SIDE PROJECT, SKUA, AND IF I CAN'T PLEASE TELL ME TO
EAT SHIT

Any chance that server is hosed up and is having its time changed constantly (like, say several times per second) between local time and UTC?

baka kaba
Jul 19, 2003

PLEASE ASK ME, THE SELF-PROFESSED NO #1 PAUL CATTERMOLE FAN IN THE SOMETHING AWFUL S-CLUB 7 MEGATHREAD, TO NAME A SINGLE SONG BY HIS EXCELLENT NU-METAL SIDE PROJECT, SKUA, AND IF I CAN'T PLEASE TELL ME TO
EAT SHIT

This might be a stupid question, but is there a design pattern for requiring classes to implement a static method? I have a bunch of classes like this:
code:
Strategy1
Strategy2
Strategy3
The idea is that they all perform some work on a data structure that's passed in, but each class does it differently - the code is potentially complex, which is why they're separated out to keep things neater, and easier to add to later. The usage in each case is the same - pass in the Map reference, and the data in it is manipulated. There's just one method, and I wanted to call it statically since there's no need for an instance of the class.

So to keep things straight, I wanted to enforce this pattern by using an interface or extending a class, so all of these classes are guaranteed to implement the static method I'm calling. I don't want to provide a default implementation, since that would allow new classes to avoid needing their own implementation, which is the whole point of these shenanigans. My initial thought is to make the method abstract in the interface/superclass, but it seems you can't define a method as abstract and static.

I get that they're sort of opposites, but the idea doesn't seem that strange to me - I want to force implementing classes to include a static method matching this signature, that's all. Is that even possible? It's not really necessary in this case, but I'm interested in the general idea of being able to force and require a certain pattern. If this is a terrible idea (or if I've not really explained myself) let me know!

This is on Android by the way, in case there's some fancy legendary developments in newer Java versions that I'll never see...

baka kaba
Jul 19, 2003

PLEASE ASK ME, THE SELF-PROFESSED NO #1 PAUL CATTERMOLE FAN IN THE SOMETHING AWFUL S-CLUB 7 MEGATHREAD, TO NAME A SINGLE SONG BY HIS EXCELLENT NU-METAL SIDE PROJECT, SKUA, AND IF I CAN'T PLEASE TELL ME TO
EAT SHIT

Nah there's no state at all, that's why I'm making them static in the first place. They run a fixed procedure on the input and spit out a result (well, update the map). It's purely an organisational thing I'm trying, keeping them in their own classes. And singletons... I went there for something else, where I really shouldn't have in retrospect. Learned a lot though! Way more than I need for this though.

I mean I could create an instance - it's run rarely, so it doesn't really matter. And I could drop the whole template idea, since each class only has that one method anyway, so it would be hard to miss out! The main reason I started looking at it was so I could throw the Javadoc comment on the abstract version, so I wouldn't have to add it manually each time. Really this is total overkill for what I'm doing, but I like the idea of things all fitting together neatly, so I thought I'd look into it at least. I definitely have easy options I can take otherwise

baka kaba
Jul 19, 2003

PLEASE ASK ME, THE SELF-PROFESSED NO #1 PAUL CATTERMOLE FAN IN THE SOMETHING AWFUL S-CLUB 7 MEGATHREAD, TO NAME A SINGLE SONG BY HIS EXCELLENT NU-METAL SIDE PROJECT, SKUA, AND IF I CAN'T PLEASE TELL ME TO
EAT SHIT

rhag posted:

Singleton ... not even once (just kidding, there are 2 valid uses of singleton).

Anyway, interface with static method? That's a wtf right there. Calling them a Strategy looks to me like you wanted to implement the strategy pattern: http://en.wikipedia.org/wiki/Strategy_pattern .

That is a valid pattern and there are very valid cases to implement that. Is your case valid? Only for you to decide.

Thanks, that's really interesting - and it's funny that I came up with that pattern and named it the exact same way. Thank you serendipity

Yeah, that's effectively what I was going for, only my idea here is to use static classes and methods since there's "no actual need" to instantiate the class. I realise that might be completely wrong (if utility classes aren't meant to be a thing), and in reality in my case it's a single object used by another single object so who cares, but the idea doesn't seem weird to me. Basically just not creating objects if you can help it, and wanting to use an interface to define that static functionality in all the classes that implement it. Is this never a thing in any OO language?

baka kaba
Jul 19, 2003

PLEASE ASK ME, THE SELF-PROFESSED NO #1 PAUL CATTERMOLE FAN IN THE SOMETHING AWFUL S-CLUB 7 MEGATHREAD, TO NAME A SINGLE SONG BY HIS EXCELLENT NU-METAL SIDE PROJECT, SKUA, AND IF I CAN'T PLEASE TELL ME TO
EAT SHIT

Yeah don't worry, this isn't optimisation so much as trying to do things cleanly, and wondering if it's even possible or right. Like I said it's effectively one object called by another object, and it only happens during initialisations which are rare anyway, so I'm totally ok with instantiating whichever strategy class I'm using (and it's basically what I'll end up doing anyway). The irony is the strategies are handling some optimisation :haw:

I'll take a look at things like Guice, cheers - it's probably more than I need for what I'm doing here, but I've been meaning to get more familiar with useful libraries

baka kaba
Jul 19, 2003

PLEASE ASK ME, THE SELF-PROFESSED NO #1 PAUL CATTERMOLE FAN IN THE SOMETHING AWFUL S-CLUB 7 MEGATHREAD, TO NAME A SINGLE SONG BY HIS EXCELLENT NU-METAL SIDE PROJECT, SKUA, AND IF I CAN'T PLEASE TELL ME TO
EAT SHIT

Java code:
Volmarias implements InterfaceFan

baka kaba
Jul 19, 2003

PLEASE ASK ME, THE SELF-PROFESSED NO #1 PAUL CATTERMOLE FAN IN THE SOMETHING AWFUL S-CLUB 7 MEGATHREAD, TO NAME A SINGLE SONG BY HIS EXCELLENT NU-METAL SIDE PROJECT, SKUA, AND IF I CAN'T PLEASE TELL ME TO
EAT SHIT

What if you make Status lowercase? It looks like you're trying to access a Status class statically at the bottom there

baka kaba
Jul 19, 2003

PLEASE ASK ME, THE SELF-PROFESSED NO #1 PAUL CATTERMOLE FAN IN THE SOMETHING AWFUL S-CLUB 7 MEGATHREAD, TO NAME A SINGLE SONG BY HIS EXCELLENT NU-METAL SIDE PROJECT, SKUA, AND IF I CAN'T PLEASE TELL ME TO
EAT SHIT

Are you at the point yet where you've stopped saying 'what the gently caress is this unreadable poo poo' about

Java code:
Cat cat = new Cat();
and started doing it intentionally yourself, because you think it's a good idea?

baka kaba
Jul 19, 2003

PLEASE ASK ME, THE SELF-PROFESSED NO #1 PAUL CATTERMOLE FAN IN THE SOMETHING AWFUL S-CLUB 7 MEGATHREAD, TO NAME A SINGLE SONG BY HIS EXCELLENT NU-METAL SIDE PROJECT, SKUA, AND IF I CAN'T PLEASE TELL ME TO
EAT SHIT

Yeah when I first started I wasn't used to the general type = new constructor() style, with the same class name referenced on both sides. Giving the variable the same name on top of that just seemed like a huge 'gently caress you' to clarity and comprehension, and these were tutorials

http://en.wikipedia.org/wiki/Buffalo_buffalo_Buffalo_buffalo_buffalo_buffalo_Buffalo_buffalo

Anyway of course now I do it myself when I don't need a descriptive name :shobon:

baka kaba
Jul 19, 2003

PLEASE ASK ME, THE SELF-PROFESSED NO #1 PAUL CATTERMOLE FAN IN THE SOMETHING AWFUL S-CLUB 7 MEGATHREAD, TO NAME A SINGLE SONG BY HIS EXCELLENT NU-METAL SIDE PROJECT, SKUA, AND IF I CAN'T PLEASE TELL ME TO
EAT SHIT

carry on then posted:

Depending on how confident you feel, the official Java Tutorials (especially Getting Started and Learning The Java Language) offer a good, if a bit brief, walkthrough on setting up the environment, the language syntax, and common classes you'll be using.

CodingBat is pretty good for practicing some problems and getting your feet wet with the basics of the language, too

baka kaba
Jul 19, 2003

PLEASE ASK ME, THE SELF-PROFESSED NO #1 PAUL CATTERMOLE FAN IN THE SOMETHING AWFUL S-CLUB 7 MEGATHREAD, TO NAME A SINGLE SONG BY HIS EXCELLENT NU-METAL SIDE PROJECT, SKUA, AND IF I CAN'T PLEASE TELL ME TO
EAT SHIT

This is probably the most 'no poo poo' thing ever but I was learning some Eclipse keyboard shortcuts the other day (we all have our hobbies) and I found out about Alt+\ :swoon:

I mean it's no ctrl+space but it's pretty cool

baka kaba
Jul 19, 2003

PLEASE ASK ME, THE SELF-PROFESSED NO #1 PAUL CATTERMOLE FAN IN THE SOMETHING AWFUL S-CLUB 7 MEGATHREAD, TO NAME A SINGLE SONG BY HIS EXCELLENT NU-METAL SIDE PROJECT, SKUA, AND IF I CAN'T PLEASE TELL ME TO
EAT SHIT

Plus an integrated debugger, stuff like a nice Git plugin in Eclipse's case, all kinds of refactoring magic, the ability to find declarations or references, pop-up documentation... I think IDEs can be pretty overwhelming at first, but once you actually get on with making something in one you start to actually learn what everything's for, and how incredibly useful it is.

Hell, they can actually help you to learn the language, with all the handy feedback and suggestions they give you when you do something even a little :raise: I don't know exactly where the dividing line between jacked up text editor and IDE lies, but they're definitely super helpful environments to work in

hooah posted:

Did you mean Alt+/? I searched the first few web pages that came up in a search for "Eclipse keyboard shortcuts", and none of them had Alt+\.

Yeah sorry, that'll teach me to phone post. The one all the way over there (that doesn't work with Alt Gr but whatever).

Hope I didn't get your hopes up there

baka kaba fucked around with this message at 02:55 on Jul 14, 2014

baka kaba
Jul 19, 2003

PLEASE ASK ME, THE SELF-PROFESSED NO #1 PAUL CATTERMOLE FAN IN THE SOMETHING AWFUL S-CLUB 7 MEGATHREAD, TO NAME A SINGLE SONG BY HIS EXCELLENT NU-METAL SIDE PROJECT, SKUA, AND IF I CAN'T PLEASE TELL ME TO
EAT SHIT

n0manarmy posted:

I'm building tests for a game that I'll probably never finish. I have a Player object that has 4 stats:

Tact, Speed, Passing, Toughness

The respective stats will determine the position of the player like so below:

Keeper - tact > speed > toughness > passing
Defender - toughness > speed > passing > tact
midfield - passing > speed > tact > toughness
striker - tact > speed > passing > toughness

The engine takes two teams and determines the lineup of 15 players out of 22, however I'm having a hard time determining how to best sort each of these objects to get the results. Each player is a Player object with an int for each stat.

I've played with the idea of doing a sorted Map and then base it from there. Or would it be best to build a class that handles a bunch of if statements to best determine the player position? Thoughts?

Is it meant to be extensible? And is every player a member of one of those groups?

I mean as it is, if you just want to determine which of those categories a player falls into you can just use a simple if block
Java code:
// check for keeper or striker
if (player.tact > player.speed) {
    if (player.toughness > player.passing) {
        return KEEPER;
    }
    else {
        return STRIKER;
    }
}
// must be a defender or midfield
else if (player.toughness > player.speed) {
    return DEFENDER;
}
else {
    // last statement works as a default position too
    return MIDFIELD;
}
But I kept that verbose because I feel like you're going to have to do a fair bit of extra logic, if only at the player generation stage (making sure you don't have equal values, ensuring every player's stats fit one of those recipes etc.)

You could put this position logic in the Player class itself though, since it's really about what the player is based on their attributes. Are you trying to select the best players for each position, like say the best two strikers out of the three you have? How involved is the comparison, do you have to weight the different attributes or can you just say 'highest tact is the winner'?

baka kaba
Jul 19, 2003

PLEASE ASK ME, THE SELF-PROFESSED NO #1 PAUL CATTERMOLE FAN IN THE SOMETHING AWFUL S-CLUB 7 MEGATHREAD, TO NAME A SINGLE SONG BY HIS EXCELLENT NU-METAL SIDE PROJECT, SKUA, AND IF I CAN'T PLEASE TELL ME TO
EAT SHIT

Yeah, try expanding each piece of that into a description of what you're checking or what you're doing. Make sure your logic is consistent, and then make sure each bit of code is doing what you want it to.

Your trigonometry code seems like it would only work in a specific case - the ball bouncing off the sides of the box at 45-degree angles, moving clockwise, always hitting each side in a fixed sequence. Like for example, if your ball was just moving horizontally, with no vertical velocity, you'd expect it to ping right back at 180 degrees, with the vertical speed unchanged. Right now your code just forces a 90-degree change in a fixed direction, which in this case means straight up or down.

So imagine that path line going horizontally straight into the wall, and then straight up, like a fixed right-angle. Now imagine tilting the whole thing up, so the incoming path is coming in from a higher point at a downward angle. What happens to the other part, after its 90-degree left turn? Where's it going? What's your code going to see the next time around?

The velocity angle idea is cool, you just need to make sure you're calculating changes to it correctly. You only have horizontal and vertical sides to worry about, so just get a diagram with the angle quadrants, and have a think about how a bounce off each side changes an angle, and what that means in terms of pi

baka kaba fucked around with this message at 19:03 on Aug 25, 2014

baka kaba
Jul 19, 2003

PLEASE ASK ME, THE SELF-PROFESSED NO #1 PAUL CATTERMOLE FAN IN THE SOMETHING AWFUL S-CLUB 7 MEGATHREAD, TO NAME A SINGLE SONG BY HIS EXCELLENT NU-METAL SIDE PROJECT, SKUA, AND IF I CAN'T PLEASE TELL ME TO
EAT SHIT

Is your position checking right? That's what I was alluding to when I said you should write your logic out as short descriptions of what you need to do - it seems really inconsistent. Sometimes you're compensating for the particular circle's radius, sometimes it's a hardcoded value, sometimes it's both!

Maybe it works out, but it's better to make things work for a general case, where you calculate the things you need to check from the data you're working with. Like in this case, you're basically taking the centre of some ball you're given, as x and y coordinates, and checking that the ball's top, left, bottom and right sides haven't gone through the top, left, bottom and right walls.

So you need to take your centre point, work out where those ball sides are (which depends on the size of the ball), and compare them to the position of the wall they might hit (which depends on the dimensions of the walled area). You can calculate all the values you need to compare from the info you're given (ball position, ball radius, width and height of the place), and if they ever change your code will spit out the appropriate values and compare the right things.

You might want to hardcode some numbers sometimes, say a fixed distance from each wall that counts as a collision (like a buffer zone or something), but in that case you should declare it as a constant with a nice readable name, then use that constant in your code. If you ever want to change that fixed value, there's only one place you need to do it! Also comment (explain) everything

baka kaba
Jul 19, 2003

PLEASE ASK ME, THE SELF-PROFESSED NO #1 PAUL CATTERMOLE FAN IN THE SOMETHING AWFUL S-CLUB 7 MEGATHREAD, TO NAME A SINGLE SONG BY HIS EXCELLENT NU-METAL SIDE PROJECT, SKUA, AND IF I CAN'T PLEASE TELL ME TO
EAT SHIT

CarrKnight posted:

Yes exactly. I'd need to tell my audience to go to their settings, lower them and so on. They aren't going to do it.
It's a pain. They'll just go somewhere else.

Web-start requires signature or lowered security settings or both.
Download and run is my current modus operandi, but i really wish they could just run from the web.

Is your audience likely to even have the JRE installed and enabled? I mean I'm someone who codes in Java and if I see a webpage trying to run a Java applet, I usually say 'gently caress this poo poo' and hit that back button. Not saying I'm a representative sample or anything, and you know your audience, but if you're looking for ease of use I'd imagine you want something that just runs 'natively' in the browser?

It might help to describe your 'game' a bit too, there's a difference between something that can just send moves to a server vs something that needs to do a lot of responsive heavy lifting on the client

baka kaba
Jul 19, 2003

PLEASE ASK ME, THE SELF-PROFESSED NO #1 PAUL CATTERMOLE FAN IN THE SOMETHING AWFUL S-CLUB 7 MEGATHREAD, TO NAME A SINGLE SONG BY HIS EXCELLENT NU-METAL SIDE PROJECT, SKUA, AND IF I CAN'T PLEASE TELL ME TO
EAT SHIT

To be specific since you said you're new to this, you're calling the redraw method of Plot, which is a class (note the capitalisation at the start of the name). By the sounds of it, redrawing is something that only makes sense in the context of an instance, which is when you actually create a Plot-type object with its own state and then do things to it, like giving it drawing data then asking it to redraw itself.

Calling a method on the general class itself will either do some general purpose function, like performing some task in the program or returning a value (like when you call Math.sin() or whatever), or it will change something about the state of all Plot objects as a whole. It won't affect a single instance like you want it to - without passing it any info, it couldn't know which Plot object you wanted to mess with. It's like calling the head office.

So what you want to do is tell a specific instance to redraw itself, by calling redraw() on that object. Since your code is running in one of those objects, which is subclassed from Plot, you can just call the method, and it'll run it on itself

(Some of that may be technically squirrelly but I'm just trying to give a general idea here)

And how come you're trying to use doubles?

e- I'm just guessing here since I haven't used what you're using, but are you using two panels, one with the grid and one to draw a line on? (Since you've got two versions of the same method.) You're not calling redraw on the grid, which seems to work, why do you need it for the line - you're not clearing it after you've drawn it, are you? And are you sure the panel isn't beneath the first one?

baka kaba fucked around with this message at 08:19 on Sep 5, 2014

baka kaba
Jul 19, 2003

PLEASE ASK ME, THE SELF-PROFESSED NO #1 PAUL CATTERMOLE FAN IN THE SOMETHING AWFUL S-CLUB 7 MEGATHREAD, TO NAME A SINGLE SONG BY HIS EXCELLENT NU-METAL SIDE PROJECT, SKUA, AND IF I CAN'T PLEASE TELL ME TO
EAT SHIT

If you're just using those p-variables as something to pass into your methods, you could just do

Java code:
pnlPlot1.setxA((int) xA);
Casting to int with (int) basically lops off the decimal, so you can just put that in the method call and it'll pass an integer value. This might be why you were getting that lossy conversion warning by the way: (int) 1.999999999999 becomes 1, which might not be what you want, so you might want to look at Math.round or something (which returns a long when you call it with a double, so you'd need to cast that to int too).

I'd be very tempted to turn all those set calls into one that passes an array, so you can do it all in a line or two, but it's not a huge deal if you're only doing it in one place

baka kaba
Jul 19, 2003

PLEASE ASK ME, THE SELF-PROFESSED NO #1 PAUL CATTERMOLE FAN IN THE SOMETHING AWFUL S-CLUB 7 MEGATHREAD, TO NAME A SINGLE SONG BY HIS EXCELLENT NU-METAL SIDE PROJECT, SKUA, AND IF I CAN'T PLEASE TELL ME TO
EAT SHIT

Hello Java friends - any chance there's an established pattern or something for this kind of thing?

I'm trying to make a superclass with some generic functionality, that can handle the specific details added by the subclasses. I want each subclass to be able to add some fields of various types, and pop them in a map pointing to their associated keys. Then the superclass will iterate over the map, grab each field variable, and call a method appropriate to its type. So something like:

Java code:
abstract class SuperButt {

  protected Map<Object, String> buttFieldsToKeys = new HashMap<Object, String>();

  public doThings() {
    for(Object buttField : buttFieldsToKeys.keySet()) {
      // get buttField's type and call the appropriate method
      // set buttField to some value as a result
    }
  }

}

class UseThisButt extends SuperButt {

  Integer numberOfButts;
  String  buttDescription;
  Double  buttFactor;
  
  public UseThisButt() {
    // add field references to buttFieldsToKeys
  }

  public doThings() {
    super();
    // fields are now populated and we can use them for nefarious purposes
  }

}
I guess I can use reflection to store a bunch of Field objects, and work with them, right? Is there a simple way to list the fields that I want to be mapped (i.e. not all of them) without having to use string versions of the names and worrying about typos and refactoring issues? Or is there a better way of approaching this?

baka kaba
Jul 19, 2003

PLEASE ASK ME, THE SELF-PROFESSED NO #1 PAUL CATTERMOLE FAN IN THE SOMETHING AWFUL S-CLUB 7 MEGATHREAD, TO NAME A SINGLE SONG BY HIS EXCELLENT NU-METAL SIDE PROJECT, SKUA, AND IF I CAN'T PLEASE TELL ME TO
EAT SHIT

Yeah you need to create each object in the array, which is what the new keyword does. It basically calls a constructor, so new Engineer("bob","dobson") is really calling the Engineer class's constructor with those two parameters, which does its business and creates the object, then new returns the reference.

So your array creation code sort of ends up like Employee[] a = { ref1, ref2 };
Like how int[] a = { 1+1, 2*5 }; will evaluate the stuff inside first and end up with int[] a = { 2, 10 };

Adbot
ADBOT LOVES YOU

baka kaba
Jul 19, 2003

PLEASE ASK ME, THE SELF-PROFESSED NO #1 PAUL CATTERMOLE FAN IN THE SOMETHING AWFUL S-CLUB 7 MEGATHREAD, TO NAME A SINGLE SONG BY HIS EXCELLENT NU-METAL SIDE PROJECT, SKUA, AND IF I CAN'T PLEASE TELL ME TO
EAT SHIT

That code was just something I tapped into the browser, and I changed it partway through when I realised a problem so yeah, it's gonna be dodgy

Ok basically I wrote a parser to run through a config file, which is organised into section groups and then objects within each group. The parser needs to process the objects in each group, pulling out values into variables, handling missing required ones (by failing the object) and providing fallback defaults for missing optional values. Then there's some validity checking of types and other referenced objects, instance creation if everything's fine, mapping that object for anything that refers to it later and so on.

The groups do some iteration over all their objects and handle success and failure depending on what happened with the objects, and propagate that back up. Also objects can sometimes contain groups!

There's a basic structure going on here and a lot of logic repetition in each section, which I really don't like, so I'm having a go at breaking this out into a generic group class and a generic object class, that can handle the basic 'get all the things, fail for missing required things, check IDs aren't already mapped'-style logic. The part I was specifically asking about was being able to list a group of variables in the specific subclass, and have the superclass handle actually populating everything in that group. The mapping was to the JSON name key that the local field should be populated with. Right now I have something like this:

Java code:
public class TextureObjectParser extends ObjectParser {
	
	// assigned from JSON
	public String typeId, name;
	public Integer objectId;
	public JSONArray params;

	@Override
	protected void setFieldsToParse() {
		setRequiredField("objectId", 	KEY_TEXTURE_ID);
		setRequiredField("typeId",  	KEY_TEXTURE_TYPE);
		setRequiredField("params",  	KEY_TEXTURE_PARAMS);

		setOptionalField("name", 	KEY_TEXTURE_NAME);		
	}
}
and later those fields are actually used in the logic (objectId is the unique identifier that's defined in the superclass). The code in the superclass is

Java code:
public abstract class ObjectParser {

	protected boolean setRequiredField(String fieldName, String jsonKey) {
		try {
			requiredVariablesToKeys.put(this.getClass().getField(fieldName), jsonKey);
		} catch (NoSuchFieldException e) {
			// couldn't find the field
			return false;
		}
		return true;
	}

	protected boolean getElementValues(JSONObject objectDef) {
		try {
			/*
			 * Loop through each set of fields (mapped by the subclass), and attempt
			 * to get and set the associated value from the JSONObject.
			 */
			// required objects
			for (Field field : requiredVariablesToKeys.keySet()) {
				String elementKey = requiredVariablesToKeys.get(field);
				try {
					field.set(this, objectDef.get(elementKey));
				} catch (IllegalAccessException e) {
					// couldn't access the field, fail the object
					return false;
				} catch (IllegalArgumentException e) {
					// couldn't get the expected type, fail the object
					return false;
				}
			}

			// optional objects
			for (Field field : optionalVariablesToKeys.keySet()) {
				/*
				 * Similar code but handles optional variables and defaults
				 */
			}
		
		} catch (JSONException e) {
			// should only fail when an expected object cannot be got
			return false;
		}
		
		// got this far, return success
		return true;
	}

Things like required name/value pairs, valid values, fallback defaults etc are all handled in a spec class, so the idea is to really keep all that in one place, let the logic do validation and setting based on that data, and any changes or extensibility just involves tweaking the spec and whatever code makes use of the change. Also none of this is dynamic - it's just meant to cut down on code reproduction and help with validation

I'm (surprise!) learning as I go with this so I'm fully ready to hear this is a terrible idea, but hopefully you get what I'm driving at at least. (The 'get the JSON data' bit isn't final yet either, which is why I was talking about calling 'the appropriate method' - a method or logic that handles and returns the correct type, basically).



tl;dr I want to programmatically assign values to arbitrary variables. In case I have caused you pain in the heart, here are some cat yawns

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply