Virtual Machines: Windows 7 host and Ubuntu 9.10 guest

I am trying out Sun’s Virtual Box this term. I’m going to be doing a fair amount of development in C and C++ which means using gcc on Ubuntu. I’m pretty invested in the software on my Windows 7 laptop and yes, I’ve got Cygwin, but I prefer working with C and C++ on Ubuntu. I tried dual booting Vista and Ubuntu before upgrading from Vista to Windows 7, and I generally didn’t like the whole dual-boot experience.

Enter Sun’s Virtual Box. I worked with virtual servers using VMWare while studying databases at BCIT last year, but I found VMWare was a real memory pig and I wanted to try something that was a) open source, b) free and c) free.

Setting up Ubuntu 9.10 as a guest on my Windows 7 host was almost too easy. The only problem? The Virtual Box User Manual doesn’t include a clear step-by-step recipe for installing Guest Additions, which is the Virtual Box “partner” we have to install on the virtual machine. It integrates the virtual machine with the host machine so you don’t have to do weird screen resize things or mouse captures or anything that offends the intuition. Here’s what I did:

  1. download and install Virtual Box
  2. download the iso for Ubuntu 9.10 and use your favourite iso mounting software to mount it in your host (I used VirtualCloneDrive)
  3. start Virtual Box and create a new Virtual Machine
  4. install Ubuntu in the Virtual Machine (Virtual Box’s wizard asks some set-up questions and you just point it to the iso and wait–go make a sandwich or something)
  5. once Ubuntu’s up and running, choose Devices/Install Guest Additions from the menu bar in the virtual machine’s window
  6. ignore the weird warning message Ubuntu spits out (check the app bar to find it if it’s not maximized)
  7. double-click the GUESTADDITION disc which appears on the Ubuntu desktop
  8. run the autorun.sh script
  9. reboot the Ubuntu machine.  Done.

It’s seriously that simple.  It’s completely moron-proof. I did this without any trouble at all. The Virtual Box wizards take over when you create a new virtual machine, and then when you open it the first time it cues you for the iso of the operating system (in my case Ubuntu 9.10) to install.

I have 2 partitions on my hard drive, a big Windows 7 partition and a smaller 25 GB docs folder which is mounted to my Windows as drive D called shared. I wanted to give the Ubuntu virtual machine read/write access to my D drive, and I did this in two steps:

  1. close the virtual machine and edit its Shared Folders (at the bottom of the virtual machine’s Details tab) to include shared
  2. reboot the virtual machine and use this command in a terminal window:
sudo mount -t vboxsf shared /home/christopher/shared

Again, crazy easy.

Talking about crazy easy, I made us new pajama bottoms during the Christmas break and started planning my next big art project.  I’ll post some pictures about it on orangewool once more work is done.

  • Share/Bookmark

Java’s Sorted Collections and the Comparable and Comparator Interfaces

Java’s SortedSet interface and the classes that implement it (like TreeSet) will store a collection of objects that are sortable. In order to make a class sortable so that we can store it in a sorted collection, we have two choices:

  1. the sorted class can implement the Comparable interface
  2. we can pass a Comparator to the collection’s constructor.

1. Comparable Interface

This is probably the easiest way to make sure we can store objects in a sorting collection. The Comparable interface has a single method that we recognize from classes like String:

int compareTo(Object o)

This method must return an integer: 0 if this.equals(o), a negative int if this is less than o, and a positive int if this is greater than o. Using the Comparable interface defines what we call the “natural ordering” for a class. Here’s an example I wrote for a PTerm, which is a term in a polynomial:

public int compareTo( PTerm other )
{
    return other.power - power;
}

2. Comparator Interface

An interesting alternative is the Comparator interface. By using the Comparator interface we can define different comparators for a class if we need to order objects of a type in different ways. The Comparator interface contains two methods:

1. equals()
2. int compare(T o1, T o2).

Here’s the trick: the class we are comparing does NOT implement Comparator–we create a brand new class that implements it, and pass the implementing class to the collection’s constructor. Here’s an example:

public class TermComparator implements Comparator<PTerm>
{
    public int compare( PTerm term1, PTerm term2 )
    {
        return term2.getPower() - term1.getPower();
    }
}

In this case, we’re sorting the terms in the polynomial by the magnitude of the exponent, but we could have also decided to sort them by the magnitude of the coefficient.

We create the TreeSet like this:

    SortedSet<PTerm> polynomial = new TreeSet<PTerm>( new TermComparator() );

So now, whenever we add a term to the polynomial, the TreeSet will use the TermComparator to decide where it goes.

Don’t forget to override equals() in the PTerm class either:

public boolean equals( Object obj )
{
    if( obj == null )
        return false;
    if( getClass() != obj.getClass() )
        return false;
    PTerm other = (PTerm) obj;
    return power == other.power;
}

  • Share/Bookmark

AT91SAM7S256 aka the NXT Brain

The Lego MindStorms NXT Brick is controlled by a pair of Atmel microcontrollers:

The AT91SAM7S256 is part of an Atmel series of low pin-count Flash microcontrollers based on the 32-bit ARM7DMI ARM Thumb RISC processor. It features:

  • a maximum clock speed of 55 MHz (48 MHz in the NXT)
  • 64 KB of high-speed on-chip SRAM
  • 256 KB of integrated high-speed Flash memory
  • 11 peripheral DMA channels
  • 1 USB 2.0 (12 MB/s) device port
  • three 16-bit timers
  • four PWM controllers
  • 32 I/O pins
  • and requires a 3.0- to 3.6-volt power supply

The key facts here are:

  • The RISC–reduced instruction set architecture–is based on ARMv4T Von Neumann architecture. The ARM7DMI processor provides 0.9 MIPS (millions of instructions per second).
  • ARM–32-bit instruction set
  • Thumb–critical subset of the ARM 32-bit instruction set that has been encoded into a 16-bit instruction set
  • Flash memory–can be programmed in-system via the JTAG-ICE interface or through a parallel interface on a production programmer prior to mounting

The ARM7DML can execute both the high-performance 320bit ARM and high-density 16-bit Thumb instruction sets. The instruction sets are toggled by the processor as an ARM state or a Thumb state. In the ARM state, the 32-bit instructions are executed conditionally, while the 16-bit Thumb state instructions are a re-encoded subset of the ARM instruction set. The stegosaurus had a brain in its arse, and basically so does the NXT Brick. The AVR 8-bit ATmega48 microcontroller acts as a co-processor. Inside it there are:

  • 4 KB reprogrammable Flash program memory
  • 512 Byte SRAM
  • 256 Byte EEPROM
  • 8-channel, 10-bit AéD converter (TQGPéMLF)
  • 23 I/O pins
  • 4 MHz clock speed

Here’s the interesting thing. The ATmega48 is much smaller than the main AT91SAM7S256 processor, but this 8-bit microprocessor achieves almost 1 MIPS per MHz by executing its RISC instruction set in a single clock cycle.

  • Share/Bookmark

Artifact J-2: Java RPN Calculator (Command Prompt)

Description

A simple command prompt calculator that uses Reverse Polish Notation.

In Reverse Polish Notation the operators follow their operands; for instance, to add three and four, one would write “3 4 +” rather than “3 + 4″. If there are multiple operations, the operator is given immediately after its second operand; so the expression written “3 − 4 + 5″ in conventional infix notation would be written “3 4 − 5 +” in RPN: first subtract 4 from 3, then add 5 to that.

The algorithm for evaluating any postfix expression is fairly straightforward:

  • While there are input tokens left
    • Read the next token from input
    • If the token is a value
      • Push it onto the stack
    • Otherwise, the token is an operator
      • It is known a priori that the operator takes n arguments
      • If there are fewer than n values on the stack
        • (Error) The user has not input sufficient values in the expression.
      • Else, Pop the top n values from the stack.
      • Evaluate the operator, with the values as arguments
      • Push the returned results, if any, back onto the stack
  • If there is only one value in the stack
    • That value is the result of the calculation
  • If there are more values in the stack
    • (Error) The user input too many values

For more information on Reverse Polish Notation read: http://en.wikipedia.org/wiki/Reverse_Polish_Notation.

Technical Knowledge

I applied my knowledge of:

  • object oriented programming in Java
  • Reverse Polish Notation
  • polymorphism.

Skills Applied

I developed these skills while making this artifact:

  • using the NetBeans IDE
  • test-driven design using Netbeans
  • object oriented programming with Java.

Notes

I wrote this for BCIT’s COMP 2526, Intermediate Java, in February 2008.

I employed an iterative, test-driven approach. My first iteration resulted in a program that used classes to deal with different operations. The second iteration reduced code duplication and improved the quality of the code with polymorphism.

The application was written using NetBeans against a series of Unit tests provided by our instructor, D’Arcy Smith.

Demonstration

Download the source code [8 KB ZIP].

Download the JAR file here [13 KB]. Navigate to the download folder with your command prompt and run using:

java -jar RPN_2.jar

  • Share/Bookmark

Artifact J-1: Codeword Parity Validator

Description

A program that tells the user if a parity-encoded word is valid or not, and if a Hamming codeword is valid or not.

A parity-encoded word is a series of binary digits (zero or one) where the left most digit is either a zero or one depending on the type of parity being used (EVEN or ODD). If a word is encoded with EVEN parity the number of one bits must be an even number. If a word is encoded with ODD parity the number of one bits must be an odd number.

For example, if the original word is: 100

  • EVEN codeword is 1100 (1 + 1 is 2, an even number)
  • ODD codeword is 0100 (1 is an odd number)

A Hamming encoded word is a series of binary digits (zero or one) where the 2x bits are either a zero or one depending on the type of parity being used (EVEN or ODD)

For example, if the original word is: 10

  • EVEN codeword is 11100
  • ODD codeword is 00110

For more information on parity checking read: http://en.wikipedia.org/wiki/Parity_bit.
For more information on Hamming codes read: http://en.wikipedia.org/wiki/Hamming_code.

Technical Knowledge

I applied my knowledge of:

  • Java basics including GUI classes
  • regular expressions
  • object oriented programming.

Skills Applied

I developed these skills while making this artifact:

  • working with the NetBeans IDE
  • test-driven design using NetBeans
  • object oriented programming with Java.

Notes

I wrote this for BCIT’s COMP 2526, Intermediate Java, in January 2008.

I employed an iterative, test-driven approach. My first iteration resulted in a program that checked the parity of a codeword entered as a command-line argument. The second iteration added logic to check Hamming codewords, and the third and final iteration added a GUI envelope for the program.

The application was written using NetBeans against a series of Unit tests provided by our instructor, D’Arcy Smith.

Demonstration

Download the source code [7 KB].

Download the JAR file here [21 KB]. Navigate to the download folder with your command prompt and run using:

java -jar Parity_Validator_3.jar

  • Share/Bookmark