Draw an Array for 3x5
TestScore tests[]; // declare the array variable
The square brackets indicate that the variable tests is an array. It will reference a list of objects, all of which will be of class TestScore. It is important to start thinking about the details of the typse involved - tests is an array of TestScore. Each element in the array is of class TestScore. These types do not match!
Notice that at this point we do not actually have a list. That is the next step.
TestScore tests[]; // declare the array variable tests = new TestScore[5]; // allocate the slots in the array
The number in square brackets indicates how big the array should be. In this case we want the array to be size 5.
Notice that we still do not have any TestScores. That is the third step in creating an array.
TestScore tests[]; // declare the array variable tests = new TestScore[5]; // allocate the slots in the array tests[0] = new TestScore(19, 20); // create element 0
The number in the square brackets is the index into the array. In this case we are setting the first element in the array (index 0) to the TestScore that we created with new and calling the constructor that takes two arguments. Do you remember what the arguments represent?
Notice that we just write the values tied to a TestScore object inside the new box. Also notice that we have only filled one element in the array.
TestScore tests[]; // declare the array variable tests = new TestScore[5]; // allocate the slots in the array tests[0] = new TestScore(19, 20); // create element 0 tests[1] = new TestScore(23, 25); // create element 1 tests[2] = new TestScore(47, 50); // create element 2
Let's update the code.
TestScore tests[]; // declare the array variable int used = 0; tests = new TestScore[5]; // allocate the slots in the array tests[0] = new TestScore(19, 20); tests[1] = new TestScore(23, 25); tests[2] = new TestScore(47, 50); used = 3;
TestScore tests[]; // declare the array variable int used = 0; tests = new TestScore[5]; // allocate the slots in the array tests[0] = new TestScore(19, 20); tests[1] = new TestScore(23, 25); tests[2] = new TestScore(47, 50); used = 3; System.out.println("The size of our array is " + tests.length + ". We are using " + used + " elements.");
This output statement introduces the length instance variable. Every array has an instance variable called length that is set to the maximum size of the array. In our example, it is 5. So, this System.out.println() statement would print
The size of our array is 5. We are using 3 elements.
TestScore tests[]; // declare the array variable int used = 0; int i = 0; String scoreLabel; tests = new TestScore[5]; // allocate the slots in the array tests[0] = new TestScore(19, 20); tests[1] = new TestScore(23, 25); tests[2] = new TestScore(47, 50); used = 3; System.out.println("The size of our array is " + tests.length + ". We are using " + used + " elements."); for (i = 0; i < used; i++) { scoreLabel = "Score " + (i + 1); System.out.println(scoreLabel + " is " + tests[i].getPointsEarned() + " out of " + tests[i].getPointsPossible()); System.out.println(" Letter grade: " + tests[i].getLetterGrade()); }
The size of our array is 5. We are using 3 elements. Score 1 is 19.0 out of 20.0 Letter grade: A Score 2 is 23.0 out of 25.0 Letter grade: A Score 3 is 47.0 out of 50.0 Letter grade: A
Notice in our for loop that we start at 0, because this is the index of the first element to process. We end the loop when (i < used) is false. It is very important that we do not try to process any more elements that the ones we are using.
Let's execute the code. First, i is set to 0. 0 is less than 3 (used), so we go into the loop.
Inside the loop, we build a string by concatenating the string "Score "
Why did we add 1 to i?
Next we use dot-notation to call the getPointsEarned() method. Let's examine this part of the code in detail.
What data type is tests[i]?
Note that these are not the same data types!
In any case, when we write tests[i], we have a TestScore object, so can use dot-notation to call methods that belong to the TestScore class.
The method returns the number of points earned, which in this case is 19. It is concatenated with the string " out of ".
On the next System.out.println(), we call the method getLetterGrade().
We are at the bottom of the loop, so execution returns to the top of the for loop.
1 is less than 3 (used), so we go into the loop.
Inside the loop, we build a string by concatenating the string "Score " and the value of (i + 1).
Now we start generating the output. First we just print the string "Score 2 is "
Next we print out the points by calling getPointsEarned() and getPointsPossible() using tests[i], which is tests[2].
Next we print out the lettergrade by calling getLetterGrade() with tests[2].
We are at the bottom of the loop, so execution returns to the top of the for loop.
2 is less than 3 (used), so we go into the loop.
Inside the loop, we build a string by concatenating the string "Score " and the value of (i + 1).
Now we start generating the output. First we just print the string "Score 3 is "
Next we print out the points by calling getPointsEarned() and getPointsPossible() using tests[i], which is tests[3].
Next we print out the lettergrade by calling getLetterGrade() with tests[3].
We are at the bottom of the loop, so execution returns to the top of the for loop.
3 is not less than 3 (used), so we fall through to the end of the loop.
Notice that we stopped at exactly the correct index. What would have happened if we had tried to process tests[3]? We would have been trying to use dot-notation with a null reference, which is not valid Java code.
What do you think would have happened if the size of our array was only 3 and we tried to process tests[3]?
As you can see, processing an array requires that you write your loops exactly right. You can only process the elements that are being used and must make sure you neve go passed the end of the array.
Source: http://public.africa.cmu.edu/abiyabani/pfun/drawArray.html
0 Response to "Draw an Array for 3x5"
Post a Comment