Advertisement
Guest User

Untitled

a guest
Oct 25th, 2016
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.15 KB | None | 0 0
  1. package edu.wit.comp1050.chunga.ica2.part2;
  2.  
  3. public class MyInteger {
  4. // Private utility class constraints
  5. private final static int DEFAULT_VALUE = 0;
  6. private final static String DEFAULT_FORMAT = "%,d";
  7.  
  8. // Instance variable
  9. private static int value;
  10.  
  11. // Constructor to set default value
  12. public MyInteger() {
  13. this(DEFAULT_VALUE);
  14. value = DEFAULT_VALUE;
  15. }
  16.  
  17. // Constructor to set specified value
  18. public MyInteger(int initialValue) {
  19. value = initialValue;
  20. }
  21.  
  22. // Getter method
  23. public static int valueOf() {
  24. return value;
  25. }
  26.  
  27. // Setter method
  28. public static int setValue(int initialValue) {
  29. return value = initialValue;
  30. }
  31.  
  32. // Boolean check method for value
  33. public boolean equals(int otherValue) {
  34. return value == otherValue;
  35. }
  36.  
  37. // String method
  38. @Override
  39. public String toString() {
  40. return toString(value);
  41. }
  42.  
  43. // String format method
  44. private static String toString(int valueToFormat) {
  45. return String.format(DEFAULT_FORMAT, valueToFormat);
  46. }
  47.  
  48. // Boolean check for if value is zero
  49. public boolean isZero() {
  50. return isZero(value);
  51. }
  52.  
  53. // Boolean check for if testing value is zero
  54. public static boolean isZero(int valueToTest) {
  55. return valueToTest == 0;
  56. }
  57.  
  58. // Boolean check for if value is even
  59. public boolean isEven() {
  60. return isEven(value);
  61. }
  62.  
  63. // Boolean check for if testing value is even
  64. public static boolean isEven(int valueToTest) {
  65. return (valueToTest % 2) == 0;
  66. }
  67.  
  68. // Boolean check for if value is odd
  69. public boolean isOdd() {
  70. return isOdd(value);
  71. }
  72.  
  73. // Boolean check for if testing value is odd
  74. public static boolean isOdd(int valueToTest) {
  75. return (valueToTest % 2) != 0;
  76. }
  77.  
  78. // Increases value by 1
  79. public int increment() {
  80. return ++value;
  81. }
  82.  
  83. // Decreases value by 1
  84. public int decrement() {
  85. return --value;
  86. }
  87.  
  88. // Method using + operator for value
  89. public int add(int adder) {
  90. value = value + adder;
  91. return value;
  92. }
  93.  
  94. // Method using - operator for value
  95. public int subtract(int subtractor) {
  96. value = value - subtractor;
  97. return value;
  98. }
  99.  
  100. // Method using * operator for value
  101. public int multiplyBy(int multiplier) {
  102. value = value * multiplier;
  103. return value;
  104. }
  105.  
  106. // Method using / operator for value
  107. public int divideBy(int divisor) {
  108. value = value / divisor;
  109. return value;
  110. }
  111.  
  112. // Method using % operator for value
  113. public int remainder(int remainder) {
  114. value = value % remainder;
  115. return value;
  116. }
  117.  
  118. @Override
  119. public boolean equals(Object otherObject) {
  120. MyInteger otherMyInteger;
  121. if (otherObject instanceof MyInteger){
  122. otherMyInteger = (MyInteger) otherObject;
  123. } else {
  124. }
  125. return false;
  126. }
  127.  
  128. public int add(MyInteger adder) {
  129.  
  130. return 0;
  131. }
  132.  
  133. public int subtract(MyInteger subtractor) {
  134.  
  135. return 0;
  136. }
  137.  
  138. public int multiplyBy(MyInteger multiplier) {
  139.  
  140. return 0;
  141. }
  142.  
  143. public int divideBy(MyInteger divisor) {
  144.  
  145. return 0;
  146. }
  147.  
  148. public boolean isPrime() {
  149. return isPrime(value);
  150. }
  151.  
  152. public static boolean isPrime(MyInteger valueToTest) {
  153. if (valueToTest == 1 || valueToTest == 2) {
  154. return true;
  155. } else {
  156. for (int i = 2; i< valueToTest; i++) {
  157. if (i % valueToTest == 0) {
  158. return false;
  159. }
  160. }
  161. }
  162. return true;
  163. }
  164.  
  165. public static int parseInt(char[] parser) {
  166.  
  167. return 0;
  168. }
  169.  
  170. // Main method to test all methods
  171. public static void main(String[] args) {
  172. MyInteger value = new MyInteger();
  173. System.out.println("The default value is: " + value);
  174. System.out.println("Is value equal to 0? " + value.isZero());
  175. System.out.println("Is value even? " + value.isEven());
  176. System.out.println("Is value odd? " + value.isOdd());
  177. System.out.println("Is value equal to 5? " + value.equals(5));
  178. System.out.println("");
  179.  
  180. MyInteger integer = new MyInteger(5);
  181. System.out.println("The specified integer is: " + integer);
  182. System.out.println("With specified integer, value has been changed to: " + valueOf());
  183. System.out.println("Value has been changed to " + setValue(1000));
  184. System.out.println("Value has incremented, the new value is: " + value.increment());
  185. System.out.println("Value has decremented, the new value is: " + value.decrement());
  186. System.out.println("Value in string format with grouping separators is: " + value.toString());
  187. System.out.println("");
  188.  
  189. System.out.println("5000 in string form with grouping seperators: " + MyInteger.toString(5000));
  190. System.out.println("");
  191.  
  192. System.out.println("Is 400 equal to 0? " + isZero(400));
  193. System.out.println("Is 400 even? " + isEven(400));
  194. System.out.println("Is 400 odd? " + isOdd(400));
  195. System.out.println("");
  196.  
  197. System.out.println("Value is currently: " + value);
  198. System.out.println("Value added by 5 equals: " + value.add(5));
  199. System.out.println("Value subtracted by 4 equals: " + value.subtract(4));
  200. System.out.println("Value multipled by 2 equals: " + value.multiplyBy(2));
  201. System.out.println("Value divided by 2 equals: " + value.divideBy(2));
  202. System.out.println("The remainder of " + value + " divided by 2 equals: " + value.remainder(2));
  203. System.out.println("The new value is now: " + value);
  204. }
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement