
Educators: Earn a free Gold upgrade by joining the PBwiki Back To School Challenge.

Questions? Join PBwiki's weekly office hours today at 1 PM Eastern and get live answers.
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:
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 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:
|
Constructor Summary
|
|
|
Base_Number() |
|
|
Base_Number(Char [] charArray, int base) |
|
·
|
Method Summary
|
|
|
int
|
toNumber() |
|
static char []
|
toCharacter(int num, int b) |
|
void
|
printNumber() |
|
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
|
|
|
|
|
|
|
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
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
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;
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:
Parameters:
num- some integer
b- the presentation format of num.
Returns
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:
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:
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;
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
|
Wiki Information |
Recent PBwiki Blog Posts |