CS 145 Lecture 2

Topics Covered

  • Can we break the paperclip chain record?
  • Boolean
  • The nature of primitive types
  • Switching between types
  • Penny-based RAM
  • Higher-level operations in the Math class
  • Calculating cow-path savings

What does this do?

  1. int a = 7;
    int b = 3;
    b = a + b;
  2. int i;
    System.out.println(i);
  3. int i = 100;
    System.out.println(i / 0);

Code

WhatDoesThisDo1.java

package lecture;

public class WhatDoesThisDo1 {
  public static void main(String[] args) {
    // Variables must be initialized!
//    int i;
//    System.out.println(i);
    
    // Crashes at runtime!
    int i = 100;
    System.out.println(i / 0);
  }
}

Paperclipping.java

package lecture;

public class Paperclipping {
  public static void main(String[] args) {
    int nClips = 22025;
    int nHours = 24;
    double hoursPerMinute = 1.0 / 60.0;
    double minutesPerSecond = 1.0 / 60.0;

    double jeannesRate = nClips / nHours * hoursPerMinute * minutesPerSecond * 6;
    System.out.println(jeannesRate);
    
    double yosRate = 4.0;
    
    System.out.println(yosRate > jeannesRate);
  }
}

CowpathSavings.java

package lecture;

public class CowpathSavings {
  public static void main(String[] args) {
    double cosOf31 = Math.cos(31);
    System.out.println(cosOf31);
    
    double a = 10.0;
    double b = 20.0;
    
    double cowpath = Math.sqrt(a * a + b * b);
    System.out.println(cowpath);
    double savings = a + b - cowpath;
    System.out.println(savings);
    
    // (10 * 10) + (20 * 20) = c * c
    // (
    
  }
}

TODO

  • Get the JDK and Eclipse installed if you plan to use your own machine.
  • A homework will be assigned Wednesday.

Haiku

folks steal my poems
thought in penless laundry rooms
written in quarters

Comments

Leave a Reply

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