Colorado State University

Recitation R8 - Java Arrays
Summer 2016

CS160: Foundations in Programming


Preview

The purpose of this lab is to:

Introduction to Arrays

Your TA will introduce arrays and review:


Interpretation Section

What does the following code print?

    // Question 1
    int [] iArray = new int [1];
    if (iArray[1] == 0)
      System.out.println("the first element of iArray is 0");

    // Question 2
    String [] sArray = {"Hello", "how's", "it", "going?"};
    System.out.println(sArray[3].substring(2,5));

    // Question 3
    double [] dArray = new double [5];
    for (int i = 0; i < dArray.length; i++){
      if (i % 2 == 1)
        dArray[i] = 3;
      else
        dArray[i] = 5;
      }
    for (int i = 0; i < dArray.length; i++)
      System.out.print(dArray[i] + " ");
    System.out.println(); // used for spacing

    // Question 4
    System.out.printf("%d\n", iArray.length + sArray.length - dArray.length);

    iArray[0] = 1;
    sArray[2] = "life";
    dArray[4] = 4;

    // Question 5
    System.out.println(iArray[0] / dArray[4]);

    // Question 6
    System.out.println(Arrays.toString(sArray));

Array Practice, Practice, and more Practice!

Do the following steps in order (and ask questions!):

  1. Create a new R8 project in Eclipse and an associated class.
  2. Put everything that follows into the main method except the methods arrayAverage and reverseArray.
  3. Create a four element array of doubles called grades that contains the following numbers in this order: 81.2, 92.5, 48.9, 78.8
  4. Create a 6 element array of ints called numbers that contains the following numbers in this order: 12, 42, 33, 67, 92, 58
  5. Create a 9 element array of Strings called arguments without using an array initializer.
  6. Print the length of grades using length.
  7. Print the length of numbers using length.
  8. Print the length of arguments using length.
  9. Print the 4th element of grades.
  10. Print the 2nd element of grades.
  11. Print the 3rd element of numbers.
  12. Set the 1st element of numbers to be 99.
  13. Set the last element of grades to be 90.5
  14. Set the 7th element of arguments to be "HelloThere"
  15. Use a loop to print each element of grades on the same line, separated by commas (with a trailing comma)
  16. Use a loop to print each element of numbers on the same line, separated by spaces (with a trailing space)
  17. Use a for-each loop to print each element of arguments on the same line, separated by an underscore (with a trailing underscore)
  18. Print the contents of grades using Arrays.toString(grades);
  19. Print the contents of numbers using Arrays.toString(numbers);
  20. Print the contents of arguments using Arrays.toString(arguments);
  21. Write a static method called arrayAverage that takes an array of doubles as a parameter and returns the average as a double. Print the result of calling arrayAverage with the array grades with exactly 3 digits after the decimal point.
  22. Write a static method called reverseArray that takes an array of doubles as a parameter and returns an array of doubles with the elements swapped. Print the result of calling reverseArray with the array grades. For example: {8.8, 1.1, 4.4, 2.2} would turn into {2.2, 4.4, 1.1, 8.8}

Practice Problems


Submit your R8.java program to Checkin tab for both partners and individually complete the Hackerrank problems.

© 2016 CS160 Colorado State University. All Rights Reserved.