Posted By


trusktr on 10/17/10

Tagged


Statistics


Viewed 736 times
Favorited by 0 user(s)

cisp401 Number.java


/ Published in: Java
Save to your folder(s)



Copy this code and paste it in your HTML
  1. import java.util.*;
  2.  
  3. public class Number {
  4.  
  5. /* Data Objects */
  6. private double value;
  7.  
  8. /* Constructors */
  9. public Number(){
  10. value = 0;
  11. }
  12. public Number(double inputvalue){
  13. value = inputvalue;
  14. }
  15. public Number(Number n){
  16. value = n.value;
  17. }
  18.  
  19. /* Accessors */
  20. public double value(){
  21. double n = value;
  22. return n;
  23. }
  24.  
  25. /* Mutators */
  26. public void set_value(double inputvalue){
  27. value = inputvalue;
  28. }
  29. public void set_value(Number inputvalue){
  30. value = inputvalue.value;
  31. }
  32. public MyString myString(int n){
  33. Formatter fmt = new Formatter();
  34. MyString output = new MyString("" + fmt.format("%."+n+"f", value));
  35. return output;
  36.  
  37. // MY OWN ALGORITHM FOR SETTING THE PRECISION OF MY NUMBER.
  38. // // say we start with value=17.55745 and we want a decimal precision of n=2.
  39. // // make an int version of double:
  40. // int b = (int)value; // b == 17
  41. // // subtract int version from double to leave the decimal places:
  42. // double c = value - b; // c = 17.55745 - 17 == .55745
  43. // // multiply by (10^n) to find the decimals.
  44. // double d = c * Math.pow(10,n); // d == 55.745
  45. // // make another int of the decimals:
  46. // int e = (int)d; // e == 55
  47. // // subtract the int from the decimal double:
  48. // double f = d - e; // f == .745
  49. // // if f is < 0.5 then leave e alone. // e == 55
  50. // // if f is >= 0.5
  51. // if ( f >= 0.5 ) {
  52. // // then add 1 to e:
  53. // e++; // e == 56
  54. // }
  55. // // now output the parts as a string:
  56. // MyString output = new MyString("" + b + "." + e);
  57. // return output;
  58.  
  59.  
  60. // // say we start with value=17.00745 and we want a decimal precision of n=2.
  61. // // make an int version of double:
  62. // int b = (int)value; // b == 17
  63. // // subtract int version from double to leave the decimal places:
  64. // double c = value - b; // c = 17.00745 - 17 == .00745
  65. // // multiply by (10^n) to find the decimals.
  66. // double d = c * Math.pow(10,n); // d == 00.745
  67. // // make another int of the decimals:
  68. // int e = (int)d; // e == 0
  69. // if (e < Math.pow(10,n-1)) {
  70. //
  71. // }
  72. // // subtract the int from the decimal double:
  73. // double f = d - e; // f == .745
  74. // // if f is < 0.5 then leave e alone. // e == 55
  75. // // if f is >= 0.5
  76. // if ( f >= 0.5 ) {
  77. // // then add 1 to e:
  78. // e++; // e == 56
  79. // }
  80. // // now output the parts as a string:
  81. // MyString output = new MyString("" + b + "." + e);
  82. // return output;
  83. }
  84. public MyString myString(){
  85. return new MyString("" + value);
  86. }
  87. public String toString(){
  88. return "" + value;
  89. }
  90.  
  91. public Number plus(Number n){
  92. return new Number(value + n.value);
  93. }
  94. public Number plus(double n){
  95. return new Number(value + n);
  96. }
  97. public Number minus(Number n){
  98. return new Number(value - n.value);
  99. }
  100. public Number minus(double n){
  101. return new Number(value - n);
  102. }
  103. public Number times(Number n){
  104. return new Number(value * n.value);
  105. }
  106. public Number times(double n){
  107. return new Number(value * n);
  108. }
  109. public Number over(Number n){
  110. return new Number(value / n.value);
  111. }
  112. public Number over(double n){
  113. return new Number(value / n);
  114. }
  115. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.