Tutorial 4: Loops

This tutorial is on loops. It has two problems.

1. Use the division method to find the GCD of two positive integers.

class GCD  {
  static int gcd2(int a, int b)  {
    // gcd of a, b by division method.
    // Does not handle degenerate cases
    // when a or b are 0 or -ve.
    if (a!=b) { 
    //make a contain max of a,b
    //Dont use max, min from Math package
    //two function calls plus the same
    //comparison twice
      if (b>a) {
        int c=a;
        a=b;
        b=c;
      }
      //this is the main loop
      while (b!=0) {
        int tmp=a%b;
        a=b;
        b=tmp;
      }
    }
    return(a);
  }
}

2. Write a class that prints out the following patterns parametrized by n.
(You must use a fixed size font like Courier to see it properly aligned.
Times Roman or Arial will not do.) The pattern is for n=5. Uses for loops.
    a 
   a a 
  a a a 
 a a a a 
a a a a a 
 a a a a 
  a a a 
   a a 
    a 


    1
   121
  12321
 1234321
123454321
 1234321
  12321
   121
    1

abcdedcba
abcd dcba
abc   cba
ab     ba
a       a
ab     ba
abc   cba
abcd dcba
abcdedcba

The code below has all three patterns.

class RecurringPatterns  {
  
  //Note that the pattern will appear
  //properly aligned only if a fixed width
  //font is used (e.g. Courier). With the normal
  //Times Roman (variable width font) the patterns
  //will not be properly aligned.
  
  //A pattern is expressed at a higher
  //level in terms of  functions which describe
  // the pattern. This makes the pattern code
  //easier to understand.
  
  static void firstPattern(int n)  {
    for (int i=1;i<=n;i++) {
      writeBlank(n-i);
      writePattern("a ",i);
      System.out.println();
    }
    for (int i=n-1;i>0;i--) {
      writeBlank(n-i);
      writePattern("a ",i);
      System.out.println();
    }
  }
  static void secondPattern(int n)  {
    for (int i=1;i<=n;i++) {
      writeBlank(n-i);
      writeConsecutive(1,i);
      writeReverseConsecutive(i-1,1);
      System.out.println();
    }
    for (int i=n-1;i>0;i--) {
      writeBlank(n-i);
      writeConsecutive(1,i);
      writeReverseConsecutive(i-1,1);
      System.out.println();
    }
  }
  static void thirdPattern(int n)  {
    //the first line
    writeConsecutiveChars(1,n-1);
    writeChar(n);
    writeReverseConsecutiveChars(n-1,1);
    System.out.println();
    //the recurring pattern  
    for (int i=n-1;i>=1;i--) {
      writeConsecutiveChars(1,i);
      writeBlank(2*(n-i)-1);
      writeReverseConsecutiveChars(i,1);
      System.out.println();
    }
    for (int i=2;i<=n-1;i++) {
      writeConsecutiveChars(1,i);
      writeBlank(2*(n-i)-1);
      writeReverseConsecutiveChars(i,1);
      System.out.println();
    }
      
    //the last line
    writeConsecutiveChars(1,n-1);
    writeChar(n);
    writeReverseConsecutiveChars(n-1,1);
    System.out.println();
  }
  static void writeBlank(int n)  {
    for (int i=1;i<=n;i++) System.out.print(" ");
  }
  static void writePattern(String pat,int n)  {
    for (int i=1;i<=n;i++) System.out.print(pat);
  }
  static void writeConsecutive(int from,int to) {
    for (int i=from;i<=to;i++) System.out.print(i);
  }
  static void writeReverseConsecutive(int from,int to) {
    for (int i=from;i>=to;i--) System.out.print(i);
  }
  //Characters are encoded as ints from 0 to 127
  //the ASCII encoding. So it type casts the int
  //to char.
  static void writeConsecutiveChars(int from,int to)  {
    for (int i=from;i<=to;i++) 
      System.out.print((char)(i+96));
  }
  static void writeChar(int i)  {
    System.out.print((char)(i+96));
  }
  static void writeReverseConsecutiveChars(int from,int to)  {
    for (int i=from;i>=to;i--) 
      System.out.print((char)(i+96));
  }
}