Java Array, Game of Life

cavalcadejewelSoftware and s/w Development

Nov 18, 2013 (4 years and 4 months ago)


Java Array, Game of Life

Prof. Sin
Min Lee

Department of Computer Science

San Jose State University


What is Java

New, general purpose object
oriented programming language
from Sun Microsystems

Allows users to interact with Web pages beyond simply:

reading them

filling out a form

answering questions

Allows interaction with program running as an extension to
your Web browser

May be used to augment web page with both a new protocol
and the program which implements that protocol

May be used for writing both network
oriented and local
application programs

History of the development of Java

Started out in 1991 as Project Green

focussed on O/S software for consumer electronic devices

James Gosling recognized inadequacy of C++ and initiated
development of Oak language

Green went through several false starts before dissolving

Small group decided to adapt Oak (later named Java) to a web

result was a new web browser, WebRunner (later
named HotJava), operational in 1994

Paper on Oak byte codes presented by Gosling at Programming
Language Design and Implementation (PLDI) conference in 1995

Highlights of the Java Language

It's Simple

It's Object Oriented

It's Safe

It's Secure

It's Portable

It's Fast (potentially)

It's Multi


It's Simple

Java has only the necessary functionality needed to implement
its feature set.

It has omitted the features of C and C++ which have been
considered to be "unsafe"

pointer "forging"

operator overloading

static objects

Memory is managed automatically, relieving the programmer
from being responsible for freeing unused space

There is no Java preprocessor

the program that you see is the
same program that the Java compiler sees.


It's Object Oriented

Syntax and semantics inherited from C and C++, but many of
its object management features come from Objective C (e.g.

Unlike C and C++, there are no stand
alone functions, but only
methods associated with a class.

Everything (except for the built
in primitive types) is either a
class, a method, or an object.

Extensive class library comes with Java, to interface with:

host operating system

window manager


Java Applications and Applets may provide their own class
library support

May not replace "system" classes (for security purposes)


It's Safe

Four different levels of safety checks and enforcement to
prevent the introduction of viruses

protect against deleting or modifying files

protect against corrupting the operator of user's computer

More strict type model than either C or C++

Arrays are first class objects

always range checked

no visible conversion to pointer plus offset

No implicit declarations in Java

Only a minimum number of implicit conversions


It's Fast (potentially)

Currently, all mainstream implementations of Java are
running Java programs interpretively

port of the interpreter developed by Sun

alternative implementation built from the Java Language and
Virtual Machine specifications

Java byte code is similar to functionality to P
code or U

Several experimental projects translating Java byte codes to
either C or native machine code

Slower than C or C++, due to mandatory run
time checks

For many small Java programs, no advantage of compiling
over interpreting


It's Multi

Threads of control are an integral part of the Java language,
not a run
time library "add

Java offers preemptive multi
threading, implemented via
the Thread class

Especially important when developing applets

provide the proper dynamics between the Java applet and
the host browser

prevent applet from usurping most of the compute cycles


One of the most basic data structures, is an array. An
array is just a number of items, of same type, stored in
linear order, one after another. Arrays have a set limit
to their size, they can’t grow beyond that limit. An array
in Java is noted as:

int array[] = new int [10];


int [] array = new int [10];

This would create an array of integers of size 10. Any
element in that array can be accessed by:

int value = array[5];

This would put the value of the 5’th (counting from 0)
element into the variable ‘value’.

Arrays are very often manipulated in loops.
For example, if you have an array named
" and you'd like to print all of
them, one per line, you'll do something like

for(int i = 0;i<myNumbers.length;i++)


Arrays are not just restricted to being
type, they can be of any type, even of your
own created class.

Arrays are nice structures, which are
generally faster than other data structures,
but they do have limits.


int numbers[10];

static int numbers[10] = { 34, 27, 16 };

static int numbers[] = { 2,
3, 45, 79,
14, 5, 9, 28,
1, 0 };

static char text[] = "Welcome to New Zealand.";

static float radix[12] = { 134.362, 1913.248 };

double radians[1000];

#include <stdio.h>
int x;
static int values[] = { 1,2,3,4,5,6,7,8,9 };
static char word[] = { 'H','e','l','l','o' };
for( x = 0; x < 9; ++x )
printf("Values [%d] is %d\n", x, values[x]);
Sample Program Output
Values[0] is 1
Values[1] is 2
Values[8] is 9

The following program shows how to initialise all
the elements of an integer based array to the value
10, using a for loop to cycle through each element
in turn

#include <stdio.h>



int count;

int values[100];

for( count = 0; count < 100; count++ )

values[count] = 10;


Arrays and pointers are very closely
connected. The name of the array is also a
pointer to the beginning of the memory
associated with the array (which is why

(1) arrays are passed as reference
parameters and

(2) you can pass an array using only its


int vec [ 9 ];

float mat [ 9] [ 20 ];

vec [ k ] = vec [ k ] * 2;

mat [ k ] [ j ] := mat [ k ] [ j ] * vec [ k ];

Often when solving problems on the computer
you would like to represent and manipulate a set
of data values having similar characteristics

Arrays provide this capability

Limits of Array

One of the major limits, is that they're fixed in
size, and can't grow or shrink to accomodate
the data. The

class in java, uses an
array, and does make it seem that the array is
growing or shrinking. In reality,

checks to see if more space is needed, if it is,
then it creates a new array, copies the old
array to the new array, and makes the new
array, it's primary array, thus, giving the
impression that the array "grew" to fit the
data. This approach can get rather slow, if a
lot of growing & shrinking is needed.

Generating a Random Number

The following code snippet demonstrates how to generate a pseudo

random integer number that falls within a specific range.

Random random, random1;

int nbr;

random = new Random();

nbr = someMinValue +

Math.abs(random.nextInt()) %



random1 = new Random(nbr);

// create another randon number generator using

// a specific seed value

There are two constructors for
Random() . When invoked

with no argument, then a new pseudo

number generator is created using the current

time of day as a seed; otherwise, a pseudo

number generator can be created using a specific seed.


dimensioned arrays have two or more
index values which specify the element in
the array.


In the above example, the first index value i
specifies a row index, whilst j specifies a
column index.