teaching machines

CS 145 Lecture 3 – Bitbucket and Variables

September 9, 2015 by . Filed under cs145, fall 2015, lectures.




We continue our treatment of the computer as a glorified calculator, but we one-up those little number crunchers in a couple of ways. First, we give our math more meaning by naming our data. The naming process is called a declaration and has this form:

type name = expression;

Technically, this is both a declaration and an initial assignment—what I call a declassignment. We could have done the two separately:

type name;
name = expression;

This wastes a line of my screen, so I don’t separate them unless I have a good reason to delay the initial assignment. By choosing meaningful names, we can demystify our mathematical expressions.

A second big advantage of programming over calculators is diversity in our data. Java provides many different data types. Some are really simple primitives, and these types all start with a lowercase first letter. We’ve already seen a few of these: whole numbers ( ints) and numbers with fractional parts ( doubles). There are several other numeric types, and we’ll discuss why there are so many. There’s also boolean and char. Then we get into more complex types like String, which are represented as classes that start with a capital letter. This wide array of types increase the possibilities of our data, and our programs will very quickly exceed what a calculator can do easily.



package lecture0909;

public class ProjectGoat {
  public static void main(String[] args) {
    double t = 0.2;
    double velocityX = 1;
    double velocityY = 9;
    double positionX = velocityX * t;
    double positionY = -4.9 * t * t + velocityY * t;
    // System.out.print("(");
    // System.out.print(positionX);
    // System.out.print(",");
    // System.out.print(positionY);
    // System.out.println(")");
    System.out.printf("(%.2f,%.2f)", positionX, positionY);


CS is three things
the science of naming things
off-by-one errors