Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Sammy Samkough
- // NumberConverter
- // Spec: To convert numbers to different bases (Binary, Octal, Decimal, Hexadecimal).
- public class NumberConverter
- {
- private int decimal; // always stores the decimal equivalent, regardless of base
- private int base;
- private String strValue;
- private boolean b;
- /** default set to base 10 w/ a value of 0 */
- public NumberConverter()
- {
- decimal = 0;
- base = 10;
- strValue = "0";
- }
- /** base 10 value is used to set decimal, base and strValue
- * note: toBaseX can do this for you also */
- public NumberConverter(int value)
- {
- decimal = value;
- base = 10;
- strValue = toBaseX(10);
- }
- /** sets the strValue and base based on parameters
- * strValue is only stored in uppercase
- * decimal is set here as well provided newValue is valid */
- public NumberConverter(String newValue, int newBase)
- {
- strValue = newValue;
- base = newBase;
- }
- //**** Accessor Methods ****//
- public String getValue()
- {
- return strValue;
- }
- public int getBase()
- {
- return base;
- }
- public int getDecimal()
- {
- return decimal;
- }
- /** sets the strValue and base based on parameters
- * decimal is set here as well, with a call to baseXToDec() */
- public void setValue(String newValue, int newBase)
- {
- strValue = newValue;
- base = newBase;
- decimal = baseXToDec();
- b = this.isValid(); // validates the strValue by using the isValid() method
- }
- public boolean isValid()
- {
- boolean valid = true;
- //Checks for base 2
- if(base < 3)
- {
- for(int i = 0 ; i < strValue.length() - 1 ; i++)
- {
- if(charToValue(strValue.charAt(i)) < 0 || charToValue(strValue.charAt(i)) > 1)
- {
- valid = false;
- break;
- }
- else
- {
- valid = true;
- }
- }
- }
- //checks for bases 3 to 9
- else if(base < 10 && base > 2)
- {
- for(int i = 0 ; i < strValue.length() - 1 ; i++)
- {
- if(charToValue(strValue.charAt(i)) < 0 || charToValue(strValue.charAt(i)) >= 8)
- {
- valid = false;
- break;
- }
- else
- {
- valid = true;
- }
- }
- }
- //checks for 10 to 16
- else if(base >= 10 && base < 17)
- {
- for(int i = 0 ; i < strValue.length() - 1 ; i++)
- {
- if(charToValue(strValue.charAt(i)) < 0 || charToValue(strValue.charAt(i)) > 15)//this statement makes sure no value other than the known hex values occur
- {
- valid = false;
- break;
- }
- else
- {
- valid = true;
- }
- }
- }
- return valid;
- }
- /** base of this object is set to x and strValue is the String value in base x
- * strValue is also returned, just for good measure
- * @param x the number base to convert to */
- public String toBaseX(int x)
- {
- String strRev = "";
- strValue = "";
- int div = decimal;
- int mod = 0;
- base = x;
- if(b)
- {
- while(div != 0)
- {
- mod = div % base;
- if(mod >= 10) // the letter's number value
- {
- strValue += (char)(mod + 55);
- }
- else // numbers values for numbers 1 to 9
- {
- strValue += (char)(mod + 48);
- }
- div = (int)(div / base);
- strRev = reverseString(strValue); // reverses the strValue in order to return the accurate format
- }
- }
- else
- {
- strRev = "invalid";
- }
- return strRev;
- }
- /** take the currently stored strValue and calculate and return the decimal value */
- public int baseXToDec()
- {
- int exp = 0;
- decimal = 0;
- for(int i = strValue.length() - 1; i >= 0; i--) //goes from right to left on the strValue
- {
- decimal += charToValue(strValue.charAt(i)) * Math.pow(base, exp);
- exp++;
- }
- return decimal;
- }
- /** this is a helper method only
- * the integer value of digit is returned
- * -1 is the return value for an error
- * @param ch a valid digit for the given number base */
- private int charToValue(char ch)
- {
- int value = 0;
- switch(ch)
- {
- // gives values of 0-9 as 0-9 and A-F as 10-15
- case '0': value = 0;
- break;
- case '1': value = 1;
- break;
- case '2': value = 2;
- break;
- case '3': value = 3;
- break;
- case '4': value = 4;
- break;
- case '5': value = 5;
- break;
- case '6': value = 6;
- break;
- case '7': value = 7;
- break;
- case '8': value = 8;
- break;
- case '9': value = 9;
- break;
- case 'A': value = 10;
- break;
- case 'B': value = 11;
- break;
- case 'C': value = 12;
- break;
- case 'D': value = 13;
- break;
- case 'E': value = 14;
- break;
- case 'F': value = 15;
- break;
- // if none of the above occur, then the value is set to -1, which later in the isValid method will spell an error
- default: value = -1;
- break;
- }
- return value;
- }
- /** This is a helper method that returns strRev as a reversed version
- * @param strFwd the string to be reversed */
- public String reverseString(String strFwd)
- {
- String strReverse = "";
- for(int i = (strFwd.length() - 1); i >= 0; i--)
- {
- strReverse += strFwd.charAt(i);
- }
- return strReverse;
- }
- /** a String with the current base and value is returned */
- public String toString()
- {
- return "" + toBaseX(base);
- }
- }
- -----------------------------------------------------------------------------------------------------------------------------------
- // Sammy Samkough
- // NumberConverter
- // Spec: To convert numbers to different bases (Binary, Octal, Decimal, Hexadecimal).
- import java.util.Scanner;
- public class NumberConverterClient
- {
- public static void main(String args[])
- {
- boolean running = true;
- boolean playing = true;
- Scanner sc = new Scanner(System.in);
- NumberConverter nc = new NumberConverter();
- String v = "";
- String c = "";
- int b;
- while(running)
- {
- playing = true;
- System.out.print("What base do you want to start at? ");
- b = sc.nextInt();
- System.out.print("\nGood choice! Now enter the value you want converted: ");
- v = sc.next();
- v = v.toUpperCase();
- nc.setValue(v, b);
- System.out.println("\nYour number in binary is: " + nc.toBaseX(2));
- System.out.println("Your number in octal is: " + nc.toBaseX(8));
- System.out.println("Your number in decimal is: " + nc.toBaseX(10));
- System.out.println("Your number in base 12 is: " + nc.toBaseX(12));
- System.out.println("Your number in hexadecimal is: " + nc.toBaseX(16));
- System.out.println("----------------------------------------------------");
- System.out.println("Want to go again? Please choose either yes or no.");
- while(playing)
- {
- c = sc.next();
- if(c.equalsIgnoreCase("yes"))
- {
- System.out.println("----------------------------------------------------");
- playing = false;
- continue;
- }
- else if(c.equalsIgnoreCase("no"))
- {
- System.out.println("Bye-bye then!");
- playing = false;
- running = false;
- break;
- }
- else
- {
- System.out.println("Improper input. Try again");
- System.out.println("");
- continue;
- }
- } // smaller while loop, "playing"
- } // large while loop, "running"
- } // main method
- } // class
- /*
- What base do you want to start at? 16
- Good choice! Now enter the value you want converted: FFF
- Your number in binary is: 111111111111
- Your number in octal is: 7777
- Your number in decimal is: 4095
- Your number in base 12 is: 2453
- Your number in hexadecimal is: FFF
- ----------------------------------------------------
- Want to go again? Please choose either yes or no.
- yes
- ----------------------------------------------------
- What base do you want to start at? 16
- Good choice! Now enter the value you want converted: BaD
- Your number in binary is: 101110101101
- Your number in octal is: 5655
- Your number in decimal is: 2989
- Your number in base 12 is: 1891
- Your number in hexadecimal is: BAD
- ----------------------------------------------------
- Want to go again? Please choose either yes or no.
- yes
- ----------------------------------------------------
- What base do you want to start at? 16
- Good choice! Now enter the value you want converted: BAT
- Your number in binary is: 101110011111
- Your number in octal is: 5637
- Your number in decimal is: 2975
- Your number in base 12 is: 187B
- Your number in hexadecimal is: B9F
- ----------------------------------------------------
- Want to go again? Please choose either yes or no.
- yes
- ----------------------------------------------------
- What base do you want to start at? 10
- Good choice! Now enter the value you want converted: 942
- Your number in binary is: 1110101110
- Your number in octal is: 1656
- Your number in decimal is: 942
- Your number in base 12 is: 666
- Your number in hexadecimal is: 3AE
- ----------------------------------------------------
- Want to go again? Please choose either yes or no.
- yes
- ----------------------------------------------------
- What base do you want to start at? 8
- Good choice! Now enter the value you want converted: 747
- Your number in binary is: 111100111
- Your number in octal is: 747
- Your number in decimal is: 487
- Your number in base 12 is: 347
- Your number in hexadecimal is: 1E7
- ----------------------------------------------------
- Want to go again? Please choose either yes or no.
- yes
- ----------------------------------------------------
- What base do you want to start at? 12
- Good choice! Now enter the value you want converted: A359b
- Your number in binary is: 110100000110000111
- Your number in octal is: 640607
- Your number in decimal is: 213383
- Your number in base 12 is: A359B
- Your number in hexadecimal is: 34187
- ----------------------------------------------------
- Want to go again? Please choose either yes or no.
- yes
- ----------------------------------------------------
- What base do you want to start at? 2
- Good choice! Now enter the value you want converted: 1011101
- Your number in binary is: 1011101
- Your number in octal is: 135
- Your number in decimal is: 93
- Your number in base 12 is: 79
- Your number in hexadecimal is: 5D
- ----------------------------------------------------
- Want to go again? Please choose either yes or no.
- yes
- ----------------------------------------------------
- What base do you want to start at? 16
- Good choice! Now enter the value you want converted: 16>;3
- Your number in binary is: invalid
- Your number in octal is: invalid
- Your number in decimal is: invalid
- Your number in base 12 is: invalid
- Your number in hexadecimal is: invalid
- ----------------------------------------------------
- Want to go again? Please choose either yes or no.
- fad
- Improper input. Try again
- no
- Bye-bye then!
- Press any key to continue . . .
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement