Examples of Reversing Numbers in a Java Program
- The modulo operator leaves many new programmers scratching their heads because many people haven't thought about the concept of a "remainder" since elementary school, and programming languages give it the funny name of "modulo." The modulo operation, which returns the remainder of the division of two integers, makes easy work of chopping one or more digits off the end of an integer simply by using powers of ten for the divisor. For example:
187 % 10 returns 7
364 % 100 returns 64
To reverse the digits in an integer, simply loop through each digit, using modulo to pop each digit off the end of the number, division and multiplication to shift the digits. For example:
int origNum = 123456789 ;
int newNum = 0 ;
while (origNum > 0) {
newNum *= 10 ;
newNum += origNum % 10 ;
origNum /= 10 ;
}
When the loop completes, newNum will hold the value 987654321. - The String and StringBuffer classes provide handy functions for converting types and manipulating strings. Using StringBuffer's reverse method makes reversing the digits in an integer quick and simple work. For example:
int origNum = 123456789 ;
int newNum = 0 ;
String str = String.valueOf(origNum) ;
StringBuffer buf = new StringBuffer(str) ;
str = buf.reverse().toString() ;
newNum = Integer.parseInt(str); - Arrays are fairly simple data structures that give you the ability to create a list of values and navigate through them. Use the math class log10 method to count the digits, create an array of integers to hold one digit at each index, and use modulo and other basic operators to deconstruct and reconstruct the number.
int origNum = 123456789 ;
int newNum = 0 ;
int count = (origNum == 0) ? 1 : (int)Math.log10(origNum) + 1 ;
int[] digits = new int[count] ;
int i ;
for (i = 0 ; i < count ; i++) {
digits[i] = origNum % 10 ;
origNum /= 10 ;
}
for (i = 0 ; i < count ; i++) {
newNum *= 10 ;
newNum += digits[i] ;
} - The Vector class is a complex data structure that holds a list of items, grows and shrinks dynamically, and provides methods for accessing the list in a manner similar to arrays. Unlike arrays, Vectors can hold a variety of different type objects, but for this example it will simply hold each digit of the integer. Since Vectors grow dynamically, and Enumerations automatically iterate through the list using the nextElement() and hasMoreElements() methods, there is no need to count the digits.
int origNum = 123456789 ;
int newNum = 0 ;
Vector digits = new Vector () ;
while (origNum > 0) {
digits.add(origNum % 10) ;
origNum /= 10 ;
}
for (Enumeration d = digits.elements() ; d.hasMoreElements() ; ) {
newNum *= 10 ;
newNum += Integer.parseInt(d.nextElement().toString()) ;
} - There are many other Java classes similar to Vector that could be explored using the number reversal exercise, such as List, Iterator, Queue, ArrayList, and LinkedList. The number reversal procedure can easily be written as a function so it can be called repeatedly in interactive programs and reused in multiple programs. Implementing classes to demonstrate the various methods is an excellent way to practice Java Object Oriented Programming techniques, such as interfaces and inheritance.
Modulo
String
Array
Vector
Summary
Source...