teaching machines

CS 1: Lecture 2 – Computer as Calculator

September 8, 2017 by . Filed under cs1, cs145, cs148, fall 2017, lectures.

Dear students,

Last time we started to explore one of the primary activities of computer science: organizing process. We started this adventure using Madeup, but today we’ll jump into Java, the language we will spend most of our time with this semester and next.

Java is a language originally developed by Sun Microsystems. It surged to popularity in part because it invented its own fake CPU—what they called the Java Virtual Machine. When a developer writes code for the JVM, that developer can take that code to a Windows machine or a Macintosh or a kiosk in a museum, and so on, and run it there without modification on that platform’s JVM. This was a big deal in a world where standards were few. Normally developers would have to write custom code for each target platform. It also was one of the first languages aware of the web. And it’s a pretty clean and consistent language, which is why we use it in this class.

Java will probably not live forever. In 2010, Oracle acquired Sun and then sued Google for “redeveloping” Java as the basis for the Android Operating System. Such tactics are not a good way to keep your language in active use. It seems in this day and age that if a language is encumbered by licensing burdens, it will die.

People outside or new to the field tend to be concerned about learning the best programming language. I tell you right now that you do not have to choose just one language. You do not have to be a Mac person or a Windows person, an iPhone or an Android person. Study hard and get a good job and your company will give you one of everything. Many projects are built using several languages.

People tend to focus on superficial distinctions of programming languages. This layer of a language is called its syntax. Syntax refers to the visual form of a language, its grammar, the rules that describe how pieces are put together, what punctuation demarcates the pieces, and what text is used to as special keywords. The deeper meanings or intents of a language’s form are its semantics. Learning syntax is daunting at first, but the syntax rules are easily looked up and syntax errors are easily fixed. After a few months of writing code, you realize that the far more difficult and interesting problems lie in learning the semantics of a language.

Today, we discuss the syntax and semantics of Java. Let’s start with a little exercise I like to call Unscramble This. Based on your reading, how can we reorder this code so that it conforms to the rules of Java?

  1. public class Greeter {
  2.   public static void main(String[] args) {
  3.     System.out.println("Hello, CS 1!");
  4.   }
  5. }

This example demonstrates three anatomical parts that we will be discussing all semester:

The statements in Java are often not that different from our own imperative statements in English:

System.out.println("Hello, CS 1!");
// ------- ------- ----------------
// subject   verb    direct object   <- English
//  object  method     parameter     <- Java

The method in this example is a special one. When you run a Java program, the JVM looks for this special main method and starts executing the statements within. It’s kind of like the ignition in a car. You automatically reach for it when you climb into a car, even if you’ve never driven it before. You just know. Since the JVM needs to locate this method, we must be exact when typing it in.

The class itself is not really all that interesting here, but Java doesn’t let us write recipes that don’t appear in recipe books.

I’ve chunked this course up into seven coherent units. Each unit reflects a different “personality” that our computer may take on:

  1. Computer as Calculator, in which we see how the computer crunches those numbers
  2. Computer as Chef, in which we see how the computer crafts its cuisine out of reusable, self-contained recipes
  3. Computer as Philosopher, in which we see how the computer ponders what is true
  4. Computer as Pilot, in which we see how the computer turns and loops through the wild blue yonder that is our code
  5. Computer as Factory Worker, in which we see how the computer accelerates drudgery
  6. Computer as Scribe, in which we see how the computer deals with persistent information
  7. Computer as Creator, in which the computer solves problems by managing actors on a stage

We start with the Computer as a Calculator. Let’s use Eclipse to write some programs that solve these problems:

To make our computation more understandable, we like to give names to our data by using variables. We did this the other day in Madeup, and we can do this also in Java. In Java, however, we need to supply one extra piece of information when we request that the computer allocate some space in RAM for us. We need to declare a variable’s type. We’ll focus on two types initially, ints and doubles:

int nEggs;
double gallons;

A statement of this form is called a declaration. A variable must be declared before it can be used. A variable can only be declared once.

After a variable is declared, we can assign it a value:

int nWeeksPerYear;
nWeeksPerYear = 52;

If we can express its value at the time of the declaration, it’s considered better form to combine the two statements into what I call a declassignment:

int nWeeksPerYear = 52;

That’s enough for today.

Here’s your TODO list of things to complete before next class:

See you next class!


P.S. It’s Haiku Friday!

“Alexa, what’s pi?”
Turns out she’s good at numbers
We’re on month eight now


Here’s the code we wrote together in class…


package lecture0908;

public class Eggsumption {
  public static void main(String[] args) {
//    int nEggsPerWeek; // declaration
//    nEggsPerWeek = 3 * 12; // assignment

    int nEggsPerWeek = 3 * 12;
    int nWeeksPerYear = 52;
    int nEggsPerYear = nEggsPerWeek * nWeeksPerYear;


package lecture0908;

public class Odd {
  public static void main(String[] args) {
    int odd27 = 27 * 2 - 1;


package lecture0908;

public class Leftovers {
  public static void main(String[] args) {
    int nEggsTotal = 342;
    int nCartons = nEggsTotal / 12;
    int nEggsInCartons = nCartons * 12;
    int nEggsLeftOver = nEggsTotal - nEggsInCartons;
//    int nEggsLeftover = nEggsTotal % 12;


package lecture0908;

public class Something {
  public static void main(String[] args) {
    int today = 5;
    int nDaysAhead = 183;
    nDaysAhead = nDaysAhead % 7;
    int dayOfBigThing = (today + nDaysAhead) % 7;