Re: Very complex project. It's hard to explain.
There is one problem, the for loops that use MyQueue.Size() and pop() together are cut short as pop reduces size, make a variable to remember the original size before you start the loop...as such:
Code java:
// this class takes in productions and generates turtle commands.
import java.util.ArrayList;
import java.io.*;
import java.util.*;
public class program6_part_3
{ // beginning of class
static Scanner console;
public static void main(String[] args) throws IOException
{ // beginning of main
console = new Scanner(new File("generator-koch.txt"));
// declares variables and initializes them
int xMin = 0;
int xMax = 0;
int yMin = 0;
int yMax = 0;
float stepSize = 0;
float angleToTurn = 0;
int numberOfProductions = 0;
int numberOfIterations = 0;
char c = 'F';
String str = "";
String startingSequence = "";
// reads in xMin
xMin = console.nextInt();
// reads in xMax
xMax = console.nextInt();
// reads in yMin
yMin = console.nextInt();
// reads in yMax
yMax = console.nextInt();
// reads in step size
stepSize = console.nextFloat();
// reads in angle
angleToTurn = console.nextFloat();
// reads in number of productions
numberOfProductions = console.nextInt();
ArrayList<Character> leftSide= new ArrayList<Character>(numberOfProductions);
ArrayList<String> rightSide = new ArrayList<String>(numberOfProductions);
/*
Read each production as follows:
i. A single character that is the left side of the production.
ii. A string that is the right side of the production.
Read the starting sequence as a string of characters.
*/
for (int penguin = 0; penguin < numberOfProductions; penguin++)
{ // beginning of for
// should read in single character that is on left side of production.
c = console.next().charAt(0);
leftSide.add((Character) c);
// should read in string that is right side of production
str = console.nextLine().trim();
rightSide.add(str);
} // end of for
// should read in starting sequence as a whole String
startingSequence = console.nextLine();
// reads in number of iterations
numberOfIterations = console.nextInt();
// creates MyQueue object "from"
MyQueue<Character> from = new MyQueue<Character>();
// puts startingSequence into from, one character at a time.
for (int aaa = 0; aaa < startingSequence.length(); aaa++)
{ // beginning of for
from.push(startingSequence.charAt(aaa));
} // end of for
// creates MyQueue object "to"
MyQueue<Character> to = new MyQueue<Character>();
for (int iter =0; iter < numberOfIterations; iter++)
{ // beginning of for
int fromLength = from.Size();
for (int iter2 = 0; iter2 < fromLength; iter2++)
{ // beginning of for
// System.out.println(iter2+ " " + from.Size() + " " + from.front() + " " + leftSide.contains(from.front()));
if(leftSide.contains(from.front()))
{ // beginning of if
for (int vvv =0; vvv<leftSide.size(); vvv++)
{ // beginning of for
if (leftSide.get(vvv).equals(from.front()))
{ // beginning of if
/* ADDED THIS TO CONTINUE ADDING CHARS NOT A STRING*/
String tmpRightSide = rightSide.get(vvv);
for(int fdsa = 0;fdsa < tmpRightSide.length();fdsa++) {
to.push(tmpRightSide.charAt(fdsa));
}
} // end of if
} // end of for
} // end of if
// if it is not the left side of a production
else
{ // beginning of else
to.push(from.front());
} // end of else
from.pop();
} // end of for
int toLength = to.Size();
for(int asdf = 0;asdf<toLength;asdf++) {
from.push(to.front());
to.pop();
}
} // end of for
// prints out four integers
System.out.println(xMin + " " + xMax + " " + yMin + " " + yMax + " ");
int fromLength = from.Size();
for(int iter = 0;iter<fromLength;iter++) {
switch(from.front()) {
case 'F': System.out.println("trace " + stepSize); break;
case '+': System.out.println("turn " + angleToTurn); break;
case '-': System.out.println("turn " + -angleToTurn); break;
case '[': System.out.println("push"); break;
case ']': System.out.println("pop"); break;
default: System.out.println("Error!!"); break;
}
from.pop();
}
} // end of main
} // end of program
that generates a final sequence with 3072 F's which seems right as 3*4*4*4*4*4 = 3072 and each of the 5 iterations replaces 1 f with 4, and you start with 3...is this right? I am getting slightly confused but am still confident that is correct.
Re: Very complex project. It's hard to explain.
What is wrong with:
Code java:
// this class takes in productions and generates turtle commands.
import java.util.ArrayList;
import java.io.*;
import java.util.*;
public class program6_part_3
{ // beginning of class
static Scanner console = new Scanner(System.in);
public static void main(String[] args)
{ // beginning of main
// declares variables and initializes them
int xMin = 0;
int xMax = 0;
int yMin = 0;
int yMax = 0;
float stepSize = 0;
float angleToTurn = 0;
int numberOfProductions = 0;
int numberOfIterations = 0;
char c = 'F';
String str = "";
String startingSequence = "";
// reads in xMin
xMin = console.nextInt();
// reads in xMax
xMax = console.nextInt();
// reads in yMin
yMin = console.nextInt();
// reads in yMax
yMax = console.nextInt();
// reads in step size
stepSize = console.nextFloat();
// reads in angle
angleToTurn = console.nextFloat();
// reads in number of productions
numberOfProductions = console.nextInt();
ArrayList<Character> leftSide= new ArrayList<Character>(numberOfProductions);
ArrayList<String> rightSide = new ArrayList<String>(numberOfProductions);
/*
Read each production as follows:
i. A single character that is the left side of the production.
ii. A string that is the right side of the production.
Read the starting sequence as a string of characters.
*/
for (int penguin = 0; penguin < numberOfProductions; penguin++)
{ // beginning of for
// should read in single character that is on left side of production.
c = console.next().charAt(0);
leftSide.add((Character) c);
// should read in string that is right side of production
str = console.nextLine();
rightSide.add(str);
} // end of for
// should read in starting sequence as a whole String
startingSequence = console.nextLine();
// reads in number of iterations
numberOfIterations = console.nextInt();
// creates MyQueue object "from"
MyQueue<Character> from = new MyQueue<Character>();
// puts startingSequence into from, one character at a time.
for (int aaa = 0; aaa < startingSequence.length(); aaa++)
{ // beginning of for
from.push(startingSequence.charAt(aaa));
} // end of for
// creates MyQueue object "to"
MyQueue<Character> to = new MyQueue<Character>();
// for the number of iterations, it will remove from the "from" MyQueue and put it in the "to" MyQueue, one character at a time
/* for (int iter =0; iter < numberOfIterations; iter++)
{ // beginning of for
for (int iter2 = 0; iter2 < from.Size(); iter2++)
{ // beginning of for
if(leftSide.contains(from.front()))
{ // beginning of if
for (int vvv =0; vvv<leftSide.size(); vvv++)
{ // beginning of for
if (leftSide.get(vvv).equals(from.front()))
{ // beginning of if
to.push(rightSide.get(vvv));
from.pop();
} // end of if
} // end of for
} // end of if
// if it is the right side of the production
else
{ // beginning of else
to.push(from.front());
from.pop();
} // end of else
*/
for (int iter =0; iter < numberOfIterations; iter++)
{ // beginning of for
for (int iter2 = 0; iter2 < from.Size(); iter2++)
{ // beginning of for
if(leftSide.contains(from.front()))
{ // beginning of if
for (int vvv =0; vvv<leftSide.size(); vvv++)
{ // beginning of for
if (leftSide.get(vvv).equals(from.front()))
{ // beginning of if
for (int help =0; help < rightSide.size(); help++)
{to.push(rightSide.get(vvv).charAt(help));
}
from.pop();
} // end of if
} // end of for
} // end of if
// if it is the right side of the production
else
{ // beginning of else
to.push(from.front());
from.pop();
} // end of else
for(int asdf = 0;asdf<to.Size();asdf++) {
from.push(to.front());
to.pop();
}
} // end of for
} // end of for
// prints out four integers
System.out.println(xMin + " " + xMax + " " + yMin + " " + yMax + " ");
if (to.empty())
{
for(int iter = 0;iter<from.Size();iter++) {
switch(from.front()) {
case 'F': System.out.println("trace " + stepSize);
break;
case '+': System.out.println("turn " + angleToTurn);
break;
case '-': System.out.println("turn " + -angleToTurn);
break;
case '[': System.out.println("push");
break;
case ']': System.out.println("pop");
break;
default: System.out.println("Error!!");
break;
}
}
}
else
{
for(int iter = 0;iter<to.Size();iter++) {
switch(to.front()) {
case 'F': System.out.println("trace " + stepSize);
break;
case '+': System.out.println("turn " + angleToTurn);
break;
case '-': System.out.println("turn " + -angleToTurn);
break;
case '[': System.out.println("push");
break;
case ']': System.out.println("pop") ;
break;
default: System.out.println("Error!!");
break;
}
} // end of main
}
}
}
This should cover in case the final sequence is to instead of from.
Re: Very complex project. It's hard to explain.
Quote:
for(int iter = 0;iter<from.Size();iter++) {
switch(from.front()) {
case 'F': System.out.println("trace " + stepSize);
break;
case '+': System.out.println("turn " + angleToTurn);
break;
case '-': System.out.println("turn " + -angleToTurn);
break;
case '[': System.out.println("push");
break;
case ']': System.out.println("pop");
break;
default: System.out.println("Error!!");
break;
}
}
will print the front item in the queue repeatedly without changing it.
Quote:
for(int asdf = 0;asdf<to.Size();asdf++) {
from.push(to.front());
to.pop();
}
as the last part of the for loop means it WILL be in the from but the if empty works fine just pointless
Re: Very complex project. It's hard to explain.
Quote:
Originally Posted by
Zula
will print the front item in the queue repeatedly without changing it.
as the last part of the for loop means it WILL be in the from but the if empty works fine just pointless
Is that with mine or with yours or with both?
:confused:
Re: Very complex project. It's hard to explain.
With yours, if you don't pop the queue, and keep using front() it'll be the same thing repeatedly. Also if you use Size() in the for loop and pop, you will be counting up and lowering the count so it will be wrong... look at my example vs. yours in the for loops
Re: Very complex project. It's hard to explain.
He said that for even number of times, or something like that, it will end up in the to Queue at the end.
I knew that my code was closer, but you said it wasn't replacing like it should.
I had like 8 .pro files, which I had to make them into .txt files so they could open for you guys.
I could only post some of them.
My loop would have worked, though you said it would have not replaced stuff like it should have.
The case of it being in the to Queue is a valid possibility. It does alter so that I'm reading originally putting it into to from the from queue. Then on the second iteration, I'm putting it into the from queue from the to queue. Then the third iteration I'm putting it into the to queue from the from queue, while changing stuff like before.
So if it comes across
the left side
F F+F-F
It adds F+F-F, one char at a time.
If it comes across a +, -, or one of those bracket things, it just adds that symbol.
So for iteration 1, if it comes across
F F+F-F
it adds F+F-F for every F it finds, I believe, could be wrong.
Here's what it says exactly:
Many of the above files for turtle commands have been generated algorithmically. This is done by using “productions”. A production is like a Math equation, with a left side and a right side. An example of a production is:
F = F+F-F
where ‘F’, ‘+’, ‘-‘ are simply symbols (you are not performing any addition or subtraction). No two productions have the same symbol on their left side. A starting sequence of symbols is provided to you, say “++F--”. One can expand this sequence by substituting for every “F” the right side of the above production. Thus after one iteration, the sequence “++F--” becomes “++F+F-F--“. In the second iteration, one can again expand the result of the previous iteration, thus making the resulting sequence longer and longer with every iteration. When a set number of iterations is reached, the resulting sequence is converted into turtle commands. Keep in mind that there may be many productions, each with a different symbol on its left side. The starting sequence may also contain many symbols. Symbols in the sequence that are not on the left side of any production are left untouched (for example, the symbols ‘+’ and ‘-‘ above).
Thanks for the help so far. I really appreciate it. Also, I should add that I had to turn it in, though it worked pretty well.