Alright, so this week we have begun to learn how to use arrays. which i find to be pretty interesting and a little bit more
easier to understand then the previous material.the best part about an array is that you can rewrite a program that would say take tons of line of code into a array hence saving space and making things easier to read.
Here is how you create a array:
int[] anArray = new int[8];
- the first part(int[]) is the array type which can be an int,double,String, etc..
- the second part(anArray) is what you want to name your array.
- and then the last part(new int[8]) is where you create the “memory slot” of the array. having the number “8″ within the brackets is just saying the array will have 8 slots of memory.
It is also worth pointing out that arrays and loops(for-loops to be exact or atleast to my understanding) go hand-to-hand.
So for example:
int[] Apples = new int[10];
int seeds = 10;
for (int i = 0; i < 10; i++) {
Apples[i] = seeds;
seeds += 10;
System.out.println(“There are ” + Apples[i] + ” Seeds” + ” in Apple # ” + i);
}
This code will first create the Array “Apples” with 10 slots. Then create a Variable that is a integer called ”seeds” that is initialized with a 10.
We then create a for loop where the initialize the integer “i” to 0. after that we have the condition-while-true, which is i < 10. This is saying as long as “i” is less than 10 we will repeat what is in the loop body. then finally we have the loop-update statement, which is i++,This is saying i = i + 1.
now that we have created the for-loop we can now move on to the loop body. I will go through this step by step:
Apples[i] = seeds; — means that we are assigning the array “Apples” to the integer seeds that is controlled by “i”(the variable i is in the for-loop).
seeds += 10; —-means that we will be adding 10 to seeds on each loop repeat. so for the first loop it would be just 10 seeds, the second loop would be 20 seeds. since we are adding 10 (Once again “seeds+=10″ is saying seeds = seeds + 10.)
and then finally we have:
System.out.println(“There are ” + Apples[i] + ” Seeds” + ” in Apple # ” + i);
This is where we print out how many seeds are in each apple. the output should look like this :
There are 10 Seeds in Apple # 0
There are 20 Seeds in Apple # 1
There are 30 Seeds in Apple # 2
There are 40 Seeds in Apple # 3
There are 50 Seeds in Apple # 4
There are 60 Seeds in Apple # 5
There are 70 Seeds in Apple # 6
There are 80 Seeds in Apple # 7
There are 90 Seeds in Apple # 8
There are 100 Seeds in Apple # 9
Remember, having the “i” outside the square brackets means that that is the # of the loop process it is at (#1,#2,#3, etc…) Where as having the “i” inside the square brackets means that it is what value the array is at in the current “memory slot”.
with that being said i’d like to discuss the three assignments we have this week.
for the first assignment we were instructed to have our finch read in the light values on its left light sensor only for thirty seconds in 3 second intervals and after doing so have it calculate and display the average,high and low of the readings. this assignment, as mentioned on the sheet, can be used without an array.however we were to do this without using an array first. so basically this first assignment is to show us how an array can be used.
for the second assignment we have is a little more challenging. from my understanding we are to create a program that allows a user to input a number of students first names and the exam score they are wanting to see. so for this it seems we have to use two arrays and then compare them together.
Now for the last assignment. I have been working on this one this morning and so far I have not ran into any “road blocks”. the assignment is to assign the finch 10 movements as well as the duration of the movements and after those movements have been completed we prompt a menu that has three choices. this is very close, if not the exact same concept as the do while assignment we had in week four. just combined with the assignment we had during week three.