Tina Comston
Comp-111 Week 10
Franklin University
Week 10
Preparation
  • Read Chapter 7 (sections 7.2 - 7.7) in the COMP 111 Reading Guide.
Guided Learning Activity
Assignment
  • Problem 1
Enhanced FOR loop

When looping through an array or ArrayList using the FOR loop you have to:
  • set your counter variable to 0, the starting element
  • set your condition to keep going through the last element
  • increment you counter
It's the same thing every time.  The developers of JAVA decided to provide an easier way to do this, without having to worry about the counter variable and the condition.  This is through the enhanced FOR loop.

The format is:

for (dataType objName: ArrayListName)
{
System.out.println(objName);
}

What this special type of FOR loop does it access each element in the ArrayList.  When it accesses the element, it puts it into the objName variable.  Then within the loop you access that objName variable.

Ok - so let's compare the regular FOR loop with the enhanced FOR loop.

for (int i = 0, i < stuName.size(); i++)
{
    System.out.println(stuName.get(i));
}

for (String tmpName: stuName)
{
    System.out.println(tmpName)
}

In the enhanced For loop, a variable is defined of the same data type as the ArrayList.  As each element in the ArrayList is accessed the element is put into the variable.  Within the loop this variable is then referenced.

YouTube video on enhanced for loops https://www.youtube.com/watch?v=aKzlcqFAoNc
    • Using an array and a while loop - solve the problem given.
    • Using an ArrayList and a for loop - solve the problem given.
    • Using an ArrayList and the enhanced FOR loop - solve the problem given.
  • Problem 2
Two-Dimensional Arrays

Up till now, the focus has been on one dimensional arrays and ArrayLists.  Java also supports multi-dimensional arrays.  When specifying a multi-dimensional array you must specify both the columns and the rows.

int newArray[][] = new int[row][column];

You would access the two dimensional array specifying both the row and the column.  Often nested loops are used:

for (int i = 0; i < row; i++)
{
for (int j = 0; j < column; j++)
{
    System.out.println(newArray[i][j]);
}
}

In the above example, the array elements in row 0 will be processed, looping through all of the columns, then row 1 looping through all the columns, then row 2 and so on.


YouTube video on 2D and 3D arrays https://www.youtube.com/watch?v=1q3brvLgYbs
    • You will be writing the test class, class, and driver program for a BattleShip game.
    • If you have never played BattleShip, the basic premise is as follows:
      • There is a grid (think of it as a 2D array or matrix)
      • Ships of various sizes are located on the grid.  The smallest size ship is 2 element positions.
      • It is your job to find all the ships by searching the grid.
      • Typically a player would call out a grid position, giving both the column and row value, the other player would then indicate either "MISS" or "HIT".  
        • If MISS, then move on to another location.
        • If HIT, then work around that position until you've found the entire ship.  (It could be horizontal, vertical or diagonal.)
  • You can try out this free online version of BattleShip at:
    • http://webbattleship.com/
 
Exam 2
  • I recommend that you call your proctor a couple of days prior to when you plan on taking the test and verify that the test has been received and that the proctor/office is open.  You will need to allow a minimum of 2-3 hours for taking the test.  It must be taken all at one time.  You cannot take half  at one time, take a break, and then take the rest.
Reflection Paper
  • Keep journaling.
Lab 4
  • Although you have a couple of weeks to complete this lab, it's never a good idea to wait until the last minute.  Read through the instructions. Make sure you have a clear understanding of what is needed.