teaching machines

CS 1: Lecture 3 – Scanner

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

Dear students,

We start today by visiting a few of your questions and observations from your quarter sheets. I read through every single one of these, and respond to a few. They help me feel the pulse of the class.

Last time we just started teaching a machine to perform some math for us. I was talking with one of you last week about learning a foreign language. That student said that the first words he was learning were all body parts—so that when you get hurt in a foreign country you can make sure the right limb gets amputated. So it is with Java. Knowing the anatomy leads to less scarring. Let’s briefly review the grammar of Java:

program:
  1 or more classes

class:
  package name;

  0 or more import statements

  public class name {
    0 or more methods
  }

method:
  public void name(...) {
    0 or more statements
  }

statement:
  type name;                 <- declaration
  name = expression;         <- assignment
  type name = expression;    <- declassignment
  object.method(expression); <- call

expression:
  324
  16.7
  name
  expression + expression
  expression - expression
  expression * expression
  expression / expression

Let’s solve a few more problems today:

In these first few problems, the data is more or less constant, and we are just using the computer as a really expensive calculator. Many real programs interact with the user. The user supplies the data at runtime, and the program responds. To provide for this dynamic behavior, we need a mechanism for getting user input. We will use another recipe book (a class) named Scanner for this task. Consider this example which computes a newborn’s Apgar score, developed in 1952 to help gauge the baby’s health right after birth:

import java.util.Scanner;

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

    System.out.print("Appearance: ");
    int appearance = in.nextInt();

    System.out.print("Pulse: ");
    int pulse = in.nextInt();

    System.out.print("Grimace: ");
    int grimace = in.nextInt();

    System.out.print("Activity: ");
    int activity = in.nextInt();

    System.out.print("Respiration: ");
    int respiration = in.nextInt();

    System.out.print("Apgar: ");
    System.out.print(appearance + pulse + grimace + activity + respiration);
  }
}

This test is a little weird because it was invented by Virginia Apgar, an anesthesiologist. She was much bolder than me.

This program has both the I and the O of IO.

How’s your Apgar score in this second birth into computer science?

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

See you next class!

Sincerely,

P.S. It’s time a haiku!

I couldn’t reach O
Till I filled the gulf between
With 1000 mes

P.P.S.

Here’s the code we wrote together in class…

PartyTime.java

package lecture0908;

public class PartyTime {
  public static void main(String[] args) {
    int nDemocrats = 15;
    int nRepublicans = 19;
    int nPresidents = 45;
    
    System.out.print("% of Democrats: ");
    System.out.println(100.0 * nDemocrats / nPresidents);
    
    System.out.print("% of Republicans: ");
    System.out.println(100.0 * nRepublicans / nPresidents);
  }
}

Apgar.java

package lecture0908;

import java.util.Scanner;

public class Apgar {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    
    System.out.print("Appearance: ");
    int appearance = in.nextInt();
    
    System.out.print("Pulse: ");
    int pulse = in.nextInt();
    
    System.out.println("Grimace: ");
    int grimace = in.nextInt();
    
    System.out.println("Activity: ");
    int activity = in.nextInt();
    
    System.out.println("Respiration: ");
    int respiration = in.nextInt();
    
    int apgar = appearance + pulse + grimace + activity + respiration;
    
    System.out.print("Apgar score: ");
    System.out.println(apgar);
  }
}

MainTester.java

package lecture0911;

public class MainTester {
  public static void main(String[] args) {
    for (int i = 0; i < args.length; ++i) {
      System.out.println(args[i]);
    }
  }
}

HelloGas.java

package lecture0911;

import java.util.Scanner;

public class HelloGas {
  public static void main(String[] args) {
    // type id = expression;
    Scanner in = new Scanner(System.in);
    
    System.out.print("Miles A? ");
    double milesA = in.nextDouble();
    
    System.out.print("Miles B? ");
    double milesB = in.nextDouble();
    
    System.out.print("Gallons? ");
    double gallons = in.nextDouble();
    
    double mpg = (milesB - milesA) / gallons;
    System.out.print("MPG: " + mpg);

  }
}

Space.java

package lecture0911;

import java.util.Scanner;

public class Space {
  public static void main(String[] args) {
     System.out.print("What country do you live in? ");
     Scanner in = new Scanner(System.in);
     String country = in.nextLine();
     
     System.out.print("Population: ");
     int population = in.nextInt();
     
     System.out.print("Area: ");
     int area = in.nextInt();
     
     System.out.println("Area per person: " + area / (double) population);
  }
}