AlgoAnim: Update the generator stuff

This commit is contained in:
M.Scholz 2012-05-16 10:36:32 +02:00
parent 5bb82966ac
commit 2ed9ecdbee
2 changed files with 159 additions and 93 deletions

View File

@ -23,12 +23,25 @@ import de.ahrgr.animal.kohnert.asugen.Font;
import generators.framework.properties.AnimationPropertiesContainer;
import algoanim.animalscript.AnimalScript;
public class RMultiGenerator implements Generator {
private Language lang;
private int b;
private int a;
public class MultiGenerator implements Generator {
/**
* static variables -> simple testing
*/
static int A = 27;
static int B = 82;
static int HIGHLIGHTING_TIME = 100; //in ticks
static int UNHIGHLIGHTING_TIME = 0; //in ticks
//=========================================================================================================
/**
* The concrete language object used for creating output
*/
private Language lang;
/**
* The info text as a SourceCode object (first slide)
*/
private SourceCode info = null;
@ -48,21 +61,56 @@ public class RMultiGenerator implements Generator {
/**
* The tables
*/
StringMatrix tb1 = null;
StringMatrix tb2 = null;
private StringMatrix tb1 = null;
private StringMatrix tb2 = null;
/**
* int-array with the values of b (only for animation)
*/
public int[] bArray = null;
private int[] bArray = null;
/**
* recursionArray
*/
public int[] recursionArray = null;
private int[] recursionArray = null;
/**
* unhighlightingTime
*/
private Timing unhighTime = new Timing(UNHIGHLIGHTING_TIME) {
@Override
public String getUnit() {
// TODO Auto-generated method stub
return "ticks";
}
};
/**
* highlightTime
*/
private Timing highTime = new Timing(HIGHLIGHTING_TIME) {
@Override
public String getUnit() {
// TODO Auto-generated method stub
return "ticks";
}
};
/**
* number of lines for tb1
*/
private int linesOfTb1 = 0;
//=========================================================================================================
/**
* Builds the information text for the first page in the animation
@ -140,7 +188,7 @@ public class RMultiGenerator implements Generator {
* @param b
* @return int number of needed lines
*/
public int calculateLines(int a, int b){
public void calculateLines(int a, int b){
//calculate needed lines -> animation is more dynamic
int aTemp = a;
@ -152,7 +200,7 @@ public class RMultiGenerator implements Generator {
//initialize the bArray und the recursionArray
this.bArray = new int[lines-1];
this.recursionArray = new int[lines-1];
return lines;
this.linesOfTb1 = lines;
}
@ -162,7 +210,7 @@ public class RMultiGenerator implements Generator {
* @param b
* @param lines
*/
public void generateTable(int a, int b, int lines){
public void generateTableTb1(int a, int b, int lines){
//create String[][] data array for tb1
String[][] tb1Data = new String[lines][4];
@ -240,10 +288,11 @@ public class RMultiGenerator implements Generator {
//create table tb2
if(this.tb2 != null){
this.tb2.hide(); //hide old version of tb2
this.tb2.put(1, 0, tmp, this.highTime, this.highTime);
}else{
this.tb2 = lang.newStringMatrix(new Offset(0, 10, this.tb1, AnimalScript.DIRECTION_SW), tb2Data, "tb2", null);
this.tb2.changeColor(AnimationPropertiesKeys.FILL_PROPERTY, Color.WHITE, null, null);
}
this.tb2 = lang.newStringMatrix(new Offset(0, 10, this.tb1, AnimalScript.DIRECTION_SW), tb2Data, "tb2", null);
this.tb2.changeColor(AnimationPropertiesKeys.FILL_PROPERTY, Color.WHITE, null, null);
}
@ -260,7 +309,7 @@ public class RMultiGenerator implements Generator {
int newLineNumber = 0;
int oldLineNumber = 0;
sc.unhighlight(2); //"return b"
this.sc.unhighlight(2); //"return b"
for(int i = this.bArray.length-1; i > 1; i--){
oldLineNumber = newLineNumber;
if(this.recursionArray[i-1] == 0){
@ -268,19 +317,23 @@ public class RMultiGenerator implements Generator {
}else{
newLineNumber = 7;
}
sc.unhighlight(oldLineNumber);
if(oldLineNumber == 5){
this.tb2.unhighlightCell(1, 0, this.unhighTime, this.unhighTime);
}
this.sc.unhighlight(oldLineNumber);
//STEP
lang.nextStep();
sc.highlight(newLineNumber);
this.sc.highlight(newLineNumber);
if(newLineNumber == 5){
this.tb2.highlightCell(1, 0, this.highTime, this.highTime);
}
//update tb2
this.bArray[i-1] = this.bArray[i-1] + this.bArray[i];
drawTb2(i);
//STEP
lang.nextStep();
}
sc.unhighlight(oldLineNumber);
this.sc.unhighlight(oldLineNumber);
}
/**
@ -290,7 +343,7 @@ public class RMultiGenerator implements Generator {
*/
public void multiply(int a, int b){
//generate information text and the header
//generate information text and the header
generateInfoText();
generateHeader();
@ -302,8 +355,8 @@ public class RMultiGenerator implements Generator {
generateSourceCode();
//calculate needed lines and generate table
int lines = calculateLines(a, b);
generateTable(a, b, lines);
calculateLines(a, b);
generateTableTb1(a, b, this.linesOfTb1);
//STEP
lang.nextStep();
@ -311,9 +364,10 @@ public class RMultiGenerator implements Generator {
//higlight line 0
this.sc.highlight(0);
russe(a, b, sc, tb1, 1, "");
// this.tb1.put(lines - 1, 2, "Ergebnis", null, null);
//this.tb1.put(lines - 1, 3, result+"", null, null);
russe(a, b, sc, tb1, 1, "");
//unhighlight last line of tb1
this.tb1.unhighlightCellColumnRange(this.linesOfTb1-2, 2, 3, this.unhighTime, this.unhighTime);
//generate recursion animation
generateRecursion();
@ -341,24 +395,17 @@ public class RMultiGenerator implements Generator {
*/
public int russe(int a, int b, SourceCode code, StringMatrix tb1, int line, String sum){
int aTb = a;
int bTb = b;
Timing sTime = new Timing(100) {
@Override
public String getUnit() {
// TODO Auto-generated method stub
return "ticks";
}
};
tb1.highlightCell(line, 0, sTime, sTime);
tb1.put(line, 0, a+"", null, null); //update column 0 (a)
tb1.put(line, 1, b+"", null, null); //update column 1 (b)
this.tb1.unhighlightCellColumnRange(line-1, 2, 3, this.unhighTime, this.unhighTime);
this.tb1.highlightCellColumnRange(line, 0, 1, this.highTime, this.highTime);
this.tb1.put(line, 0, a+"", null, null); //update column 0 (a)
this.tb1.put(line, 1, b+"", null, null); //update column 1 (b)
//STEP
lang.nextStep();
this.tb1.unhighlightCellColumnRange(line, 0, 1, this.unhighTime, this.unhighTime);
code.unhighlight(0);
code.highlight(1);
@ -371,8 +418,9 @@ public class RMultiGenerator implements Generator {
//write last line
sum = sum+"+"+b;
//update table
tb1.put(line, 2, "Ja", null, null); //update column 2 (addieren)
tb1.put(line, 3, sum, null, null); //update column 3 (Summe)
this.tb1.highlightCellColumnRange(line, 2, 3, this.highTime, this.highTime);
this.tb1.put(line, 2, "Ja", null, null); //update column 2 (addieren)
this.tb1.put(line, 3, sum, null, null); //update column 3 (Summe)
this.bArray[line] = b;
return b;
}
@ -399,8 +447,9 @@ public class RMultiGenerator implements Generator {
}
}
//update table
tb1.put(line, 2, "Ja", null, null); //update column 2 (addieren)
tb1.put(line, 3, sum, null, null); //update column 3 (Summe)
this.tb1.highlightCellColumnRange(line, 2, 3, this.highTime, this.highTime);
this.tb1.put(line, 2, "Ja", null, null); //update column 2 (addieren)
this.tb1.put(line, 3, sum, null, null); //update column 3 (Summe)
//STEP
lang.nextStep();
@ -414,8 +463,9 @@ public class RMultiGenerator implements Generator {
else{
code.toggleHighlight(4, 7);
//update table
tb1.put(line, 2, "Nein", null, null); //update column 2 (addieren)
tb1.put(line, 3, sum, null, null); //update column 3 (Summe)
this.tb1.highlightCellColumnRange(line, 2, 3, this.highTime, this.highTime);
this.tb1.put(line, 2, "Nein", null, null); //update column 2 (addieren)
this.tb1.put(line, 3, sum, null, null); //update column 3 (Summe)
//STEP
lang.nextStep();
@ -427,17 +477,20 @@ public class RMultiGenerator implements Generator {
return russe(a/2, b*2, code, tb1, line+1, sum);
}
}
public void init(){
lang = new AnimalScript("Russische Bauenmultiplikation [DE]", "Ulf Gebhardt, Michael Scholz", 800, 600);
}
public String generate(AnimationPropertiesContainer props,Hashtable<String, Object> primitives) {
b = (Integer)primitives.get("b");
a = (Integer)primitives.get("a");
B = (Integer)primitives.get("b");
A = (Integer)primitives.get("a");
RMultiGenerator multi = new RMultiGenerator();
multi.multiply(a, b);
init(); //initialize the language
lang.setStepMode(true);
this.multiply(A, B);
return lang.toString();
}
@ -456,53 +509,53 @@ public class RMultiGenerator implements Generator {
public String getDescription(){
return "Das hier vorgestellte Verfahren eignete sich fŸr die Multiplikation zweier ganzer Zahlen. Es ist auch unter den"
+"\n"
+"Namen &Aumlgyptischen Multiplizieren, Abessinische Bauernregel oder Verdopplungs-Halbierungs-Methode"
+"\n"
+"bekannt. Die Geschichte des vorgestellten Rechenverfahrens f&uumlhrt bis auf die &Aumlgypter zurŸck. Sie"
+"\n"
+"nutzten diese Methode nachweislich zur Multiplikation zweier ganzer Zahlen. Das Verfahren baut auf dem Teile und Herrsche Prinzip"
+"\n"
+"(Divide et impera ) auf und lŠsst sich somit leicht mittels Rekursion implementieren.\n"
+"\n"
+"Die Funktionsweise l&aumlsst sich in die folgenden f&uumlnf Schritte gliedern:\n"
+"\n"
+"1. Schreibe die beiden zu multiplizierenden Zahlen nebeneinander.\n"
+"\n"
+"2. Die linke Zahl wird halbiert (Reste werden abgerundet), die rechte Zahl wird verdoppelt.\n"
+"\n"
+"\t Die beiden berechneten Zahlen werden in die darauffolgende Zeile geschrieben.\n"
+"\n"
+"3. Schritt 2 wird solange wiederholt, bis in der linken Spalte eine 1 steht.\n"
+"\n"
+"4. Nun streicht man alle Zeilen, in denen die linke Zahl gerade ist.\n"
+"\n"
+"5. Schlussendlich werden alle &uumlbrigen Zahlen der rechten Spalte addiert."
+"\n"
+" ";
+"\n"
+"Namen &Aumlgyptischen Multiplizieren, Abessinische Bauernregel oder Verdopplungs-Halbierungs-Methode"
+"\n"
+"bekannt. Die Geschichte des vorgestellten Rechenverfahrens f&uumlhrt bis auf die &Aumlgypter zurŸck. Sie"
+"\n"
+"nutzten diese Methode nachweislich zur Multiplikation zweier ganzer Zahlen. Das Verfahren baut auf dem Teile und Herrsche Prinzip"
+"\n"
+"(Divide et impera ) auf und lŠsst sich somit leicht mittels Rekursion implementieren.\n"
+"\n"
+"Die Funktionsweise l&aumlsst sich in die folgenden f&uumlnf Schritte gliedern:\n"
+"\n"
+"1. Schreibe die beiden zu multiplizierenden Zahlen nebeneinander.\n"
+"\n"
+"2. Die linke Zahl wird halbiert (Reste werden abgerundet), die rechte Zahl wird verdoppelt.\n"
+"\n"
+"\t Die beiden berechneten Zahlen werden in die darauffolgende Zeile geschrieben.\n"
+"\n"
+"3. Schritt 2 wird solange wiederholt, bis in der linken Spalte eine 1 steht.\n"
+"\n"
+"4. Nun streicht man alle Zeilen, in denen die linke Zahl gerade ist.\n"
+"\n"
+"5. Schlussendlich werden alle &uumlbrigen Zahlen der rechten Spalte addiert."
+"\n"
+" ";
}
public String getCodeExample(){
return "public int russe(int a, int b){"
+"\n"
+" if(a == 1){"
+"\n"
+" return b;"
+"\n"
+" }"
+"\n"
+" if(a % 2 == 1){"
+"\n"
+" return b + russe(a/2, b*2);"
+"\n"
+" }else{"
+"\n"
+" return russe(a/2, b*2);"
+"\n"
+" }"
+"\n"
+"}"
+"\n";
+"\n"
+" if(a == 1){"
+"\n"
+" return b;"
+"\n"
+" }"
+"\n"
+" if(a % 2 == 1){"
+"\n"
+" return b + russe(a/2, b*2);"
+"\n"
+" }else{"
+"\n"
+" return russe(a/2, b*2);"
+"\n"
+" }"
+"\n"
+"}"
+"\n";
}
public String getFileExtension(){
@ -518,7 +571,7 @@ public class RMultiGenerator implements Generator {
}
public String getOutputLanguage() {
return RMultiGenerator.JAVA_OUTPUT;
return Generator.JAVA_OUTPUT;
}
}

View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="ISO-8859-1"?>
<PropertiesTreeModel xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="PropertiesTreeModel.xsd">
<Folder name="Root">
<Primitive type="int">
<name>a</name>
<value><int>27</int></value>
</Primitive>
<Primitive type="int">
<name>b</name>
<value><int>82</int></value>
</Primitive>
</Folder>
</PropertiesTreeModel>