Advertisement
Guest User

Untitled

a guest
Jan 21st, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 KB | None | 0 0
  1. //Name: Michael Zhan
  2. //Date: 20180110
  3. //Lab: Occurences
  4.  
  5.  
  6. import java.util.Scanner;
  7. import static java.lang.System.*;
  8.  
  9.  
  10. public class Occurrences
  11. {
  12. private int[] list;
  13.  
  14. public Occurrences(String line)
  15. {
  16. Scanner counter = new Scanner(line);
  17. int count = 0;
  18. int trash = 0;
  19. while (counter.hasNextInt())
  20. {
  21. trash=counter.nextInt();
  22. count++;
  23. }
  24.  
  25. list = new int[count];
  26.  
  27. Scanner chopper = new Scanner(line);
  28. int i =0;
  29. while (chopper.hasNextInt())
  30. {
  31. list[i]=chopper.nextInt();
  32. i++;
  33. }
  34. }
  35.  
  36. public void setList(String line)
  37. {
  38. Scanner counter = new Scanner(line);
  39. int count = 0;
  40. int trash = 0;
  41. while (counter.hasNextInt())
  42. {
  43. trash=counter.nextInt();
  44. count++;
  45. }
  46.  
  47. list = new int[count];
  48.  
  49. Scanner chopper = new Scanner(line);
  50. int i =0;
  51. while (chopper.hasNextInt())
  52. {
  53. list[i]=chopper.nextInt();
  54. i++;
  55. }
  56. }
  57.  
  58. public String toString()
  59. {
  60. String output="";
  61. int zero=0;
  62. int one=0;
  63. int two=0;
  64. int three=0;
  65. int four=0;
  66. int five=0;
  67. int six=0;
  68. int seven=0;
  69. int eight=0;
  70. int nine=0;
  71.  
  72. for (int x: list)
  73. {
  74. if (x==0)
  75. zero++;
  76. if (x==1)
  77. one++;
  78. if (x==2)
  79. two++;
  80. if (x==3)
  81. three++;
  82. if (x==4)
  83. four++;
  84. if (x==5)
  85. five++;
  86. if (x==6)
  87. six++;
  88. if (x==7)
  89. seven++;
  90. if (x==8)
  91. eight++;
  92. if (x==9)
  93. nine++;
  94. }
  95.  
  96. output += "Number\t\t\tOccurrences";
  97. output+= "\n\n0\t\t-\t\t"+zero+" ";
  98. for (int i=0; i<zero; i++)
  99. output += ("*");
  100. output+= "\n\n1\t\t-\t\t"+one+" ";
  101. for (int i=0; i<one; i++)
  102. output += ("*");
  103. output+= "\n\n2\t\t-\t\t"+two+" ";
  104. for (int i=0; i<two; i++)
  105. output += ("*");
  106. output+= "\n\n3\t\t-\t\t"+three+" ";
  107. for (int i=0; i<three; i++)
  108. output += ("*");
  109. output+= "\n\n4\t\t-\t\t"+four+" ";
  110. for (int i=0; i<four; i++)
  111. output += ("*");
  112. output+= "\n\n5\t\t-\t\t"+five+" ";
  113. for (int i=0; i<five; i++)
  114. output += ("*");
  115. output+= "\n\n6\t\t-\t\t"+six+" ";
  116. for (int i=0; i<six; i++)
  117. output += ("*");
  118. output+= "\n\n7\t\t-\t\t"+seven+" ";
  119. for (int i=0; i<seven; i++)
  120. output += ("*");
  121. output+= "\n\n8\t\t-\t\t"+eight+" ";
  122. for (int i=0; i<eight; i++)
  123. output += ("*");
  124. output+= "\n\n9\t\t-\t\t"+nine+" ";
  125. for (int i=0; i<nine; i++)
  126. output += ("*");
  127. output += "\n\n";
  128. return output;
  129. }
  130. }
  131. =======================================================================================================================================
  132. //Name: Michael Zhan
  133. //Date: 20180110
  134. //Lab: Occurences Runner
  135.  
  136. public class OccurrencesRunner
  137. {
  138. public static void main( String [] args)
  139. {
  140. Occurrences test = new Occurrences("1 5 3 4 5 5 5 4 3 2 5 5 5 3 0 4 5");
  141. System.out.println(test);
  142.  
  143. test.setList("2 3 4 5 6 7 8 9 0 2 3 5 6 8 8 8 9 4 5");
  144. System.out.println(test);
  145.  
  146. test.setList("2 3 4 5 6 7 8 2 0 2 3 5 6 8 8 8 9 4 5");
  147. System.out.println(test);
  148.  
  149. System.out.println ("program completed by: Michael Zhan");
  150. }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement