Advertisement
brilliant_moves

Assignment8.java

Dec 3rd, 2014
439
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 3.09 KB | None | 0 0
  1. import java.util.Scanner;
  2. public class Assignment8 {
  3.    //Simple method for printing arrays.
  4.    public static void printArray( int [] array ) {
  5.       for( int i = 0; i < array.length; i++ ) {
  6.          System.out.print( array[i] + " " );
  7.       }
  8.       System.out.println();
  9.    }
  10.    
  11.    //Method that initalizes the array elements to their index + 1, so
  12.    // 1 2 3 4 5 ....
  13.    public static void initializeArray( int [] array ) {
  14.       for( int i = 0; i < array.length; i++ ) {
  15.          array[i] = i + 1;
  16.       }
  17.    }
  18.    
  19.    //TODO: Write a method that squares each element of the array and
  20.    //and returns the result. Note: we did something very close with
  21.    //a method that doubled each element of the array.
  22.    public static int [] squareArray( int [] array ) {
  23.       for(int i=0; i<array.length; i++) {
  24.          array[i] = array[i] * array[i];
  25.       }
  26.       return array;
  27.    }
  28.    
  29.    //TODO: Write a method that reverses all elements of the array and
  30.    //and returns the result. NOTE: This is in the book! You may use the
  31.    //book's code.
  32.    public static int [] reverseArray( int [] array ) {
  33.       int len = array.length;
  34.       int[] newArray = new int[len];
  35.       for (int i=0; i<len; i++) {
  36.         newArray[(len-1)-i]=array[i];
  37.       }
  38.       return newArray;
  39.    }
  40.    
  41.    //TODO: Write a method that transposes each pair of elements in the
  42.    //array. It swaps each element with the element next to it, so
  43.    // For even size: 1 2 3 4 5 6 transposed is 2 1 4 3 6 5, and
  44.    // For od size:   1 2 3 4 5 transposed is 2 1 4 3 5
  45.    public static int [] transposeArray( int [] array ) {
  46.       for (int i=0; i<array.length-1; i+=2) {
  47.          int temp = array[i];
  48.          array[i] = array[i+1];
  49.          array[i+1] = temp;
  50.       }
  51.       return array;
  52.    }
  53.    
  54.    //The main method does not need to be changed. It is complete.
  55.    //You only need to fill in the three methods directly above.
  56.    public static void main ( String [] args ) {
  57.       int arraySize = 0;
  58.       Scanner input = new Scanner(System.in);
  59.      
  60.       //Creates an array of numbers. Size based on user input.
  61.       System.out.print("Enter size of array: ");
  62.       arraySize = input.nextInt();
  63.       int [] numbers = new int [arraySize];
  64.      
  65.       //Initialize and print array using given methods.
  66.       initializeArray( numbers );
  67.       System.out.println("Values in array are:");
  68.       printArray(numbers);
  69.      
  70.       //Square all the numbers in the array and print.
  71.       numbers = squareArray(numbers);
  72.       System.out.println("Values in array when squared are:");
  73.       printArray(numbers);
  74.      
  75.       //Re-initialize then reverse all the numbers in the array and print.
  76.       initializeArray( numbers );
  77.       numbers = reverseArray(numbers);
  78.       System.out.println("Values in array when reversed are:");
  79.       printArray(numbers);
  80.      
  81.       //Re-initialize then transpose all the numbers in the array and print.
  82.       initializeArray( numbers );
  83.       numbers = transposeArray(numbers);
  84.       System.out.println("Values in array when transposed are:");
  85.       printArray(numbers);
  86.    }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement