teaching machines

CS 145 Lecture 7

Topics Covered

  • what does this do?
  • generating a random spelunking workout
  • for loop = while loop, but more compact
  • sum numbers 1 to n
  • an acrostic editor
  • mouse trails

What does this do?

  1. public static String ?(??) {
     return text + " :)";
    }
  2. public static ? ??(String text) {
      return text.length() == 0;
    }
  3. public static ? ??(String text) {
      int index = text.indexOf('.');
      return text.substring(0, index + 1);
    }

Code

SpelunkingFitnessDisplay.java

package lecture;

import java.util.Random;

public class SpelunkingFitnessDisplay {

  public static void main(String[] args) {
    
//    int i = 0;
//    while (i < 13) {
//      printLine();
////      i = i + 1;
//      ++i;
//    }
    
    for (int i = 0; i < 13; ++i) {
      printLine();
    }

  }
  
  public static void printLine() {
    Random generator = new Random();
    int nDots = generator.nextInt(11);
    
    while (nDots > 0) {
      System.out.print("*");
      nDots = nDots - 1;
    }
    
    System.out.println();
  }
}

AcrosticWriter.java

package lecture;

import java.util.Scanner;

public class AcrosticWriter {
  public static void main(String[] args) {
    
    
    
    Scanner in = new Scanner(System.in);
    System.out.print("Theme: ");
    String theme = in.nextLine();

    // grab letter i
    // prompt for word that starts with i^th letter
    // grab letter i + 1
    // prompt for word that starts with (i+1)^th letter

    int themeLength = theme.length();
    for (int i = 0; i < themeLength; ++i) {
      char letter = theme.charAt(i);
      System.out.print(letter);
      in.nextLine();
    }
  }
  
  public static int sum(int n) {
    
    return 1 + 2 + 3 + 4 + ... + n; 
  }
}

TODO

  • Read sections 3.1 through 3.3 again.

Haiku

Never lose it again!
Install the new Lonely Phone!
while (noOneNear) beep

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *