I am struggling with this example an I was hoping I could getsome guidance on it.
zyDE 5.2.1: Modifying IntegerWithBase to print bases greaterthan 10.
Define a new private method within IntegerWithBase calledtoAlphaNumDigit() that takes an integer value as an argument andreturns a char representing the digit. For argument values between0 and 9, the method should simply return the unicode value for thatargument (i.e., a char value between 48 and 57). For argumentvalues greater than or equal to 10, the method should returnunicode values corresponding to a lower-case letter in thealphabet(i.e., a char value between 97 and 122). Thus, thestatement toAlphaNumDigit(15);, for example, should return the charvalue 102, which corresponds to the letter “f”.
Use this private method to convert the remainder values computedwithin toString() to the appropriate characters. For example,creating the object IntegerWithBase tempNumInBase16 = newIntegerWithBase(255,16); should output “ff”.
public class IntegerWithBase {
private int decimalValue;
private int baseFormat;
public IntegerWithBase(int inDecimal, int inBase){
this.decimalValue = inDecimal;
this.baseFormat = inBase;
}
@Override
public String toString() {
int quotientVal;
int remainderVal;
int dividendVal;
String resultVal;
resultVal = “”;
dividendVal = decimalValue;
if (baseFormat > 1) {
// Loopiteratively determines each digit
do {
quotientVal = dividendVal / baseFormat;
remainderVal = dividendVal % baseFormat;
// FIXME: Use toAlphaNumDigit() to append the remainder as a charvalue to the result
resultVal = remainderVal + resultVal;
dividendVal = quotientVal;
} while(quotientVal > 0);
}
else {
resultVal =String.valueOf(decimalValue);
}
return resultVal;
}
// FIXME: Define private methodtoAlphaNumDigit()
}
Expert Answer
Answer to I am struggling with this example an I was hoping I could get some guidance on it. zyDE 5.2.1: Modifying IntegerWithBase…