SwitchDisplay.java 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. Console.println("Display Options"
  33. + "\n1: Binary"
  34. + "\n2: Octal"
  35. + "\n3: Decimal"
  36. + "\n4: Hexadecimal"
  37. + "\n5: Cancel - returns to Main Menu");
  38. }
  39. public String switchDisplayOutput(String mode, int baseInput) {
  40. switch (mode) {
  41. case "1":
  42. return toBinary(baseInput);
  43. case "2":
  44. return toOctal(baseInput);
  45. case "3":
  46. return toDecimal(baseInput);
  47. case "4":
  48. return toHexa(baseInput);
  49. case "5":
  50. return mode;
  51. default:
  52. return "Invalid Input";
  53. }
  54. }
  55. /**
  56. * Converts the user input into corresponding base types
  57. */
  58. public String toBinary(int userInput) {
  59. return Integer.toBinaryString(userInput);
  60. }
  61. public String toOctal(int userInput) {
  62. return Integer.toOctalString(userInput);
  63. }
  64. public String toDecimal(int userInput) {
  65. return Integer.toBinaryString(userInput);
  66. }
  67. public String toHexa(int userInput) {
  68. return Integer.toHexString(userInput);
  69. }
  70. }