Anda di halaman 1dari 3

//Created by Dennis Kager 2/15/16

public class Base{ //This class converts decimal numbers to any base from 2 - 10
private
private
private
private
private
private

int _base;
int _num;
boolean _pError1; //Parameter error variable
boolean _pError2;
boolean bError;
boolean nError;

public Base(){ //Default constructor


this(10, 0);
_pError1 = true; //If the user put no parameter when
//declaring the new object, then the error variable is set
to "true"
}
public Base (int b, int n){ //Constructor calls setBase() and setNuum() to set
the values of _base and _num
if(b > 1 && b < 11)
setBase(b);
else{
setBase(10);
bError = true;
}
if(n > -1)
setNum(n);
else{
setNum(1);
nError = true;
}
}
public String toString(){ //Returns what the user entered if no method is call
ed
return "Error: No Method Called";
}
public void setBase(int b){ //Sets the base
_base = b;
}
public void setNum(int n){ //Sets the number entered
_num = n;
}
public String convert(){
_pError2 = true; //If no parameters were given when calling convert(), then
the
//error variable is set to "true"
return convert(_base, _num);
}
public String convert(int _base, int _num){ //Converts the decimal value enter
ed into a different base

int [] guide = new int [16];


int [] convertedBack = new int [16]; //Holds the converted number, but backw
ards
int [] converted = new int [16];//Holds the correct converted number by stor
ing the convertedBack[] array backwards
int c = 0;
String output = "";
boolean oError = false;
int flag = 0;
double max = 0.0;
if(_base < 2 || _base > 10){ //Checks to make sure the base is valid
_base = 10;
bError = true;
}
if(_num < 0){ //Checks to make sure the number is valid
_num = 0;
nError = true;
}
if(bError == false && nError == false){ //Will only run if the base and numb
er entered are valid
for(int i = 0; i < guide.length; i++){ //Sets up the guide[] array with th
e appropriate values to convert, which
//is determined by what base the nu
mber is going to be converted to
guide[i] = (int) Math.pow(_base, i);
}
for(int i = 0; i < guide.length; i++){ //Finds the maximum value that the
array can store
max += (double) guide[i];
}
if(_num > max){ //If the converted number is greater than what the array c
an store,
//then the error variable is set to "true"
oError = true;
}
for(int i = 15; i > -1; i--){ //Converts the number and stores it in the c
onvertedBack[] array (but backwards)
if (_num >= guide[i]){
convertedBack[i] = (int) (_num / guide[i]);
_num %= guide[i];
}else
convertedBack[i] = 0;
}
for(int i = 15; i > -1; i--){ //Takes the convertedBack[] array and stores
it backwards in the converted[] array
//to get the correct converted number
converted[c] = convertedBack[i];
c++;
}
for(int i = 0; i < guide.length; i++){ //Makes the converted number into a
String
output += Integer.toString(converted[i]);

}
for(int i = 0; i < output.length(); i++){ //Gets rid of all the excess zer
os in front of the converted number
if(output.charAt(i) == '0')
flag++;
else
break;
}
if(flag == output.length()) //If all the digits of the converted number ar
e zero,
//then this sets the String to "0"
output = "0";
else
output = output.substring(flag, output.length()); //If not all digits
are zero, then this shortens the output
if(oError == true) //Error is thrown if the converted number exceeds the 1
6-bit limit
output = "\nError: Overflow";
if(_pError1 == true && _pError2 == true) //Error is thrown if no parameter
s were assigned to convert() or to
//the object when it was instanti
ated
output = "\nError: No Parameters Given";
}
if(bError == true) //Error is thrown if the base is invalid
output = "\nError: Invalid Base";
if(nError == true) //Error is thrown if the number is invalid
output += "\nError: Invalid Number";
return output; //Returns the converted number and/or any errors
}
}

Anda mungkin juga menyukai