SwitchDisplay.java 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. String mode = "Invalid Input";
  33. while (mode.equals("Invalid Input")) {
  34. int baseInput = Console.getIntegerInput("Enter an integer to see the base conversions.");
  35. Console.println("Display Options"
  36. + "\n1: Binary"
  37. + "\n2: Octal"
  38. + "\n3: Decimal"
  39. + "\n4: Hexadecimal"
  40. + "\n5: Cancel - returns to Main Menu");
  41. // Asks for the mode
  42. mode = Console.getStringInput("Enter the desired display mode: ");
  43. String convertedInput = switchDisplayOutput(mode, baseInput);
  44. Console.println(convertedInput);
  45. // passes to switchDisplayOutput(mode) >> returns string
  46. // displays the returned string
  47. // while loops !cancel
  48. }
  49. }
  50. public String switchDisplayOutput(String mode, int baseInput) {
  51. switch (mode) {
  52. case "1":
  53. return toBinary(baseInput);
  54. case "2":
  55. return toOctal(baseInput);
  56. case "3":
  57. return toDecimal(baseInput);
  58. case "4":
  59. return toHexa(baseInput);
  60. case "5":
  61. return mode;
  62. default:
  63. return "Invalid Input";
  64. }
  65. }
  66. /**
  67. * Converts the user input into corresponding base types
  68. */
  69. public String toBinary(int userInput) {
  70. return Integer.toBinaryString(userInput);
  71. }
  72. public String toOctal(int userInput) {
  73. return Integer.toOctalString(userInput);
  74. }
  75. public String toDecimal(int userInput) {
  76. return Integer.toBinaryString(userInput);
  77. }
  78. public String toHexa(int userInput) {
  79. return Integer.toHexString(userInput);
  80. }
  81. }