Advertisement
Guest User

Untitled

a guest
May 28th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.21 KB | None | 0 0
  1. /**
  2. * APNum class
  3. * You must use basic ADTs in your implementation.
  4. * The number of digits in an APnum should only be limited by the availability of memory
  5. *
  6. * Don't forget about other classes you need to write.
  7. * Please make sure your code is readable. Code style will be marked as well as code correctness.
  8. */
  9.  
  10. int num;
  11. int x = 0; //Used in APnextdigit
  12.  
  13. public APnum(){
  14. this.num=0;
  15. }
  16.  
  17. private int getNum(APnum n){
  18. return n.num;
  19. }
  20.  
  21. public class APnum {
  22.  
  23.  
  24.  
  25.  
  26.  
  27. /**
  28. * Adds a digit represented by the character supplied in the second argument as the next
  29. * least significant digit to the value of the fi rst argument.
  30. * (Thus, the value of the first argument is updated.)
  31. * @param n ADT argument
  32. * @param c one of the characters '0', '1', '2', '3', '4', '5', '6', '7', '8' or '9'.
  33. */
  34. public static void APin(APnum n, char c){
  35. num = getNum(n)*10 + (Integer.parseInt(Character.toString(c))).intValue();
  36. }
  37.  
  38. /**
  39. * Takes as its input a reference to an APnum and complements i
  40. * @param n ADT argument
  41. */
  42. public static void APneg(APnum n){
  43. num = getNum( n) * (-1);
  44. }
  45.  
  46. /**
  47. * Adds the APnum supplied in the first argument to the APNumber supplied in the second
  48. * argument. The value of the first APNumber is updated with the new value while the value
  49. * of the second APNumber remains unchanged
  50. * @param operand1 first ADT argument
  51. * @param operand2 second ADT argument
  52. */
  53. public static void APadd(APnum operand1, APnum operand2){
  54. num = getNum(operand1) + getNum(operand2);
  55. // Fill your code in here, required time O(n)
  56. }
  57.  
  58. /**
  59. * Compares the values of the two APnums supplied as arguments and returns true if
  60. * they are equal and false otherwise. The values of the supplied APnums must not change
  61. * @param operand1 first ADT argument
  62. * @param operand2 second ADT argument
  63. * @return true if operand1 and operand 2 are equal and false otherwise
  64. */
  65. public static boolean APeq(APnum operand1, APnum operand2){
  66. if (getNum(operand1) == getNum(operand2)){
  67. return true;
  68. }
  69. return false;
  70. }
  71.  
  72. /**
  73. * Compares the values of the two APnums supplied as arguments and returns true if
  74. * the first is strictly less than the second and false otherwise. The values of the supplied
  75. * APnums must not change
  76. * @param operand1 first ADT argument
  77. * @param operand2 second ADT argument
  78. * @return true if first operand is strictly less than the second operand and false otherwise
  79. */
  80. public static boolean APless(APnum operand1, APnum operand2){
  81. if (getNum(operand1) < getNum(operand2)){
  82. return true;
  83. }
  84. return false;
  85. }
  86.  
  87. /**
  88. *
  89. * @param n ADT argument
  90. * @return the most significant digit of the absolute value of its argument if the value is non-zero.
  91. * Otherwise, returns the blank character ' '
  92. */
  93. public static char APfirstdigit(APnum n){
  94. num=Integer.toString(getNum(n)).charAt(0);
  95. // Fill your code in here, required time O(1)
  96. }
  97.  
  98. /**
  99. *
  100. * @param n ADT argument
  101. * @return the next most significant digit of the absolute value of its argument if it exists.
  102. * Otherwise, returns the blank character ' '
  103. */
  104. public static char APnextdigit(APnum n){
  105. if(x < Integer.toString(getNum(n)).length()){
  106. return Integer.toString(getNum(n)).charAt(x);
  107. }
  108. return " ";
  109. }
  110.  
  111.  
  112. public class APsystem {
  113. /**
  114. * Place for class members
  115. * (not necessary, depending on implementation there can be members and there can be no members)
  116. */
  117.  
  118. /**
  119. * Will be invoked at the start of the tests, initializer of APSystem contents.
  120. * Can be empty if you don't need to initialize anything in APsystem
  121. * Tests will start according to the following example:
  122. * APsystem aps = new APsystem();
  123. * aps.APinit();
  124. * APnum n = aps.APzero();
  125. * APnum.APin(n,'1');
  126. */
  127. public void APinit(){
  128. // Fill your code in here
  129. }
  130. /**
  131. *
  132. * @return a new APNumber with the value 0
  133. */
  134. public APnum APzero(){
  135. APnum n = new APnum();
  136. }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement