Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Automaton JAVA Implement the full class Automaton from the lesson. Test the clas

ID: 3663048 • Letter: A

Question

Automaton JAVA

Implement the full class Automaton from the lesson. Test the class in a main() that works like the main() from the lesson. Show at least four different sample runs of width 89, giving 50 generations per run (to keep the output manageable). Show me the runs for rules 4, 126 and 130. Add at least one more rule of your choice.

Here is a repeat of the specification of the class.

The meaning and behavior of members and methods is in the modules, but here is a summary.

Automaton(int rule) - a constructor. Through the mutator, below, we'll sanitize rule and then convert it to our internal representation. We also need to establish the seed: a single 1 in a sea of 0s (in this assignment a 1 means an asterisk, '*', and a 0 means a blank, ' '. This is accomplished more directly by setting thisGen to "*" and extremeBit to " ".

String toStringCurrentGen() - This returns a string consisting of the current generation, thisGen, but does so by embedding it in a return String whose length is exactlydisplayWidth long. If thisGen is smaller than displayWidth, it is positioned in the center of the larger return string, with the excess padded by extremeBit characters. If thisGen islonger than displayWidth, it has to be truncated (on both ends) so that it is perfectly centered in the returned string, any excess trimmed off both ends, equally. You can easily do this with simple String objects and concatenation.

Two Mutators:boolean setRule(int newRule) - converts the int newRule, a number from 0-255, to an array of eight booleans. For example, if the newRule is 182, then this is the binary number 1 0 1 1 0 1 1 0, which means that the resultant array will be

rule[7] = true,
rule[6] = false ,
rule[5] = true,
rule[4] = true,
rule[3] = false,
rule[2] = true,
rule[1] = true ,
rule[0] = false.

I have color coded two of the bits in 182 and their corresponding booleans in the array rule[] so you can easily see how the conversion needs to work. As usual, this must sanitize the int so that only values in the legal range 0-255 are allowed.

boolean setDisplayWidth(int width) - this is described by the earlier description of displayWidth and MAX_DISPLAY_WIDTH. I repeat that only odd widths are allowed (for centering purposes).

void propagateNewGeneration() - this is the workhorse function. It will use the three private members thisGen, extremeBit and rule[], and create the next generation from them. This method must first append two extremeBits to each side of thisGen in order to provide it with enough bits (or chars) on each end needed by rule. This adds four chars to thisGen, temporarily. We then apply rule in a loop to the new, larger, thisGen, which creates a separate (local) nextGen String. If you do this correctly, nextGen will be two characters smaller than the recently augmented thisGen (but still two larger than the original thisGen that entered the method). [You must understand this statement, conceptually, before you write your first line of code, or you will be doomed.] Then, you replace the old thisGen with our new nextGen.

In brief, we pad thisGen with four extremeBits, then apply rule, and we have a new nextGen, two larger than we started out with. We copy that back to thisGen to complete the cycle.

Finally, we have to apply rule to three consecutive extremeBits to figure out what the new extremeBit will be for the next generation (" " or "*"?) . What do I mean by "three consecutive"? We apply rule to an int representing a 3-bit pattern inside the old generation. In this case, we are doing this way out where all the bits are the same:  extremeBit. So each input will be three 0s or three 1s -- an int 0 or an int 7 -- depending on the value of extremeBit. The result will enable us determine the new value of extremeBit. We must do this before we return, so extremeBit is correct for the next call to this method.

Supply a sample set of at least four runs which include the rules, 4, 126, 130, and one or more of your choice. One run would look something like this:

The above pattern is shorter than an actual run that 50 generations would produce.

Explanation / Answer

class Automaton
{
public final static int MAX_DISPLAY_WIDTH = 121;
private boolean rules[];
private String thisGen;   
String extremeBit;
int displayWidth;
public Automaton(int new_rule)
{
rules = new boolean[8];
resetFirstGen();
extremeBit = " ";
setDisplayWidth(MAX_DISPLAY_WIDTH);
SetRule(new_rule);
}
public Automaton()
{
this(0);
}
public void resetFirstGen()
{
thisGen = "*";
}
public boolean SetRule(int new_rule)
{
if(new_rule > 255 || new_rule < 0)
{
for (int i = 0; i < rules.length; i++)
{
rules[i] = false;
}
return false;
}
else
{
for (int i = 0; i < rules.length; i++)
{
if (new_rule % 2 == 1)
{
rules[i] = true;
}
else
{
rules[i] = false;
}

new_rule = new_rule / 2;
}
}
return true;
}
public boolean setDisplayWidth(int width)
{
if (width > MAX_DISPLAY_WIDTH)
{
return false;
}
displayWidth = width;
return true;
}
public String toStringCurrentGen()
{
int blanks = (displayWidth - thisGen.length())/2;
String buffer = "";
String newString = thisGen;
if (blanks > 0)
{
for (int i = 0; i < blanks; i++)
{
buffer = buffer + extremeBit;
}
newString = buffer + thisGen + buffer;
}
else if (blanks < 0)
{
newString = newString.substring(-blanks, thisGen.length()+blanks);
} //else newString = thisGen;
return newString;
}
public void propagateNewGeneration()
{
String newGen = "";
thisGen = extremeBit + extremeBit + thisGen + extremeBit + extremeBit;
//System.out.println("thisGen length = " + thisGen.length());
for(int i = 1; i < thisGen.length()-1; i++)
{
int bin = 0; //binary value of index number
int pow = 4;
for (int j = 0; j < 3; j++)
{
if (thisGen.charAt(i-1+j) == '*')
{
bin += pow;
}
pow = pow / 2; // 4, 2, 1;
}
if(rules[bin])
{
newGen = newGen + '*';
} else { newGen = newGen + ' '; }
}
if (extremeBit == " " && rules[0] ) { extremeBit = "*"; };
if (extremeBit == "*" && !rules[7] ) { extremeBit = " "; };
thisGen = newGen;
//
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote