zh_stoqnov

ArrayTest

Jan 30th, 2015
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. package ArrayTest;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class ArrayTest {
  6. public static void main(String[] args) {
  7. @SuppressWarnings("resource")
  8. Scanner scn = new Scanner(System.in);
  9.  
  10. int sizeOfArray = scn.nextInt();
  11. scn.nextLine();
  12. long[] array = new long[sizeOfArray];
  13.  
  14. for (int i = 0; i < array.length; i++) {
  15. array[i] = scn.nextLong();
  16. }
  17.  
  18. String command = scn.next();
  19.  
  20. while (!command.equals("stop")) { //changed value from "over" to "stop"
  21. String line = scn.nextLine().trim();
  22. int[] params = new int[2];
  23.  
  24. if(command.equals("add") || command.equals("subtract") || command.equals("multiply")) {
  25. // changed form substract to subtract
  26. String[] stringParams = line.split(" ");
  27. params[0] = Integer.parseInt(stringParams[0]);
  28. params[1] = Integer.parseInt(stringParams[1]);
  29.  
  30. performAction(array, command, params);
  31. } else {
  32. performAction(array, command, params); //inserted performAction in an else statement
  33. }
  34.  
  35. printArray(array);
  36. System.out.print('\n');
  37.  
  38. command = scn.next();
  39. }
  40. }
  41.  
  42. static void performAction(long[] arr, String action, int[] params){
  43. //removed array
  44. int pos = params[0];
  45. long value = params[1]; //changed type to long
  46.  
  47. switch (action) {
  48. case "multiply":
  49. arr[pos - 1] *= value; //changed to arr, added -1 so we get our desired position
  50. break;
  51. case "add":
  52. arr[pos - 1] += value; //changed to arr, added -1 so we get our desired position
  53. break;
  54. case "subtract":
  55. arr[pos - 1] -= value; //changed to arr, added -1 so we get our desired position
  56. break;
  57. case "lshift":
  58. arrayShiftLeft(arr); //changed to arr
  59. break;
  60. case "rshift":
  61. arrayShiftRight(arr); //changed to arr
  62. break;
  63. }
  64. }
  65.  
  66. private static void arrayShiftRight(long[] array) {
  67. long temp = array[array.length - 1]; //created a temp variable to store our temporary value
  68. for (int i = array.length-1; i >= 1 ; i--) {
  69. array[i] = array[i - 1];
  70. }
  71. array[0] = temp; // assigned the temp value to array member
  72. }
  73.  
  74. private static void arrayShiftLeft(long[] array) {
  75. long temp = array[0]; //created a temp variable to store our temporary value
  76. for (int i = 0; i < array.length - 1; i++) {
  77. array[i] = array[i+1];
  78. }
  79. array[array.length - 1] = temp; ///assigned the temp value
  80. }
  81.  
  82. private static void printArray(long[] array) {
  83. for (int i = 0; i < array.length; i++) {
  84. System.out.print(array[i] + " ");
  85. }
  86. }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment