– String Solutions

4.1 Count Words

Write a method that returns the number of words in a String

4.1.1 Solution

 1:  public class NNStrings {
 2:
 3:      NNStrings()
 4:      {
 5:          String sentence = "I never saw a purple cow";
 6:          System.out.println(countWords(sentence));
 7:      }
 8:
 9:      int countWords(String s)
10:      {
11:         String [] words = s.split("\\s+");
12:         return words.length;
13:      }
14:
15:      public static void main(String[] args) {
16:          // TODO code application logic here
17:          new NNStrings();
18:      }
19:  }

4.2 Count the letter ‘e’

Write a method that counts the instances of the letter ‘e’ in a string.

4.2.1 Solution

 1:  public class NNStrings {
 2:
 3:      NNStrings()
 4:      {
 5:          String sentence = "I never saw a purple cow";
 6:          System.out.println(countEs(sentence));
 7:      }
 8:
 9:      int countEs(String s)
10:      {
11:          int count = 0;
12:          for(int i=0; i<s.length(); i++)
13:          {
14:              if(s.substring(i,i+1).matches("e"))
15:                  count++;
16:          }
17:          return count;
18:      }
19:
20:      public static void main(String[] args) {
21:          // TODO code application logic here
22:          new NNStrings();
23:      }
24:  }

4.3 Count Alphanumerics

Write a method that returns the number of alphanumeric characters (A-Z, a-z and 0-9) in a string.

4.3.1 Solution

 1:  public class NNStrings {
 2:
 3:      NNStrings()
 4:      {
 5:          String sentence = "I never saw a purple cow";
 6:          String title = "1984 by George Orwell.";
 7:          System.out.println(countChars(sentence));
 8:          System.out.println(countChars(title));
 9:      }
10:
11:      int countChars(String s)
12:      {
13:          int count = 0;
14:          for(int i=0; i<s.length(); i++)
15:          {
16:              if(s.substring(i,i+1).matches("[A-Za-z0-9]"))
17:                  count++;
18:          }
19:          return count;
20:      }
21:
22:      public static void main(String[] args) {
23:          // TODO code application logic here
24:          new NNStrings();
25:      }
26:  }

4.4 Reverse String

Write a method that returns a string, reversed

4.4.1 Solution

 1:  public class NNStrings {
 2:
 3:      NNStrings()
 4:      {
 5:          String sentence = "I never saw a purple cow";
 6:          System.out.println(reverse(sentence));
 7:          System.out.println(reverseH(sentence));
 8:          System.out.println(reverseSB(sentence));
 9:      }
10:
11:      // A Haskell-y solution
12:      String reverse(String s)
13:      {
14:          String reverse= "";
15:          for (int i = 0; i<s.length(); i++)
16:          {
17:              reverse = s.substring(i,i+1) + reverse;
18:          }
19:          return reverse;
20:      }
21:
22:      // A recursive Haskell-y solution
23:      String reverseH(String s)
24:      {
25:          if (s.length() == 0) return "";
26:          else if (s.length() == 1) return s;
27:          else return  reverseH(s.substring(1)) + s.substring(0,1);
28:      }
29:
30:      //A StringBuffer solution
31:      String reverseSB(String s)
32:      {
33:          String reverse = new StringBuffer(s).reverse().toString();
34:          return reverse;
35:      }
36:      public static void main(String[] args) {
37:          // TODO code application logic here
38:          new NNStrings();
39:      }
40:  }

4.5 Palindromes

Write a method to test if a string is a Palindrome

4.5.1 Solution

 1:  public class NNStrings {
 2:
 3:      NNStrings()
 4:      {
 5:          String sentence = "I never saw a purple cow";
 6:          String palindrome = "rotavator";
 7:          System.out.println(isPalindrome(sentence));
 8:          System.out.println(isPalindrome(palindrome));
 9:      }
10:
11:      // A Haskell-y solution
12:      String reverse(String s)
13:      {
14:          String reverse= "";
15:          for (int i = 0; i<s.length(); i++)
16:          {
17:              reverse = s.substring(i,i+1) + reverse;
18:          }
19:          return reverse;
20:      }
21:      boolean isPalindrome(String s)
22:      {
23:          return s.equals(reverse(s)) ? true : false;
24:      }
25:
26:      public static void main(String[] args) {
27:          // TODO code application logic here
28:          new NNStrings();
29:      }
30:  }

4.6 More Palindromes

Write an improved palindrome method that will disregard spaces, punctuation and case and thus recognise sentences such as “A man, a plan, a canal, Panama!” as a palindrome.

4.6.1 Solution

 1:  public class NNStrings {
 2:
 3:      NNStrings()
 4:      {
 5:          System.out.println(isImpPalindrome(pal2));
 6:      }
 7:
 8:      // A Haskell-y solution
 9:      String reverse(String s)
10:      {
11:          String reverse= "";
12:          for (int i = 0; i<s.length(); i++)
13:          {
14:              reverse = s.substring(i,i+1) + reverse;
15:          }
16:          return reverse;
17:      }
18:
19:      boolean isImpPalindrome(String s)
20:      {
21:          String lower = s.toLowerCase();
22:          String stripped = lower.replaceAll("[^A-Za-z0-9]", "");
23:          return stripped.equals(reverse(stripped)) ? true : false;
24:      }
25:
26:      public static void main(String[] args) {
27:          // TODO code application logic here
28:          new NNStrings();
29:      }
30:  }

4.7 Remove v*w*ls

Write a method that will replace the vowels in a word with asterisks

4.7.1 Solution

 1:  public class NNStrings {
 2:
 3:      NNStrings()
 4:      {
 5:          String sentence = "I never saw a purple cow";
 6:          System.out.println(star(sentence));
 7:      }
 8:
 9:
10:      String star(String s)
11:      {
12:          return s.replaceAll("[AEIOUaeiou]", "*");
13:      }
14:
15:      public static void main(String[] args) {
16:          // TODO code application logic here
17:          new NNStrings();
18:      }
19:  }

4.8 Spell Out

Write a method to spell out words so that, for example, This becomes T-H-I-S.

4.8.1 Solution

 1:  public class NNStrings {
 2:
 3:      NNStrings()
 4:      {
 5:          String sentence = "I never saw a purple cow";
 6:          System.out.println(spellOut(sentence));
 7:      }
 8:
 9:      String spellOut(String s)
10:      {
11:          s = s.toUpperCase();
12:          String spell = "";
13:          for (char c: s.toCharArray())
14:          {
15:              spell = spell + c + "-";
16:          }
17:
18:          spell = spell.replaceAll("-\\s+-|-$"," ");
19:          return spell;
20:      }
21:
22:
23:      public static void main(String[] args) {
24:          // TODO code application logic here
25:          new NNStrings();
26:      }
27:  }

4.9 Substitution Cipher

In a simple substitution cipher, A =1 , B=2, C=3 etc. Write a method that encodes a sentence using this cipher

4.9.1 Solution

 1:  public class NNStrings {
 2:
 3:      NNStrings()
 4:      {
 5:          System.out.println(encode("Hello World"));
 6:      }
 7:      String encode(String s)
 8:      {
 9:          String encoding = "";
10:          s=s.toLowerCase();
11:          String [] words = s.split("\\s+");
12:          for (String word : words)
13:          {
14:              for(int i = 0; i<word.length() - 1 ; i++)
15:              {
16:                  encoding = encoding + ((int)word.charAt(i)-96) + ",";
17:              }
18:              encoding = encoding + ((int)word.charAt(word.length()-1)-96) + " ";
19:          }
20:
21:          return encoding;
22:      }
23:
24:      public static void main(String[] args) {
25:          // TODO code application logic here
26:          new NNStrings();
27:      }
28:  }

4.10 Decoder

Write a decoder method for your substitution Cipher

4.10.1 Solution

 1:  public class NNStrings {
 2:
 3:      NNStrings()
 4:      {
 5:          System.out.println(decode("9 14,5,22,5,18 19,1,23 1 16,21,18,16,12,5 3,15,23"));
 6:      }
 7:
 8:      String decode(String s)
 9:      {
10:          String decoding = "";
11:          String [] words = s.split("\\s+");
12:          for (String word : words)
13:          {
14:              String [] codes = word.split(",");
15:              for (String code: codes)
16:              {
17:                  int c = Integer.parseInt(code.trim());
18:                  decoding = decoding + (char)(c+96);
19:              }
20:              decoding = decoding + " ";
21:          }
22:          return decoding;
23:      }
24:
25:      public static void main(String[] args) {
26:          // TODO code application logic here
27:          new NNStrings();
28:      }
29:  }

4: Strings

4.1 Count Words

Write a method that returns the number of words in a String

4.1.1 Example

1:  String s = "I never saw a purple cow"
2:
3:  System.out.println(countWords(s));
4:   *** Output ***
5:  6

4.2 Count the letter ‘e’

Write a method that counts the instances of the letter ‘e’ in a string.

4.2.1 Example

1:  String s = "I never saw a purple cow"
2:
3:  System.out.println(countEs(s));
4:   *** Output ***
5:  3

4.3 Count Alphanumerics

Write a method that returns the number of alphanumeric characters (A-Z, a-z and 0-9) in a string.

4.3.1 Example

1:  String s = "1984 by George Orwell."
2:
3:  System.out.println(countChars(s));
4:   *** Output ***
5:  18

4.4 Reverse String

Write a method that returns a string, reversed

4.4.1 Example

1:  String s = "I never saw a purple cow"
2:
3:  System.out.println(reverse(s));
4:   *** Output ***
5:  woc elprup a was reven I

4.5 Palindromes

Write a method to test if a string is a Palindrome

4.5.1 Example

1:  String sentence = "I never saw a purple cow"
2:  String palindrome = "rotavator"
3:
4:  System.out.println(isPalindrome(sentence));
5:  System.out.println(isPalindrome(palindrome));
6:   *** Output ***
7:  False
8:  True

4.6 More Palindromes

Write an improved palindrome method that will disregard spaces, punctuation and case and thus recognise sentences such as “A man, a plan, a canal, Panama!” as a palindrome.

4.6.1 Example

1:  String s = "I never saw a purple cow"
2:  String p = "Rise to vote, Sir!"
3:
4:  System.out.println(isPalindrome(s));
5:  System.out.println(isPalindrome(p));
6:   *** Output ***
7:  False
8:  True

4.7 Remove v*w*ls

Write a method that will replace the vowels in a word with asterisks

4.7.1 Example

1:  String s = "I never saw a purple cow"
2:
3:  star(s)
4:   *** Output ***
5:   * n*v*r s*w * p*rpl* c*w

4.8 Spell Out

Write a method to spell out words so that, for example, This becomes T-H-I-S.

4.8.1 Example

1:  String s = "I never saw a purple cow"
2:
3:  spellOut(s)
4:   *** Output ***
5:  I N-E-V-E-R S-A-W A P-U-R-P-L-E C-O-W

4.9 Substitution Cipher

In a simple substitution cipher, A =1 , B=2, C=3 etc. Write a method that encodes a sentence using this cipher

4.9.1 Example

1:  String s = "Hello World"
2:
3:  encode(s)
4:   *** Output ***
5:  8,5,12,12,15 23,15,18,12,4

4.10 Decoder

Write a decoder method for your substitution Cipher

4.10.1 Example

1:  String s = "9 14,5,22,5,18 19,1,23 1 16,21,18,16,12,5 3,15,23"
2:
3:  decode(s)
4:   *** Output ***
5:  i never saw a purple cow

– Array Solutions

3 Array Solutions

3.1 Print Array

Write a method that prints out a string array, one element per line

3.1.1 Solution

    public class NNArrays {
    NNArrays()
    {
        String [] breakfast = {"Sausage", "Eggs", "Beans", "Bacon", "Tomatoes", "Mushrooms"};
        printArray(breakfast);
        //Alternative Method
        printArrayAlt(breakfast);
    }
    void printArray(String [] array)
    {
        for(int i = 0; i<array.length; i++)
        {
            System.out.println(array[i]);
        }
    }
    //Alternative method using for each loop
    void printArrayAlt(String [] array)
    {
        for(String s: array)
        {
            System.out.println(s);
        }
    }
    public static void main(String[] args)
    {
        new NNArrays();
    }
}

3.2 Last Element of Array

Write a method that returns the last element of a string array

3.2.1 Solution

    public class NNArrays {
    NNArrays()
    {
        String [] breakfast = {"Sausage", "Eggs", "Beans", "Bacon", "Tomatoes", "Mushrooms"};
      System.out.println(lastElement(breakfast));
    }
    String lastElement(String [] array)
    {
        return array[array.length-1];
    }
    public static void main(String[] args)
    {
        new NNArrays();
    }
}

3.3 Last But One Element of Array

Write a method that returns the last but one element of a string array

3.3.1 Solution

    public class NNArrays {
    NNArrays()
    {
        String [] breakfast = {"Sausage", "Eggs", "Beans", "Bacon", "Tomatoes", "Mushrooms"};
        //Added two extra arrays for testing
        String [] coin = {"Heads", "Tails"};
        String [] lonely = {"solo"};
        System.out.println(lastButOne(breakfast));
        System.out.println(lastButOneSafer(breakfast));
        System.out.println(lastButOneSafer(coin));
        System.out.println(lastButOneSafer(lonely));
    }
    String lastButOne(String [] array)
    {
        return array[array.length-2];
    }
    // The previous method will throw an
    // arrayIndexOutOfBoundsException for arrays of length <2.
    // This method will return an empty string if length <2
    String lastButOneSafer(String [] array)
    {
        if (array.length<2)
        {
            return "";
        }
        else
        {
            return array[array.length-2];
        }
    }
    public static void main(String[] args)
    {
        new NNArrays();
    }
}

3.4 Reverse an Array

Write a method that returns elements of an Array in reverse order

3.4.1 Solution

    public class NNArrays {
    NNArrays()
    {
        String [] breakfast = {"Sausage", "Eggs", "Beans", "Bacon", "Tomatoes", "Mushrooms"};
        //Added two extra arrays for testing
        String [] coin = {"Heads", "Tails"};
        String [] lonely = {"solo"};
        printArray(reverse(breakfast));
        printArray(reverse(coin));
        printArray(reverse(lonely));
    }
    void printArray(String [] array)
    {
        for(int i = 0; i<array.length; i++)
        {
            System.out.println(array[i]);
        }
    }
    String [] reverse(String [] array)
    {
        for(int i = 0; i<array.length/2;i++)
        {
            String temp = array[i];
            array[i] = array[array.length-i-1];
            array[array.length-i-1] = temp;
        }
        return array;
    }
    public static void main(String[] args)
    {
        new NNArrays();
    }
}

3.5 Palindromic Arrays

Write a method that tests to see if an array is palindromic, i.e. the elements are the same when reversed.

3.5.1 Solution

    public class NNArrays {
    NNArrays()
    {
        String [] breakfast = {"Sausage", "Eggs", "Beans", "Bacon", "Tomatoes", "Mushrooms"};
        String [] lonely = {"solo"};
        String [] palindromic = {"Sausage", "Eggs", "Beans",
                                 "Beans", "Eggs", "Sausage"};
        System.out.println(isPalindrome(palindromic));
        System.out.println(isPalindrome(breakfast));
        System.out.println(isPalindrome(lonely));
    }
    boolean isPalindrome(String [] array)
    {
        boolean isPal = true;
        for (int i = 0; i<array.length/2; i++)
        {
            if (!array[i].equals(array[array.length -i -1]))
                isPal = false;
        }
        return isPal;
    }
    // The following method doesn't work.
    // Need to compare individual elements
    boolean isPalindromeWrong(String [] array)
    {
        if (array.equals(reverse(array)))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    String [] reverse(String [] array)
    {
        for(int i = 0; i<array.length/2;i++)
        {
            String temp = array[i];
            array[i] = array[array.length-i-1];
            array[array.length-i-1] = temp;
        }
        return array;
    }
    public static void main(String[] args)
    {
        new NNArrays();
    }
}

3.6 Consecutive Duplicates

Write a method to print out an int array with consecutive duplicates eliminated

3.6.1 Solution

    public class NNArrays {
    NNArrays()
    {
        int [] nums = {1,1,3,3,3,2,2,2,1,1,1,1,4,4,4,4};
        int [] num2 = {1,1};
        int [] num1 = {1};
        compress(nums);
        compress(num2);
        compress(num1);
    }
    void compress(int [] array)
    {
        System.out.println(array[0]);
        for (int i = 1; i<array.length; i++)
        {
            if (array[i]!=array[i-1])
            {
                System.out.println(array[i]);
            }
        }
    }
    public static void main(String[] args)
    {
        new NNArrays();
    }
}

3.7 Pack Duplicates

Pack consecutive duplicates of a char array into Strings

3.7.1 Solution

    public class NNArrays {
    NNArrays()
    {
        char [] letters = {'a', 'a', 'a', 'a', 'b', 'c', 'c', 'a', 'a', 'd', 'e', 'e', 'e', 'e'};
        pack(letters);
    }
    void pack(char [] array)
    {
        String s = Character.toString(array[0]);
        for(int i = 1; i<array.length; i++)
        {
            if(array[i] != array [i-1])
            {
                System.out.print(s+ ", ");
                s = Character.toString(array[i]);
            }
            else
            {
                s = s + array[i];
            }
        }
        System.out.println(s);
    }
    public static void main(String[] args)
    {
        new NNArrays();
    }
}

3: Arrays

3.1 Print Array

Write a method that prints out a string array, one element per line

3.1.1 Example

 1:  String [] breakfast = {"Sausage", "Eggs", "Beans", "Bacon", "Tomatoes", "Mushrooms"};
 2:
 3:  printArray(breakfast)
 4:   *** Output ***
 5:  Sausage
 6:  Eggs
 7:  Beans
 8:  Bacon
 9:  Tomatoes
10:  Mushrooms

3.2 Last Element of Array

Write a method that returns the last element of a string array

3.2.1 Example

1:  String [] breakfast = {"Sausage", "Eggs", "Beans", "Bacon", "Tomatoes", "Mushrooms"}
2:
3:  System.out.println(lastElement(breakfast));
4:   *** Output ***
5:  Mushrooms

3.3 Last But One Element of Array

Write a method that returns the last but one element of a string array

3.3.1 Example

1:  String [] breakfast = {"Sausage", "Eggs", "Beans", "Bacon", "Tomatoes", "Mushrooms"}
2:
3:  System.out.println(lastButOne(breakfast));
4:   *** Output ***
5:  Tomatoes

3.4 Reverse an Array

Write a method that reverses the elements of an Array

3.4.1 Example

 1:  String [] breakfast = {"Sausage", "Eggs", "Beans", "Bacon", "Tomatoes", "Mushrooms"};
 2:
 3:  System.out.println(reverse(breakfast));
 4:   *** Output ***
 5:  : Mushrooms
 6:  : Tomatoes
 7:  : Bacon
 8:  : Beans
 9:  : Eggs
10:  : Sausage
11:  : Tails

3.5 Palindromic Arrays

Write a method that tests to see if an array is palindromic, i.e. the elements are the same when reversed.

3.5.1 Example

1:  String [] palindromic = {"Sausage", "Eggs", "Beans", "Beans", "Eggs", "Sausage"};
2:  String [] breakfast = {"Sausage", "Eggs", "Beans", "Bacon", "Tomatoes", "Mushrooms"}
3:
4:  System.out.println(isPalindrome(palindromic));
5:  System.out.println(isPalindrome(breakfast));
6:   *** Output ***
7:  True
8:  False

3.6 Consecutive Duplicates

Write a method to print out an int array with consecutive duplicates eliminated

 1:  int [] nums = {1,1,3,3,3,2,2,2,1,1,1,1,4,4,4,4};
 2:
 3:  compress(nums)
 4:   *** Output ***
 5:  : 1
 6:  : 3
 7:  : 2
 8:  : 1
 9:  : 4
10:  : 1
11:  : 1
12:

3.7 Pack Duplicates

Pack consecutive duplicates of a char array into Strings

1:  char [] letters = {'a' 'a' 'a' 'a' 'b' 'c' 'c' 'a' 'a' 'd' 'e' 'e' 'e' 'e'};
2:
3:  pack(nums)
4:   *** Output ***
5:  : aaaa, b, cc, aa, d, eeee
6:

– Loop Solutions

1:  package nnloops;
  2:
  3:  import java.util.Scanner;
  4:
  5:  public class NNLoops {
  6:
  7:      NNLoops()
  8:      {
  9:          //oneToTen();
 10:          //oddNumbers();
 11:          //squareNumbers();
 12:          //random4();
 13:          //even(20);
 14:          //powers(8);
 15:          //areWeThereYet();
 16:          //triangle();
 17:          //tableSquare();
 18:          tableSquares(6);
 19:
 20:      }
 21:
 22:      void oneToTen()
 23:      {
 24:          for(int i=1; i <11; i++)
 25:          {
 26:              System.out.println(i);
 27:          }
 28:      }
 29:
 30:      void oddNumbers()
 31:      {
 32:          for (int i=1; i<20; i+=2)
 33:          {
 34:              System.out.println(i);
 35:          }
 36:      }
 37:
 38:      void squareNumbers()
 39:      {
 40:          for (int i=1; i<11; i++)
 41:          {
 42:              System.out.println(i*i);
 43:          }
 44:      }
 45:
 46:      void random4()
 47:      {
 48:          for (int i=0; i<4; i++)
 49:          {
 50:              System.out.println((int)(Math.random()*10)+1);
 51:          }
 52:      }
 53:
 54:      void even(int n)
 55:      {
 56:          for (int i=1; i<n; i+=2)
 57:          {
 58:              System.out.println(i);
 59:          }
 60:      }
 61:
 62:      void powers (int n)
 63:      {
 64:          for (int i =1; i<=n; i++)
 65:          {
 66:              System.out.println(Math.pow(2, i));
 67:          }
 68:      }
 69:
 70:      void areWeThereYet()
 71:      {
 72:          Scanner scan = new Scanner(System.in);
 73:          do
 74:          {
 75:              System.out.println("Are we there yet?");
 76:          } while(!scan.nextLine().equals("Yes"));
 77:          System.out.println("Good!");
 78:      }
 79:
 80:      void triangle()
 81:      {
 82:          for(int i = 0; i<6; i++)
 83:          {
 84:              for (int j= 0; j<i; j++)
 85:              {
 86:                  System.out.print("*");
 87:              }
 88:              System.out.println("");
 89:          }
 90:      }
 91:
 92:      void tableSquare()
 93:      {
 94:          for(int i = 1; i<=4; i++)
 95:          {
 96:              for (int j=1; j<=4; j++)
 97:              {
 98:                  System.out.print("|"+ i*j +"\t");
 99:              }
100:              System.out.println("|");
101:
102:          }
103:      }
104:
105:      void tableSquares(int n)
106:      {
107:          for(int i = 1; i<=n; i++)
108:          {
109:              for (int j=1; j<=n; j++)
110:              {
111:                  System.out.print("|"+ i*j +"\t");
112:              }
113:              System.out.println("|");
114:
115:          }
116:      }
117:
118:
119:      public static void main(String[] args) {
120:         new NNLoops();
121:      }
122:  }

2: Loops

2.1 1 to 10

Write a method that prints the numbers 1 to 10

2.1.1 Example

 1:  oneToTen()
 2:   *** Output ***
 3:  1
 4:  2
 5:  3
 6:  4
 7:  5
 8:  6
 9:  7
10:  8
11:  9
12:  10

2.2 Odd Numbers

Write a method that prints the positive odd numbers less than 20

2.2.1 Example

 1:  oddNumbers()
 2:   *** Output ***
 3:  1
 4:  3
 5:  5
 6:  7
 7:  9
 8:  11
 9:  13
10:  15
11:  17
12:  19

2.3 Square Numbers

Write a method that prints the square numbers up to 100

2.3.1 Example

 1:  squares()
 2:   *** Output ***
 3:  1
 4:  4
 5:  9
 6:  16
 7:  25
 8:  36
 9:  49
10:  64
11:  81
12:  100

2.4 Random Numbers

Write a for loop to print out four random integers between 1 and 10

2.4.1 Example

1:  random4()
2:   *** Output ***
3:  3
4:  5
5:  2
6:  8

2.5 Even Numbers < n

Write a method to print out the positive even numbers less than n

2.5.1 Example

 1:  even(20)
 2:   *** Output ***
 3:  2
 4:  4
 5:  6
 6:  8
 7:  10
 8:  12
 9:  14
10:  16
11:  18

2.6 Powers of 2

Write a method to print out the powers of 2 from 21 up to 2n

2.6.1 Example

 1:  powers(8)
 2:   *** Output ***
 3:  2
 4:  4
 5:  8
 6:  16
 7:  32
 8:  64
 9:  128
10:  256

2.7 Are we there yet?

Write a program that outputs “Are we there yet?” and then waits for input. If the input is “Yes” the program outputs “Good!” and exits, otherwise the program loops.

2.7.1 Example

1:  "Are we there yet?"
2:  No
3:  "Are we there yet?"
4:  Spoons
5:  "Are we there yet?"
6:  Yes
7:  Good!

2.8 Triangle

Write a method that uses nested loops to produce the following pattern

1:  triangle()
2:   *** Output ***
3:  *
4:  **
5:  ***
6:  ****
7:  *****

2.9 Table Square

Write a method that prints out a 4 x 4 table square

2.9.1 Example

1:  tableSquare()
2:   *** Output ***
3:  A 4 x 4 table square
4:  | 1 | 2 |  3 |  4 |
5:  | 1 | 2 |  3 |  4 |
6:  | 2 | 4 |  6 |  8 |
7:  | 3 | 6 |  9 | 12 |
8:  | 4 | 8 | 12 | 16 |

2.10 Table Squares

Extend your answer to the last question produce a method that will print out a n x n table square

2.10.1 Example

 1:  tableSquares(6)
 2:   *** Output ***
 3:  A 6 x 6 table square
 4:  | 1 |  2 |  3 |  4 |  5 |  6 |
 5:  | 2 |  4 |  6 |  8 | 10 | 12 |
 6:  | 3 |  6 |  9 | 12 | 15 | 18 |
 7:  | 4 |  8 | 12 | 16 | 20 | 24 |
 8:  | 5 | 10 | 15 | 20 | 25 | 30 |
 9:  | 6 | 12 | 18 | 24 | 30 | 36 |

1: How to Answer These Questions

Write methods to solve all of the questions. Here are two example questions and their solutions. Notice that the first method prints a value, the second method returns a value.

1.1 Hello <Name>

Write a method that accepts a name as a parameter and prints out “Hello ” <name>

1.1.1 Example

1:  hello("Kim")
2:   *** Output ***
3:  Hello Kim

1.2 Average of two numbers

Write a method that accepts two numbers and returns the average of the two numbers.

1.2.1 Example

1:  System.out.println(average(3,4));
2:   *** Output ***
3:  3.5

1.3 Solutions

 1:  public class NinetyNine {
 2:
 3:      NinetyNine()
 4:      {
 5:          hello("Kim");
 6:          System.out.println(average(3,4));
 7:      }
 8:
 9:      void hello(String s)
10:      {
11:          System.out.println("Hello " + s);
12:      }
13:
14:      double average(double x, double y)
15:      {
16:          return (x+y)/2;
17:      }
18:
19:      public static void main(String[] args)
20:      {
21:          new NinetyNine();
22:      }
23:  }
24: