Advertisement
Guest User

Untitled

a guest
Dec 10th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.72 KB | None | 0 0
  1. package programming.exp;
  2. import acm.program.ConsoleProgram;
  3.  
  4. public class ComplexMathSolver extends ConsoleProgram {
  5. @Override
  6. public void run() {
  7. HandyInt factor = new HandyInt(5);
  8. HandyInt exp = new HandyInt(3);
  9.  
  10. HandyInt result = multiplyWithPowerOfTwo(factor, exp);
  11.  
  12. println(factor + " * 2^" + exp + " = " + result);
  13. }
  14.  
  15. /**
  16. * Returns the result of multiplying {@code factor} with 2 to the power of
  17. * {@code exp}.
  18. *
  19. * @param factor
  20. * the handy integer to multiply with the power.
  21. * @param exp
  22. * the exponent of the power.
  23. * @return the result of the multiplication.
  24. */
  25. private HandyInt multiplyWithPowerOfTwo(HandyInt factor, HandyInt exp) {
  26. HandyInt one = new HandyInt(1);
  27.  
  28. while (exp.getInt() > 0) {
  29. factor = factor.add(factor);
  30. exp = exp.sub(one);
  31. }
  32. return factor;
  33. }
  34. }
  35.  
  36. --------------------------------------------------------------------------------------------------------------------------------
  37.  
  38. package programming.exp;
  39.  
  40. /**
  41. * An integer that provides arithmetic operations for great glory.
  42. */
  43. public class HandyInt {
  44. /** The integer represented by an instance of this class. */
  45. private int theInt;
  46.  
  47. /**
  48. * Constructs a new handy integer representing the given int.
  49. *
  50. * @param theInt
  51. * the integer to be represented by this instance.
  52. */
  53. public HandyInt(int theInt) {
  54. this.theInt = theInt;
  55. }
  56.  
  57. /**
  58. * Constructs a new handy integer representing the integer represented by
  59. * the given handy integer.
  60. *
  61. * @param handyInt
  62. * the handy integer to intialize the new object with.
  63. */
  64. public HandyInt(HandyInt handyInt) {
  65. this.theInt = handyInt.theInt;
  66. }
  67.  
  68. /**
  69. * Returns the integer represented by this instance.
  70. *
  71. * @return the represented integer.
  72. */
  73. public int getInt() {
  74. return theInt;
  75. }
  76.  
  77. /**
  78. * Returns a handy integer that represents the sum of this and the other
  79. * handy integer.
  80. *
  81. * @param other
  82. * the handy integer to add.
  83. * @return sum of the two handy integers.
  84. */
  85. public HandyInt add(HandyInt other) {
  86. theInt += other.theInt;
  87. return this;
  88. }
  89.  
  90. /**
  91. * Returns a handy integer that represents the result of subtracting the
  92. * other integer from this one.
  93. *
  94. * @param other
  95. * the handy integer to subtract from this one.
  96. * @return difference of the two handy integers.
  97. */
  98. public HandyInt sub(HandyInt other) {
  99. theInt -= other.theInt;
  100. return this;
  101. }
  102.  
  103. @Override
  104. public String toString() {
  105. return Integer.toString(theInt);
  106. }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement