teaching machines

CS 145 Lecture 3 – Hello Again, Java

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

Dear students,

We start today with a little segment I like to call What Does This Do?. I show you a short snippet of code, you slurp it into your brain, swish it around a few times, and figure out what it does. The task would be much easier if I picked good meaningful names for the variables, but I haven’t. Here we go.

WDTD #1
import java.util.Scanner;

public class Foo1 {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    double flarg = in.nextDouble(); 
    double blop = in.nextDouble(); 
    double nerb = flarg * blop / 2.0;
    System.out.println(nerb);
  } 
}
WDTD #2
import java.util.Scanner;

public class Foo2 {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    double a = in.nextDouble(); 
    double b = in.nextDouble(); 
    double c = a + (b - a) * 0.5;
    System.out.println(c);
  } 
}
WDTD #3
import java.util.Scanner;

public class Foo3 {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int n = in.nextInt();
    n = n / 10 * 10;
    System.out.println(n);
  } 
}

In case you’re wondering, I like little games like this and we will play them often. I use them because some of the most enjoyable instruction I’ve ever experienced was from Mrs. Lee, my high school Spanish teacher. She knew that learning a language required a lot of practice, and she got us practicing by having us play games in her class—all the time.

This weekend I was trying to get my one-year-old to talk more. With my first child, I went overboard. One of his first words, when he was just nine months old, was Nicaragua. That kid will resent me soon. So, I’m going slower with my fourth. When I think about what words he should know first, I think body parts. Why? Because kids are always getting hurt or feeling sick, and I need them to be able to communicate about what ails them. So it is with you. You’ve got to know the body parts of our program, so that you can tell me about what ails you.

Let’s just jot down some notes about the grammar of Java that we’ve seen so far:

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

Last time we ended after seeing how to get user input. However, the user experience was a little sad. The computer was sitting there waiting for input, but the user had no indication of that. We must add a prompt? But where and how?

Someone asked on a quarter sheet if there were other ways to study the material for this course. There are many. The one I recommend the most, because it accompanies the textbook, is .

These “texty” programs probably don’t resemble what you know as programs. As we have time this semester, we may introduce graphical user interfaces with windows and buttons. These things build off of the elements that we are discussing now.

Now, let’s solve some problems!

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

See you next class!

Sincerely,

P.S.

Here’s the code we wrote together in class…

Electronizer.java

package lecture0912;

import java.util.Scanner;

public class Electronizer {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    
    System.out.print("Enter atomic number: ");
    int atomicNumber = in.nextInt();
    
    System.out.print("Number of electrons: ");
    System.out.println(atomicNumber);
  }
}

EggCounter.java

package lecture0912;

import java.util.Scanner;

public class EggCounter {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    
    System.out.print("Hey, Farmer S*****. How many eggs did you collect? ");
    int nEggs = in.nextInt();
    int nCartons = nEggs / 12;
    int nLeftover = nEggs % 12;
    
    System.out.println("Leftover: " + nLeftover);
  }
}

SomethingBig.java

package lecture0912;

import java.util.Scanner;

public class SomethingBig {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    
    System.out.print("What day is it? 0-6? ");
    int dayOfWeek = in.nextInt();
    
    System.out.print("How many days away? ");
    int offset = in.nextInt();
    
    int target = (dayOfWeek + offset) % 7;
    System.out.println("Target: " + target);
  }
}

Presidents.java

package lecture0912;

public class Presidents {
  public static void main(String[] args) {
    int nPresidents = 42;
    int nRepublicans = 18;
    int nDemocrats = 15;
    
    System.out.println(nRepublicans / (double) nPresidents);
  }
}