This week's book giveaways are in the Scala and Android forums.
We're giving away four copies each of Machine Learning Systems: Designs that scale and Xamarin in Action: Creating native cross-platform mobile apps and have the authors on-line!
See this thread and this one for details.
Win a copy of Machine Learning Systems: Designs that scale this week in the Scala forum
or Xamarin in Action: Creating native cross-platform mobile apps in the Android forum!

Nathan Leniz

Ranch Hand
+ Follow
since Nov 26, 2006
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Nathan Leniz

I would ask why you'd want to brute force something like this, because choosing 4 out of a set of 36 produces only 82,251 combinations while choosing 8 produces a much more respectable 145,008,513. All that is assuming you'll be matching a string in which the order doesn't matter. I see in your example you are using it as a "password" which would mean that given the same set of 36 characters and trying to brute force a password (which should, but not always, result in the order mattering) consisting of 8 characters, it could take you set^subset attempts, in this case a staggering 2,821,109,907,456 total permutations.

My math is a bit rusty and the numbers might be off, but Brute Forcing something like that is astronomical. If it's still something you want to give a shot at, the Google bot returned this link for me. http://www.merriampark.com/perm.htm
8 years ago
Henry, thanks. I was stupidly trying to call a function called removeHandler(Connection toRemove) in Server from within the close() method of Connection. Now I've changed it so it doesn't do that, I'll try and tackle that problem later, but if there is more than one connection to the server, it hangs. If the other connection sends the shutdown command after the first one, then it works. So on and so forth if there are multiple connections to it.

Also, I'm trying to think of a way to call close() on every connection when the shutdown command is given, but is it necessary? If I leave all the individual in and out's open when I terminate the program will that have any adverse reactions? I feel as if I shouldn't do that for some reason, but now I don't know.
8 years ago
Greetings,

As the title says I'm having an issue with a concurrentModificationException, but I'm not sure why.

I have written a chat server with a parser that can read in certain commands, and all of them work except the command to shut them down. I won't paste all the code, just the relevant parts, and the error message.


If I'm understanding it correctly the exception is thrown when a thread tries to modify something while another thread is iterating over it. What I'm not understand if this is true, is why it's doing it as I don't see how any thread but the thread the Server is running in is modifying it.

Is it because even though that function resides in the Server class, the function is called from a Connection thread? But even so, how is the handlers Vector being modified by something else? I'm actually to the point now I think I may be confusing myself.

Lastly, it leads me to wonder how any Server is brought down remotely as any connection to it is going to be serviced in a thread (I think). I could probably bring the server down, but I feel like I should be doing it by going through each thread and closing the in and out before I bring the server down itself.
8 years ago
So I started to really think about the problem and how to code it. The first thing I realized when I was making a map to test it with is that it'd be a huge pain in the but to enter each point and the distances to other points manually. So I made this little distance calculator that figures out the distance between two points on an X Y coordinate system.

Anyway, here is what I made up to calculate the distances between coordinates. It creates a temporary internal point to form a right triangle on the X Y grid, gets the distances of the edges, then used the Pythagorean theorem to figure out the actual distance. At least I think it does, it's worked so far with everything I've thrown at it. I apologize for horrid style or comments, I'm rusty and it makes sense to me.



Now to move on to the ant colony. Any ideas? Also, anyone else happen to have worked on or be working on some nifty algorithms? This kind of stuff is pretty interesting, but unfortunately a lot of my Army buddies aren't really all that into it.
I was tooling around on Wikipedia to pass some time and I came across this page on the Travelling Salesman Problem. I find the problem fascinating as well as all the different solutions (although my math finds them quite the opposite), but while reading I noticed one possible search method called Ant Colony Optimization or Ant Colony System.

Just wondering if anyone else has heard of it or has experience with it, and could talk about it a little bit. I think it'd be kind of cool to build up a little app that actually showed the ants going along the various routes with some graphics showing the amount of pheromone building up on each path.

I found an applet that does it in a way, but never seems to end. There also seems to be a good number of Google returns when searching for it to a number of academic sources.

Quoted from Wikipedia --

Artificial intelligence researcher Marco Dorigo described in 1997 a method of heuristically generating "good solutions" to the TSP using a simulation of an ant colony called ACS (Ant Colony System).[19] It uses some of the same ideas used by real ants to find short paths between food sources and their nest, an emergent behaviour resulting from each ant's preference to follow trail pheromones deposited by other ants.
ACS sends out a large number of virtual ant agents to explore many possible routes on the map. Each ant probabilistically chooses the next city to visit based on a heuristic combining the distance to the city and the amount of virtual pheromone deposited on the edge to the city. The ants explore, depositing pheromone on each edge that they cross, until they have all completed a tour. At this point the ant which completed the shortest tour deposits virtual pheromone along its complete tour route (global trail updating). The amount of pheromone deposited is inversely proportional to the tour length; the shorter the tour, the more it deposits.



One possible implementation I had in mind was finding the average distance between all cities and putting this distance into each ant plus a % as the ant's "stamina" so to speak. Each city the ant visits would reset the stamina, but an ant that spends too much time before reaching a city would die, allowing another ant/thread to start. I was thinking that might speed it up a little bit. However, I also wonder if there could mathematically be a leg in the optimal route that is longer than the average of all the distances, and this would cause it to fail.

So, anyone have any thoughts or ever tried to/successfully implemented this? I'd like to see what the JavaRanch community has to say.
Chances are your mac is set up and ready to go out of the box. Just follow the below steps to test it.

1. Right click on your desktop, and create a new folder. Name the folder Java.2
2. If you aren't familiar with spotlight, open it up by pushing and holding the command key (key with an apple on it) and the space bar. Spotlight will spew forth from the upper right hand corner of your screen most likely.
3. Type the world terminal. You'll see it work at first, then you see a bunch of sub areas within spotlight such as Top Hit, Applications, Definition, so on and so forth. Next to applications you'll see a little black box and the word terminal. Click it, that's the one you want.
4. Your terminal application will open and you'll see your prompt. Mine looks like
5. Type the following My prompt changed to nathans-macbook-pro: Java Nathan$
6. Type pico HelloMac.java
7. Your terminal app will morph into what appears to be a text editor. Type the following into it
8. Push and hold control, then the x button. Then hit enter. Your prompt should return to normal.
9. Type javac HelloMac.java
10. Type java HelloMac and you should see Hello, Mac! displayed in your terminal. Doing that let's you know it works. That should also set you up for using your terminal and spotlight.

If you want to use TextEdit for your editor, you'll need to set it up a bit at first before you can effectively use it to start going through the book. But before you do that, you'll want to open up your Java folder, and drag the HelloMac.java and HelloMac.class files you see into the trash.

1. Using spotlight, open textedit
2. With textedit open, select the preferences, or use the keyboard shortcut by pressing and holding the command key and pushing the comma key.
3. Under the word Format, make sure the button next to plain text is highlighted. By default the one next to rich text most likely is.
4. Go the to Open and Save tab, and make sure you select the option to automagically at .txt if you don't specify a file extension. This will save you heartache if you use .txt files a lot.
5. You can make sure the above will now work for you by pushing and holding the command key, then hitting the q key after you save your changes to your preferences. command q is the mac keyboard shortcut for quit. After you do that reopen textedit, copy and paste the above code into it, then from the file menu choose save as.
6. Navigate to your Java folder, and save it as HelloMac.java and remember that you have to type out .java after the name of the file in order to let the computer know that's the extension you want it as. If you don't, it will add .txt and make it into a simple text file, and when you try to use your compiler it won't be happy.
7. After saving your file, open up your terminal and navigate to your java folder if you aren't there already. Unless you hard closed it it should already be there though.
8. Follow steps 9 and 10 above, and you should see Hello, Mac! displayed once again.

As mentioned there are better text editors and there are IDEs to use as well. Also as David Newton said you should become familiar with some of the features on your mac as they can save you time and help you get around. Also reading a good bash tutorial and saving it in your favorites would be good.

If you have any problems don't hesitate to ask. Good luck and happy coding.
9 years ago
Here is a topic covering that question.

I found this on the sun forums.

"There is actually no boolean type. A Java expression that operates on boolean values is compiled to use the int data type to represent boolean variables. This means a boolean is usually a 32-bit variable. However arrays of booleans are treated as arrays of bytes, so booleans are 8 bits in an array"
[ July 26, 2008: Message edited by: Nathan Leniz ]
9 years ago
Opening a new window in a browser is as simple as using some of the additional options in the <a href... block.

W3C tutorial
9 years ago
Hi Daniel.

This assignment gave me fits. I'd highly recommend searching for all topics titled sortnames in the cattledrive forum. Read through a few, the information found there is invaluable.

This thread was pretty good as far as dropping a key hint in the right direction, and there are several others which now (looking back on the problem and knowing what needs to be done) almost give you the answer. Also I did a google search for "how to sort an arraylist alphabetically" after looking through the search results on JavaRanch and studying Collections on the Java tutorial through Sun, and the answer almost jumped out and bit me.

Just remember that you'll need to implement an interface, where one of the implemented methods does some comparing. You need to tell it exactly where to compare.
9 years ago
This should help clear it up.

Link to a Sun pdf.

But I don't think I've seen just one angle bracket like that before, sorry, thought you meant two surrounding brackets.
[ July 24, 2008: Message edited by: Nathan Leniz ]
9 years ago
I don't see how it's poor at all. Array indexes are just that. If I have 20 elements in an array, the one in 19th position (the last one) is 19 places away from the1st element, which is zero away from the beginning. Makes complete sense to me.

Here's an article on why we should start from 0.
And here is a link to an older JavaRanch subject.
9 years ago
Static means it belongs to the class, not an instance.

If I have a class called Animals with a static variable called legs, I can make 20 instances of Animals but the legs aren't unique to each one. Give the following a try...

We created 3 different TestStatic instances, but changing the value in the 3rd TestStatic object also affected 1 and 2. This should illustrate it.
9 years ago
The only thing I can see is you could easily cut down on your println() statements. In both your if/else and switch blocks, no matter what you alter the value with, you still print the user's subtotal.

Ah, forgot to add...

If you are expecting only two inputs and nothing else, you can catch that input before executing through the rest of your program, and repeat the catching until the user enters something valid.

[ July 21, 2008: Message edited by: Nathan Leniz ]
9 years ago
Are you compiling both classes at the same time? i.e.
[B][/B]

Imagine you're making a salad, and the recipe says you need lettuce, carrots, cucumbers, feta cheese, bacon bits, croutons, and ranch dressing. The name of this salad is called "The JavaRanch Mix". So you put all the ingredients minus the croutons in a pile.

Now, you don't actually have the the croutons made, but you have a recipe for them and all the ingredients to make them.

Think of javac like a super smart kitchen appliance. Your java files are the recipes and they contain the ingredients too. If you feed javacl the recipe and the ingredients for "The JavaRanch Mix" it will fail, because your croutons aren't made. But if you give it the recipe and ingredients for both the salad and the croutons, it's smart enough to know it has to make the croutons first, then it'll mix all the ingredients together and make what you want.

EDIT--
If you are compiling both, then try navigating up one directory and type


I don't know why, but when I'm working with classes in packages I always have to go up a directory.
[ July 21, 2008: Message edited by: Nathan Leniz ]
9 years ago
This is also relating to your other post, but if you have declared a class public, the file name must match the class name. You can only have one public class per file.

If you have non public classes, you can put them into a file named whatever you want. But again, as soon as you declare one of those classes public, the file name has to match that public class' name.
9 years ago