Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Create a new Eclipse project for Lab 7
- // Copy/Paste this starter code to a new java class called ArrayPractice.
- // Delete any code generated by Eclipse before copying.
- // The code will not compile until you finish some steps.
- public class ArrayPractice {
- public static void main(String[] args) {
- /*
- * CSCI 201 Lab 7
- *
- * NOTE: This is a longer lab assignment than normal and will count double (20 total points). If
- * you work quickly on the easier portions (1-9, 11, 12), you should be able to finish in the
- * lab. The numbers in square brackets are relative difficulty/weight of each question.
- *
- * You can work with a partner, but everyone must submit individually.
- *
- * Submit .java file electronically Web-CAT by Sunday night.
- *
- * Copy and paste this starter code. Add your solutions according to the comments.
- * ------------------------------------------------------------------------------------------
- *
- * 1. [1] Create an array of the first 10 odd integers using an initializer list. Call the array
- * anything you'd like.
- */
- /*
- *
- * 2. [1] Print the length of this array in an informative sentence; use the length property of
- * the array to get the length -- don't hard code the value 10.
- */
- /*
- *
- * 3. [3] Use a for-each loop to display the contents of this array on a single line,
- * space-delimited.
- */
- System.out.println(); // leave this here.
- /*
- *
- * 4. [3] Use a for-loop to subtract one (1) from each element in the array.
- */
- /*
- *
- * 5. [2] Print the first and last elements in the array after doing step 4. When you index the
- * last element, do not hard-code or use a variable for the last index number; instead use an
- * expression based on the length of the array that will work no matter what the length of the
- * array actually is.
- */
- /*
- *
- * 6. [5] Rotate each item in the array one place to the right. The last item should move to the
- * first position, but all other items shift one place to the right. Consider solving this one
- * on paper first using a small array -- write a pseudocode that works and then write the code.
- */
- /*
- * 7. [1] Copy/paste the code from step 5 to show your first/last rotated values.
- */
- /*
- * 8. [1] Create a blank array to hold 1000 integers.
- */
- /*
- * 9. [3] Fill this array with the first 1000 multiples of four using a for-loop.
- */
- /*
- * 10. [8] Use a for-loop to randomly shuffle the contents of this array as we learned in class.
- * The basic idea is to loop through the array once. For each item, generate a random partner to
- * switch with and swap the values. See the book for more clues.
- */
- /*
- * 11. [3] Copy the contents of this array to a new array of the same length. Do not use the
- * System.arraycopy method to accomplish this -- do it manually.
- */
- /*
- * 12. [3] Print the contents, from index 333 to index 382, of the new, copied-into array using
- * a for-loop, newline-delimited.
- */
- /*
- * 13. [12] Write a helper method after the main method: public static boolean isSorted(int[]
- * list) that returns true if the array supplied is sorted in ascending order and returns false
- * if it is not.
- *
- * 14. [4] Here in the main method, create three small test arrays using initializer lists --
- * two that are in sorted order and one that is not. Call your method from step 13 with these
- * three arrays and make the main method print out a sentence stating if the array is sorted or
- * not.
- */
- // Dr. V has done this for you, but study the code.
- int[] test1 = {1, 2, 3};
- int[] test2 = {11, 22, 33};
- int[] test3 = {11, 2, 32};
- if (isSorted(test1)) System.out.println("test1 is sorted");
- else System.out.println("test1 is not sorted");
- if (isSorted(test2)) System.out.println("test2 is sorted");
- else System.out.println("test2 is not sorted");
- if (isSorted(test3)) System.out.println("test3 is sorted");
- else System.out.println("test3 is not sorted");
- }
- // here is the helper methods
- public static boolean isSorted(int[] list) {
- return false; // delete this when you're ready to start the method
- // your code here.
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement