|
Thread Rules 1. This is not a "do my homework for me" thread. If you have specific questions, ask, but don't post an assignment or homework problem and expect an exact solution. 2. No recruiting for your cockamamie projects (you won't replace facebook with 3 dudes you found on the internet and $20) 3. If you can't articulate why a language is bad, don't start slinging shit about it. Just remember that nothing is worse than making CSS IE6 compatible. 4. Use [code] tags to format code blocks. |
On September 13 2013 01:06 supereddie wrote:Show nested quote +On September 12 2013 10:34 berated- wrote: Hmm. This seems like bad advice. Why would you want to take the time that your js works perfectly in every browser when most web devs are doing it for you by using jquery? Use a cdn, use gzip compression, its not that big.
This particular problem can be easliy solved with standard javascript that is supported in all browsers. There are no browser-specific functions needed - just standard javascript. You don't need external files, libraries etc. Every function is builtin in the javascript engine in a browser. It is complete overkill to use jQuery for this simple problem.
Absolutely, as well as many other problems if you look at them under a microscope. I think it's a big assumption to think that this will be the only problem you need to solve on a website.
|
On September 13 2013 06:45 darkness wrote: I was wondering how variables' scope is dealt with in regards to Java GUI. Is it global or do you need to declare everything as a field? All in all, how you deal with variables? Examples would be nice if you have nearby.
There are only a couple scopes for java and they all work the same whether you are doing a main method, server programming, or GUI programming, so I'm not quite sure why you specify in regards to GUI? Do you have an example of where the confusion comes in, that might be easier than people just coming up with examples for you.
Generally speaking your life will be better if you figure out how to develop without a dependence on static variables, unless the variables are truly static (like constants). It's not that statics are (all) bad, they generally just lead to harder to test code because of the lack of seams for dependency injection and can lead to trouble in multithreaded environments if you don't have a good understanding of what is happening.
|
On September 13 2013 11:16 berated- wrote:Show nested quote +On September 13 2013 06:45 darkness wrote: I was wondering how variables' scope is dealt with in regards to Java GUI. Is it global or do you need to declare everything as a field? All in all, how you deal with variables? Examples would be nice if you have nearby. There are only a couple scopes for java and they all work the same whether you are doing a main method, server programming, or GUI programming, so I'm not quite sure why you specify in regards to GUI? Do you have an example of where the confusion comes in, that might be easier than people just coming up with examples for you. Generally speaking your life will be better if you figure out how to develop without a dependence on static variables, unless the variables are truly static (like constants). It's not that statics are (all) bad, they generally just lead to harder to test code because of the lack of seams for dependency injection and can lead to trouble in multithreaded environments if you don't have a good understanding of what is happening.
I'm just puzzled how variables are usually passed when it comes to Java GUI. Do you declare them as fields? Is that the general practice? Or via object references? Is MVC "the way to go" or simple stuff like class ButtonDemo_Extended from http://www.codeproject.com/Articles/33536/An-Introduction-to-Java-GUI-Programming good enough? Should I also use IDE to develop GUIs?
|
On September 14 2013 07:51 darkness wrote:Show nested quote +On September 13 2013 11:16 berated- wrote:On September 13 2013 06:45 darkness wrote: I was wondering how variables' scope is dealt with in regards to Java GUI. Is it global or do you need to declare everything as a field? All in all, how you deal with variables? Examples would be nice if you have nearby. There are only a couple scopes for java and they all work the same whether you are doing a main method, server programming, or GUI programming, so I'm not quite sure why you specify in regards to GUI? Do you have an example of where the confusion comes in, that might be easier than people just coming up with examples for you. Generally speaking your life will be better if you figure out how to develop without a dependence on static variables, unless the variables are truly static (like constants). It's not that statics are (all) bad, they generally just lead to harder to test code because of the lack of seams for dependency injection and can lead to trouble in multithreaded environments if you don't have a good understanding of what is happening. I'm just puzzled how variables are usually passed when it comes to Java GUI. Do you declare them as fields? Is that the general practice? Or via object references? Is MVC "the way to go" or simple stuff like class ButtonDemo_Extended from http://www.codeproject.com/Articles/33536/An-Introduction-to-Java-GUI-Programming good enough? Should I also use IDE to develop GUIs?
I think all of these questions are dependent upon what you are trying to accomplish. There are always tradeoffs that you have to consider -- there isn't always a right or wrong answer, more of a what is best for my situation.
I personally do more java webapp development, so, I can't speak with 100% certainty, but MVC is a pattern that I enjoy using. It logically makes a lot of sense and allows for your code to be better modularized. Learning how to write that way would certainly extend past your current development.
As to whether the simple example is good enough, what are you trying to do? Are you the only one working on it? If so, then it's probably fine. The more complex that software gets, the more you are going to wish your code parts are a little less coupled so that you can get the amount of reuse and maintainability you are looking for. However, if you are never going to get to that level of complexity, why waste time setting up code infrastructure when you could have been done with the project much faster.
Do you want to use an IDE? The few times I've played with swing, I've just used netbeans because I have no vested interest in becoming an expert in it. I wanted to be able to do something quick and get it running, not so much learn all of the intricacies of everything.
|
On September 12 2013 01:59 HardlyNever wrote: Criticizing a working solution, without posting a better working solution, is definitely less constructive.
Dude if you take people criticizing your code so personally you're going to have a heart attack by the time you're 35.
|
Hey guys, I just basically started programming, but I have trouble understanding some minor things
-Parameters, (passing down?!, Why it's used, why not just use globally, which to me makes it easier/simpler?, how to apply in the method if used?, etc) When I look it up and try to understand online definition,examples, explanations, they are hard to understand...
-Use of "this.", "constructor", etc. (I feel like I'm really weak on method things T_T). -All I know in java is... Scanner,loops, if statements, making method,calling them but without parameters(T_T), and alittle of arrays. -At school we're just doing Karel the Robot things, At the moment, creating Ellipse Circle etc.
If you can answer this in a easy-understanding way it'd be awesome! you can pm me or write here. Also if you can also have like 1:1 chat with me it'd be awesome too. Thanks!
|
On September 14 2013 07:51 darkness wrote:Show nested quote +On September 13 2013 11:16 berated- wrote:On September 13 2013 06:45 darkness wrote: I was wondering how variables' scope is dealt with in regards to Java GUI. Is it global or do you need to declare everything as a field? All in all, how you deal with variables? Examples would be nice if you have nearby. There are only a couple scopes for java and they all work the same whether you are doing a main method, server programming, or GUI programming, so I'm not quite sure why you specify in regards to GUI? Do you have an example of where the confusion comes in, that might be easier than people just coming up with examples for you. Generally speaking your life will be better if you figure out how to develop without a dependence on static variables, unless the variables are truly static (like constants). It's not that statics are (all) bad, they generally just lead to harder to test code because of the lack of seams for dependency injection and can lead to trouble in multithreaded environments if you don't have a good understanding of what is happening. I'm just puzzled how variables are usually passed when it comes to Java GUI. Do you declare them as fields? Is that the general practice? Or via object references? Is MVC "the way to go" or simple stuff like class ButtonDemo_Extended from http://www.codeproject.com/Articles/33536/An-Introduction-to-Java-GUI-Programming good enough? Should I also use IDE to develop GUIs? Unless its a totally trivial GUI, you usually write event handlers using anonymous inner classes. When you instantiate the event handler, it gets linked to a particular control in your UI (p.addActionListener(...) in this case), and when the event occurs, the code in the event handler gets triggered. This code can usually pass variables around like any other code.
This code declares an Action Listener which handles opening a file browser for a Sudoku game I built for a college project. The program was written MVC-style, this is a snippet from the Controller. The vairable "storedBoard" is the Model, which is basically just a big string representing the current state of the puzzle. When the "Open" menu item is chosen, a standard Java file browser is opened, the user picks a file, and assuming that nothing goes wrong (broken file or whatever), that file gets passed to the Model via the loadFile(File) method. "storedBoard.emptyStacks();" resets the undo/redo feature for the newly loaded puzzle.
p = new JMenuItem("Open"); p.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent arg0) { storedBoard.getModel().stop(); String filename = File.separator+"tmp"; JFileChooser fc = new JFileChooser(directory);
fc.showOpenDialog(boardFrame); File selFile = fc.getSelectedFile(); if (selFile != null) { storedBoard.loadFile(selFile); storedBoard.emptyStacks(); } } });
There's about a dozen more of these little snippets in my program, each one calling different methods in the Model or View depending one what menu item it handles. I provided visibility to both the Model and the View first thing at run-time. My Main method was a separate class that simply instantiated the Model, and the View, then passed them both to the Controller via the constructor. The Controller just stores them as class variables.
|
On September 15 2013 03:45 TimKim0713 wrote: Hey guys, I just basically started programming, but I have trouble understanding some minor things
-Parameters, (passing down?!, Why it's used, why not just use globally, which to me makes it easier/simpler?, how to apply in the method if used?, etc) When I look it up and try to understand online definition,examples, explanations, they are hard to understand...
Using global variables like this is generally considered bad because when programs start to get big, it is hard to keep track of them all. This could lead to a situation where you accidentally reuse some global variable somewhere, and it will be a huge pain to track down why a problem is happening. Parameters avoid this by having what you call a scope, you can access them only inside the function, when the function ends/returns, they will go away, and they are all declared at the top of each function which tells you right away what you have to work with in case someone else wrote the code.
Global variables can be nice in small programs, but they can make writing even slightly complicated things a pain.
-Use of "this.", "constructor", etc. (I feel like I'm really weak on method things T_T).
"this" is used to refer to the current object. Say you have an apple class and an apple named myApple. If you were to call a function pick(), you could do myApple.pick(); But what if you were in that pick function and wanted to pass the object "myApple" to some other function of a different class? You could do myOtherObject.otherFunction(this); to pass it. There are other uses of "this" where you use it to increase readability and make clear what functions or variables you are refering to as well such as calling a second apple function inside of the first apple function, or accessing the object's variables inside a function.
A constructor is how you create a new object. When you want to make a new object, the constructor is what the program will look for to know how to create that new object.
If you just started a CS class, this stuff may not have been fully explained yet so I hope I have not confused you. Overall I found writing code to be the best way to understand how these things worked and why I would want to use them.
|
On September 15 2013 03:45 TimKim0713 wrote: -Parameters, (passing down?!, Why it's used, why not just use globally, which to me makes it easier/simpler?, how to apply in the method if used?, etc) When I look it up and try to understand online definition,examples, explanations, they are hard to understand...
In small projects, yes, parameters seem to be a hassle. However, once a program becomes bigger, it becomes important to know when things happen, e.g. when and where a variable was set to a value, when a database connection was established, etc. With globals, a function never knows if all the variables really exist and a programmer using a function never knows what variables that function actually changes. You can't always go through all functions you use and check what they really access, so by using parameters and no global variables, you always know that it only affects what you give it to affect, nothing more. You don't have any sudden side effects messing with the rest of the program.
Every function should always just be dependant on the parameters it is given (and class variables it has access to) and otherwise be independent from the environment it is run in since it's often very helpful to run a function on it's own to test for problems, even without explicit unit tests, and if you first need to initialize a state you can't know without first digging into the function, creating such independent tests becomes a huge problem.
We have a similar situation in the company i just started working in. The code heavily relies on singletons, which look like objects but are actually sort of global variables with lazy loading and while some of the developers would love to add unit tests, it's simply impossible because almost every method is dependant on other code running first and you can't set up any test without first running basically the whole project first and it's almost impossible to refactor it into a better form.
"this.", constructors, destructors, etc. are fairly simple to understand once you understand the reasoning behind it. In modern programming, the above mentioned independency of functions is a core idea that is also used for methods and classes.
A class should always be in a valid state, meaning if you need to run it's methods in a specific order, it's poorly programmed and a trap for any future programmer working with it. A constructor serves to set up a valid state by taking all neccessary parameters to get it into that state and then doing the neccessary initialization so other methods don't have to check the state every time they are called.
Class variables are accessible to all methods in the class, so it's easy to accidently use a variable name used in the class a second time in a method. To avoid conflicts, the "this" keyword makes sure even if you accidently used a variable name twice, it uses the one you meant to use, i.e. the one from the class, not the one inside the method. As an example, if your class does something with a "CustomerList", it might have a parameter "customerList" in the constructor and a class variable "customerList" and to set the class variable in the constructor, you can simply do "this.customerList = customerList". Personally, i strongly recommend always using this when accessing class variables, even when whatever language you use doesn't explicitely require it, just to avoid future pitfalls. With most IDEs it's not even more to type.
|
@DeltaX and Morfildur, thank you so much!
|
On September 15 2013 03:51 Millitron wrote:Show nested quote +On September 14 2013 07:51 darkness wrote:On September 13 2013 11:16 berated- wrote:On September 13 2013 06:45 darkness wrote: I was wondering how variables' scope is dealt with in regards to Java GUI. Is it global or do you need to declare everything as a field? All in all, how you deal with variables? Examples would be nice if you have nearby. There are only a couple scopes for java and they all work the same whether you are doing a main method, server programming, or GUI programming, so I'm not quite sure why you specify in regards to GUI? Do you have an example of where the confusion comes in, that might be easier than people just coming up with examples for you. Generally speaking your life will be better if you figure out how to develop without a dependence on static variables, unless the variables are truly static (like constants). It's not that statics are (all) bad, they generally just lead to harder to test code because of the lack of seams for dependency injection and can lead to trouble in multithreaded environments if you don't have a good understanding of what is happening. I'm just puzzled how variables are usually passed when it comes to Java GUI. Do you declare them as fields? Is that the general practice? Or via object references? Is MVC "the way to go" or simple stuff like class ButtonDemo_Extended from http://www.codeproject.com/Articles/33536/An-Introduction-to-Java-GUI-Programming good enough? Should I also use IDE to develop GUIs? Unless its a totally trivial GUI, you usually write event handlers using anonymous inner classes. When you instantiate the event handler, it gets linked to a particular control in your UI (p.addActionListener(...) in this case), and when the event occurs, the code in the event handler gets triggered. This code can usually pass variables around like any other code. This code declares an Action Listener which handles opening a file browser for a Sudoku game I built for a college project. The program was written MVC-style, this is a snippet from the Controller. The vairable "storedBoard" is the Model, which is basically just a big string representing the current state of the puzzle. When the "Open" menu item is chosen, a standard Java file browser is opened, the user picks a file, and assuming that nothing goes wrong (broken file or whatever), that file gets passed to the Model via the loadFile(File) method. "storedBoard.emptyStacks();" resets the undo/redo feature for the newly loaded puzzle. p = new JMenuItem("Open"); p.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent arg0) { storedBoard.getModel().stop(); String filename = File.separator+"tmp"; JFileChooser fc = new JFileChooser(directory);
fc.showOpenDialog(boardFrame); File selFile = fc.getSelectedFile(); if (selFile != null) { storedBoard.loadFile(selFile); storedBoard.emptyStacks(); } } }); There's about a dozen more of these little snippets in my program, each one calling different methods in the Model or View depending one what menu item it handles. I provided visibility to both the Model and the View first thing at run-time. My Main method was a separate class that simply instantiated the Model, and the View, then passed them both to the Controller via the constructor. The Controller just stores them as class variables.
Yeah, I've checked some MVC code and I like how the constructor is used in this case. However, what does this mean?
storedBoard.getModel().stop();
Variable.method.method?
|
Yeah, I've checked some MVC code and I like how the constructor is used in this case. However, what does this mean? storedBoard.getModel().stop(); Variable.method.method?
What it means is that storedBoard.getModel() is returning an Object, on which he is calling the stop method of. This is probably a variable of storedBoard called Model.
|
|
On September 15 2013 09:11 dae wrote:Show nested quote +Yeah, I've checked some MVC code and I like how the constructor is used in this case. However, what does this mean? storedBoard.getModel().stop(); Variable.method.method? What it means is that storedBoard.getModel() is returning an Object, on which he is calling the stop method of. This is probably a variable of storedBoard called Model.
Right, thanks. Is this considered 'ok to use'? Isn't it less readable if you skip a line or two like that?
For example, isn't this slightly more readable:
Object thatReturnedObject;
thatReturnedObject = storedBoard.getModel(); thatReturnedObject.stop();
|
On September 15 2013 09:23 darkness wrote:Show nested quote +On September 15 2013 09:11 dae wrote:Yeah, I've checked some MVC code and I like how the constructor is used in this case. However, what does this mean? storedBoard.getModel().stop(); Variable.method.method? What it means is that storedBoard.getModel() is returning an Object, on which he is calling the stop method of. This is probably a variable of storedBoard called Model. Right, thanks. Is this considered 'ok to use'? Isn't it less readable if you skip a line or two like that? For example, isn't this slightly more readable: Object thatReturnedObject; thatReturnedObject = storedBoard.getModel(); thatReturnedObject.stop();
Generally it's just a bit aggravating writing three lines where you could be doing one, and most people who've worked in an OO language for a little while will understand that a method called getX is probably just returning you the object X. It doesn't hurt your clarity that much at all.
|
On September 15 2013 09:31 Cyx. wrote:Show nested quote +On September 15 2013 09:23 darkness wrote:On September 15 2013 09:11 dae wrote:Yeah, I've checked some MVC code and I like how the constructor is used in this case. However, what does this mean? storedBoard.getModel().stop(); Variable.method.method? What it means is that storedBoard.getModel() is returning an Object, on which he is calling the stop method of. This is probably a variable of storedBoard called Model. Right, thanks. Is this considered 'ok to use'? Isn't it less readable if you skip a line or two like that? For example, isn't this slightly more readable: Object thatReturnedObject; thatReturnedObject = storedBoard.getModel(); thatReturnedObject.stop(); Generally it's just a bit aggravating writing three lines where you could be doing one, and most people who've worked in an OO language for a little while will understand that a method called getX is probably just returning you the object X. It doesn't hurt your clarity that much at all.
Adding to this: If you're intending to use the object for quite a while (call multiple of its functions etc. afterwards) it might be better to read using the temporary variable to interact with it. But in a case where you only call 1 or 2 methods it's clear what you mean if you use the short form.
|
On September 15 2013 09:23 darkness wrote:Show nested quote +On September 15 2013 09:11 dae wrote:Yeah, I've checked some MVC code and I like how the constructor is used in this case. However, what does this mean? storedBoard.getModel().stop(); Variable.method.method? What it means is that storedBoard.getModel() is returning an Object, on which he is calling the stop method of. This is probably a variable of storedBoard called Model. Right, thanks. Is this considered 'ok to use'? Isn't it less readable if you skip a line or two like that? For example, isn't this slightly more readable: Object thatReturnedObject; thatReturnedObject = storedBoard.getModel(); thatReturnedObject.stop(); The readability depends on the particular method where it is used it may well be that storeBoard.getModel().stop() is more readable since you don't have to track out what the thatReturnedObject is as in the second example, if the get call was way earlier in the method.
Also what people that learn to program dont realize is that when you start to work you end up writing THOUSANDS and thousands of lines of code, and if I have to type 3 lines instead of one over months time spending writing those useless lines could be spend doing something else. This is why people that go from java to scala for example feel more productive since the language allows you to do more with less sytax.
|
Alright. Thank you for explanation.
|
I've been wondering how one can keep record of unlimited number of users in a struct way fashion in Java.
For example,
class Person { private String name; private String surname; private int age;
Person(String name, String surname, int age) { this.name = name; this.surname = surname; this.age = age; }
// some getter methods public int getAge() { return age; }
// etc... and setter methods too public void setAge(int age) { this.age = age; } }
I know I can have a single record by having:
Person bill = new Person("Bill", "Gates", 57);
How can I have an unlimited number of people though? Is it possible without creating individual objects manually?
|
On September 16 2013 03:39 darkness wrote:I've been wondering how one can keep record of unlimited number of users in a struct way fashion in Java. For example, class Person { private String name; private String surname; private int age;
Person(String name, String surname, int age) { this.name = name; this.surname = surname; this.age = age; }
// some getter methods public int getAge() { return age; }
// etc... and setter methods too public void setAge(int age) { this.age = age; } }
I know I can have a single record by having: Person bill = new Person("Bill", "Gates", 57); How can I have an unlimited number of people though? Is it possible without creating individual objects manually? Use an array (or, for your purposes, an ArrayList would probably be more suitable because). Technically not "unlimited" because of hard-coded limitations on integer size, but unless you're planning to have a couple of billion entries it's not something you generally have to plan for.
Unless I've completely mistaken your question.
|
|
|
|