teaching machines

CS 145 Lecture 5 – Problems

September 12, 2014 by . Filed under cs145, fall 2014, lectures.

Agenda

TODO

What Type is This?

  1. Average number of children
  2. A month
  3. An answer to a multiple choice question
  4. Sex
  5. Gender
  6. Name
  7. The number of seconds since January 1, 1970
  8. A 1-to-5 star rating
  9. A phone number
  10. A letter grade

Program This

Pick one of the following to solve. Declare variables, craft expressions, and print out the requested information.

  1. You are given n pictures, all square-shaped and of the same size. How can you lay them out so that they are as many rows as there are columns?
  2. Your kid brother has invented a guess-a-number game. You say the number, and he tells you how far away you are from the right number. For instance, suppose the number is 9. You guess 11. He says “2 away!” You guess 8, he says “1 away!”. You guess 7, he says “2 away!” Play one guess-response exchange.
  3. On day 0, 1 person gets a terrible disease. On day 1, this person infects 10 others. On day 2, these 10 people each infect 10 others, for a total of 100 new infections. On day three, the 100 each infect 10 others, for a total of 1000 new infections. How many days pass before half the US population (313.9 million) is infected?
  4. A knight is on a toroidal chessboard. That is, if it moves off the edge of the board, it comes back in on the opposite side. Suppose the knight is at position (c, r). What is its new position if it moves from the top of an L to its bottom-right?

Code

Projectile.java

package lecture0912;

public class Projectile {
  public static void main(String[] args) {
    final double acceleration = -4.9;
//    acceleration = 8;
    final double initialVelocityY = 30.0;
    final double initialVelocityX = 10.0;
    final double initialHeight = 10.0;
    double time;
    double positionY;
    double positionX;
    
    time = 0.0;
    positionY = acceleration * time * time + 
                initialVelocityY * time +
                initialHeight;
    positionX = initialVelocityX * time;
    System.out.println("(" + positionX + "," + positionY + "),");
    
    time = 1.0;
    positionY = acceleration * time * time + 
                initialVelocityY * time +
                initialHeight;
    positionX = initialVelocityX * time;
    System.out.println("(" + positionX + "," + positionY + "),");
    
    time = 2.0;
    positionY = acceleration * time * time + 
                initialVelocityY * time +
                initialHeight;
    positionX = initialVelocityX * time;
    System.out.println("(" + positionX + "," + positionY + "),");

    time = 3.0;
    positionY = acceleration * time * time + 
                initialVelocityY * time +
                initialHeight;
    positionX = initialVelocityX * time;
    System.out.println("(" + positionX + "," + positionY + "),");
 
    time = 4.0;
    positionY = acceleration * time * time + 
                initialVelocityY * time +
                initialHeight;
    positionX = initialVelocityX * time;
    System.out.println("(" + positionX + "," + positionY + "),");
  }
}

Pictures.java

package lecture0912;

public class Pictures {
  public static void main(String[] args) {
    int n = 64;
    System.out.println(Math.sqrt(n));
  }
}

GuessANumber.java

package lecture0912;

public class GuessANumber {
  public static void main(String[] args) {
    int n = 5;
    int guess = 23;
    
    System.out.println(Math.abs(guess - n));
  }
}

Disease.java

package lecture0912;

public class Disease {
  public static void main(String[] args) {
    int population = 313900000;
    System.out.println(Math.log10(population));
  }
}

Knight.java

package lecture0912;

public class Knight {
  public static void main(String[] args) {
    int c = 6;
    int r = 7;
    int newC = (c + 1) % 8;
    int newR = (r + 2) % 8;
    System.out.printf("(%d,%d) to (%d,%d)", c, r, newC, newR);
  }
}

Haiku

on our feelings about being wrong:
We’re wrong when we code
Wrong as a sculptor is wrong
At a ball of clay