Hello,
I have to write a class test about our last project in school.
I just copied what the teatcher wrote down in the script, but nobody undersands what effect those
command blocks have. If I would know what they cause, I would maybe be able to change them.
I will mark the command-parts with
Just write what simple effect the entered commands have.
Thank you :)
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.Random;
import java.util.*;
import java.lang.*;
--------------------------------------------------
/**
* Write a description of class MyWorld here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Medium extends World
{
public static final int cellsX = 80;
public static final int cellsY = 80;
private static final int cellSize = 10;
public static final double forceConst = 1.0;
public static double attractiveForce = 0.1;
public static double forceThreshold = 0.1;
public static double temperature = 5;
public enum Kind
{
KATION, ANION, NEUT
}
Random rand = new Random(); // Initialisierung einer Zufallszahl
public static class Vector
{
public double x,y;
public Vector()
{
this.x = 0.0;
this.y = 0.0;
}
public Vector (double x, double y)
{
this.x = x;
this.y = y;
}
}
public static class Cell
{
public int x,y;
public Particle particle;
boolean occupied;
public Cell()
{
this.x = 0;
this.y = 0;
this.particle = null;
this.occupied = false;
}
public Cell(int x, int y, Particle particle, boolean occupied)
{
this.x = x;
this.y = y;
this.particle = particle;
this.occupied = occupied;
}
}
public static Cell[][] cell = new Cell[cellsX][cellsY];
public static List<Cell> freeCells = new ArrayList<Cell>();
public static List<Cell> occupiedCells = new ArrayList<Cell>();
public static List<Cell> chargedCells = new ArrayList<Cell>();
public static List<Particle> chargedParticles = new ArrayList<Particle>();
------------------------------------------------
/**
* Constructor for objects of class MyWorld.
*
*/
public Medium()
{
// Create a new world with cellsX x cellsY cells with a cell size of cellSize x cellSize pixels.
super(cellsX, cellsY, cellSize);
/*
* Initialisierung aller Zellen
* und Vorbereiten der Mengen der freien und besetzen Zellen
*/
freeCells.clear(); // Liste freier Zellen leeren
occupiedCells.clear(); // Liste besetzter Zellen leeren
chargedCells.clear(); // Liste besetzter geladener Zellen leeren
chargedParticles.clear(); // Liste geladener Teilchen leeren
for (int x=0; x<cellsX; x++) // Initialisierung aller Zellen des Mediums
{
for (int y=0; y<cellsY; y++)
{
cell[x][y] = new Cell(x,y,null,false); // Verwendung des polymorphen Konstruktors
freeCells.add(cell[x][y]); // Liste freier Zellen füllen
}
}
}
--------------------------------------------------
/**
* createParticle - erzeugt ein Particle der Art kind und der Ladung charge
* an einer gegebenen Position
*/
public void createParticle(Kind kind, int charge, int x, int y)
{
Particle particle;
particle=new Particle(kind, charge, x, y);
addObject(particle,x,y);
cell[x][y].particle = particle;
cell[x][y].occupied = true;
freeCells.remove(cell[x][y]);
occupiedCells.add(cell[x][y]);
if (charge!=0)
{
chargedCells.add(cell[x][y]);
chargedParticles.add(particle);
}
}
-----------------------------------------------------
/**
* createParticles - erzeugt n Teilchen der Art kind und der Ladung charge an zufälligen Positionen
*/
public void createParticles(int n, Kind kind, int charge)
{
int x,y;
Cell freeCell;
if (freeCells.size()>=n) {
for(int i=0; i<n; i++)
{
freeCell = freeCells.get(rand.nextInt(freeCells.size()));
createParticle(kind, charge,freeCell.x,freeCell.y);
}
}
else
{
System.out.println("Not enough free cells, only "+freeCells.size()+" cells available!"); // exception
}
}
--------------------------------------------
/**
* createKations - erzeugt n Teilchen der Art KATION und der Ladung charge=1 an zufälligen Positionen
*/
public void createKations(int n)
{
createParticles(n, Kind.KATION, 1);
}
-------------------------------------------------------
/**
* createKations - erzeugt n Teilchen der Art KATION und der Ladung charge an zufälligen Positionen
*/
public void createKations(int n, int charge)
{
createParticles(n, Kind.KATION, charge);
}
-----------------------------------------------------
/**
* createAnions - erzeugt n Teilchen der Art ANION und der Ladung charge=-1 an zufälligen Positionen
*/
public void createAnions(int n)
{
createParticles(n, Kind.ANION, -1);
}
---------------------------------------------------
/**
* createAnions - erzeugt n Teilchen der Art ANION und der Ladung charge an zufälligen Positionen
*/
public void createAnions(int n, int charge)
{
createParticles(n, Kind.ANION, charge);
}
------------------------------------------------
/**
* createNeuts - erzeugt n Teilchen der Art NEUT und der Ladung charge=0 an zufälligen Positionen
*/
public void createNeuts(int n)
{
createParticles(n, Kind.NEUT, 0);
}
--------------------------------------------------------
/**
* createKationsAnions - erzeugt jeweils n Teilchen der Art KATION und ANION und der Ladung +1 und -1 an zufälligen Positionen
*/
public void createKationsAnions(int n)
{
createKations(n);
createAnions(n);
}
-----------------------------------
/**
* createKationsAnions - erzeugt jeweils n Teilchen der Art KATION und ANION und der Ladung +charge und -charge an zufälligen Positionen
*/
public void createKationsAnions(int n, int charge)
{
int chargeKation, chargeAnion;
chargeKation = charge;
chargeAnion = -charge;
createKations(n,chargeKation);
createAnions(n,chargeAnion);
}
-------------------------------------------
/**
* force - Kraft auf ein Teilchen particle von einem anderen otherParticle
*/
public static Vector force(Particle particle, Particle otherParticle)
{
Vector unitVector, force=new Vector();
double distanceSquare, distance, forceValue;
if (particle != otherParticle)
{
distanceSquare = Math.pow(otherParticle.x - particle.x, 2) + Math.pow(otherParticle.y - particle.y, 2);
distance = Math.sqrt(distanceSquare);
forceValue = (-forceConst*particle.charge*otherParticle.charge + Medium.attractiveForce)/Math.pow(distance,2);
unitVector = unitVector((otherParticle.x - particle.x), (otherParticle.y - particle.y));
force = multiplyVector(unitVector, forceValue);
}
return force;
}
---------------------------------------
/**
* totalForce - Kraft auf ein Teilchen particle von allen anderen Teilchen in der Liste otherParticles
*/
public static Vector totalForce(Particle particle, List<Particle> otherParticles)
{
Vector totalForce = new Vector();
Particle otherParticle;
for (int i=0; i<otherParticles.size();i++)
{
otherParticle = otherParticles.get(i);
totalForce.x += force(particle,otherParticle).x;
totalForce.y += force(particle,otherParticle).y;
}
return totalForce;
}
-------------------------------------------
/**
* unitVector - erzeugt einen Einheitsvektor in Richtung der Koordinaten x,y
*/
public static Vector unitVector(double x, double y)
{
double length;
length = Math.sqrt(x*x + y*y);
return new Vector(x/length,y/length);
}
----------------------------------------------
/**
* multiplyVector - multipliziert Vector vector mit dem Skalar scalar
*/
public static Vector multiplyVector(Vector vector, double scalar)
{
vector.x *= scalar;
vector.y *= scalar;
return vector;
}
----------------------------------------------
/**
* Setze temperatur Wert
*/
public void setTemperature(double temperature)
{
this.temperature = temperature;
}
-------------------------------------------------------
/**
* Setze forceThreshold Wert
*/
public void setForceThreshold(double forceThreshold)
{
this.forceThreshold = forceThreshold;
}
-----------------------------------------
/**
* Setze attractiveForce Wert
*/
public void setAttractiveForce(double attractiveForce)
{
this.attractiveForce = attractiveForce;
}
---------------------------------------------------
}

