Advertisement
Guest User

Untitled

a guest
Oct 27th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.33 KB | None | 0 0
  1. // Create a new Eclipse project for Lab 7
  2. // Copy/Paste this starter code to a new java class called ArrayPractice.
  3. // Delete any code generated by Eclipse before copying.
  4. // The code will not compile until you finish some steps.
  5.  
  6. public class ArrayPractice {
  7.  
  8. public static void main(String[] args) {
  9. /*
  10. * CSCI 201 Lab 7
  11. *
  12. * NOTE: This is a longer lab assignment than normal and will count double (20 total points). If
  13. * you work quickly on the easier portions (1-9, 11, 12), you should be able to finish in the
  14. * lab. The numbers in square brackets are relative difficulty/weight of each question.
  15. *
  16. * You can work with a partner, but everyone must submit individually.
  17. *
  18. * Submit .java file electronically Web-CAT by Sunday night.
  19. *
  20. * Copy and paste this starter code. Add your solutions according to the comments.
  21. * ------------------------------------------------------------------------------------------
  22. *
  23. * 1. [1] Create an array of the first 10 odd integers using an initializer list. Call the array
  24. * anything you'd like.
  25. */
  26.  
  27.  
  28. /*
  29. *
  30. * 2. [1] Print the length of this array in an informative sentence; use the length property of
  31. * the array to get the length -- don't hard code the value 10.
  32. */
  33.  
  34.  
  35.  
  36. /*
  37. *
  38. * 3. [3] Use a for-each loop to display the contents of this array on a single line,
  39. * space-delimited.
  40. */
  41.  
  42.  
  43.  
  44. System.out.println(); // leave this here.
  45. /*
  46. *
  47. * 4. [3] Use a for-loop to subtract one (1) from each element in the array.
  48. */
  49.  
  50.  
  51.  
  52. /*
  53. *
  54. * 5. [2] Print the first and last elements in the array after doing step 4. When you index the
  55. * last element, do not hard-code or use a variable for the last index number; instead use an
  56. * expression based on the length of the array that will work no matter what the length of the
  57. * array actually is.
  58. */
  59.  
  60.  
  61.  
  62. /*
  63. *
  64. * 6. [5] Rotate each item in the array one place to the right. The last item should move to the
  65. * first position, but all other items shift one place to the right. Consider solving this one
  66. * on paper first using a small array -- write a pseudocode that works and then write the code.
  67. */
  68.  
  69.  
  70.  
  71. /*
  72. * 7. [1] Copy/paste the code from step 5 to show your first/last rotated values.
  73. */
  74.  
  75.  
  76.  
  77. /*
  78. * 8. [1] Create a blank array to hold 1000 integers.
  79. */
  80.  
  81.  
  82.  
  83. /*
  84. * 9. [3] Fill this array with the first 1000 multiples of four using a for-loop.
  85. */
  86.  
  87.  
  88.  
  89. /*
  90. * 10. [8] Use a for-loop to randomly shuffle the contents of this array as we learned in class.
  91. * The basic idea is to loop through the array once. For each item, generate a random partner to
  92. * switch with and swap the values. See the book for more clues.
  93. */
  94.  
  95.  
  96.  
  97. /*
  98. * 11. [3] Copy the contents of this array to a new array of the same length. Do not use the
  99. * System.arraycopy method to accomplish this -- do it manually.
  100. */
  101.  
  102.  
  103.  
  104. /*
  105. * 12. [3] Print the contents, from index 333 to index 382, of the new, copied-into array using
  106. * a for-loop, newline-delimited.
  107. */
  108.  
  109.  
  110.  
  111. /*
  112. * 13. [12] Write a helper method after the main method: public static boolean isSorted(int[]
  113. * list) that returns true if the array supplied is sorted in ascending order and returns false
  114. * if it is not.
  115. *
  116. * 14. [4] Here in the main method, create three small test arrays using initializer lists --
  117. * two that are in sorted order and one that is not. Call your method from step 13 with these
  118. * three arrays and make the main method print out a sentence stating if the array is sorted or
  119. * not.
  120. */
  121.  
  122. // Dr. V has done this for you, but study the code.
  123. int[] test1 = {1, 2, 3};
  124. int[] test2 = {11, 22, 33};
  125. int[] test3 = {11, 2, 32};
  126. if (isSorted(test1)) System.out.println("test1 is sorted");
  127. else System.out.println("test1 is not sorted");
  128. if (isSorted(test2)) System.out.println("test2 is sorted");
  129. else System.out.println("test2 is not sorted");
  130. if (isSorted(test3)) System.out.println("test3 is sorted");
  131. else System.out.println("test3 is not sorted");
  132. }
  133.  
  134. // here is the helper methods
  135. public static boolean isSorted(int[] list) {
  136. return false; // delete this when you're ready to start the method
  137. // your code here.
  138.  
  139.  
  140.  
  141. }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement