teaching machines

CS145 Lecture 10 – Workings of Memory


  • what ?s
  • program this
  • generating lines from points
  • visualizing memory


  • Sync on Bitbucket and pull in Eclipse to get a fixed SpecChecker. See Piazza post @68 for more details.


Most education researchers will tell you that lectures are probably not the best way to learn. Just like the best way to stay healthy is not watching other people work out and eat salads. What tends to stick in the brain are not thing things in which you have been instructed, but the things that you have constructed. The more active participation you take, the more efficient your learning process will be. That’s why I try to throw things back at you during our class time. Like this exercise:

Last class we wrote getSlope. With a neighbor, write the related method getIntercept.

We’ll use your definition to create some random line art.

When we write methods, we create little isolated worlds of computation that fit into larger worlds. Control gets passed back and forth between these worlds, and having a proper mental model of what’s actually happening inside the computer is essential to exploiting┬áthem effectively. We’re going to investigate how memory changes as we declare variables and call methods. After we do an example walkthrough together, I’ll ask you to pair up and walk through these two exercises together. One partner leads on exercise A, and the other partner leads on exercise B.

Exercise A

public class RectangleRatio {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int width = in.nextInt();
    int height = in.nextInt();
    int area = getArea(width, height);
    int perimeter = getPerimeter(width, height);
    System.out.println("Area: " + area);
    System.out.println("Perimeter: " + perimeter);
    System.out.println("Ratio: " + getRatio(width, height));

  public static int getArea(int width, int height) {
    int a = width * height;
    return a;

  public static int getPerimeter(int width, int height) {
    width = 2 * width; // not a great practice
    height = 2 * height;
    return width + height;

  public static double getRatio(int width, int height) {
    int area = getArea(width, height);
    int perimeter = getPerimeter(width, height);
    double ratio = area / (double) perimeter;
    return ratio;

Exercise B

public class GuessWord {
  public static void main(String[] args) {
    String word = "garbage";
    boolean isWinner = play(word);
    System.out.println("It is " + isWinner + " that you are a winner.");

  private static boolean play(String word) {
    Random g = new Random();
    String masked = mask(g, word);
    masked = mask(g, masked);

    System.out.println("What is " + masked + "?");
    Scanner in = new Scanner(System.in);
    String guess = in.nextLine();
    boolean isSame = guess.equals(word);
    return isSame;

  private static String mask(Random g, String toMask) {
    int length = toMask.length();
    int i = g.nextInt(length);
    char c = toMask.charAt(i);
    String masked = toMask.replace(c, '_');
    return masked;

We’ve seen two strategies for thinking about┬ácode now, neither of which require a computer:

  1. Expression evaluation, one operator at a time
  2. Memory tracing

Don’t forget to think away from the machine. It’s not better than you. That said, this Java Visualizer of tracing execution.



package lecture0925;

import java.util.Random;

public class Line {

  public static void main(String[] args) {
//    System.out.println(slope(4, 20, 2, 10));
//    System.out.println(intercept(7, 7, 0, 0));
//    System.out.println(intercept(7, 7, 6, 6));
//    System.out.println(intercept(0, 1, 7, 7));

    Random g = new Random();
    System.out.println(getEquation(g.nextInt(21) - 10, g.nextInt(21) - 10, g.nextInt(21) - 10, g.nextInt(21) - 10));


  private static double slope(double x1,
                              double y1,
                              double x2,
                              double y2) {
    return (y2 - y1) / (x2 - x1);

  private static double intercept(double x1,
                                  double y1,
                                  double x2,
                                  double y2) {
    double slope = slope(x1, y1, x2, y2);
    double intercept = y1 - slope * x1;
    // y1 - slope(x1, y1, x2, y2) * x1 -> intercept
    return intercept;
  public static String getEquation(double x1,
                                   double y1,
                                   double x2,
                                   double y2) {
    System.out.printf("(%f,%f)%n", x1, y1);
    System.out.printf("(%f,%f)%n", x2, y2);
    String equation = "y = " + slope(x1, y1, x2, y2) + " * x + " + intercept(x1, y1, x2, y2);
    return equation;


on showing your work:
D on my homework?
Teacher, my answers are right
“I can’t C your steps…”


Leave a Reply

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