Computer Basics

Designing a
Web Site


Programming Basics
Web Programming and Security

Using the Internet

About the Computer Gal

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


CS331: Program 1 Commented Code

/*
    All classes in Java are "data structures"
    This program uses the following classes/data structures:
       1. class YOUR NAME
       2.
class CACanvas
       3. class CAControls
       4. class CATools
*/


/* Import statements bring in code already written and stored with all the Java stuff. In C, they are called libraries. */
 1.  import java.awt.*; // a large collection of classes for building graphical user interfaces in Java
 2. import java.awt.event.*;
  //Provides the classes necessary to create an applet and the classes an applet uses to communicate with its applet context.
 3 .  import java.applet.*;
 4.  import java.util.BitSet;
// The class Math contains methods for performing basic numeric operations such as the elementary exponential, logarithm, square root, and trigonometric functions.  
 import java.lang.Math;


/* This class adds to what Applet can do.
     This class contains main, so it is the "engine" for the whole collection.
     There are three global variables.
         1. SIZE
         2. controls
         3. canvas
*/

/* Nora McDougall
CS331
Programming Assignment 1

Naming conventions:
1. All one word variables will be lower case.
2. All multiword variables will have the first letter of each word capitalized.

DISCLAIMER: you are more than welcome to "check out" my code to understand it and get ideas,
but please don't copy it as your own homework. I don't promise it's correct, but its output is
is at least similar to correct.
*/

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.util.BitSet;
import java.lang.Math;

public class NoraMcDougallPA1 extends Applet {
static int SIZE=128;
CAControls controls; // The controls for controlling the CA
CACanvas canvas; // The drawing area to display CA

public void init() {
setLayout(new BorderLayout());
canvas = new CACanvas();
add("Center", canvas);
add("South", controls = new CAControls(canvas));
canvas.setControls(controls);
}

public void destroy() {
remove(controls);
remove(canvas);
}

public void start() {
controls.setEnabled(true);
}

public void stop() {
controls.setEnabled(false);
}

public static void main(String args[]) {
Frame f = new Frame("Nora's Program #1");
NoraMcDougallPA1 caLab = new NoraMcDougallPA1();

caLab.init();
caLab.start();

f.add("Center", caLab);
f.setSize(SIZE*5, SIZE*5);
f.show();
}

public String getAppletInfo() {
return "This Applet designed to explore simple one dimensional cellular automata.\n To be used to complete assignments in CS 331.";
}
}

class CACanvas extends Canvas {
private CATools catools=new CATools();
private BitSet rule=new BitSet(8);
private int intrule;
private CAControls controls;

public void paint(Graphics g) {
Rectangle r = getBounds();
int hlines = r.height / 5;
int vlines = r.width / 5;
BitSet line=new BitSet(r.width/5);

// Redraw controls to include rule number.
controls.setLabel("Rule Number: "+this.getRule());

// Draw the background grid
g.setColor(Color.red);
for (int i = 0; i <= hlines; i++) {
g.drawLine(0, i * 5, r.width, i * 5);
}
for (int i = 0; i <= vlines; i++) {
g.drawLine(i * 5, 0, i * 5, r.height);
}
// Draw the cellular automata state
g.setColor(Color.blue);
line.set(line.size()/2);
drawRow(0,line,g);
for (int i=1;i<r.height/5;i++){
line=catools.updateLine(rule,line);
drawRow(i,line,g);
}
}

private void drawRow(int row, BitSet data, Graphics g) {
for (int i=0;i<data.size();i++){
if (data.get(i))
g.fillRect(i*5,row*5,5,5);
}
}

public void setControls(CAControls c){
controls=c;
}

public void setRule(int r){
intrule=r;
redraw();
}

public int getRule(){
return intrule;
}

public void redraw(){
rule=catools.setBitRule(intrule);
repaint();
}
}

class CAControls extends Panel
implements ActionListener {
CACanvas canvas;
Scrollbar rulenumber;
private Label l = new Label("Rule Value: 0 ");

public CAControls(CACanvas canvas) {
Button b = null;
rulenumber=new Scrollbar(Scrollbar.HORIZONTAL,0,1,0,256);
rulenumber.addAdjustmentListener(new ScrollBarListener(canvas));
this.canvas = canvas;
add(this.l);
add(rulenumber);
b = new Button("Quit");
b.addActionListener(this);
add(b);
}

public void setLabel(String s){
l.setText(s);
}

public void actionPerformed(ActionEvent ev) {
String label = ev.getActionCommand();
if (label.equals("Quit"))
System.exit(0);
}
}

class ScrollBarListener implements AdjustmentListener {
private CACanvas canvas;

public ScrollBarListener(CACanvas c){
canvas=c;
}

public void adjustmentValueChanged(AdjustmentEvent e){
canvas.setRule(e.getValue());
}
}

class CATools {

/*
This is the first method of CATools
PRECONDITION: this method receives a decimal number and turns it into a BitSet
of 8 binary digits. Because this implementation assumes 8 digits, it can only
use decimal numbers from 0 to 255. Any numbers above 255 is replaced with 255.
Any numbers below 0 are replaced with 0.
WHAT IT DOES: this method loops dividing the passed in value and results by 2 and
uses the mod operator to determine the binary for each of the 8 positions.
POSTCONDITION: this method returns an 8 digit BitSet that is equivalent to the
decimal number, with the exceptions noted in the precondition.
*/
public BitSet setBitRule(int rule)
{
/* number of indexes in BitSet. This is one of the parameters passed in when a new
BitSet is created.
*/
int size = 8;

/* will hold the passed value/2 each iteration of the for loop until the loop
has run through the 8 places
*/
int half;

/* this variable will hold the 0 or 1 value returned by the modulus operation. This
value will be passed to the BitSet
*/
int BitValue;

// new BitSet object to return
BitSet bitset = new BitSet(size);

/* These two if statements are needed by Program 1 in case the passed in value is too
large or negative
*/
if(rule < 0)
{
rule = 0;
}

if(rule > 255)
{
rule = 255;
}

/* Runs through the 8 indexes of the passed in value. If the value doesn't have
8 binary places it will end up dividing 0 by 2, which will keep the 0 values in the
upper indexes.
*/
for(int count = 7; count >= 0; --count)
{
// Divides by 2 and sets the new value to divide by 2 the next time.
half = rule/2;
/* Figures out whether there is a remainder or not. If no remainder that spot gets 0
otherwise that spot gets 1 (1/2 rounds (mod) up to 1)
*/
BitValue = rule%2;

// only changes that spot if it needs a 1 because the default values are all 0
if(BitValue == 1)
{
// the indexes and count
bitset.set(count);
} // end if

// starts the loop again with the halfed value
rule = half;

} // end for

return bitset;
}

/* Precondition:
1) Receive a number in the from the slider that has been converted into a 8 digit BitSet.
2) Receive a 255 bit line BitSet.
Steps (see comments in code for specifics):
1) Set variables
2) Set new line with added ends.
3) Set triplets and find matching indexes in rule
4) Set rule value in new line index
5) Reset triplet for next iteration
6) Return new line
Postcondition:
Returns a new line based on manipulation of the original line with the rule.
*/
public BitSet updateLine(BitSet rule, BitSet line)
{
/* Step 1: These are the variables */
int i, j; // counters
int size = line.size(); // runs the counters through the size of line
BitSet line2 = new BitSet (size + 2); // new line to hold first and last digit
BitSet NewLine = new BitSet(size); // new line to return
BitSet triplets = new BitSet(3); // hold sets of three bits from line
int CurrentIndex = 0; // holds index based on triplet

// Step 2: Fill the second line with: last, repeat original, first
if(line.get(size) == true)
line2.set(0);
// puts the last in the first

for(i = 0; i < size; ++i)
{
if(line.get(i) == true)
{
line2.set(i + 1);
} // end if
} // repeat original

if(line.get(0) == true)
line2.set(size + 1);
// puts the first in the last

// Step 3: Get triplet BitSets and find line index to change based on rule
for(j = 0; j < size; ++j)
/* Loop through the whole line. This goes through size because once the inner loop starts
through, the triplet picks up the two past the starting index.
*/
{
for(i = 0; i<3; ++i) // loop through three indexes
{
if(line2.get(j + i))
{
triplets.set(i);
// picks up the first three digits from rule (sets i if it's 1)
} // end if

// Find a binary number (0 - 7) that matches the Triplet
// this will find the index of the rule
CurrentIndex = 0;
if(triplets.get(0) == true)
CurrentIndex = 4;
if(triplets.get(1) == true)
CurrentIndex += 2;
if(triplets.get(2) == true)
CurrentIndex += 1;
}

// Step 4: Fill j index in line with matching rule index value.
if(rule.get(CurrentIndex) == true)
{
NewLine.set(j);
}

// Step 5: reset triplets for next time
triplets.set(0, 3, false);
// if you don't reset triplet, it will keep the ones left from this iteration.
}
// Step 6: return new line
return NewLine;
}
}



Nora McDougall | Missoula, Montana 59801 | 406.543.1452 | the_computer_gal@yahoo.com
Copyright 2003, Nora McDougall