Pages

10.26.2013

Java Comparator vs Comparable

The Comparable and Comparator interface both let objects in Java be sorted, but go about it in different ways. Let's say that you are a sort method, and I pass you a collection of beer. Using comparable (built into the beer) you can sort the beer by name. Duh, it's on the bottle.

But what if I ask you to sort my beer by temperature, coldest first? I would need to provide you with a tool (a comparator) , in this case, an infrared temperature sensor in order for you to accomplish the task. Using the temperature sensor I've provided you, it's now possible to sort the beer by temperature.

Similarly, if I asked you to sort beer by alcohol content, I would need to give you a hydrometer to check the specific gravity. This is akin to yet another comparator. Notice that none of these comparators are built into the beer itself.

Have a look at an example I've whipped up. Download the files from drive and compile them using javac *.java on your machine. Then run the one called BeerFridge.java using the command java BeerFridge

10.24.2013

How to lock the screen in OSX

From day one of Mac ownership, one glaring omission has really bothered me.   There's no obvious way to initiate the screen saver or turn off the screen in order to lock the machine.  Locking the screen is a modern must-do in any environment.  Every other OS places this option in plain sight making it impossible to overlook.  With OSX, both options are obscurely hidden enough that no casual user would ever find them without Googling or mashing random key combinations for half an hour.

I'm here to tell you how to do both things.  First the easy one. To turn the screen off, effectively locking the machine, press Control + Shift + Eject on your Mac keyboard.  The more difficult trick is locking the screen by starting the screen saver.  Open up Keychain Access (hint: it's under Apps>Utilities) and choose Preferences from the menu bar.  Now check the box for "Show keychain status in menu bar."  You will now see a little lock shaped icon in the menu bar that allows you to lock the screen.



10.23.2013

OSX Mavericks

It's about time... OSX Mavericks addresses some pretty serious issues I've had with my Mac since day one.  Why these things took so long to be included in OSX is beyond me.  The two new features I'm interested in are the improved handling of dual monitors and the battery graphing feature in Activity Monitor.  

The ability to hide the unnecessary GUI clutter with full screen app is pretty nifty indeed, but I never used it. In the past two versions of OSX, full screening an app with dual monitors caused the app to take over one monitor (expected) and the OS to render a worthless gray texture on the other (WTF).  Full screening an app now leaves the other monitor free to display other programs, the reason why I bought the second monitor to begin with.  Also, the menu bar is now displayed on both monitors instead of only the primary monitor. 

The second irksome issue Apple addressed was that of battery time tracking.  I can see how long my phone has been on battery, so why couldn't I do this on my Mac?   Now Activity Monitor includes a graph of the machine's battery charge over time, so I can see how long my battery has actually been running my machine, instead of just guessing. This pleases me in a very nerdy control freak sort of way. I'm sure there are others out there that will appreciate this new feature.  

There's not a whole lot to lose by upgrading to Mavericks; hell, it's free, and it runs on machines made back in 2007.  It's nice to get a free upgrade every once in a while, especially after having to deal with Mountain Lion for the last year.  




10.06.2013

Make Eclipse Create Zips of Your Source Automatically

For all of my university programming classes, I have to turn in a zip file with my source code.  It isn't a big deal to create a zip file when I'm done, but occasionally I submit multiple iterations before I'm finally satisfied.  In those instances, creating zip files manually through finder is tedious.

To solve this problem, I created a very simple shell script and integrated it into Eclipse.  Eclipse will run the script automatically every time any files in the project are saved or created.  This way, my zip file always contains the most up-to-date source files.

To get started, you'll need to create a shell script by copying and pasting the following lines into an empty file, then save the file and make it executable using chmod 755 filename.

#! /bin/bash
#Prefix to prepend to dirname, used for naming the zip file
UNAME=$1
#get the name of the directory this is being run from
PROJECTNAME=$2
zip $UNAME$PROJECTNAME src/*.java

Once you've got the script saved in a safe place, open up your project in Eclipse.  (Note that I name my projects according to the assignment.)  Right click on your project in the Package Explorer pane and select Properties.  Choose the Builders link from the list on the left.  Click the New button on the right.  Select Program from the next window that appears.  Give your new configuration a name.  Click Browse Filesystem and find your script for the first blank.  Then Browse Workspace and pick your project for the "Working Directory" blank.  At the bottom: Variables >> Edit Variables >> New.  Name your variable something descriptive, like canvas_user_name, and provide a value, like daschelb. Then click OK >> OK, then select your new variable and click OK once more.  Click variables again, only this time choose the one named project_name.  Make sure there is a space between the variables in the "Arguments" area.  



Next click the Refresh tab and check the box for Refresh resources upon completion and tick the radio button for The project containing the selected resources. Forget about the Refresh tab.  Those settings seem to cause an infinite loop of recompiling. 



Finally click the Build Options tab and select Launch in background and During auto builds.



That's it. Eclipse will now start generating a zip file for you every time it compiles your code. (BTW, the script places the zip in the root of your project folder.)