CS 112 Lab 1

Asymptotic notation

We will review order notation and follow examples presented here,

http://www.cs.unc.edu/~plaisted/comp122/02-asymp.ppt

Recursion example

We will define recursive methods for the following,

  • String decimalToBinary( int decimalValue )

    use the recursion,
    binary( n ) = binary( n / 2 ) + "0 | 1"

    what is the base case?

  • int hexToDecimal( String hexValue )

    use the recursion,
    decimal( hex ) = decimalVal( hex.last_char ) + 16 * decimal( hex.prefix )

    again define the base case.

We can now convert hex to binary using decimalToBinary( hexToDecimal( hexValue ) )

The following method to converts a hex character ( 0 - 9, A - F ) to decimal ( 0 - 15 ).

int hexCharToDecimal( char hexChar )
{
 if( a >= '0' && a <= '9' )
 {
   return (int)(a - '0');
 }
 else if( a >= 'A' && a <= 'F' )
 {
   return 10 + (int)(a - 'A');
 }
 else
 {
  System.out.println("Error input");
  System.exit(0);
  return 0;
 }
}

URL

http://cs-people.bu.edu/tvashwin/cs112_spring08/lab01.html