Advertisement
Guest User

Untitled

a guest
Oct 21st, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.38 KB | None | 0 0
  1. import java.util.Random;
  2. import java.util.Arrays;
  3.  
  4. public class Implementation
  5. {
  6. private int[] vector;
  7. private int x;
  8.  
  9. public Implementation(int[] array, int x)
  10. {
  11. this.vector = array;
  12. this.x = x;
  13. fillArray();
  14.  
  15. }//end constructor
  16.  
  17. public int[] getVector()
  18. {
  19. return vector;
  20. }
  21.  
  22. public int getX()
  23. {
  24. return x;
  25. }
  26.  
  27. public void printArray() /*method for printing an array*/
  28. {
  29. int i;
  30.  
  31. for(i = 0; i < vector.length; i++)
  32. {
  33. System.out.println("Value nr " + i + "." + " "+ " " + vector[i]);
  34. }
  35. }
  36.  
  37. public void sortArray() /*sorting array*/
  38. {
  39. Arrays.sort(vector);
  40. }
  41.  
  42. public void fillArray() /*filling array by random values*/
  43. {
  44. int i;
  45. Random random;
  46.  
  47. random = new Random();
  48.  
  49. for(i=0; i< vector.length; i++)
  50. {
  51. vector[i] = random.nextInt(100) + 1;
  52. }
  53. }
  54. public int getIndexOfX() /*zwracanie indeksu wartosci szukanej w tablicy*/
  55. {
  56. int i;
  57. int indexOfX;
  58.  
  59. indexOfX=-1;
  60.  
  61. for(i=0; i< vector.length; i++)
  62. {
  63. if(indexOfX == -1)
  64. {
  65. if(vector[i] == x)
  66. {
  67. indexOfX = i;
  68. } //end if
  69. } //end if
  70. } //end for(i=0; i<Wektor.length; i++)
  71.  
  72. return indexOfX;
  73. }
  74.  
  75. public boolean ifIndexExistsInArray() /*metoda sprawdzająca czy szukana wartość jest w tablicy*/
  76. {
  77. int i;
  78. boolean ifExists;
  79.  
  80. ifExists=false;
  81.  
  82. for(i=0; i< vector.length; i++)
  83. {
  84. if (ifExists == false)
  85. {
  86. if (vector[i] == x)
  87. {
  88. return true;
  89. } //end if
  90.  
  91. } //end for(i=0; i< Wektor.length; i++)
  92. }
  93. return ifExists;
  94. }
  95. public int findIndexOfX()
  96. {
  97. int i;
  98. int j;
  99. int k;
  100.  
  101.  
  102. sortArray();
  103. i = 0;
  104. j = vector.length-1;
  105. k = -1;
  106.  
  107. if(x>= vector[i] && x <= vector[j])
  108. {
  109. for (i = 0, j = vector.length - 1; i < vector.length && j > 0; i++, j--)
  110. {
  111. k = ((j - i) / 2) + i;
  112. if (x >= vector[i] && x < vector[((j - i) / 2) + i])
  113. {
  114. j = k;
  115. k = ((j - i) / 2) + i;
  116. }
  117.  
  118. if (x >= vector[k])
  119. {
  120. i = k;
  121. k = ((j - i) / 2) + i;
  122. }
  123.  
  124. if (x == vector[i])
  125. {
  126. return i;
  127. }
  128.  
  129. if (x == vector[j])
  130. {
  131. return j;
  132. }
  133.  
  134. if (x == vector[k])
  135. {
  136. return k;
  137. }
  138. }
  139. }
  140. return k;
  141. }
  142. public String toString()
  143. {
  144. String description1;
  145. String description2;
  146.  
  147. description1 = "Your value is not exist in this array";
  148. description2 = ("Number " + x + " is on " + findIndexOfX() + " index in array");
  149.  
  150. if(findIndexOfX()==-1)
  151. {
  152. return description1;
  153. }
  154. else
  155. {
  156. return description2;
  157. }
  158. }
  159. }
  160. import java.util.Random;
  161. import java.util.Scanner;
  162.  
  163. public class Main {
  164.  
  165. public static void main (String[]args)
  166. {
  167. Scanner scanner;
  168. Random random;
  169. int[] intArray;
  170. int i;
  171. Implementation implementation;
  172. int x;
  173.  
  174. i=0;
  175. intArray = new int[100];
  176. random = new Random();
  177. scanner=new Scanner(System.in);
  178.  
  179. System.out.println("Give an number which you want to find:");
  180. x = scanner.nextInt();
  181.  
  182. implementation = new Implementation(intArray, x);
  183. implementation.sortArray();
  184. implementation.printArray();
  185. System.out.println();
  186. System.out.println(implementation.toString());
  187.  
  188.  
  189.  
  190. }
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement