John 3:16
Jayesh A Lalwani wrote:It's good to keep all strings in one place. It's better to keep it in a resource file outside of your java class. It makes it easier to intertionalize your program. You can easily switch your language from English to say French by changing your resource file. ALso, it makes it easier for non-programmers to translate the file for you. Look at Resource Bundles to learn how to do this in Java
If you have final static variables that have to be used outside a class, the convention is to make them public. The reason why you want data members to be private is a) you want to hide data which makes it easier to change your implementation and b) you want to control access; ie; you want to control which classes can modify the member. Well, by definition static final members aren't extensible, and no one can modify them. So, there is no advantage in hiding the members behind methods. You might as well make them public.
John 3:16
Jordan D. Williams wrote:3. This might be OK for a small program since it has 20-30 strings. How would doing something like this impact a program that has thousands or tens of thousands of strings?
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:
Jordan D. Williams wrote:3. This might be OK for a small program since it has 20-30 strings. How would doing something like this impact a program that has thousands or tens of thousands of strings?
Just to add to Jayesh's good advice: for very large numbers of messages you may want to think about storing them in a database.
For one thing, DB's such as JavaDB can actually be deployed with the application; for another, it makes you think about your messages as having a 'structure', just one example being the messages produced by a database like Oracle, which have (a) a unique ID and (b) a category (error, warning, information etc.).
Winston
Jayesh A Lalwani wrote:Just to add one more thing:- Whether you put the messages in a property file or a database, they will have to be keyed. You will need a key to get to the message.
For example you will have a property file that says
The java code will need the keys to get to the message. You should put the keys in public static final variables
John 3:16
Jordan D. Williams wrote:Thanks for the good advice Winston! I think still very far away from creating such a program, but good information is always useful. What would you say is a "very large number of strings"?
Does this look like a good implementation? Please, don't be afraid to tell me I am doing it all wrong if that's the case. it is my first try...
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:
Jordan D. Williams wrote:3. This might be OK for a small program since it has 20-30 strings. How would doing something like this impact a program that has thousands or tens of thousands of strings?
Just to add to Jayesh's good advice: for very large numbers of messages you may want to think about storing them in a database.
For one thing, DB's such as JavaDB can actually be deployed with the application; for another, it makes you think about your messages as having a 'structure', just one example being the messages produced by a database like Oracle, which have (a) a unique ID and (b) a category (error, warning, information etc.).
Winston
Dennis Deems wrote:It's rare that I disagree with the advice Winston gives, but here I do. Text is notoriously volatile, because it can often be quite difficult to arrive at the desired wording. That's why larger companies tend to employ non-technical people who are good with words to take care of their text...What's the overhead compared to using the resource bundle?
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:
...
I'd say: Not bad for a first cut; but a bit procedural...
...
A couple things to think about:
What about a simple generic message parser that can plug in "parameterized" (ugh) values to a message in a similar way to an SQL PreparedStatement or String.format() (indeed, the latter may be an excellent way to write it). Whatever piece of code is actually displaying the message must know what it's displaying (because it has to know the key), and therefore what parameters to plug in. The content of your messages would suggest that you want to support different types of "equation". I would definitely implement these as separate classes, with maybe an interface (Equation?) that ties them together.
HIH
Winston
Winston Gutkowski wrote: ... But, in the context of Jordan's program, you're probably right: a database is overkill.
John 3:16
Winston Gutkowski wrote:
Dennis Deems wrote:It's rare that I disagree with the advice Winston gives, but here I do. Text is notoriously volatile, because it can often be quite difficult to arrive at the desired wording. That's why larger companies tend to employ non-technical people who are good with words to take care of their text...What's the overhead compared to using the resource bundle?
Yes I see your point, but I think it does depend on the complexity of the text involved - particularly if there are any semantic rules or hierarchy.
I HATE (and I do mean HATE) XML; but I can see its uses for very large and complex configurations, and it's better for non-volatile stuff. To me, if you have several thousand messages that need maintaining you should write software to do it, not rely on the nous of a junior person who will probably be the scapegoat if anything naughty happens.
But, in the context of Jordan's program, you're probably right: a database is overkill.
Winston
John 3:16
Jordan D. Williams wrote:On another note... Why do you hate XML? Seems very useful and relatively simple to use.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Jordan D. Williams wrote:I have to agree with you on the code being procedural. I started with procedural languages and I am still trying to wrap my head around OOP.
To be honest, I have absolutely no clue how to eliminate that code and make it more object oriented.
And yes, I have a class for each equation type and an Equation interface that implements a solve() method that takes varargs parameters so it can deal with different equations as necessary. I don't know if that clarification help my code look not-so-procedural .
I also have to say that the idea about a properties file was not mine. The idea was given to me by Jayesh A Lalwani in the second post of this thread. I had no idea the ResourceBundle class existed before that, so all credit goes to him about that one. I did do the research and figured out how to use it though .
My idea was to practice writing software in a way that is designed to be easily localized. I don't know if ResourceBundle is the best option, but I had to start somewhere.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Jordan D. Williams wrote:To be honest, I have absolutely no clue how to eliminate that code and make it more object oriented.
Junilu Lacar wrote:
Jordan D. Williams wrote:To be honest, I have absolutely no clue how to eliminate that code and make it more object oriented.
This might be a little too soon for you but the general process is called Refactoring. Here's the one in particular that you want to perform to eliminate the problem (aka "code smell") that Winston talked about: http://sourcemaking.com/refactoring/replace-conditional-with-polymorphism. But then again, it doesn't look like you're a newbie programmer, just OO concepts, so it might make perfect sense to you when you read it.
Winston Gutkowski wrote: ... It took me close to 8 years (and two OO languages) before my "Eureka" moment, and I regard myself as reasonably intelligent. ...
Winston Gutkowski wrote:
Phew. End of rant.
Winston Gutkowski wrote:
3. Try 'em out, in order of importance; first one gets.
John 3:16
Junilu Lacar wrote:Here's the one in particular that you want to perform to eliminate the problem (aka "code smell") that Winston talked about: http://sourcemaking.com/refactoring/replace-conditional-with-polymorphism.
"The good news about computers is that they do what you tell them to do. The bad news is that they do what you tell them to do." -- Ted Nelson
Jordan D. Williams wrote:
How exactly do I implement that? I know a little a bit about interfaces, but I am not sure I know how to do that... Specifically, how can I test if the "first" (most important) class can figure out if it "understands" what has been given to it?Winston Gutkowski wrote:3. Try 'em out, in order of importance; first one gets.
Do you mean by using a switch statement rather than the traditional procedural logic of if-then/if-then-else?
By the way, all the equation classes will take double coefficients as parameters. And in the next version, maybe they will take BigDecimal as coefficients, since I have been lead to believe from other discussions on this forum that it's more precise that double. Is that correct?
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Jordan D. Williams wrote:Wow... You really DO hate XML! Some of your reasoning I did not understand, but what I did understand sounds like big holes in their design.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote: ... if you add the canSolve(String) method I suggested, each of your Equation types will "know" whether it can solve 'problem' or not, so:Job done. Your "Equation management" code (or class) doesn't need to know anything about the type of problem supplied; it simply needs to know how to find the right Equation to solve it. It's the job of the Equation class to "know" whether it can solve a particular problem or not.
Does that make it any clearer?
...
Winston
John 3:16
Jordan D. Williams wrote:... OK when I write it out that way it does look like a lot like a switchboard...
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote: If you follow me so far, let me know and we can go on to the slightly trickier canSolve() → solve() idea.
John 3:16
Jordan D. Williams wrote:The way I had it before was like this:
1. Ask the user what type of equation they want to solve.
2. Ask them to enter the appropriate coefficients based on the selection.
3. Based on their selection, have a switch routine that will creates the appropriate Object that knows how to solve the equation...
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Jordan D. Williams wrote:...using a CoeffGetter.class that can get any number of coefficients...
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:
Jordan D. Williams wrote:...using a CoeffGetter.class that can get any number of coefficients...
I just read this bit, and there's absolutely no problem with that, apart from it possibly being difficult to create custom prompts (and that's where your selector.properties file might help), but again, all Quadratic.getValues() needs to do is call that class's method, viz (from above):Make sense?
Winston
John 3:16
Jordan D. Williams wrote:I have to say that looking at your example code blew my mind. It took me about an hour to understand what was going on in there.
Could you explain, or give me e reference, a little bit about that whole TEMPLATE thing? And how can the Manager class have an equations field of type Equation that is an array?
Basically the manager class, when instantiated creates an array of that contains values of type Equation and references the TEMPLATE field in each equation type, that in turn is a reference to instantiate an object of that give equation type? That is why the TEMPLATE field is static - it has to be accessible from outside the class, without have an object instantiated first. TEMPLATE is public since it has to be visible to the Manager class. We don't want anyone to be able to change the value of it since it will break the program. Therefore you also make the TEMPLATE field final.
What confuses me is that you never instantiate the Manager calss?! Did you just forget to instantiate it? I highly doubt that! ...
The other thing that confuses me is how can you reference a field inside of a class and then have it instantiate an object of the same class? Is it because it is static final (static making it a class field rather than a instance field, and final making it immutable and thereby making it [arguably] a unique field)?
Thanks for all your time that you are investing in me, Winston (and everyone else!). I really appreciate it.
By the way.. I am on Eastern Standard Time (UTC -05:00)
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:
I think you may be getting close to your "Eureka" moment
To be honest, it's one of those times when you wish you could define static methods in an interface, because it would be nice to be able to enforce the "template" strategy on all Equations, but without using something like a Factory pattern (you'll find out about patterns later), it'd be quite difficult.
Basically, the presence of the main() method allows it to be called from the command line, viz:
java [-jar whatever] Manager
and it gets automatically loaded by the JVM.
No probs. That's what we're here for; and it's particularly nice for us when
(a) people listen.
(b) they start to "get it"; and it sounds like you are.
Daylight I hope; everybody else is.
I'm on BST (Belgian Summer Time (UTC +02:00)), so I guess we'll just have to keep an eye out for messages.
John 3:16
Jordan D. Williams wrote:I have actually heard of design patterns. Junilu Lacar referenced this site and it talks about all kinds of things like ULM design, design patterns, refactoring, etc. It's not Java specific, and it's a bit too advanced for me right now, but it's a great source of information.
Would that also work if I compile/run in Eclipse? Everything I have read so far only talks about the main() method being executed at launch.
Well, '(b)' might take a while, but given enough time I would like to believe that anything can get through my thick, procedurally brainwashed mind!
Thanks again for everything. I will post the code with your implementation as soon as I have it ready... It might take a while!
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Jordan D. Williams wrote:
Winston Gutkowski wrote:I'm on BST (Belgian Summer Time (UTC +02:00)), so I guess we'll just have to keep an eye out for messages.
It's really called Belgian Summer Time? That's awesome! Sounds 'fun'! Here.. meh... It's just 'Standard' time... Nothing special about it! ;)
Mike Simmons wrote:
Actually, no one uses Eastern Standard Time right now. You are using Eastern Daylight Time - what we call daylight saving, they call summer time. For programmers, it's useful to be aware of the distinction.Jordan D. Williams wrote:
It's really called Belgian Summer Time? That's awesome! Sounds 'fun'! Here.. meh... It's just 'Standard' time... Nothing special about it! ;)Winston Gutkowski wrote:I'm on BST (Belgian Summer Time (UTC +02:00)), so I guess we'll just have to keep an eye out for messages.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Mike Simmons wrote:Mmm, I contend that the time zone is not actually Eastern Standard Time; it's the Eastern Time Zone. Java's library is misleading...
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:
Mike Simmons wrote:Mmm, I contend that the time zone is not actually Eastern Standard Time; it's the Eastern Time Zone. Java's library is misleading...
You may be right, but I fear that history, mnemonics and the 'Net are against you (your own link refers to a page called 'Eastern_standard_time').
Winston Gutkowski wrote:The name "Eastern Time Zone" is totally ambiguous anyway
Winston Gutkowski wrote:unless you're entirely US-centric
Winston Gutkowski wrote:I'm also pretty sure that the official designation in the TZ libs is 'EST'.
Mike Simmons wrote:
Winston Gutkowski wrote:I'm also pretty sure that the official designation in the TZ libs is 'EST'.
If you do TimeZone.getTimeZone("EST"), you get a TimeZone that (a) also identifies itself as "Eastern Standard Time", but (b) does not indulge in any daylight savings nonsense. Because, ahem, that wouldn't be EST, would it?
The docs do correctly warn against using those three-letter codes, since there's ambiguity whether (for example) CST should be Central Standard Time, or China Standard Time. (Ignoring the further ambiguity about "central".) They don't address the further (and IMO worse) ambiguity from people who request "EST" without knowing what that actually means, and expect it to use DST.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Junilu Lacar wrote:I've heard a lot of talk about Joda Time lately. Maybe it's time (excuse the pun) to check it out.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:PS: Sorry Jordan, we've got totally off-topic here.
Winston
John 3:16
Jordan D. Williams wrote:By the way I did read your 'Eureka' moment paper and I am sorry, but I will have to disappoint you... Most of it went over my head! . I can't really go into details about that now, since I am pressed by time, but soon...
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:
Mike Simmons wrote:The docs do correctly warn against using those three-letter codes, since there's ambiguity whether (for example) CST should be Central Standard Time, or China Standard Time. (Ignoring the further ambiguity about "central".) They don't address the further (and IMO worse) ambiguity from people who request "EST" without knowing what that actually means, and expect it to use DST.
Don't know if I agree with you there: Time zones are geographic.
Winston Gutkowski wrote:DST is simply an implementation (usually national policy, but not always; check out Saskatchewan time),
Winston Gutkowski wrote:so it seems to me that if you're going to key on anything, it should be the base time zone (which is the way tz does (or at least did) it), and that base time zone should contain information about any DST/regional variants.
Winston Gutkowski wrote:Also, as you say, people are familiar with 'EST' as a mnemonic. Do you know the mnemonic for "Eastern Time"?
Winston Gutkowski wrote:There is one, and it looks suspiciously like a tz record key to me.
Winston Gutkowski wrote:Seems to me that splitting hairs like that (even if "correct") is going to cause more problems than it solves, particularly as it's not consistent.
Winston Gutkowski wrote:'WET' does use daylight savings, despite the fact that in the summer it's called WEST, which isn't even recognized by java.util.TimeZone.
Winston Gutkowski wrote:[EDIT]: Sorry Mike, couldn't resist this: What do you get when you execute
TimeZone.getTimeZone("EST5EDT").getDisplayName()?
Maybe he went home and went to bed. And took this tiny ad with him:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
|