Array Possible Pre-Lab

advertisement
CS-161
Lab #6: Introduction to Arrays
General
At the top of the every document that you create or modify (word processing files or source files)
include a class JAVADOC comment with your identifying information:
/**
* Description of the class or document.
*
* @author YOUR NAME
* @version CS161 Lab 6, 20XX.01.09 (replace with the date that you last edited the lab)
*/
Submit in this lab via Moodle using the submit Lab one link. Your assignment must be uploaded
by midnight of the due date. Moodle will automatically close the link at that time. I highly
recommend you do not wait until the very last minute to submit your work.
Concepts
This lab introduces you to the concept of “Arrays” and additional loop syntax of “for” loops for
fixed iteration. Additionally, it will further your experience with problem solving and programming
logic sequencing.
Background
Read and review starting at section 4.12 thru the end of chapter 4 from the text. I STRONGLY
recommend doing as many of the exercises on the notebook example as possible. The will give
you the proper background on using Arrays in Java.
This week’s lab is an easier lab to give you some experience with Arrays and For loops to
prepare for Lab #7.
Assignment Instructions
Using the CODE PAD to experiment with Arrays:
Enter EACH of the following lines into the CODE PAD of BlueJ (you may need to create a
project first, then under the view menu select “Show code pad”): Cutting and pasting from a
Word processor will probably result in errors, so please just type the lines into codepad. Make
sure you look at the result of each operation and understand why the result is what it is!
1) int[] myNums = {2, 4, 6, 8, 10};
2) myNums[0]
3) myNums[3]
4) for (int temp : myNums) { System.out.println(temp); }
5) myNums[0] = 3
6) myNums[0]
7) myNums.length
8) myNums[ myNums.length – 1]
9) int i = 2;
10) myNums[ i]
11) myNums[ ++i]
12) i
13) myNums[ myNums[0]]
14) for (int j = 0; j < myNums.length; j++) {
myNums[ j ] = j * j; }
Western Oregon University
Page 1 of 2
CS-161
Lab #6: Introduction to Arrays
15) for (int j = 0; j < myNums.length; j++)
System.out.println( myNums[ j]); }
{
TURN IN:
Copy your CODE PAD dialog of these experiments by selecting (shift+arrow keys or left
click mouse and drag to select) all of the text in the code pad and copying it (ctrl+c) and
then pasting it into your word processor. TURN IN a MS-Word, RTF, or Open Document file
containing your code pad dialog for this part of the lab.
1) Log in to Moodle.
2) Click the submit Lab One link, browse to your document and upload the document you
created.
3) If you need to submit an updated version, click on the submit link and you will see the
file you previously uploaded, click the edit these files button. On the right of the file
name you will see this icon . Click on the icon, delete your previous submission, and
then upload your updated version.
Western Oregon University
Page 2 of 2
Download