teaching machines

CS 1: Lecture 4 – Mathematical Data and Operations

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

Dear students,

CS 145: Yesterday we had our first lab. Tuesdays are a marathon day for me, and I am in class from 8 AM to 5:15 PM (with a lunch break). Your positive attitudes make it bearable and worth it. Many folks finished within the lab time. This will not always be the case, and you are a perfectly fine human being and computer scientist even if you didn’t finish the exercises. But do finish them before next lab.

CS 148: Let’s start with a little segment that I like to call Program This:

An obnoxious uncle gives you a truckload of pennies for your birthday. How many whole dollars have you received? How many cents beyond that dollar amount? Write a main method that gets appropriate input from the user and outputs the desired values.

We’ve been discussing the computer as a calculator. We’ve seen a couple different kinds of data: int and double. We’ve also seen some other types: String and Scanner. Let’s introduce another one quick: char.

We use chars to store single characters:

char first = 'A';
char filler = ',';

Strings are just sequences of chars. In fact, we can add chars and Strings together to produce a longer String:

String nounA = "watashi";
String nounB = "tomodachi";
String nounC = "denwa bangoo";
System.out.println(nounA + filler + nounB + filler + nounC);

Now, characters don’t really have anything to do with math, do they? Well, it turns out they too can be added with numbers. We can find the average letter:

System.out.println(('a' + 'z') / 2);

Do we get the expected result here? No, we get a number. It turns out that Java implicitly converts these chars to ints in order to do the math. But what number is associated with a character? That mapping is determined by the Unicode standard, which extends an older standard called ASCII.

Once we’ve gone to int-land, how do we get back to char-land? Perhaps this way?

char average = ('a' + 'z') / 2;
System.out.println(average);

This fails to compile. We must explicitly force the int back to a char:

char average = (char) (('a' + 'z') / 2);
System.out.println(average);

That gives us a new rule for our grammar. This operation is called casting, and it’s another kind of expression:

(type) expression

We’ll talk far more about Strings, chars, and Unicode later. I mention them now because your UshSure homework deals with chars.

We’ve also seen the arithmetic operators: +, -, *, and /. And we’ve seen how to get user input with a Scanner. Let’s do a couple more problems of calculation:

All of these problems are best solved with an operator that we probably didn’t run into in our math classes: the % or remainder operator. It’s like the / operator, but instead of giving the quotient, it gives the remainder. We read a % b as “a modulo b” or “a mod b.”

We also introduce the Math class. This class has a bunch of pre-written recipes for computing various mathematical operations. We can see what it all provides by looking at its documentation. I usually find this documentation by searching java ClassName in my favorite search engine.

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!

n kids and two teams
Oddly, n % 2 was 1
I never got picked

P.P.S.

Here’s the code we wrote together in class…

Characterizing.java

package lecture0913;

public class Characterizing {
  public static void main(String[] args) {
    char first = 'a';
    char filler = ',';
//    String filler = " no ";
    
    String nounA = "watashi";
    String nounB = "tomodachi";
    String nounC = "denwa bangoo";
    
    System.out.println(nounA + filler + nounB + filler + nounC);
  }
}

Average.java

package lecture0913;

public class Average {
  public static void main(String[] args) {
    char first = 'a';
    char last = 'z';
    char average = (char) ((first + last) / 2);
    System.out.println(average);
//    System.out.println(('a' + 'z') / 2);
  }
}

Farmer.java

package lecture0913;

import java.util.Scanner;

public class Farmer {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    
    System.out.print("Farmer, how many eggs? ");
    int nEggs = in.nextInt();
    System.out.println(nEggs % 12);
  }
}

Uncle.java

package lecture0913;

import java.util.Scanner;

public class Uncle {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    
    System.out.println("Number of pennies: ");
    int nPennies = in.nextInt();
    
    int nDollars = nPennies / 100;
    int nCents = nPennies % 100;
    
//    System.out.println("$" + nDollars + "." + nCents);
    System.out.printf("$%d.%02d", nDollars, nCents);
  }
}