Advertisement
Crenox

Number & NumberAnalyzer Java Program

Jan 9th, 2015
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.58 KB | None | 0 0
  1. // Name: Sammy Samkough
  2. // Prog: Number
  3. // Spec: Completing methods for a Number class and a Number Analyzer class that consists of an ArrayList of Number objects.
  4. // The Number class stores a number and has the ability to determine if a number is odd or perfect.
  5.  
  6. public class Number
  7. {
  8. private Integer number;
  9.  
  10. public Number()
  11. {
  12. number = 0;
  13. }
  14.  
  15. public Number(int num)
  16. {
  17. number = num;
  18. }
  19.  
  20. public void setNumber(int num)
  21. {
  22. number = new Integer(num);
  23. }
  24.  
  25. // gets the number
  26. public int getNumber()
  27. {
  28. return number;
  29. }
  30.  
  31. // checks if the number is odd
  32. public boolean isOdd()
  33. {
  34. if ((number % 2) == 0)
  35. {
  36. return false;
  37. }
  38. else
  39. {
  40. return true;
  41. }
  42. }
  43.  
  44. // checks if the number is a perfect number (all factors added up equals the total)
  45. public boolean isPerfect()
  46. {
  47. int temp = 0;
  48.  
  49. for(int i = 1; i <= number / 2; i++)
  50. {
  51. if(number % i == 0)
  52. {
  53. temp += i;
  54. }
  55. }
  56.  
  57. if(temp == number)
  58. {
  59. return true;
  60. }
  61. else
  62. {
  63. return false;
  64. }
  65. }
  66.  
  67. public String toString()
  68. {
  69. return number + "";
  70. }
  71. }
  72. -------------------------------------------------------------------------------------------------------------------------------
  73. // Name: Sammy Samkough
  74. // Prog: Number
  75. // Spec: Completing methods for a Number class and a Number Analyzer class that consists of an ArrayList of Number objects.
  76. // The Number class stores a number and has the ability to determine if a number is odd or perfect.
  77.  
  78. public class NumberRunner
  79. {
  80. public static void main(String args[])
  81. {
  82. int[] nums = {7, 28, 496, 1111, 199, 201, 17};
  83. Number x;
  84.  
  85. for(int num : nums)
  86. {
  87. x = new Number(num);
  88.  
  89. if(x.isOdd())
  90. {
  91. System.out.println(x + " is odd.");
  92. }
  93. else
  94. {
  95. System.out.println(x + " is not odd.");
  96. }
  97.  
  98. if(x.isPerfect())
  99. {
  100. System.out.println(x + " is perfect. \n");
  101. }
  102. else
  103. {
  104. System.out.println(x + " is not perfect. \n");
  105. }
  106. }
  107. }
  108. }
  109. /*
  110. 7 is odd.
  111. 7 is not perfect.
  112.  
  113. 28 is not odd.
  114. 28 is perfect.
  115.  
  116. 496 is not odd.
  117. 496 is perfect.
  118.  
  119. 1111 is odd.
  120. 1111 is not perfect.
  121.  
  122. 199 is odd.
  123. 199 is not perfect.
  124.  
  125. 201 is odd.
  126. 201 is not perfect.
  127.  
  128. 17 is odd.
  129. 17 is not perfect.
  130.  
  131. Press any key to continue . . .
  132. */
  133. -------------------------------------------------------------------------------------------------------------------------------
  134. // Name: Sammy Samkough
  135. // Prog: NumberAnalyzer
  136. // Spec: Completing methods for a Number class and a Number Analyzer class that consists of an ArrayList of Number objects.
  137. // The Number class stores a number and has the ability to determine if a number is odd or perfect.
  138.  
  139. import java.util.ArrayList;
  140. import java.util.Scanner;
  141.  
  142. public class NumberAnalyzer
  143. {
  144. private ArrayList<Number> list;
  145.  
  146. public NumberAnalyzer()
  147. {
  148. setList("");
  149. }
  150.  
  151. public NumberAnalyzer(String numbers)
  152. {
  153. setList(numbers);
  154. }
  155.  
  156. public void setList(String numbers)
  157. {
  158. list = new ArrayList<Number>();
  159. Scanner sc = new Scanner(numbers);
  160.  
  161. while(sc.hasNext())
  162. {
  163. list.add(new Number(sc.nextInt()));
  164. }
  165. }
  166.  
  167. // gets the isOdd() method from Number and analyzes it
  168. public int countOdds()
  169. {
  170. int oddCount = 0;
  171. int size = list.size();
  172.  
  173. for (int i = 0; i < size; i++)
  174. {
  175. if (list.get(i).isOdd())
  176. {
  177. oddCount++;
  178. }
  179. }
  180. return oddCount;
  181. }
  182.  
  183. // gets the isOdd() method from Number and analyzes it, then reverses it to get the evens
  184. public int countEvens()
  185. {
  186. int evenCount = 0;
  187. int size = list.size();
  188.  
  189. for (int i = 0; i < size; i++)
  190. {
  191. if (!list.get(i).isOdd())
  192. {
  193. evenCount++;
  194. }
  195. }
  196.  
  197. return evenCount;
  198. }
  199.  
  200. // gets the isPerfect() method from Number and analyzes it
  201. public int countPerfects()
  202. {
  203. int perfectCount = 0;
  204. int size = list.size();
  205.  
  206. for (int i = 0; i < size; i++)
  207. {
  208. if (list.get(i).isPerfect())
  209. {
  210. perfectCount++;
  211. }
  212. }
  213.  
  214. return perfectCount;
  215. }
  216.  
  217. public String toString()
  218. {
  219. return "";
  220. }
  221. }
  222. -------------------------------------------------------------------------------------------------------------------------------
  223. // Name: Sammy Samkough
  224. // Prog: NumberAnalyzer
  225. // Spec: Completing methods for a Number class and a Number Analyzer class that consists of an ArrayList of Number objects.
  226. // The Number class stores a number and has the ability to determine if a number is odd or perfect.
  227.  
  228. import java.util.ArrayList;
  229. import java.util.Scanner;
  230.  
  231. public class NumberAnalyzerRunner
  232. {
  233. public static void main(String args[])
  234. {
  235. NumberAnalyzer test = new NumberAnalyzer("5 12 9 6 1 4 8 6");
  236. System.out.print(test);
  237. System.out.println("odd count = " + test.countOdds());
  238. System.out.println("even count = " + test.countEvens());
  239. System.out.println("perfect count = " + test.countPerfects()+ "\n\n\n");
  240.  
  241. test.setList("5 12 3 7 28 496 81 65 33 11");
  242. System.out.println(test);
  243. System.out.println("odd count = " + test.countOdds());
  244. System.out.println("even count = " + test.countEvens());
  245. System.out.println("perfect count = " + test.countPerfects()+ "\n\n\n");
  246.  
  247. test.setList("1 2 3 4 5 6 7 8 11 13 151 16 17 18");
  248. System.out.println(test);
  249. System.out.println("odd count = " + test.countOdds());
  250. System.out.println("even count = " + test.countEvens());
  251. System.out.println("perfect count = " + test.countPerfects()+ "\n\n\n");
  252. }
  253. }
  254. /*
  255. odd count = 3
  256. even count = 5
  257. perfect count = 2
  258.  
  259.  
  260.  
  261.  
  262. odd count = 7
  263. even count = 3
  264. perfect count = 2
  265.  
  266.  
  267.  
  268.  
  269. odd count = 8
  270. even count = 6
  271. perfect count = 1
  272.  
  273.  
  274.  
  275. Press any key to continue . . .
  276. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement