SwitchDisplay.java 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /**
  2. * Write a description of class SwitchDisplay here.
  3. *
  4. * Scientific Calculator Lab
  5. *
  6. * Brings up the switch display menu
  7. * Binary
  8. * Octal
  9. * Decimal
  10. * Hexadecimal
  11. *
  12. * Changes the mode for EVERYTHING
  13. *
  14. * Find a way to store the input and the input as a diff base
  15. * then whenever the base is changed, display the base.
  16. * pass the input into the methods to return the string.
  17. * EVERY TIME AN INPUT IS PLACED, IT WILL BE DISPLAYED AS AN OCTAL
  18. * boolean baseChange == true;
  19. * display the changed display
  20. */
  21. public class SwitchDisplay
  22. {
  23. // instance variables - replace the example below with your own
  24. /**
  25. * Switch Display Mode method
  26. * -> binary, octal, decimal, hexadecimal
  27. * switchDisplayMode() rotates through the options
  28. * switchDisplayMode(String mode) sets the display to the mode given
  29. */
  30. public void switchDisplay()
  31. {
  32. int baseInput = Console.getIntegerInput("Enter an integer to see the base conversions.");
  33. // try catch for int vs double or other input
  34. Console.println("Display Options"
  35. + "\nBinary"
  36. + "\nOctal"
  37. + "\nDecimal"
  38. + "\nHexadecimal"
  39. + "\nCancel: returns to Main Menu");
  40. // Accepts next input to display mode.
  41. String mode = Console.getStringInput("Enter the base option to display.").toLowerCase();
  42. if (mode.equals("binary") == true) {
  43. Console.println(baseInput + "in binary: " + toBinary(baseInput));
  44. } else if (mode.equals("octal") == true) {
  45. Console.println(baseInput + "in octal: " + toOctal(baseInput));
  46. } else if (mode.equals("decimal") == true) {
  47. Console.println(baseInput + "in decimal: " + baseInput);
  48. } else if (mode.equals("hexadecimal") == true) {
  49. Console.println(baseInput + "in hexadecimal: " + toHexa(baseInput));
  50. } else if (mode.equals("cancel") == true) {
  51. Console.println("Returning to Main Menu");
  52. // calls back to main application menu
  53. } else {
  54. Console.println("Invalid input.");
  55. }
  56. }
  57. /**
  58. * Converts the user input into corresponding base types
  59. */
  60. public String toBinary(int userInput) {
  61. return Integer.toBinaryString(userInput);
  62. }
  63. public String toOctal(int userInput) {
  64. return Integer.toOctalString(userInput);
  65. }
  66. public String toDecimal(int userInput) {
  67. return Integer.toBinaryString(userInput);
  68. }
  69. public String toHexa(int userInput) {
  70. return Integer.toHexString(userInput);
  71. }
  72. }