Name __________________________                                                                      Date: 12/16/05

AP CS Java  - Mr. Merlis                                                                                             Block 4 ACE

 

Day 34 – WHAT’S GOING ON

TODAY’S AGENDA

 

-          RECURSION.

-          Work on RecursionPractice: à See the day 32 agenda.

-          BASE CONVERSION Lab à Inside “Recusion” folder create a new project “Conversions”

 

BASE CONVERSION

 

We work in a base 10 system.

In computer science, other popular bases include base 2, 8, and 16.

 

Base 2 is also called___________  Base 8 is also called_____________        Base 16 is also called_____________

 

The Sumerians created what the Bablyonians made famous, namely the base ____  system.  (Sexagesimal)

 

 

Lab 4 – Write a Converter class that can convert decimal numbers to any base 2 -16 inclusive.

 

The class should have a private instance variable, itsBase that gets a value in the constructor. 

For bases over 10, use the additional digits

 

A – 10                    B – 11                     C – 12                     D – 13                     E – 14                     F – 15

 

 

Use the else if structure to check the value of the remainder.  For example

else if (remainder == 10)

{

                return “” + convert(quotient)  + “A”;

}

etc.

 

Recursion Extra CreditWrite a method for the Converter class that will convert numbers of other bases to base 10.   Overload the convert method under the following heading

 

public int convert(String number, int base)

// precondition:  number is a correctly formatted number in the given base

// postcondition:  number will be converted to a base ten integer and returned.

 

One algorithm to convert from base n to base 10, given a base n number as a string

 

answer = rightmost digit

placevalue = 1

while there are digits to process

                 placevalue * = n

                answer +=placevalue * next digit to the left

 

Write a recursive algorithm to do the conversion and implement it.