teaching machines

CS 145 Lecture 2 – Computer as Calculator

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

Agenda

TODO

Note

Last lecture we talked about the ideas of computer science through the lens of Madeup. But we’ll need to view these ideas through many lenses before we start to really see what’s at the core. Today we’ll dive into Java.

To help shape our discussion, this course is organized into personalities or roles that the computer takes on as we write software. It starts as a simple calculator, crunching numbers. Then it becomes a chef, organizing processes into reusable recipes. Then it becomes a philosopher, pondering questions about our data. Then it becomes a pilot, looping and flying around obstacles in our code. Then it becomes a factory worker, dutifully processing streams of data that come its way. Then it becomes a scribe, recording and recalling data using files. Finally, we see the computer as a creator, breathing life into self-contained objects that have their own identity and interact with others.

Before we can calculate, we need to get to a place where we can run our code. Let’s look at a minimal working example of runnable Java code. To help us savor every sweet line, let’s reveal and discuss it in stages:

  1: public class MyLittleProgram {
  2:   public static void main(String[] args) {
  3:     System.out.println("Goodbye, Pluto!");
  4:   }
  5: }

Key ideas here are the main method, the organizing class structure, the boundaries marked by braces, and the line that does the printing.

Programming languages follow a grammar not unlike our spoken languages. What’s legal is determined by the language’s syntax. The computer is really good at catching our violations of the syntax rules. Sadly, computers are not so good at understanding our intent or meaning. This aspect is called semantics.

Let’s play a little game to introduce the mathematical operators. What are the possible semantics or meaning of the following expressions? What is the programmer’s intent?

public class SomeExpressions {
  public static void main(String[] args) {
  1:     System.out.println(2 * 24);
  2:     System.out.println(1 + 2 * 24);
  3:     System.out.println(109 / 6);
  4:     System.out.println(10 - 1);
  }
}

With the exception of division, the behavior of the mathematical operators should not surprise you. What’s left is to start using them to solve problems. So, here’s a problem:

With a neighbor, write a statement that calculates and prints the remainder of 27 divided by 4.

If we have time, we may introduce variables.

Haiku

on the role of the human brain:
Math’s a coconut
Computers may crack the shell
Inside is what counts