Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- 1. Submit 2 png files: source and output.
- 2. Do P6.7 on page 307.
- 3. write a method called arrayReverse() to reverse and return the array elements with the definition:
- public static int[] arrayReverse(int[] ary)
- 4. also write another method called arrayInput() to prompt users for the input and return the array:
- public static int[] arrayInput()
- 5. also write another method calld arrayPrint() to print the array.
- public static int[] arrayPrint(int[] ary)
- 6. Tips for arrayReverse()
- For example, if an integer array is: int[] a={7,8,9,11, 12, 13}
- The first array element a[0] will be exchanged with the last element a[5].
- a[0] is 7 and a[5] is 13.
- use i=0 and j=5, if i < j, then doing the first swap, otherwise stop the loop and return the array
- (i is the first element's location or index)
- (j is the last element's location or index)
- 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
- 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
- 7. Tips for arrayInput(); // myArray.java is uploaded in iCollege/Content. It shows how to store the input into the array
- You will include the Scanner statement in this method
- ask users to enter 6 integers
- 8. Tips for arrayOutput(); // myArray.java is uploaded in iCollege/Content. It shows how to print the array
- 9. Use the following main method to test your code
- public class...
- {
- public static void main(...)
- {
- int[] aryOrig = new int[6];
- int[] aryReversed;
- aryOrig = arrayInput();
- arrayPrint(aryOrig);
- aryReversed = arrayReverse(aryOrig);
- arrayPrint(aryReversed);
- }
- }
- 10: Sampe run outptu:
- Enter 6 integers and press Q to stop: 3 4 5 6 7 8 Q
- before reverse: 3 4 5 6 7 8
- after reverse: 8 7 6 5 4 3
- */
- import java.io.*;
- import java.util.*;
- public class ProjectReverseTheArray
- {
- public static void main(String[] args)
- {
- int[] aryOrig = new int[6];
- int[] aryReversed;
- aryOrig = arrayInput();
- //System.out.println(arrayPrint); //(aryOrig);
- arrayPrint(aryOrig);
- //aryReversed = arrayReverse(aryOrig);
- //arrayPrint(aryReversed);
- }
- public static int[] arrayInput()
- {
- int[] aryOrig = new int[6];
- Scanner in = new Scanner(System.in);
- System.out.println("Enter 6 integers and press Q to stop: ");
- while (in.hasNextInt())
- {
- aryOrig[0]= in.nextInt();
- aryOrig[1]= in.nextInt();
- aryOrig[2]= in.nextInt();
- aryOrig[3]= in.nextInt();
- aryOrig[4]= in.nextInt();
- aryOrig[5]= in.nextInt();
- }
- return aryOrig;
- }
- /* public static int arrayReverse
- {
- } */
- public static int[] arrayPrint(int[] ary)
- {
- int[] aryOrig = int[] arrayInput;
- int[] arrayPrint;
- for (int i = 0; i < 6; i++)
- {
- System.out.println("before reverse: " + aryOrig[i] + " ");
- }
- return arrayPrint;
- }
- /* System.out.println("before reverse: " + aryOrig[0] + " " +
- aryOrig[1] + " " +
- aryOrig[2] + " " +
- aryOrig[3] + " " +
- aryOrig[4] + " " +
- aryOrig[5]);
- System.out.println("after reverse: " + aryReversed[0] + " " +
- aryReversed[1] + " " +
- aryReversed[2] + " " +
- aryReversed[3] + " " +
- aryReversed[4] + " " +
- aryReversed[5]);
- */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement