R4.14) How do you get the last digit of an integer? The first digit? That is, if
ID: 3650376 • Letter: R
Question
R4.14) How do you get the last digit of an integer? The first digit? That is, if n is 23456, how do you find out that the first digit is 2 and the last digit is 6? Do not convert the number to a string. Hint: %, Math.logExplanation / Answer
1) 2 is an int (integer), 2.0 is a float or double, '2' is a char (character), "2" and "2.0" are Strings. 2) y = x+x computes the sum of the integer x with itself. so y will have the value 4. t = s + s means that the String s is concatenated with itself, resulting in t being the String "22". 3) Yes, both are the same VALUE. Not the same objects though. 4) int lastDigit = n % 10; int firstDigit = n; while (firstDigit > 9) { firstDigit = firstDigit / 10; } 5) A variable can be declared final. A final variable may only be assigned to once. It is a compile time error if a final variable is assigned to unless it is definitely unassigned (§16) immediately prior to the assignment. A blank final is a final variable whose declaration lacks an initializer.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.