Site Navigation

Solution 5: Java and TileLand

  • Part A Repetition
    • Make sure you are able to upload the Tileland programs with the output the following program with repetitions 0, 1,2, 4, and 8.
      public class PLoop{
        public static void main(String[] args){
          int repetitions=4;
          for (int i=0;i<6;i++){
            System.out.print("triangle blue ");
            spacer(repetitions,"orange ");
          }
        }
        public static void spacer(int rep, String colour){
          for (int i=0;i< rep;i++){
            System.out.print("square "+colour);
          }
        }
      }
      
    • When repetitions is set to 8, Tileland doesn't make the loop because it's too big to fit
  • Part B Spirals
    1. notice the use of spacer. (although the other strategies work I would like you to be familiar with this one)
      public class Spiral{
        public static void main(String[] args){
          for (int i=0;i<14;i++){
            spacer(i+1,"red ");
            System.out.print("turn ");
          }
        }
        public static void spacer(int rep, String colour){
          for (int i=0;i< rep;i++){
            System.out.print("square "+colour);
          }
        }
      }
      
    2. a simple add of the print before the loop
        public static void main(String[] args){
          System.out.print("square red ");
          for (int i=0;i<14;i++){
            spacer(i+1,"red ");
            System.out.print("turn ");
          }
        }
      
    3. introduce an if
        public static void main(String[] args){
          System.out.print("square pink ");
          for (int i=0;i<14;i++){
            if (i%2==0)
              spacer(i+1,"red ");
            else
              spacer(i+1,"pink ");
            System.out.print("turn ");
          }
        }
      
    4. introduce another condition
        public static void main(String[] args){
          System.out.print("square pink ");
          for (int i=0;i<14;i++){
            if (i%2==0)
              spacer(i+1,"red ");
            if (i%2==1)
              spacer(i+1,"purple ");
            if (i%2==2)
              spacer(i+1,"pink ");
            System.out.print("turn ");
          }
        }
      
  • Why does the center square of the last three patterns mess up the number pattern of the differently coloured squares? In the program, how can the first square be created?
      The pattern of the number of squares goes 1, 1, 2, 3, 4, etc. So the first one doesn't fit the just add one square. As seen in the code, just simply deal with it first.
  • In theory, all of the spiral programs from PolygonR&D can be rewritten into Java programs. Explain why PolygonR&D's expand type (see Code5.java) programs would be not as easy to translate.
      The spiral programs are a single process walking through the patterns. With careful study of the program, Java could be used to build the same spiral. The expand type programs use multiple processes. This makes direct translation into Java programs complicated. Since we use Java to write Tileland code and Tileland has only one process, this approach will not work for expand type programs.