CS 1: Lecture 2 – Computer as Calculator
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?
public class Greeter {
public static void main(String[] args) {
System.out.println("Hello, CS 1!");
}
}
This example demonstrates three anatomical parts that we will be discussing all semester:
- statement, which is a command telling the computer to do something
- method, which is a named sequence of commands, kind of like a recipe
- class, which for now is a group a methods, kind of like a recipe book
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:
- Computer as Calculator, in which we see how the computer crunches those numbers
- Computer as Chef, in which we see how the computer crafts its cuisine out of reusable, self-contained recipes
- Computer as Philosopher, in which we see how the computer ponders what is true
- Computer as Pilot, in which we see how the computer turns and loops through the wild blue yonder that is our code
- Computer as Factory Worker, in which we see how the computer accelerates drudgery
- Computer as Scribe, in which we see how the computer deals with persistent information
- 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:
- How many eggs does the Johnson family eat in a year?
- What’s the 27th odd number?
- What percentage of presidents have been Democrats? Republicans?
- What was my car’s most recent MPG?
- How much space in square feet does each person get in the country of _______?
- A farmer sells as many full cartons of eggs as possible, and eats the rest. How many does she eat?
- 183 days from now, something big will happen. What day of the week is that?
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, int
s and double
s:
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:
- Try writing a program for a roommate or friend. Ask them for some data and retrieve it using a
Scanner
. Compute something and show them the result. This is not required, and there’s nothing to turn in. - The lab for next Tuesday is posted. If you would like to start early, I invite you to do so. You can submit your checkpoints right away at the beginning of lab and then leave, giving the rest of us more space! No pressure, however.
- Set up your homework repository by completing Homework 0, Parts 1 and 2. Report any issues on Piazza. You may also want to read Picturing Git.
- Homework 1 will be officially assigned on Monday.
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
P.P.S.
Here’s the code we wrote together in class…
Eggsumption.java
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;
System.out.println(nEggsPerYear);
}
}
Odd.java
package lecture0908;
public class Odd {
public static void main(String[] args) {
int odd27 = 27 * 2 - 1;
System.out.println(odd27);
}
}
Leftovers.java
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;
System.out.println(nEggsLeftOver);
// int nEggsLeftover = nEggsTotal % 12;
}
}
Something.java
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;
System.out.println(dayOfBigThing);
}
}