teaching machines

CS1: Lecture 2 – Computer as Calculator

September 6, 2019 by . Filed under cs1, fall 2019, lectures.

Dear students,

Last time we inspected several programs and formulated these several decrees made by the designers of Java that explain why our programs are what they are:

Today let’s write some programs from scratch that abide by these decrees and introduce a few new ones.

Course Structure

I’ve chunked this course up into seven 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 cooks up a cuisine from 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 Creator, in which the computer solves problems by managing actors on a stage

There’s a homework for each of these themes.

Today we start with the first theme: Computer as a Calculator. We start here for a couple of reasons. First, you have spent a lot of time with numbers already, and we can build on these past experiences. Second, computers themselves were initially invented to be programmable calculators. The ENIAC, for instance, helped us figure out how to aim our guns at the enemy under all sorts of variables like elevation, wind speeds, and so on. Another early computer built by Herman Hollerith helped tabulate census data. Hollerithm’s company would go on to become IBM.

It should be no surprise that computers sprung up in a world of numbers. Computers do not have souls and ultimately aren’t well-suited to soulwork. The mechanical activity of mathematics is also soulless and deterministic, involving a lot of well-defined processes or algorithms that require no judgement or choice. Choosing algorithms and applying their results does require a soul, so I’m not saying mathematicians don’t have souls.

Problems

A lot of technology tutorials and books like to introduce a technology by enumerating all the features of a tool to you at once. I don’t find going through lists of ideas without application an effective way to learn, and we will not do that in this class. Instead, we will just solve a bunch of smallish math problems today by writing programs.

There are just a few features that we need to know to write a whole family of useful, number-crunching programs. Earlier we saw that data can be named and that data always has a type. For whole number data, we declare an int. For real number data, we declare a double. The type that can grab keyboard input is Scanner. By decree 7, it can send data back to us with its nextInt and nextDouble methods.

Let’s use IDEA to write some programs that solve these problems:

As we solve these problems, we’ll establish another decree.

Decree 8: data can be crafted to a different type.

Suppose you are trying to subdivide a pizza fairly. You might write this program:

int circle = 360;
int nEaters = 11;
double degreesPerWedge = circle / nEaters;

But you’ll find that you sometimes have leftover pizza. That’s because the / operates on an int and an int, thereby producing an int. It throws away or truncates the fraction. This rounds down positive numbers and rounds up negative numbers. Sometimes this called rounding toward zero. Note that it doesn’t matter that degreesPerWedge is a double. The / operation happens before the =.

One dissatisfying solution to this problem is to force at least one of the named data to be a double:

int circle = 360;
double nEaters = 11;
double degreesPerWedge = circle / nEaters;

But it doesn’t make sense for nEaters to be a double. We can’t have fractional people—though we may feel like we can after this first week of classes.

A better solution is to just temporarily convert one of the values to a double by casting it:

int circle = 360;
int nEaters = 11;
double degreesPerWedge = circle / (double) nEaters;

TODO

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

See you next class!

Sincerely,

P.S. It’s time for a haiku!

Think seven ate nine?
Who runs the operation?
The Number Cruncher

P.P.S.

Here’s the code we wrote together in class…

Unscramble3.java

package lecture0906.cs145;

import java.util.Scanner;

public class Unscramble3 {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int x = in.nextInt();
    int y = in.nextInt();
    int result = x + y + x + y;
    System.out.println(result);
  }
}

Unscramble4.java

package lecture0906.cs145;

import java.util.Scanner;

public class Unscramble4 {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    String a = in.nextLine();
    String b = in.nextLine();
    String c = a + " \u2764\ufe0fs " + b;
    System.out.println(c);
  }
}

Eggs.java

package lecture0906.cs145;

import java.util.Scanner;

public class Eggs {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    
    int nEggsPerDay = in.nextInt();
    int nPeople = in.nextInt();
    int nDaysInYear = 365;

    int nEggsConsumedAnnually = nEggsPerDay * nPeople * nDaysInYear;
    System.out.println(nEggsConsumedAnnually);
  }
}

SerialOdd.java

package lecture0906.cs145;

import java.util.Scanner;

public class SerialOdd {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);

    System.out.print("What number do you want, foobag? ");
    int n = in.nextInt();
    int term = 2 * n - 1;

    System.out.println(term);
  }
}

PresidentPercentage.java

package lecture0906.cs145;

import java.util.Scanner;

public class PresidentPercentage {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);

    System.out.print("# of presidents: ");
    int nPresidents = in.nextInt();

    System.out.print("# of democrats: ");
    int nDemocrats = in.nextInt();

    System.out.print("# of republicans: ");
    int nRepublicans = in.nextInt();

    double percentageDemocrat = 100 * nDemocrats / (double) nPresidents;
    double percentageRepublican = 100 * nRepublicans / (double) nPresidents;

    System.out.println("Percent democrat: " + percentageDemocrat + "%");
    System.out.println("Percent republican: " + percentageRepublican + "%");
  }
}

Unscramble4.java

package lecture0906.cs148;

import java.util.Scanner;

public class Unscramble4 {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    String a = in.nextLine();
    String b = in.nextLine();
    String c = a + " \u2764s " + b;
    System.out.println(c);
  }
}

Eggs.java

package lecture0906.cs148;

import java.util.Scanner;

public class Eggs {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);

    System.out.print("How many dozen eggs do you buy per week, foobag? ");

    int nDozensPerWeek = in.nextInt();
    int nWeeksPerYear = 52;
    int nEggsPerDozen = 12;
    int nEggsPerYear = nDozensPerWeek * nWeeksPerYear * nEggsPerDozen;

    System.out.println(nEggsPerYear + " eggs / year");
  }
}

Oddish.java

package lecture0906.cs148;

import java.util.Scanner;

public class Oddish {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);

    System.out.print("Which odd do you want? ");
//    in.hasNextInt()
    int i = in.nextInt();
    while (i <= 0) {
      System.out.print("Try that again: ");
      i = in.nextInt();
    }
    int odd = 2 * i - 1;

    System.out.println(odd);
  }
}

Presidentage.java

package lecture0906.cs148;

import java.util.Random;
import java.util.Scanner;

public class Presidentage {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);

    System.out.print("# of presidents: ");
    int nPresidents = in.nextInt();

    System.out.print("# of Republicans: ");
    int nRepublicans = in.nextInt();

    System.out.print("# of Democrats: ");
    int nDemocrats = in.nextInt();

    double percentageRepublicans = nRepublicans / (double) nPresidents;
    double percentageDemocrats = nDemocrats / (double) nPresidents;

    System.out.println("Republicans: " + percentageRepublicans);
    System.out.println("Democrats: " + percentageDemocrats);

    Random generator
  }
}