Recitation R8
- Java Arrays
Summer 2016
CS160: Foundations in Programming
Preview
The purpose of this lab is to:
- Introduce you to arrays.
- Practice Reading Code.
- Practice the basics of arrays.
- Complete a set of exercises on Hackerrank.
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!):
- This project will be done in teams of two assigned by the TA.
- Each team will work on one computer, and you will switch who is typing midway.
- To submit, each person in the team will submit the same file.
- Create a new R8 project in Eclipse and an associated class.
- Put everything that follows into the main method except the methods arrayAverage and reverseArray.
- 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
- Create a 6 element array of ints called numbers that contains the following numbers in this order: 12, 42, 33, 67, 92, 58
- Create a 9 element array of Strings called arguments without using an array initializer.
- Print the length of grades using length.
- Print the length of numbers using length.
- Print the length of arguments using length.
- Print the 4th element of grades.
- Print the 2nd element of grades.
- Print the 3rd element of numbers.
- Set the 1st element of numbers to be 99.
- Set the last element of grades to be 90.5
- Set the 7th element of arguments to be "HelloThere"
- 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 for-each 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);
- 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.
- 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
- Log into Hackerrank
- Complete the section named R8
- These practice problems will be open after this recitation as an additional resource.
- Remember, you can use the examples from this recitation and the lecture notes.
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.