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 3 of 3 FirstFirst 123
Results 51 to 56 of 56

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

  1. #51
    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 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:
    // 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.

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

    javapenguin (November 3rd, 2010)

  3. #52
    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.

    What is wrong with:
    // 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.

  4. #53
    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(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.

    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
    Last edited by Zula; November 3rd, 2010 at 09:54 PM.

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

    javapenguin (November 3rd, 2010)

  6. #54
    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.

    Quote Originally Posted by Zula View Post
    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?


  7. #55
    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 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

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

    javapenguin (November 4th, 2010)

  9. #56
    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.

    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.

Page 3 of 3 FirstFirst 123

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, 02: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