CS 163/164, Fall 2016
Lab 10 - Array Practice
Wed-Fri, Oct. 5-7, 2016
Objectives of this Lab
- Introduce you to arrays,
- reinforce the basics of arrays, and
- practice reading instructions carefully.
Introduction
Your TA will introduce Java array basics:
- How to create arrays.
- How to find the length of an array.
- How to access elements in an array.
- How to modify elements in an array.
- How to print all the elements in an array using a loop.
- How to print all the elements in an array Arrays.toString().
Array Practice, Practice, and more Practice
The automated grading program will be running throughout this lab.
Do the following steps in order (and ask questions!):
- Create a new R10 project in Eclipse and an associated class.
- Put everything that follows into the main method except the methods arrayAverage and reverseArray.
- Create a 5 element array of doubles called grades that contains the following
numbers in this order: 81.2, 92.5, 48.9, 78.8, and 45.5.
- Create a 7 element array of ints called numbers that contains the following
numbers in this order: 12, 42, 33, 67, 92, 58, and 33.
- Create a 9 element array of Strings called arguments without using an array initializer.
- Print (on one line) the length of grades using length.
- Print (on one line) the length of numbers using length.
- Print (on one line) the length of arguments using length.
- Print (on one line) the first element of grades.
- Print (on one line) the third element of grades.
- Print (on one line) the last element of numbers.
- Set the second to last element of grades to be 90.5.
- Set the third element of numbers to be 99.
- Set the first six elements of arguments to be "Java", and the rest to be "C++".
- Use a loop to print each element of grades on the same line, separated by commas (with a trailing comma).
- Use a loop to print each element of numbers on the same line, separated by spaces (with a trailing space).
- Use a loop to print each element of arguments on the same line, separated by an underscore (with a trailing underscore).
- Print the contents of grades using Arrays.toString(grades);
- Print the contents of numbers using Arrays.toString(numbers);
- Print the contents of arguments using Arrays.toString(arguments);
- FINAL STEP: Write a public static 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.
Extra Credit
Write a public 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}
Submit R10.java to Checkin