CS 1: Lecture 4 – Mathematical Data and Operations
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 char
s to store single characters:
char first = 'A';
char filler = ',';
String
s are just sequences of char
s. In fact, we can add char
s and String
s 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 char
s to int
s 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 String
s, char
s, and Unicode later. I mention them now because your UshSure
homework deals with char
s.
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:
- A farmer sells as many cartons of a dozen eggs as she can completely fill, and eats however many eggs are left. How many eggs does the farmer get to eat?
- 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?
- Aliens have fired off a meteor that will destroy Earth X hours from now. What time of day will it hit Earth? (Use military time.)
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:
- Finish lab if you didn’t do so yesterday. Do not wait until 30 minutes before the next lab to do so.
- Check your email for a grade report that I sent out yesterday.
- Homework 1 is due before September 22. When I say before September 22, I mean I will run the SpecChecker sometime on September 22. If I wake up at 12:03 AM that day, I will run the SpecChecker then.
- You will need to pull from the template to get the homework 1 SpecChecker. The required setup and process is described in homework 0, part 2.
See you next class!
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);
}
}