Advertisement
Guest User

Untitled

a guest
Aug 26th, 2016
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.24 KB | None | 0 0
  1. Advanced Java Homework – Base Conversions
  2.  
  3. Write a program that performs the following tasks:
  4.  
  5. Display a friendly greeting to the user
  6. Prompt the user for the value to convert (a String)
  7. Accept that String
  8. Prompt the user for the value of the initial base (an integer)
  9. Accept that integer
  10. Prompt the user for the desired base of the output (an integer)
  11. Accept that integer
  12. If the String is not a legal expression of a number in the initial base, display an error message and exit the program.
  13. Convert the value represented by the String in the initial base to the desired base.
  14. Display the result.
  15.  
  16. The program should also accept all three parameters from the command line if provided. Command-line parameters are accepted in the same order as the input prompts. We will accept any base from 2 to 36. Base 36 uses the ten integers 0-9 and the twenty-six letters A-Z.
  17.  
  18. The validator and the actual conversion routine should be contained in methods:
  19.  
  20. public static boolean isValidInteger(String theValue, int theBase){
  21. // contract: returns true if theInteger is a valid expression in theBase;
  22. // false otherwise.
  23.  
  24. public static String convertInteger(String theValue, int initialBase, int finalBase){
  25. // contract: converts theInteger from initialBase to finalBase and returns the
  26. // result as a String.
  27. // precondition: theValue must be a valid expression in initialBase.
  28.  
  29. Note that for every method that you write, you should state the contract and any preconditions as shown above. If the preconditions aren’t met, the method should throw an exception but we don’t know how to do that yet, so just exit the program.
  30.  
  31. Note that Java will do all of this for you in one line of code. Do not use these built-in functions but write the algorithms yourself. In addition, you’ll need to use the BigInteger class, which allows arbitrary-length integers. So your conversion algorithm is:
  32.  
  33. Check the validity of the input String
  34. Convert the input String into a BigInteger (in base 10) as shown in class.
  35. Convert that BigInteger into a String representation of that value in the desired target base as shown in class.
  36.  
  37. We will go over the algorithm and review command-line parameters in class.
  38.  
  39. Submit your Java source code in the Moodle drop box.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement