Thursday, March 22, 2012

Code from recitation 8

  1. Searching through a 2D array for a particular element.
     ArraySearch.java 

  1  
  2  public class ArraySearch {
  3   public static void main(String[] args) {
  4    String[][] str = {
  5     { "a", "b", "c", "d" },
  6     { "e", "f", "g", "h" }
  7    };
  8  
  9    String goal = "c";
 10    
 11    boolean error = true;
 12    for (int i=0; i<str.length; i++) {
 13     for (int j=0; j<str[i].length; j++) {
 14      if (str[i][j].equals(goal)) {
 15       error = false;
 16       System.out.println("row = " + i);
 17       System.out.println("col = " + j);
 18       break;
 19      }
 20     }
 21     if (!error) {
 22      break;
 23     }
 24    }
 25    if (error) {
 26     System.out.println("Error");
 27    }
 28   }
 29  }
 30  
 31  

  1. Running out of memory
     OutOfMemory.java 

  1  
  2  public class OutOfMemory {
  3   public static void main(String[] args) {
  4    int[] x = new int[1];
  5    while (true) {
  6     int y = x.length;
  7     y = y * 2;
  8     System.out.println(y);
  9     x = new int[y];
 10    }
 11   }
 12  }
 13  
 14  

Tuesday, March 20, 2012

Recitation 7 Code

  1. I don't remember what this was for, but here it is!
     FillWithSevens.java 

  1  public class FillWithSevens {
  2      public static void main(String[] args) {
  3          int[] x = new int[3];
  4  
  5          for (int i=0; i<3; i++) {
  6              x[i] = 7;
  7          }
  8      }
  9  }


  1. Modifying an array:
     ModifyArray.java 

  1  class ModifyArray {
  2      public static void main(String[] args) {
  3          int[] x = { 1, 1 };
  4          int[] y = x;
  5  
  6          y[0] = 3;
  7  
  8          System.out.println(x[0]);
  9      }
 10  }

3
  1. Reverse an array in a new array:
     ReverseNewArray.java 

  1  public class ReverseNewArray {
  2      public static void main(String[] args) {
  3          int[] x = { 1, 2, 3 };
  4  
  5          int[] y = new int[x.length];
  6  
  7          int len = x.length;
  8          int j = 0;
  9          for (int i=len-1; i>=0; i--) {
 10              y[j] = x[i];
 11              System.out.println(java.util.Arrays.toString(y));
 12              j++;
 13          }
 14          System.out.println(java.util.Arrays.toString(x));
 15      }
 16  }

[3, 0, 0]
[3, 2, 0]
[3, 2, 1]
[1, 2, 3]
  1. Reverse an array in place:
     ReverseInPlace.java 

  1  public class ReverseInPlace {
  2      public static void main(String[] args) {
  3          int[] x = { 1, 2, 3, 4, 5, 6, 7, 8 };
  4  
  5          int j = x.length-1;
  6          for (int i=0; i<x.length/2; i++) {
  7              int f = x[i];
  8              x[i] = x[j];
  9              x[j] = f;
 10              System.out.println(java.util.Arrays.toString(x));
 11              j--;
 12          }
 13      }
 14  }

[8, 2, 3, 4, 5, 6, 7, 1]
[8, 7, 3, 4, 5, 6, 2, 1]
[8, 7, 6, 4, 5, 3, 2, 1]
[8, 7, 6, 5, 4, 3, 2, 1]

Tuesday, March 6, 2012

Code from recitation 6

  1. Take a String, and make just the first letter capital.
     Capital.java 

  1  public class Capital {
  2  
  3      public static void main(String[] args) {
  4          String x = "abcdefgh";
  5  
  6          String y = x.substring(0, 1).toUpperCase();
  7          String z = x.substring(1, x.length());
  8          System.out.println(y + z);
  9      }
 10  }

Abcdefgh
  1. See if a particular String belongs to an array (note that we haven't gotten to arrays yet. But we will).
     InList.java 

  1  public class InList {
  2      public static void main(String[] args) {
  3          String[] a = { "a", "aa", "aaa" };
  4  
  5          String x = "asdfjeoi";
  6          boolean in = false;
  7          for (int i=0; i<a.length; i++) {
  8              String y = a[i];
  9              if (x.equals(y)) {
 10                  in = true;
 11                  break;
 12              }
 13          }
 14          if (in == true) {
 15              System.out.println("x is in a");
 16          }
 17          else {
 18              System.out.println("x is NOT in a");
 19          }
 20      }
 21  }

x is NOT in a
  1. Go through the words in a sentence and count how many letters are in each:
     LetterCount.java 

  1  public class LetterCount {
  2      public static void main(String[] args) {
  3          String x = " words in a sentence ";
  4  
  5          for (int i=0; i<x.length(); i++) {
  6  
  7              if (x.charAt(i) == ' ') {
  8                  for (int j=i+1; j<x.length(); j++) {
  9                      if (x.charAt(j) == ' ') {
 10                          int length = j - i - 1;
 11                          String word = x.substring(i, j);
 12                          System.out.println(word);
 13                          System.out.println(length);
 14                          break;
 15                      }
 16                  }
 17              }
 18          }
 19      }
 20  }

 words
5
 in
2
 a
1
 sentence
8

Note the extra spaces around the word in order to make our algorithm work. We could also have written:

  1  String x = "words in a sentence";
  2  x = " " + x + " ";