Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4.  
  5. public class Array<E> {
  6.  
  7. private E data[]; // declared to be an Object since it would be too
  8. // complicated with generics
  9. private int size;
  10.  
  11. public Array(int size) {
  12. data = (E[]) new Object[size];
  13. this.size = size;
  14. }
  15.  
  16. public void set(int position, E o) {
  17. if (position >= 0 && position < size)
  18. data[position] = o;
  19. else
  20. System.out.println("Ne moze da se vmetne element na dadenata pozicija");
  21. }
  22.  
  23. public E get(int position) {
  24. if (position >= 0 && position < size)
  25. return data[position];
  26. else
  27. System.out.println("Ne e validna dadenata pozicija");
  28. return null;
  29. }
  30.  
  31. public int getLength() {
  32. return size;
  33. }
  34.  
  35. public int find(E o) {
  36. for (int i = 0; i < size; i++) {
  37. if (o.equals(data[i]))
  38. return i;
  39. }
  40. return -1;
  41. }
  42.  
  43. public void insert(int position, E o) {
  44. // before all we check if position is within range
  45. if (position >= 0 && position <= size) {
  46. // first resize the storage array
  47. E[] newData = (E[]) new Object[size + 1];
  48. // copy the data prior to the insertion
  49. for (int i = 0; i < position; i++)
  50. newData[i] = data[i];
  51. // insert the new element
  52. newData[position] = o;
  53. // move the data after the insertion
  54. for (int i = position; i < size; i++)
  55. newData[i + 1] = data[i];
  56. // replace the storage with the new array
  57. data = newData;
  58. size = size + 1;
  59. }
  60. }
  61.  
  62. public void delete(int position) {
  63. // before all we check if position is within range
  64. if (position >= 0 && position < size) {
  65. // first resize the storage array
  66. E[] newData = (E[]) new Object[size - 1];
  67. // copy the data prior to the delition
  68. for (int i = 0; i < position; i++)
  69. newData[i] = data[i];
  70. // move the data after the deletion
  71. for (int i = position + 1; i < size; i++)
  72. newData[i - 1] = data[i];
  73. // replace the storage with the new array
  74. data = newData;
  75. size = size - 1;
  76. }
  77. }
  78.  
  79. public void resize(int newSize) {
  80. // first resize the storage array
  81. E[] newData = (E[]) new Object[newSize];
  82. // copy the data
  83. int copySize = size;
  84. if (newSize < size)
  85. copySize = newSize;
  86. for (int i = 0; i < copySize; i++)
  87. newData[i] = data[i];
  88. // replace the storage with the new array
  89. data = newData;
  90. size = newSize;
  91. }
  92.  
  93.  
  94.  
  95. public static int brojDoProsek(Array<Integer> niza) {
  96. // Vashiot kod tuka...
  97. int dolzina = niza.size;
  98. double prosek;
  99. int sum = 0;
  100. for(int i=0; i<niza.size; i++) {
  101. sum += niza.get(i);
  102. }
  103. prosek = sum/dolzina;
  104.  
  105. }
  106.  
  107. public static void main(String[] args) throws IOException {
  108. BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
  109. String s = stdin.readLine();
  110. int N = Integer.parseInt(s);
  111.  
  112. // Vashiot kod tuka...
  113. Array<Integer> niza = new Array<Integer>(N);
  114. for(int i=0; i<N; i++) {
  115. s = stdin.readLine();
  116. niza.set(i,Integer.parseInt(s));
  117. }
  118.  
  119. System.out.println(brojDoProsek(niza));
  120. }
  121.  
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement