Advertisement
Guest User

Untitled

a guest
Nov 18th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.10 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Main {
  4. private String type;
  5. private String output;
  6. private int number;
  7.  
  8. public Main(String type) {
  9. this.type = type;
  10.  
  11. }
  12. public void readNum() {
  13. Scanner num = new Scanner(System.in);
  14. System.out.println("podaj liczbe");
  15. this.number = num.nextInt();
  16. }
  17.  
  18. public void convert() {
  19. if(type.equals("1"))
  20. decToBin();
  21. else if(type.equals("2"))
  22. decToOct();
  23. else if(type.equals("3"))
  24. decToHex();
  25. }
  26.  
  27. public void decToBin() {
  28. int x;
  29. int num = this.number;
  30. String storage="";
  31. char dig[]={'0','1'};
  32. while(num>0)
  33. {
  34. x=num%2;
  35. storage=dig[x]+storage;
  36. num=num/2;
  37. }
  38. this.output = storage;
  39. }
  40.  
  41. public void decToOct() {
  42. int x;
  43. int num = this.number;
  44. String storage="";
  45. char dig[]={'0','1','2','3','4','5','6','7'};
  46. while(num>0)
  47. {
  48. x=num%8;
  49. storage=dig[x]+storage;
  50. num=num/8;
  51. }
  52. this.output = storage;
  53. }
  54.  
  55. public void decToHex() {
  56. int x;
  57. System.out.print("Geben Sie Ihre Dezimalzahl ein: ");
  58. int num = this.number;
  59. String storage="";
  60. char dig[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
  61. while(num>0)
  62. {
  63. x = num%16;
  64. storage = dig[x]+storage;
  65. num = num/16;
  66. }
  67. this.output = storage;
  68. }
  69.  
  70. public void resultOut() {
  71. System.out.println(this.output);
  72. }
  73.  
  74. public static void main(String[] args) {
  75. Scanner in = new Scanner(System.in);
  76. String result = "";
  77. System.out.println("Geben Sie 1 ein, um den Dez zu Bin Programm aufzurufen, 2 um den Dez zu Oktal oder 3 um den Dez zu Hexa aufzurufen");
  78. result = in.nextLine();
  79. Main rs = new Main(result);
  80. rs.readNum();
  81. rs.convert();
  82. rs.resultOut();
  83. }
  84.  
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement