Thursday, May 31, 2012
Programming : Get base( change any integer to any base )
This program gets the base and positive inter from a user then, convert the integer to the base entered
and checks the result using honor method
For example, following is the output on the screen
Enter base(> 1) 2
Enter Integer(>0) 68
1b6 0b5 0b4 0b3 1b2 0b 0
is the base b=2 expansion of 68
/**
* @author Hun Wie <hoonTheo@gmail.com>
* @date 2011.0224
*/
package getbase;
import java.util.*;
public class Main {
private static final int INITIAL = 100;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner in = new Scanner( System.in);
int base = -1;
int number = -3;
boolean isCorrectBase = false; // flag for a correct condition
while (!isCorrectBase) { // loop while the entered value > 1
try {
System.out.print("Enter base(> 1) ");
base = in.nextInt();
if ( base > 1)
{
isCorrectBase = true; // integer has been entered
}
else
{
System.out.print("You entered base <= 1 ");
// isCorrectBase = false;
}
}
catch (Exception e)
{
System.out.println("You entered wrong input");
in.nextLine();
}
}//end of while
boolean isCorrectExpansion = false; // flag for a correct condition
while (!isCorrectExpansion) { // loop while the entered value is not an integer
try {
System.out.print("Enter Integer(>0) ");
number = in.nextInt();
if ( number > 0)
{
isCorrectExpansion = true; // integer has been entered
}
else
{
System.out.print("You entered Integer <=0 ");
// isCorrectExpansion = false;
}
}
catch (Exception e)
{
System.out.println("You entered wrong input");
in.nextLine();
}
}//end of while
int [] array = new int[INITIAL];
int remainder = 100;
//count for how many number should be in the array
int count = 0;
// runs until the number is greater than or equal to base
while (number >= base)
{
remainder = number % base ;
array[count] = remainder;
count++;
number = number/base;
}
array[count] = number;
int index = count;
//copy the original array to display
int [] copyArray = new int [count + 1];
//array copy
for ( int i = 0; i < copyArray.length; i++)
{
copyArray[i] = array[count--];
}
display(copyArray, index);
// check if result is same with honorMethod
int result = honorMethod( base, array);
System.out.println("is the base b=" + base + " expansion of " + result);
}
/**
* display methods
* @param array[] copied array to be displayed
* @param index original array
*/
public static void display ( int [] array, int index)
{
for (int i =0; i <array.length; i++)
{
if(index > 1)
{
System.out.print(array[i] + "b" + index-- + " ");
}
else if (index == 1)
{
System.out.print(array[i] + "b ");
index--;
}
else
{
System.out.println(array[i]);
}
}
} //end for display
/**
* honor methods
* @param x base
* @param array[] original array
* @return result
*/
public static int honorMethod(int base, int [] array)
{
int order = array.length - 1;
int result = array[order];
for (int i = order - 1 ; i >= 0 ; --i )
{
result = result * base + array[i];
}
return result;
}
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment