teaching machines

CS 145 Lecture 2 – Hello, Java

September 9, 2016 by . Filed under cs145, fall 2016, lectures.

Dear students,

Last time we peaked into a primary activity 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 developed by Sun Microsystems. It surged to popularity because in part because it invented its own fake CPU—what they called the Java Virtual Machine. When a developer wrote code for the JVM, that developer could take that code to a Windows machine or a Macintosh or a video game console, 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.

People outside or new to the field tend to be concerned about learning the best 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.

What people focus on when they don’t know a language is called syntax. This is visual form of a language, its grammar, the rules that describe all the punctuation and special keywords and so on. 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?

  1. public class Greeter {
    
  2.   public static void main(String[] args) {
    
  3.     System.out.println("Hello, CS 145!");
    
  4.   }
    
  5. }
    

This example demonstrates three anatomical parts that we will be discussing all semester:

The statements in Java are often not that different from our own imperative statements in English:

System.out.println("Hello, CS 145!");
// ------- ------- ----------------
// 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.

Now let’s use Eclipse to write some more interesting programs that solve these problems:

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, ints and doubles:

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;

In the 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 and the mouse clicks, and the program responds. To provide for this, 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 {
  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 program has both the I and the O of IO.

That’s enough for today. 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 Haiku Friday!

Computers are gloves
Gloves that let us touch numbers
And not get thirty

P.P.S.

Here’s the code we wrote together in class…

Egger.java

package lecture0909;

public class Egger {
        public static void main(String[] args) {
                
                int nWeeksInYear = 52;
                int nCartonsPerWeek = 3;
                int nEggsPerCarton = 12;
                
                System.out.println(nWeeksInYear * nCartonsPerWeek * nEggsPerCarton);
        }
}

Odd.java

package lecture0909;

public class Odd {
        public static void main(String[] args) {
                int n = 27;
                int odd = n * 2 - 1;
                System.out.println(odd);
        }
}

CivicForTheWin.java

package lecture0909;

import java.util.Scanner;

public class CivicForTheWin {
        public static void main(String[] args) {
                Scanner in = new Scanner(System.in);
                
                //double milesA = 106987;
                //double milesB = 107306;
                //double gallons = 9.903;
                double milesA = in.nextDouble();
                double milesB = in.nextDouble();
                double gallons = in.nextDouble();
                
                double mpg = (milesB - milesA) / gallons;
                System.out.println(mpg);
        }
}