first commit

This commit is contained in:
Boris
2024-01-15 20:14:10 +00:00
commit 8c81ee28b7
3106 changed files with 474415 additions and 0 deletions

View File

@@ -0,0 +1,14 @@
#BlueJ class context
comment0.params=numberOfCells
comment0.target=Automaton(int)
comment0.text=\n\ Create\ a\ 1D\ automaton\ consisting\ of\ the\ given\ number\ of\ cells.\n\ @param\ numberOfCells\ The\ number\ of\ cells\ in\ the\ automaton.\n
comment1.params=
comment1.target=void\ print()
comment1.text=\n\ Print\ the\ current\ state\ of\ the\ automaton.\n
comment2.params=
comment2.target=void\ update()
comment2.text=\n\ Update\ the\ automaton\ to\ its\ next\ state.\n
comment3.params=
comment3.target=void\ reset()
comment3.text=\n\ Reset\ the\ automaton.\n
numComments=4

View File

@@ -0,0 +1,69 @@
import java.util.Arrays;
/**
* Model a 1D elementary cellular automaton.
*
* @author David J. Barnes and Michael Kölling
* @version 2016.02.29 - version 2
*/
public class Automaton
{
// The number of cells.
private final int numberOfCells;
// The state of the cells.
private int[] state;
/**
* Create a 1D automaton consisting of the given number of cells.
* @param numberOfCells The number of cells in the automaton.
*/
public Automaton(int numberOfCells)
{
this.numberOfCells = numberOfCells;
state = new int[numberOfCells];
// Seed the automaton with a single 'on' cell in the middle.
state[numberOfCells / 2] = 1;
}
/**
* Print the current state of the automaton.
*/
public void print()
{
for(int cellValue : state) {
System.out.print(cellValue == 1 ? "*" : " ");
}
System.out.println();
}
/**
* Update the automaton to its next state.
*/
public void update()
{
// Build the new state in a separate array.
int[] nextState = new int[state.length];
int left = 0;
int center = state[0];
for(int i = 0; i < state.length; i++) {
int right = i + 1 < state.length ? state[i + 1] : 0;
nextState[i] = (left + center + right) % 2;
left = center;
center = right;
}
state = nextState;
}
/**
* Reset the automaton.
*/
public void reset()
{
Arrays.fill(state, 0);
// Seed the automaton with a single 'on' cell.
state[numberOfCells / 2] = 1;
}
}

View File

@@ -0,0 +1,17 @@
#BlueJ class context
comment0.params=numberOfCells
comment0.target=AutomatonController(int)
comment0.text=\n\ Create\ an\ AutomatonController.\n\ @param\ numberOfCells\ The\ number\ of\ cells\ in\ the\ automaton.\n
comment1.params=
comment1.target=AutomatonController()
comment1.text=\n\ Create\ an\ AutomatonController\ with\n\ a\ default\ number\ of\ cells.\n
comment2.params=numSteps
comment2.target=void\ run(int)
comment2.text=\n\ Run\ the\ automaton\ for\ the\ given\ number\ of\ steps.\n\ @param\ numSteps\ The\ number\ of\ steps.\n
comment3.params=
comment3.target=void\ step()
comment3.text=\n\ Run\ the\ automaton\ for\ a\ single\ step.\n
comment4.params=
comment4.target=void\ reset()
comment4.text=\n\ Reset\ the\ automaton.\n
numComments=5

View File

@@ -0,0 +1,60 @@
/**
* Set up and control an elementary cellular automaton.
*
* @author David J. Barnes and Michael Kölling
* @version 2016.02.29
*/
public class AutomatonController
{
// The automaton.
private Automaton auto;
/**
* Create an AutomatonController.
* @param numberOfCells The number of cells in the automaton.
*/
public AutomatonController(int numberOfCells)
{
auto = new Automaton(numberOfCells);
auto.print();
}
/**
* Create an AutomatonController with
* a default number of cells.
*/
public AutomatonController()
{
this(50);
}
/**
* Run the automaton for the given number of steps.
* @param numSteps The number of steps.
*/
public void run(int numSteps)
{
for(int step = 1; step <= numSteps; step++) {
step();
}
}
/**
* Run the automaton for a single step.
*/
public void step()
{
auto.update();
auto.print();
}
/**
* Reset the automaton.
*/
public void reset()
{
auto.reset();
auto.print();
}
}

View File

@@ -0,0 +1,11 @@
Project: automaton
Aothors: David J. Barnes and Michael Kölling
This project is part of the material of the book
Objects First with Java - A Practical Introduction using BlueJ
Sixth edition
David J. Barnes and Michael Kölling
Pearson Education, 2016
It is discussed in chapter 7.

View File

@@ -0,0 +1,45 @@
#BlueJ package file
dependency1.from=AutomatonController
dependency1.to=Automaton
dependency1.type=UsesDependency
objectbench.height=76
objectbench.width=737
package.editor.height=400
package.editor.width=629
package.editor.x=70
package.editor.y=80
package.numDependencies=1
package.numTargets=2
package.showExtends=true
package.showUses=true
project.charset=UTF-8
readme.editor.height=700
readme.editor.width=900
readme.editor.x=53
readme.editor.y=23
target1.editor.height=774
target1.editor.width=867
target1.editor.x=454
target1.editor.y=65
target1.height=50
target1.name=Automaton
target1.naviview.expanded=true
target1.showInterface=false
target1.type=ClassTarget
target1.typeParameters=
target1.width=90
target1.x=170
target1.y=160
target2.editor.height=777
target2.editor.width=994
target2.editor.x=286
target2.editor.y=23
target2.height=50
target2.name=AutomatonController
target2.naviview.expanded=false
target2.showInterface=false
target2.type=ClassTarget
target2.typeParameters=
target2.width=160
target2.x=70
target2.y=70