/** * Exercise 7.10 p 251 b and c. Skip a. */ public class Exercise710 { /** * Return the largest of the 3 doubles a, b and c */ public double maxOf(double a, double b, double c) { double max = a; if (max < b) max = b; if (max < c) max = c; return max; } /** * Return the integer value of an hexadecimal character.
* Return -1 if the character is not an hexadecimal character. */ public int hexValue(char c) { // To compute the integer values, use the characters' Unicode codes // e.g.for c='0',...,'9', c-'0' is understood as Unicode(c)-Unicode('0') // Since '0','1','2',...,'9' are in this order in Unicode, // c-'0' gives the integer value of the digit represented by c // Do the same thing with c='A',...,'F', but add 10 since A is 10 // in hexadecimal. if (c >= '0' && c <= '9') return c - '0'; else if (c >= 'A' && c <= 'F') return c - 'A' + 10; else return -1; } /** * Starts the application */ public static void main(String[] args) { Exercise710 e = new Exercise710(); System.out .println("e.maxOf(3.2, -5, 10.9) = " + e.maxOf(3.2, -5, 10.9)); System.out.println("e.hexValue('B') = " + e.hexValue('B')); } }