CS 163/164, Fall 2017
Lab 11 - Array Practice
Tuesday, Oct. 3rd or
Wednesday, Oct. 4th
Objectives of this Lab
- Introduce you to arrays,
- reinforce the basics of arrays, and
- practice reading instructions carefully.
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
Do the following steps in order (and ask questions!):
- Create a new R11 project in Eclipse and an associated class.
- Put everything that follows into the main method except the method arrayAverage.
- 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.
Show your R11.java to the TA for grading.