Computer Science Canada Instantiate an array of objects and class stuff |
Author: | Justin_ [ Sat Apr 08, 2006 12:56 pm ] | ||
Post subject: | Instantiate an array of objects and class stuff | ||
The main problem I'm having is on line 13, where I'm trying to instantiate 26 Memory objects. But the other problem is I'm not sure if I should have put the Memory class with my SmarterHoudini or if I should have put it in the implementation program which simply instantiates SmarterHoudini and calls the "ownThisMaze" function. import becker.robots.*;
|
Author: | Andy [ Sat Apr 08, 2006 1:07 pm ] | ||
Post subject: | |||
oh okay, i see what's going on. see when you had private Memory[] memory = new Memory[25];, you already instanciated the object. so when you cannot do memory[i](this); because you can only call the constructor when you're making the object..
that should work |
Author: | Justin_ [ Sat Apr 08, 2006 1:11 pm ] |
Post subject: | |
thanks, the keyword new confuses me a bit. In C++ it is used to declare memory allocated on the freestore, but I do not think this is the case in java. |
Author: | Justin_ [ Sat Apr 08, 2006 1:16 pm ] |
Post subject: | |
I have another question. In the Memory class, there is an array of objects called Directions. I want to make a method that wipes out the information stored in this array. How do I do this? Just the information, not the array itself. |
Author: | rizzix [ Sat Apr 08, 2006 1:20 pm ] | ||
Post subject: | |||
It's the same in java. this:
You can then fill it up with Memory objects, as you wish. Just think of arrays in java as Objects. Do not think of them as arrays in c++ (which are basically pointers). |
Author: | rizzix [ Sat Apr 08, 2006 1:29 pm ] | ||||
Post subject: | |||||
Justin_ wrote: I have another question. In the Memory class, there is an array of objects called Directions. I want to make a method that wipes out the information stored in this array. How do I do this?
Just the information, not the array itself. If your array is declared like this:
to get rid of the objects it contains simply fill the array with "null". Java's garbage collection will free those objects for you (if they are not refered to elsewhere in your code) so it would look something like this:
|
Author: | Justin_ [ Sat Apr 08, 2006 1:31 pm ] |
Post subject: | |
Aight. Was that also aimed at answering my question on how to delete the data in the Directions array? I need to delete the values but not the array itself. Since I don't know what Directions really are, this is why I'm confused about getting rid of the data. Also, I have to compare if a direction = a direction but since getDirection() returns a direction object I can't compare them. Is it possible for me to be able to create a method to compare them without being able to see what is inside Directions? |
Author: | Justin_ [ Sat Apr 08, 2006 1:32 pm ] |
Post subject: | |
Sorry I just got your latter post, please disregard all enquiries relating to the deletion of data in the directions array that took place in my last post. |
Author: | rizzix [ Sat Apr 08, 2006 1:34 pm ] |
Post subject: | |
Justin_ wrote: Also, I have to compare if a direction = a direction but since getDirection() returns a direction object I can't compare them. Is it possible for me to be able to create a method to compare them without being able to see what is inside Directions?
You can't compare something you can't see, there's got to be some kind of public method etc, that you can access so that you can compare "Directions". If there is, then it is possible. |
Author: | wtd [ Sat Apr 08, 2006 2:15 pm ] | ||
Post subject: | |||
Creating an object that happens to be assigned to an element in an array is no different from creating an object that will be bound to any other variable. How did you come up with:
|
Author: | wtd [ Sat Apr 08, 2006 2:46 pm ] | ||
Post subject: | |||
While I'm at it, how about some code formatting that doesn't make wtd want to gouge his eyes out with a rusty spork? ![]() Horizontal scrollbars are the devil's playthings.
Now... Line 10 concerns me. In this case, 25 is a magic number. Where does it come from? Oh, yes... it comes from the upper bound of the "memory" array. Well, how about we replace that with "memory.length"? If you go back and decide to change it in the future, you'll only have to change it once. Line 11 I have already referred to. Line 16: "scan" is called. It's supposed to return an int, but looking at the method, it never returns anything. Even if it did, what is the significance of the int it is returning? The method name isn't expressive enough to tell us that, nor is any intermediate assignment of the return value of "scan" to a variable used to tell us that information. The "proceed" method has control flow that fills me with horror. Since "waysOpen" is theoretically not modified by the methods called within the "proceed" method, there is no reason for separate conditional statements. They should be using "else", or perhaps even better... "switch". Line 34: where does this "ways" come from? Is this simply a typo? Using a switch statement would have made this mistake avoidable. General statement: In some places you explicitly prefix variable accesses and method calls with "this", and in others you do not. I suggest picking one style and sticking with it. Consistency counts for a lot. Line 57: the "scan" method. You have one piece of code repeated several times. This strikes me as redundant. You should probably factor it out into a separate method with an expressive name. Line 101: Why the empty constructor? Lines 110 and 116: Is that an extraneous semi-colon or are you trying to do something that's beyond me? General statement: with the amount of manipulation you do with the "directions" array, are you sure you do not want some resizeable container, like an ArrayList? |
Author: | wtd [ Sat Apr 08, 2006 3:37 pm ] |
Post subject: | |
As an additional note, I am convinced that functional programming would allow you to learn a great deal, if you are willing to leave behind (for a time) what you think you know about how to program. I am not insulting you with that suggestion, by the way. It was a very humbling experience for me, and yet, very rewarding. I would suggest in your case that you use Objective-Caml. |
Author: | Justin_ [ Sat Apr 08, 2006 9:39 pm ] |
Post subject: | |
The code there is just me writing code without testing it. I do appreciate the advanced insights, but the crap about syntax is not needed, I mean, a smart programmer should identify that this is code that has been untested. You strike me as a smart enough individual, so I must conclude that you harp on the syntactic errors and things that I have clearly displayed a knowledge of in previous posts, to be condescending rather than pedagogical. I had not even tried to compile this code at the time of posting it because I wasn't submitting a book to a publisher. Again, thank you for the advanced insight on programming architecture, just know that you make yourself look like a fool when you point out things that I have already displayed knowledge of. I hope in the future you will concentrate less on being a debugger. You seem to have the impression I think I am a good programmer. My opinion is that I am not nor will I ever be. |
Author: | wtd [ Sat Apr 08, 2006 9:56 pm ] |
Post subject: | |
If you understand the importance of writing code that has syntactic clarity, then you would do it. I do not harp on this to be condescending, but rather because it is absolutely crucial. The best debugger ever created is the human brain, but for that debugger to come into play, we have to be able to parse the code. Having well-formatted code is not something we do to please others... it's something we do to make our own lives easier. |
Author: | Justin_ [ Sat Apr 08, 2006 10:00 pm ] |
Post subject: | |
My code is flawlessly formatted when it is complete. When i copy and paste it into the forum it jarbles it, but this is beyond my control. My syntax is also flawless, when it is complete. If you like to excercise your debugger, do so, but do not point out things that I already know, it is condescending. It's like if Dan Brown gave me his unedited novel and asked me to read it to tell him what I thought about the characters, and I started circling all the punctuation errors and spelling errors that are clearly not spelling errors but typing errors. |
Author: | Justin_ [ Sat Apr 08, 2006 10:02 pm ] |
Post subject: | |
Granted, maybe I am expecting too much from your common sense. The reason I am bringing this up is because this is a consistent thing. You consistently harp on my typing errors. Maybe you do just lack common sense in which case I appologize. |
Author: | wtd [ Sat Apr 08, 2006 10:10 pm ] |
Post subject: | |
Justin_ wrote: When i copy and paste it into the forum it jarbles it, but this is beyond my control.
You can't edit plain text in a browser's textarea widget? If your code formatting is perfect when you're done, that means you must put a fair deal of time into rejiggering things. Why take the time to do it wrong, then take more time to fix it? Why not just do it right the first time? |
Author: | Justin_ [ Sat Apr 08, 2006 10:28 pm ] |
Post subject: | |
Okay man, I'll tell you what, since you never admit your wrong, we'll do it this way... I'll give an in depth analysis of which parts common sense should already tell you that I know. If you dispute any of this, I will then cite posts where I displayed working knowledge of them, taking time to ensure you posted in the same thread and said enough in those posts to indicate that you had looked over the code thoroughly. Line 10 concerns me. In this case, 25 is a magic number. Where does it come from? Oh, yes... it comes from the upper bound of the "memory" array. Well, how about we replace that with "memory.length"? If you go back and decide to change it in the future, you'll only have to change it once. ----this one is GOOD. Line 11 I have already referred to. ----BAD Line 16: "scan" is called. It's supposed to return an int, but looking at the method, it never returns anything. Even if it did, what is the significance of the int it is returning? The method name isn't expressive enough to tell us that, nor is any intermediate assignment of the return value of "scan" to a variable used to tell us that information. ----SO SO, YOU DIDN'T HAVE TO MENTION IT DIDIN'T RETURN ANYTHING, THAT WAS MERELY A TYPO I MADE. The "proceed" method has control flow that fills me with horror. Since "waysOpen" is theoretically not modified by the methods called within the "proceed" method, there is no reason for separate conditional statements. They should be using "else", or perhaps even better... "switch". ----GOOD Line 34: where does this "ways" come from? Is this simply a typo? Using a switch statement would have made this mistake avoidable. ---WAYS IS INDEED A TYPO, DAH; NICE TIP ABOUT THE SWITCH STATEMENT. General statement: In some places you explicitly prefix variable accesses and method calls with "this", and in others you do not. I suggest picking one style and sticking with it. Consistency counts for a lot. ----I PREFIX 'THIS' WHEN THE METHOD BELONGS TO THE CLASS BUT IS NOT DECLARED EXPLICITLY. THIS MAY OR MAY NOT BE BAD CODING PRACTICE BUT IT IS SOMETHING I PERSONALLY ADHERE TO. Line 57: the "scan" method. You have one piece of code repeated several times. This strikes me as redundant. You should probably factor it out into a separate method with an expressive name. ----THIS IS DUE TO THE FACT IT IS UNFINISHED. NOTE: THAT THIS IS ONE REASON YOU SHOULDN'T DEVIATE FROM WHAT THE TOPIC STARTER EXPLICITLY ASKS. Line 101: Why the empty constructor? ----UNFINISHED Lines 110 and 116: Is that an extraneous semi-colon or are you trying to do something that's beyond me? ----BAD BAD BAD BAD BAD BAD BAD General statement: with the amount of manipulation you do with the "directions" array, are you sure you do not want some resizeable container, like an ArrayList? ----OKAY, WELL I DON'T KNOW WHAT AN ARRAY LIST IS, NO HARM IN POINTING IT OUT TO ME BUT THE WAY YOU POINT THINGS OUT ALWAYS SEEMS TO BE IN A CONDESCENDING MANNER. EXAMPLE OF A COOLER WAY TO PHRASE IT: "Your direction array would be more suited for an ArrayList." Again, take it with a grain of salt, I still think you're a cool guy wtd, its just my pet peeve is guys who purposely try to make me look like an idiot... |
Author: | wtd [ Sat Apr 08, 2006 10:37 pm ] |
Post subject: | |
We're all idiots. There are many many people out there who terrify me with the level of knowledge and wisdom they possess with regards to programming. I've learned one thing that's more important than anything else: The first step in learning is humiliation. ![]() |
Author: | wtd [ Sat Apr 08, 2006 10:40 pm ] |
Post subject: | |
Oh, and I cannot get inside your head (yet...), so it's very hard for me to figure out when a mistake is a typo and when it is not. Errors are errors, and that includes the stupid ones. Having them pointed out is the first step in fixing them. |
Author: | Justin_ [ Sat Apr 08, 2006 10:46 pm ] |
Post subject: | |
I agree with your points, but you are missing out on a lesson here. I am teaching you how to help people more efficiently by being honest, not one of those "Oh thank you thank you" types. You help me, I tried to help you. Anyway, enough about this, life goes on... I am eating pizza yum. |
Author: | wtd [ Sat Apr 08, 2006 10:49 pm ] |
Post subject: | |
Did I lie somewhere? Should I help people by leting them make mistakes and think those mistakes are correct? Should I just tell people the correct way to do things without ascertaining an underlying reason the mistake was made? Would I really be helping them if I didn't help them gain a more solid fundamental grasp of the subject? |
Author: | [Gandalf] [ Sat Apr 08, 2006 11:25 pm ] |
Post subject: | |
Who's making you look bad? I'm positive wtd had no purpose to originally replying to this topic other than helping you (and others who may read this topic later on) out. Personally, I think it's you who: Justin_ wrote: are convinced [we're] out to get you. You're entirely dillusional though.
Justin_ wrote: My code is flawlessly formatted when it is complete. When i copy and paste it into the forum it jarbles it, but this is beyond my control.
The reason this happens is because your tabs are too large. I suggest you go into your editor/IDE's options and change them to something reasonable like 3 or 4 spaces. |
Author: | Justin_ [ Sat Apr 08, 2006 11:29 pm ] |
Post subject: | |
Gandolf wrote: The reason this happens is because your tabs are too large. I suggest you go into your editor/IDE's options and change them to something reasonable like 3 or 4 spaces. Thanks, I hate IDE's but they make me use them, that tip will come in useful in the future. |
Author: | apomb [ Sat Apr 08, 2006 11:54 pm ] | ||
Post subject: | |||
please, do us all a favour justin_ and admit that that was the way you were taught to format code... i was also taught to format my code the way you have it there.
so, thank you wtd, i was unaware that this way was incorrect. i am going to change the way i format my code. yes, i did look at your sig now, wtd =D oh and if you Justin_ wrote: When i copy and paste it into the forum it jarbles it, but this is beyond my control.
then, how, pray-tel, did wtd get his code to look so good? ![]() |
Author: | Dan [ Sun Apr 09, 2006 12:10 am ] |
Post subject: | |
Justin_ have you ever hured the expesion begers can't be choisers? When you are asking for help on a comunity like this you should be thankfull that you get your question awsered. Personaly i whould thinking that geting extera coments on my code and the way i am going about things whould be a bounes and that it whould help me learn. Persoanly i have difreing options about formating code but it dose not mean i whould make a small rant about it when some one says it dose not compley with the standers. I find it easyer to understand code when it is formated slight difrently then the java standered and for my own uses i could debug faster with my style. How ever i agknoagle that my way is not the norm. As for typos in code, when posting for help it realy is a good idea to at least run your code threw a complier once and get it to a point where there are as few syentx errors as posaible so the peoleop helping you will have an easyer time debuing and will not make a big post of all the small errors witch you seem to dislike. You have to rember that no one here is geting payed or any compesation to help you or any one eltes and we are just trying to share the knogeal. As such we should respected the peoleop that give us help and if you do not like there advice simpley don't take it, no need to compain about them giving to much help. |
Author: | Justin_ [ Sun Apr 09, 2006 11:44 am ] |
Post subject: | |
See Hacker Dan, this is where you are wrong. I don't need to test my program if my question is about how I should use my classes and how to instantiate the arrays. I used my actual code rather than waste time making up an example. On the other hand if my question was: "Debug my code, I can't get it working" or something along those lines then yes I would be completely out of line to suggest that pointing out all of these errors is a waste of a person's time. But I see that all you compsci.ca chaps don't care about my argument. I have to be honest I'm not used to people disregarding what I have to say, so I thank you all for showing me what it's like to be the underdog. Still though, no hard feelings or anything, but I'm right. |
Author: | Justin_ [ Sun Apr 09, 2006 11:59 am ] |
Post subject: | |
And CompWiz, if you were taugh to format code like that, you should have a brain enough to rebel against such inconsistent, disorganized code. Whenever I see a formatting standard that I think serves no purpose, I rebel as Hacker Dan does. But my rebelling would never entail a retarded alternative like the code you presented CompWiz. |
Author: | md [ Sun Apr 09, 2006 12:10 pm ] | ||
Post subject: | |||
Justin_ you post code for others to critique. If you don't like it then don't post. wtd gave you some good sugestions (as he almost always does) and you take issue with it without reason. Either learn to accept what people say without getting angry (especially since it seems your original question was answered) or go somewhere else. As for compwiz's code that was an example of bad formatting. It was pretty obvious too... try reading before posting; it allowes you to actually get all of the things you want to say into one post. Personally I prefer the following formatting convention:
Nice and easy to read; blocks are well defined, variables easy to pick out and operators and variables well seperated. Plus it translates among many different languages so my code is always readable. |
Author: | Justin_ [ Sun Apr 09, 2006 12:44 pm ] |
Post subject: | |
That's my favourite too Cornflake. And if you notice, all I said was, thanks for the tips, and these are the tips that you didn't need to tell me (for future reference). I wasn't trying to be rude but was trying to tell wtd what he can ignore in the future. Also, I touched on how it almost seems condescending the way he picks on my typo's, this was used as a device as well. The device was used to the effect of dissuading wtd from pointing out my typos. So I was saying: Not necessary + it pisses me off. Two reasons why he shouldn't do it in the future. I've seen many people asking for help, giving code that doesn't work whatsoever, but used as a device to better convey the question they are asking. So, do not tell me that I am submitting my code to be published in the New York Times when all I want are straightforward answers, using my code as a device to convey the questions I'm asking. |
Author: | wtd [ Sun Apr 09, 2006 1:27 pm ] | ||||
Post subject: | |||||
Cornflake wrote:
The problem with this, is that it does not take advantage of a lot of subtle visual cues that can aid in parsing the code. Consider the fact that there's no space between the "for" and the opening parenthesis. This is also a fairly standard convention for function calls. Therefore, even though you intellectually know that "for" is a keyword, your brain has to correct its instant (and subconscious) desire to tag it as a function call. Consider that opening braces are placed on their own lines. While this is fairly standard C, C++, Obj-C and D convention (C# conventions are rather more complex and nuanced), there is good reason for the standards for Java as set forth by Sun. Again, it comes down to visual cues. It is perfectly legal to have a set oif braces containing code separate from any other organizational construct in the code. Your brain knows this, and looks for it. Again, your "for(...)" looks like a fucntion call at first glance to your brain. So, the natural thing for you to first assume (subconsciously), is that you have a function call, followed by a block of code, and that you've simply forgotten the semi-colon after the function call. You have to work to correct this with your conscious mind. The conscious mind has a lot less processing power than the sub and unconsious mind, so you're wasting power here. The last thing I'll note is that, while I understand the goal in introducing extra parentheses in the condition, for such a simple condition, there is no need to disambiguate, and the extra parentheses add an obstacle to the visual parsing of the expression. They force the mind to consciously evaluate where the match is to the opening parenthesis for the "for" construct. And now, let's look an at example with no pesky syntax to get in the way. ![]()
|
Author: | rizzix [ Sun Apr 09, 2006 1:44 pm ] | ||||
Post subject: | |||||
judge yourself: which one is easier to grasp at first sight?
|
Author: | Justin_ [ Sun Apr 09, 2006 2:07 pm ] |
Post subject: | |
To Justin, they are equal. I only prefer the brace on its own seperate line because I think it looks neater. |
Author: | Dan [ Sun Apr 09, 2006 2:23 pm ] |
Post subject: | |
This is geting realy off topic. If we whont to have a debate/discution about formating java code then we should probly make a few topic about it. There for since the question of this thread has been aswered i am locking it. This is not ment to offended any one. Just trying to keep some order. |
Author: | wtd [ Sun Apr 09, 2006 2:55 pm ] |
Post subject: | |
The debate rages on at Free Range Code. http://freerange.compsci.ca/viewtopic.php?pid=218#p218 |