Richard_Sekol

ex7.2

Nov 15th, 2014
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | None | 0 0
  1. package chap7;
  2.  
  3. public class excercise2 {
  4. private int[] values;
  5.  
  6. public ArrayMethods(int[] iniVal) {
  7. values = iniVal;
  8. }
  9. public void swapFirstAndLast()
  10. {
  11. int x = values[0];
  12. int y = values.length;
  13. values[0] = values[y-1];
  14. values[y-1] = x;
  15. }
  16. public void shiftRight() {
  17. int shift = values[values.length -1];
  18. if(values.length >= 2){
  19. System.arraycopy(values,0,values,1, values.length -1);}
  20. values[0] = shift;}
  21.  
  22. public void evensWithZero(){
  23. for(int i = 0; i < values.length ; i++){
  24. if(values[i] % 2 == 0 ){
  25. values[i] = 0;
  26. }
  27. }
  28. }
  29. public void replaceWithLargestNeighbor(){
  30. for (int i = 1; i < values.length - 1; i++) {
  31.  
  32. if (values[i + 1] > values[i - 1]) {
  33. values[i] = values[i + 1];}
  34.  
  35. else if (values[i - 1] > values[i + 1]) {
  36. values[i] = values[i - 1];}
  37. }
  38.  
  39. }
  40. public void removeMiddleElement(){
  41. int count = values.length ;
  42. int[] copy;
  43. if (count % 2 == 0)
  44. {
  45. int middle1 = count / 2;
  46. int middle2 = (count / 2);
  47. copy = new int[values.length - 2];
  48. System.arraycopy(values, 0, copy, 0, middle1 - 1);
  49. System.arraycopy(values, middle2 + 1 , copy, middle2 - 1, middle2 - 1);}
  50. else
  51. {
  52. copy = new int[values.length - 1];
  53. int middle1 = count / 2;
  54. int middle2 = (count / 2);
  55. System.arraycopy(values, 0, copy, 0, middle1);
  56. System.arraycopy(values, middle2 +1, copy, middle2, (count / 2 ));}
  57.  
  58.  
  59. public void evensToFront(){
  60. int swapPos = 0;
  61. for (int i = 0; i < values.length; i++) {
  62. if (values[i] % 2 == 0) {
  63. int temp = values[swapPos];
  64. values[swapPos] = values[i];
  65. values[i] = temp;
  66. swapPos += 1;}
  67. }}
  68. public static int secondLargestElement(int values[]) {
  69. int largest = values[0];
  70. int secondLargest = largest;
  71. for (int i = 0; i < values.length; i++) {
  72. if (values[i] > largest) {
  73. int temp = largest;
  74. largest = values[i];
  75. secondLargest = temp;}}
  76. return secondLargest;}
  77.  
  78. public static boolean isSorted(int[] values) {
  79. for (int i = 0; i < values.length - 1; i++) {
  80. if (values[i] > values[i + 1]) {
  81. return false;
  82. }
  83. }
  84. return true;
  85. }
  86.  
  87. public static boolean hasAdjacentDuplicates(int[] values) {
  88. for (int i = 0; i < values.length - 1; i++) {
  89. if (values[i] == values[i + 1]) {
  90. return true;}
  91. }
  92. return false;}
  93.  
  94. public static boolean hasDuplicateElements(int[] values) {
  95. for (int i = 0; i < values.length; i++) {
  96. for (int j = 0; j < values.length; j++) {
  97. if (i != j && values[i] == values[j]) {
  98. return true;
  99. }
  100. }}
  101. return false;
  102. }
  103.  
  104.  
  105. }
Advertisement
Add Comment
Please, Sign In to add comment