By Armando Fonseca: Tuesday March, 18 ,2008

Class Base_Number

 


public class Base_ Number

extends Object

 

The Base_Number  convert an array of char  supplied in hex, octal, binary or decimal and print out a the number  as a string or array of char in all four formats stated above.   A  base or radix is usually the number of unique digits, including zero, that a positional numeral system uses to represent numbers.   A  numeral system is a mathematical notation for representing numbers of a given set by symbols in a consistent manner. For example:

 

Suppose num = 13, where num is a decimal interger, then:

 

Base:

 

Digits that can be used to represent num:

 

num in its numeral system representation:

 

2

 

0, 1

 

1101

 

8

 

0, 1, 2, 3, 4, 5, 6, 7

 

15

 

10

 

0, 1, 2, 3, 4, 5, 6, 7, 8, 9

 

13

 

16

 

0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F

 

D

 

 

The reasons why Base_Number accepts and prints an array of char only in hex, octal, binary or decimal are the following:

 

  • In Computing, transistors have only two possible states: "open" and "closed". Substituting open=1 and closed=0 yields the entire set of binary numbers. This numeral system of base 2 is the basis for digital computers.

     

  • Base 8 and 16 are multiples of base 2, we often use them to avoid writing large bit pattern.

     

  • Base 10 is not multiple of 2 but  is de-facto-standard for humans to represent numbers.

 

The client is who determines the inputs of Base_Number. For example:

 

Char [] cArray =  {f, f};

Int base = 16;

Base_Number num = new Base_Number(cArray, base);

 

It would print out the following:

 

Here is the conversion FF of base 16.

base 2: 11111111 

base 8: 377

base 10: 255

base 16: FF

 

 

The parameters in the constructor are type Char [] and int. An array of character is used instead StringBuilder, StringBuffer or String because it use less space and has random access. An integer of type int is used in the Constructor to specify the base format of the array of character hold in the parameter Char []. The  private parameters are:
  • Char [] charArray;
  • int base;

 

The Base_Number classes provides methods for returning an integer representing array data, for returning an array of characters representing a number to base, and for validate strings of characters.

 

The client must be aware that the class Base_Number needs improvements and the current version would not run correctly in these situations: 

  • If a null is passed instead of an array of characters in the constructor Th next cases would throw a null pointer exception. The class Base_Number doesn’t not have any exception handling but the next version would address these problems by accepting in one place only an array of character as the only parameter in the constructor. The constructor would throw an exception if the array is an invalid input:
    • Case 1: [] cArray  = null; 
    • Case 2: num.isValid(str.toCharArray(), base); 
    • Case 3: Base_Number num = new Base_Number(cArray, base);
    • Case 4: toNumber(); 
    • Case 5: printNumber();
    • Case 6: toString();

 

  • If a base other than 2, 8, 10, 16 is passed in the method toCharacter(int num, int b), where num is the number the client want to convert to base b, it would allow to convert to other bases and if the base is <  2 or > 10  it would return and invalid  string of characters.   Next version,  toCharacter  would check for invalid bases so it can run properly and independently.

 

Constructor Summary

 

 

 

Base_Number()
          Initializes a newly created
Base_Number object so that it represents an empty cha

 

 

 

Base_Number(Char [] charArray, int base)
          Allocates a new Base_Number that contains the sequence of characters currently contained in the array argument representing a number of base represented in the base  argument

 

 

 

 

·          

 

 

 

Method Summary

 

 

 

 int

 

 

toNumber()
          Returns the integer represented by the data array.

 

static char []

 

 

toCharacter(int num, int b)
          Returns the array of characters represented in num in base b.

 

 void

 

 

printNumber()
          Prints the contents of the data array.

 

 boolean

 

 

isValid(char [] a, int base)          

         Returns true  if the characters of array are within the base‘s range of digits

 

Methods inherited from class java.lang.Object

 

 

 

toString()

 

 

 

 

 

Constructor Details

 

 

 

Base_Number

 

public Base_Number()

 

Initializes a newly created Base_Number. Note that use of this constructor is unnecessary since charArray  = null would throw a nullPointerExepcetion

Base_Number

 

Public Base_Number(char [] charArray, int base) 

 

 

Initializes a newly created Base_Number object so that it represents the same sequence of characters as the argument; in other words, this.charArray = charArray . charArray represent a 

 

number  with a base determined by the base argument.

 

Parameters 

 

charArray– an array of char.

base-an integer.

 

 

Method Detail

 

 

 

toNumber

 

public int toNumber()

 

 Converts an array of chars in to an integer by using horner's method.

Specified by: 

length in interface array of char.

 

Returns:

the integer of the number representing charArray.

 

Throws:

nullPointerException if a = null;

 

Errors:

If  charArray = null, it would not compute. Also, it base = 0 then it would returns 0.  although the computation is valid, it doesn't make sense all integers represented  by 0.

 


toCharacter

 

public char [] toCharacter(int num, int b)

 

Returns

the number representation of num with base b as  array of character. The index ranges from 0 to length() - 1. The first char value of the sequence is at index 0, the next at index 1, and so on, as for array indexing.  

 

 

Specified by:

 

Professor  Richard Glass

 

Parameters:

num- some integer

b- the presentation format of num.

 

Returns

 

An array of characters representing  the number with the  base b argument.

 

Throws

none

 

Errors:

 If the base b < 2 or ( b > 10 and b != 16 ) It would return an un-expected presentation.


printNumber

 

public void printNumber()

 

prints the number representation of charArray with base b  in four bases.

 

Specified by:

Professor  Richard Glass

 

Parameters:

none 

 

 

Returns: 

none

 

Throws:

nullPointerException if charArray = null;

 

Errors:

 If the base data field is other than 2, 8, 10,  or 16 It will print an un-expected presentation.


isValid

public  static boolean isValid(char [] a, int base)

 

validates input according to its base.For example, having  a number FF and base 16 would return true, but mFF would return false because  is not part of the set of digits used for  numerial representation. the same behavior is with other bases ie, 123 with base 2 would return false. this method is static, so it can check other arrays of character with bases 2. 8. 10 and 16.

 

Specified by:

Professor  Richard Glass

 

Parameters:

a- an Array of characters reference(not a copy)

base- the presentation format in which we are asuming the numerical representation of array of character a is.

 

Returns:

true if  a[] contains characters that fall within the range of digits of particular base.

 

Throws:

nullPointerException if a = null;

 

Errors:

Sadly, If  a[] = {f, f, m} the  isValid method checks the first letter if is A,B,C,D,E, or F, and if a[0] contains one of those characters, it would return true immediatly with out even checking the rest of the array of    character.

 


Page Information

  • 5 months ago [history]
  • View page source
  • You're not logged in
  • No tags yet learn more

Wiki Information

Recent PBwiki Blog Posts