Intro
So I've been working really hard on schoolwork for the past couple days. Usually, my comp sci periods are used for other things like extra homework, browsing reddit, or writing blog posts. Recently, however, I've actually been doing work in the class. I'll show you a little of what I've been doing.
Unit 7 Assignment 1
This assignment, we were supposed to simulate the Game of Life using 2D arrays. The main issue was getting the neighbor counting method called
neighbors() to work correctly, and then working out the changeCells() method to address all situations.Here's the
neighbors() method:public int neighbors(int row, int col)
<>{
int count=0;
for(int i=row-1; i=row+1; i++)
{
for(int j=col-1; j=col+1; j++)
{
try
{
if(board[i][j].equals("*"))
count++;
}
catch(ArrayIndexOutOfBoundsException except_1)
{
continue;
}
}
}
if(board[row][col].equals("*"))
{
occupied = true;
return count-1;
}
else
{
occupied = false;
return count;
}
} This wasn't too hard, but identifying and isolating the area which needed the try/catch block took a little thinking. Also, if the
return count-1 isn't included, the whole game gets thrown off.Then came the
changeCells() method, which is the main driving force behind this project. It calls the neighbors() method to identify how many neighbors each individual cell has, and then, based on that number, either generates a new cell, keeps the current cell, or kills the current cell. This process goes through the entirety of the 2D array until a new generation is created.Here's the
changeCells() method:public void changeCells()
{
String[][]temp = new String[6][6];
for(int i=0; i<board.length; i++)
{
System.arraycopy(board[i],0,temp[i],0,board[0].length);
}
for(int j=0; j<6; j++)
{
for(int k=0; k<6; k++)
{
int num = neighbors(j,k);
if(num==3 && occupied==false)
temp[j][k] = ("*");
else if((num<2 || num>3) && occupied==true)
temp[j][k] = (" ");
}
}
board = temp;
}Making sure that each 'if' statement was correct was crucial, but they were spelled out quite reasonably in the project description. This project was fun and a thinking challenge for a while not because of its complexity, but because of the variety of different (wrong) answers you can get while working on it.
Unit 7 Assignment 2
electionResults needed to be created, and this class had a method called (ready for it?) electionResults() which has three parameters: the number of candidates, the number of precincts, and a 2D array associating the two (candidates = rows, precincts = columns). The voting results weren't generated randomly, but taken from text files. The totals per precinct and per candidate had to be totaled, and then a winner was determined based on the greatest percentage-getter out of each set of elections.Everything else was nice, with just some simple sorting and formatting going on, but determining the winner efficiently was fun to think about. Since the total votes of both the precinct and candidate types are stored in independent, short arrays, I opted to use the sequential search method because it'd make short work of the small arrays and could quickly identify the greatest number in the array to determine the winner.
This sequential search
int winner = 0;
int small = b[0];
for(int ex=0; ex<b.length; ex++)
{
if(b[ex]>small)
winner = (ex+1);
else if(b[ex]==small)
winner = (ex+1);
}addresses two conditions: the first is if a number is greater than the first number in the array (as defined by the variable
small ) then the candidate equals the array position plus one. If the first number is the greatest, however, it also returns the position plus one.These projects are fun to work on, and I thoroughly enjoy them. I like the thinking and the analysis, and sometimes just having the pleasure of saying "I did that - I did that" makes it that much better.



