Draw an Array for 3x5

  • The first step is to declare the array variable.
                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.

  • Now we allocate the slots with new.
                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.

  • Now we allocate the elements in the array with new.
                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.

  • Let's create two more TestScore elements in our 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                      

  • Notice our picture - how big is the array? In other words, how many elements can it store? ANSWER ( How many elements do we actually have in our array?
  • The fact that our array can hold up to 5 TestScores and we only have have 3 is called using a partially filled array. Our code should keep track of how many elements are actually being used in the array. The reason we need to do this is so that when we process the array, we only process the elements that are being used.

    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;                      
  • Now let's process our array. First, we will print the size of the array and the number of elements we are using.
                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.                      
  • Next let's process the array - we will print out the the points earned and the points possible for each TestScore, followed by the the corresponding letter grade. Our code will use a for loop. First we must add some declarations.
                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()); }                                    
  • If we run our code, we will see this output.
                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                      
  • Let's examine some of the details of this code.

    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?

  • Now we start generating the output. First we just print the string "Score 1 is "

    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?

    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 ".

  • Next we call the method getPointsPossible(), which returns 20.0.

    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.

  • i is incremented.

    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.

  • i is incremented.

    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.

  • i is incremented.

    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]?

  • Trying to access passed the end of an array is called an out of bounds subscript error. When the program runs, Java will generate an ArrayIndexOutOfBoundsException.

    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.

  • brucedroasing.blogspot.com

    Source: http://public.africa.cmu.edu/abiyabani/pfun/drawArray.html

    0 Response to "Draw an Array for 3x5"

    Post a Comment

    Iklan Atas Artikel

    Iklan Tengah Artikel 1

    Iklan Tengah Artikel 2

    Iklan Bawah Artikel