Advertisement
Guest User

Untitled

a guest
Oct 13th, 2015
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.92 KB | None | 0 0
  1. /*
  2. 1. Submit 2 png files: source and output.
  3.  
  4. 2. Do P6.7 on page 307.
  5.  
  6. 3. write a method called arrayReverse() to reverse and return the array elements with the definition:
  7.  
  8. public static int[] arrayReverse(int[] ary)
  9.  
  10. 4. also write another method called arrayInput() to prompt users for the input and return the array:
  11.  
  12. public static int[] arrayInput()
  13.  
  14. 5. also write another method calld arrayPrint() to print the array.
  15.  
  16. public static int[] arrayPrint(int[] ary)
  17.  
  18. 6. Tips for arrayReverse()
  19.  
  20. For example, if an integer array is: int[] a={7,8,9,11, 12, 13}
  21.  
  22. The first array element a[0] will be exchanged with the last element a[5].
  23.  
  24. a[0] is 7 and a[5] is 13.
  25.  
  26. use i=0 and j=5, if i < j, then doing the first swap, otherwise stop the loop and return the array
  27.  
  28. (i is the first element's location or index)
  29.  
  30. (j is the last element's location or index)
  31.  
  32. increment i by 1 and decrement j by 1, if i < j, then doing the second swap, otherwise stop the loop and return the array
  33. increment i by 1 and decrement j by 1, if i < j, then doing the third swap, otherwise stop the loop and return the array
  34.  
  35. 7. Tips for arrayInput(); // myArray.java is uploaded in iCollege/Content. It shows how to store the input into the array
  36.  
  37. You will include the Scanner statement in this method
  38.  
  39. ask users to enter 6 integers
  40.  
  41. 8. Tips for arrayOutput(); // myArray.java is uploaded in iCollege/Content. It shows how to print the array
  42.  
  43. 9. Use the following main method to test your code
  44.  
  45. public class...
  46.  
  47. {
  48.  
  49. public static void main(...)
  50.  
  51. {
  52.  
  53. int[] aryOrig = new int[6];
  54.  
  55. int[] aryReversed;
  56.  
  57. aryOrig = arrayInput();
  58.  
  59. arrayPrint(aryOrig);
  60.  
  61. aryReversed = arrayReverse(aryOrig);
  62.  
  63. arrayPrint(aryReversed);
  64.  
  65. }
  66.  
  67. }
  68.  
  69. 10: Sampe run outptu:
  70.  
  71. Enter 6 integers and press Q to stop: 3 4 5 6 7 8 Q
  72.  
  73. before reverse: 3 4 5 6 7 8
  74.  
  75. after reverse: 8 7 6 5 4 3
  76.  
  77. */
  78. import java.io.*;
  79. import java.util.*;
  80. public class ProjectReverseTheArray
  81. {
  82. public static void main(String[] args)
  83. {
  84. int[] aryOrig = new int[6];
  85. int[] aryReversed;
  86. aryOrig = arrayInput();
  87. //System.out.println(arrayPrint); //(aryOrig);
  88. arrayPrint(aryOrig);
  89. //aryReversed = arrayReverse(aryOrig);
  90. //arrayPrint(aryReversed);
  91.  
  92. }
  93. public static int[] arrayInput()
  94. {
  95. int[] aryOrig = new int[6];
  96. Scanner in = new Scanner(System.in);
  97. System.out.println("Enter 6 integers and press Q to stop: ");
  98. while (in.hasNextInt())
  99. {
  100. aryOrig[0]= in.nextInt();
  101. aryOrig[1]= in.nextInt();
  102. aryOrig[2]= in.nextInt();
  103. aryOrig[3]= in.nextInt();
  104. aryOrig[4]= in.nextInt();
  105. aryOrig[5]= in.nextInt();
  106. }
  107. return aryOrig;
  108. }
  109.  
  110. /* public static int arrayReverse
  111. {
  112.  
  113. } */
  114. public static int[] arrayPrint(int[] ary)
  115. {
  116. int[] aryOrig = int[] arrayInput;
  117. int[] arrayPrint;
  118. for (int i = 0; i < 6; i++)
  119. {
  120. System.out.println("before reverse: " + aryOrig[i] + " ");
  121. }
  122. return arrayPrint;
  123. }
  124.  
  125. /* System.out.println("before reverse: " + aryOrig[0] + " " +
  126. aryOrig[1] + " " +
  127. aryOrig[2] + " " +
  128. aryOrig[3] + " " +
  129. aryOrig[4] + " " +
  130. aryOrig[5]);
  131. System.out.println("after reverse: " + aryReversed[0] + " " +
  132. aryReversed[1] + " " +
  133. aryReversed[2] + " " +
  134. aryReversed[3] + " " +
  135. aryReversed[4] + " " +
  136. aryReversed[5]);
  137. */
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement