Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 2 of 3 FirstFirst 123 LastLast
Results 26 to 50 of 56

Thread: Very complex project. It's hard to explain.

  1. #26
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Very complex project. It's hard to explain.

    Any idea what he means by swap "from" and "to"

    Here's what he said:

    4. For each iteration:
    a. For each character that you remove from the “from” queue:
    i. If it is the left side of any production, add the right side of that production to the “to” queue. Otherwise, add the character you read from the “from” queue to the “to” queue.
    b. Swap “from” and “to”.

    I already think I took care of part a, but what does he want me to do for part b?

    I don't know how to swap "from" and "to".

  2. #27
    Member
    Join Date
    Oct 2010
    Location
    Denver, CO
    Posts
    55
    Thanks
    1
    Thanked 30 Times in 29 Posts

    Default Re: Very complex project. It's hard to explain.

    With scanner.next() it gives you what is inbetween spaces, so if you use

    for (int penguin = 0; penguin < numberOfProductions; penguin++)
    {
    // should read in single character that is on left side of production.  
    c = console.next().getCharAt(0);
     
    // should read in string that is right side of production
    str = console.next();
    }

    then aslong as there are no spaces in the right hand side, which I didn't see any yet, str will contain full lefthand side.

  3. The Following User Says Thank You to Zula For This Useful Post:

    javapenguin (November 3rd, 2010)

  4. #28
    Member
    Join Date
    Oct 2010
    Location
    Denver, CO
    Posts
    55
    Thanks
    1
    Thanked 30 Times in 29 Posts

    Default Re: Very complex project. It's hard to explain.

    He means make a queue of all the chars in the starting sequence, then for each char, check if it is a lhs(left hand side) symbol, if so, take the corresponding rhs and substitute the symbol

  5. The Following User Says Thank You to Zula For This Useful Post:

    javapenguin (November 3rd, 2010)

  6. #29
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Question Re: Very complex project. It's hard to explain.

    Quote Originally Posted by Zula View Post
    With scanner.next() it gives you what is inbetween spaces, so if you use

    for (int penguin = 0; penguin < numberOfProductions; penguin++)
    {
    // should read in single character that is on left side of production.  
    c = console.next().getCharAt(0);
     
    // should read in string that is right side of production
    str = console.next();
    }

    then aslong as there are no spaces in the right hand side, which I didn't see any yet, str will contain full lefthand side.
    I want str to contain right hand side.

    c contains left hand side

    Did I do something wrong?

  7. #30
    Member
    Join Date
    Oct 2010
    Location
    Denver, CO
    Posts
    55
    Thanks
    1
    Thanked 30 Times in 29 Posts

    Default Re: Very complex project. It's hard to explain.

    	static Scanner in;
        public static void main(String[] args) throws IOException {
        	in = new Scanner(new File("generator-koch.txt"));
        	for(int i = 0;i<6;i++) {
        		System.out.println(in.next());
        	}
        	int numberOfProductions = (int)(Integer.parseInt(in.next()));
        	char c = ' ';
        	String str = "";
        	System.out.println("Read Production");
        	for (int penguin = 0; penguin < numberOfProductions; penguin++)
    		{
    			// should read in single character that is on left side of production.
    			c = in.next().charAt(0);
     
    			// should read in string that is right side of production
    			str = in.next();
    		}
    		System.out.println("LHS: " + c + " RHS: " + str);
        	while(in.hasNext()) {
        		System.out.println(in.next());
        	}
        }

    the output is:
    --------------------Configuration: Turtle - JDK version 1.6.0_22 <Default> - <Default>--------------------
    -200
    1400
    -400
    1200
    5
    60
    Read Production
    LHS: F RHS: F-F++F-F
    F++F++F
    5
     
    Process completed.

    So if you can utilize the first data properly, that gives you lhs and rhs

    your right, right hand side is str, I am a dumba** sometimes, heh

  8. The Following User Says Thank You to Zula For This Useful Post:

    javapenguin (November 3rd, 2010)

  9. #31
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Question Re: Very complex project. It's hard to explain.

    Quote Originally Posted by Zula View Post
    He means make a queue of all the chars in the starting sequence, then for each char, check if it is a lhs(left hand side) symbol, if so, take the corresponding rhs and substitute the symbol
    Does my code do that?

    If not, how do I do that?


  10. #32
    Member
    Join Date
    Oct 2010
    Location
    Denver, CO
    Posts
    55
    Thanks
    1
    Thanked 30 Times in 29 Posts

    Default Re: Very complex project. It's hard to explain.

    But what you need to do is in the for loop, once you read in a production, stick it in two queues, one for the lhs(from) and one for the rhs(to)

  11. The Following User Says Thank You to Zula For This Useful Post:

    javapenguin (November 3rd, 2010)

  12. #33
    Member
    Join Date
    Oct 2010
    Location
    Denver, CO
    Posts
    55
    Thanks
    1
    Thanked 30 Times in 29 Posts

    Default Re: Very complex project. It's hard to explain.

    It appears it would yes.

  13. The Following User Says Thank You to Zula For This Useful Post:

    javapenguin (November 3rd, 2010)

  14. #34
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Very complex project. It's hard to explain.

    Ok, but I'm only reading in from "from" and putting stuff in "to": How does he want me to swap it?

    I'm wondering if he means to then do the same with to and read it into from, then do the same with from, then read it into to, then do the same with to and read it into from, etc.

    How do you do that?

  15. #35
    Member
    Join Date
    Oct 2010
    Location
    Denver, CO
    Posts
    55
    Thanks
    1
    Thanked 30 Times in 29 Posts

    Default Re: Very complex project. It's hard to explain.

    For the generator-koch.txt file, I get
    F-F++F-F++F-F++F-F++F-F++F-FF-F++F-F++F-F++F-F++F-F++F-FF-F++F-F++F-F++F-F++F-F++F-FF-F++F-F++F-F++F-F++F-F++F-FF-F++F-F++F-F++F-F++F-F++F-F
    as the product of the 5 iterations of the production and starting sequence

  16. The Following User Says Thank You to Zula For This Useful Post:

    javapenguin (November 3rd, 2010)

  17. #36
    Member
    Join Date
    Oct 2010
    Location
    Denver, CO
    Posts
    55
    Thanks
    1
    Thanked 30 Times in 29 Posts

    Default Re: Very complex project. It's hard to explain.

    Make a for loop that goes to.length iterations and pop them from the 'to' queue into the 'from' queue

    for(int asdf = 0;asdf<to.length();asdf++) {
        from.push(to.front());
        to.pop();
    }

  18. The Following User Says Thank You to Zula For This Useful Post:

    javapenguin (November 3rd, 2010)

  19. #37
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Very complex project. It's hard to explain.

    For the code:
    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
    } // end of for
    } // end of for

    How do I get it to do what I just did above, but this time replacing "from" and "to"?

    And for the third, doing what I just did above, replacing the "from" and "to" from iteration 2, so it'll be reading in from "from" and moving to "to" again? And then for the fourth, how do I get it to do the same as the third iteration, but replacing the "from" and "to", and so on till the iterations are done.

    Then for the last step, how can I find a way for it to tell which queue has the final sequence in it so that I can do this part and finally test the bloody program out in UNIX?

    For whichever queue has the final sequence, interpret the characters and print out the corresponding results using System.out.println:
    a. “F”: This corresponds to the “trace” command. Print “trace” followed by the step size from 2(b) above.
    b. “+”: This is a counter-clockwise “turn”. Print “turn” followed by the angle from 2(c) above.
    c. “-“: This is a clock-wise “turn”. Print “turn” followed by the negative of the angle from 2(c) above.
    d. “[“: This is a “push”. Print “push”.
    e. “]”: This is a “pop”. Print “pop”.

  20. #38
    Member
    Join Date
    Oct 2010
    Location
    Denver, CO
    Posts
    55
    Thanks
    1
    Thanked 30 Times in 29 Posts

    Arrow Re: Very complex project. It's hard to explain.

    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
        } // end of for
     
        for(int asdf = 0;asdf<to.length();asdf++) {
            from.push(to.front());
            to.pop();
        }
    } // end of for

    so that after each iteration, it swaps the queues, then does the same thing again try that


    For whichever queue has the final sequence, interpret the characters and print out the corresponding results using System.out.println:
    was that how he worded it? Because doing that I said will always leave the final sequence in the "from" queue
    Last edited by Zula; November 3rd, 2010 at 07:09 PM.

  21. The Following User Says Thank You to Zula For This Useful Post:

    javapenguin (November 3rd, 2010)

  22. #39
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Very complex project. It's hard to explain.

    Here's what he said for part 3:

    Part 3: Generating Turtle Commands
    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).
    Some of the above pictures have been generated this way. In part 3, you have to write a program that reads productions and symbols from the keyboard, iterates over a given sequence, and convert the resulting sequence into turtle commands.
    What to do:
    1. Write a class MyQueue that behaves like a queue. You must use your own DoublyLinkedList class to write this queue. You are not allowed to use ArrayList or java’s LinkedList class for this purpose!
    2. Write a program that reads input from the keyboard in the following order (follow along using one of the .pro files provided for this part):
    a. Read in four integers xmin, xmax, ymin, ymax.
    b. Read a single float number which is the “step size” of the trace command.
    c. Read a single float number which is the angle you turn by using the turn command.
    d. Read the number of productions (an integer).
    e. 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.
    f. Read the starting sequence as a string of characters.
    g. Read the number of iterations, which is an integer.
    3. Create two queue objects, and put the starting sequence in the first one, one character at a time. This is the “from” queue, and the other is the “to” queue.
    4. For each iteration:
    a. For each character that you remove from the “from” queue:
    i. If it is the left side of any production, add the right side of that production to the “to” queue. Otherwise, add the character you read from the “from” queue to the “to” queue.
    b. Swap “from” and “to”.
    5. Print the four integers xmin xmax ymin ymax.
    6. For whichever queue has the final sequence, interpret the characters and print out the corresponding results using System.out.println:
    a. “F”: This corresponds to the “trace” command. Print “trace” followed by the step size from 2(b) above.
    b. “+”: This is a counter-clockwise “turn”. Print “turn” followed by the angle from 2(c) above.
    c. “-“: This is a clock-wise “turn”. Print “turn” followed by the negative of the angle from 2(c) above.
    d. “[“: This is a “push”. Print “push”.
    e. “]”: This is a “pop”. Print “pop”.

  23. #40
    Member
    Join Date
    Oct 2010
    Location
    Denver, CO
    Posts
    55
    Thanks
    1
    Thanked 30 Times in 29 Posts

    Default Re: Very complex project. It's hard to explain.

    No offense but your teacher's outline is a bit weird, steps 1&2 are on a different scope than 3,4,5&6, 4,5&6 should have been under 3 and it should have been worded different....but anway, okay, the final step is just to print out the "from" queue in the manner he described. Probably another for loop and a switch(from.front()) with the cases as the chars and the from.pop() after the switches
    Last edited by Zula; November 3rd, 2010 at 07:08 PM.

  24. The Following User Says Thank You to Zula For This Useful Post:

    javapenguin (November 3rd, 2010)

  25. #41
    Member
    Join Date
    Oct 2010
    Location
    Denver, CO
    Posts
    55
    Thanks
    1
    Thanked 30 Times in 29 Posts

    Default Re: Very complex project. It's hard to explain.

    I misspoke, it ends in the "from" queue, so print from the "from" queue

  26. The Following User Says Thank You to Zula For This Useful Post:

    javapenguin (November 3rd, 2010)

  27. #42
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Very complex project. It's hard to explain.

    Not to seem like an idiot, but I have never done a switch structure before.

    How would you fix this code to get one and then work for the last step?

    // 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().getCharAt(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.getCharAt(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
    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
        } // end of for
     
        for(int asdf = 0;asdf<to.length();asdf++) {
            from.push(to.front());
            to.pop();
        }
    } // end of for
    } // end of for
    } // end of for
     
    // prints out four integers
    System.out.println(xMin + " " + xMax + " " + yMin + " " + yMax + " ");
    } // end of main
     
    } // end of program

  28. #43
    Member
    Join Date
    Oct 2010
    Location
    Denver, CO
    Posts
    55
    Thanks
    1
    Thanked 30 Times in 29 Posts

    Default Re: Very complex project. It's hard to explain.

    // 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 (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
    			/* 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));
    							}
    							from.pop();
    	                  	} // 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());
    	            	from.pop();
    	        	} // end of else
    	    	} // end of for
    			for(int asdf = 0;asdf<to.size();asdf++) {
    	        	from.push(to.front());
    	        	to.pop();
    	    	}
    		} // end of for
     
    		// prints out four integers
    		System.out.println(xMin + " " + xMax + " " + yMin + " " + yMax + " ");
     
    		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;
    			}
    			from.pop();
    		}
    	} // end of main
     
    } // end of program

    Alright..give that a try, I changed the way righthand string was added to the "to" queue as chars are needed, not one str in there.

  29. The Following User Says Thank You to Zula For This Useful Post:

    javapenguin (November 3rd, 2010)

  30. #44
    Member
    Join Date
    Oct 2010
    Location
    Denver, CO
    Posts
    55
    Thanks
    1
    Thanked 30 Times in 29 Posts

    Default Re: Very complex project. It's hard to explain.

    Problem though, the final sequence from the generator-koch.txt file has spaces for some reason...so an error in the code somewheres

    make it str = console.nextLine().trim(); in the penguin loop

    But I believe it is still not making the correct final sequence...it should be :
    F-F++F-F++F-F++F-F++F-F++F-FF-F++F-F++F-F++F-F++F-F++F-FF-F++F-F++F-F++F-F++F-F++F-FF-F++F-F++F-F++F-F++F-F++F-FF-F++F-F++F-F++F-F++F-F++F-F
    but is generating
    -F++F-F++F-F++F-F-F-F++F-F++F-F++F-F-F-F++F-F++F-F++F-F-F-F++F-FF-F++F-F-F--

    which is surely wrong since the start sequence for generator-koch has an f first, as does the right hand side, so it should end with an f first positively
    Last edited by Zula; November 3rd, 2010 at 08:07 PM.

  31. The Following User Says Thank You to Zula For This Useful Post:

    javapenguin (November 3rd, 2010)

  32. #45
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Very complex project. It's hard to explain.

    Why are you popping from "from"?

    // 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().getCharAt(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.getCharAt(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
    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
        } // end of for
     
        for(int asdf = 0;asdf<to.length();asdf++) {
            from.push(to.front());
            to.pop();
        }
    } // end of for
    } // end of for
    } // end of for
     
    // prints out four integers
    System.out.println(xMin + " " + xMax + " " + yMin + " " + yMax + " ");
    } // end of main
     
    } // end of program
    Last edited by javapenguin; November 3rd, 2010 at 08:41 PM.

  33. #46
    Member
    Join Date
    Oct 2010
    Location
    Denver, CO
    Posts
    55
    Thanks
    1
    Thanked 30 Times in 29 Posts

    Default Re: Very complex project. It's hard to explain.

    Hmm...well, you need to empty from as you go through it or it will build up, but I see that doing so changes Size() and makes me run out faster than I should.

  34. The Following User Says Thank You to Zula For This Useful Post:

    javapenguin (November 3rd, 2010)

  35. #47
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Very complex project. It's hard to explain.

    For iteration 1:

    You pop a character from one queue, and either put it directly into the other queue, else you put the right side of the corresponding production into the other queue.

    Now at the end of the first iteration, the "from" queue is empty and the "to" queue is full.

    For iteration 2:

    You now pop a character from the full queue and do the same thing. Thus what was the "to" queue in iteration 1 becomes the "from" queue here.

    You don't necessarily need to duplicate entire queues. Just change the meaning of what "from" and "to" mean.

    Here is an example:

    The production is F=F+F

    The starting sequence is F-F

    Iteration 1:

    From queue: F-F
    To queue: empty

    After iteration 1:
    From queue: empty
    To queue: F+F-F+F

    Iteration 2:
    From queue: F+F-F+F
    To queue: empty

    After iteration 2:
    From queue: empty
    To queue: F+F+F+F-F+F+F+F

    I hope that clarifies things.
    Just got a response back.

    Am I doing it right?

  36. #48
    Member
    Join Date
    Oct 2010
    Location
    Denver, CO
    Posts
    55
    Thanks
    1
    Thanked 30 Times in 29 Posts

    Default Re: Very complex project. It's hard to explain.

    There appears to be a problem with your MyQueue or DoublyLinkedList classes...the to queue is not growing properly. my mistake not true..same poping reducing size problem
    Last edited by Zula; November 3rd, 2010 at 08:54 PM.

  37. #49
    Member
    Join Date
    Oct 2010
    Location
    Denver, CO
    Posts
    55
    Thanks
    1
    Thanked 30 Times in 29 Posts

    Default Re: Very complex project. It's hard to explain.

    Iteration 1:

    From queue: F-F
    To queue: empty

    After iteration 1:
    From queue: empty
    To queue: F+F-F+F

    Iteration 2:
    From queue: F+F-F+F
    To queue: empty

    After iteration 2:
    From queue: empty
    To queue: F+F+F+F-F+F+F+F
    In that example what happens between "After Iteration 1" when the from queue is empty and "Iteration 2" where it has data...you must transfer it...

  38. The Following User Says Thank You to Zula For This Useful Post:

    javapenguin (November 3rd, 2010)

  39. #50
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Arrow Re: Very complex project. It's hard to explain.

    Quote Originally Posted by Zula View Post
    In that example what happens between "After Iteration 1" when the from queue is empty and "Iteration 2" where it has data...you must transfer it...
     
    // 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
    }
    }
    }

    What will this code do?

    Last edited by javapenguin; November 3rd, 2010 at 09:10 PM.

Page 2 of 3 FirstFirst 123 LastLast

Similar Threads

  1. How do you design a complex program
    By mydarkpassenger in forum Java Theory & Questions
    Replies: 5
    Last Post: March 19th, 2010, 06:52 PM
  2. way too hard to explain this in short. please help guys!!
    By humdinger in forum Collections and Generics
    Replies: 4
    Last Post: March 15th, 2010, 05:57 AM
  3. Can anyone explain this code for me
    By gudwindavids in forum Object Oriented Programming
    Replies: 1
    Last Post: December 11th, 2009, 01:29 PM
  4. Help me this code! Someone please explain strings!
    By helpthiscode in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 16th, 2009, 03:13 AM
  5. can anyone explain this?
    By chronoz13 in forum Java Theory & Questions
    Replies: 4
    Last Post: October 12th, 2009, 02:51 AM