// SLIGHTLY-CLEANED-UP version of DrJava Interactions
//    window from CS 235 - Week 2 Lecture, 2015-08-31

Welcome to DrJava.  Working directory is /Users/smtuttle/humboldt/f15cs235/235lectures/235lect02

> // Java programming language - the language
> //   in which Java classes, applications, etc.
> //   are written

> // Java platform - predefined set of Java classes
> //    that exist on a typical Java installation
> //    and are available for use in Java programs
> 
> //    (these predefined packages and classes
> //    are sometimes called the "core Java APIs"
> //    or the "Java Runtime Environment" (JRE))
> 
> // API - application programming interface

> // reminder: Java is VERY object-oriented,
> //     ALL Java code must be within classes
> //     or class-like "things"

// NOTE -- we opened GameDie.java in DrJava, and
//     compiled it -- turned out we had to use the
//     Compile button, couldn't run from Interactions:

> javac GameDie.java
Invalid top level statement

// but after pushed DrJava Compile button:

> GameDie die;
> die
null

> die = new GameDie(3);
> die
GameDie@74803f78

> // more differences between C++ and Java :
> // Java CARES about names of files containing
> //       code, and C++ does not
  // e.g.: a Java source code file must contain exactly
  //       one public class, e.g. MyClass, and
  //       be named exactly that class' name with
  //       a suffix of .java, e.g., MyClass.java
  //
  // and: when compiled, the resulting bytecode
  //      will be (and must be) in a file named
  //      the class name with a suffix of .class,
  //      e.g., MyClass.class
  //     
  // (but, as we'll see, you can also have NON-public
  //     classes along with the one public class in
  //     a file, and each of them will also result in
  //     a .class file with a particular name...)

> // here are some of Java's naming standards (style!):
> // *   class names start with an uppercase letter,
> //     are written in CamelCase, and are often "noun-y"
> // *   method and variable names should start with a
> //     lowercase letter, written in a camelCase
> //     *   method names are often "verb-y"
> //     *   accessors/getters in Java often start
> //         with get -- getNumSides, getNumRolls
> //     *   modifiers/setters in Java often start
> //         with set -- setFont, setSize
> 

// declaring and instantiating variables of primitive types 
//     is very similar to C++:

> int val;
> val = 576;
> val
576

// BUT: every *object* variable in Java is actually a
//      reference -- Java's word for a "polite" pointer!
// SO an object variable might not reference
//      ANY object at some period in time!

> GameDie billy;
> billy
null

> billy = new GameDie(val);
> billy
GameDie@351b45b3

> billy.getNumSides()
576

> billy.roll()
308
> billy.roll()
12
> billy.roll()
300
> billy.roll()
555
> billy.roll()
30
> billy.roll()
199
> billy.roll()
318

> billy.getNumSides()
576
> billy.getNumRolls()
7

// like C++, Java is strongly-typed -- must DECLARE a
//     name before you use it;
// Java's identifier rules are pretty similar to
//     C++'s -- must start with a letter, followed by
//     0 or more letters, digits, or underscore --
//     BUT any Unicode character that counts as a letter
//     is considered a letter, and likewise for digits

//     ...and you can't use a keyword as an identifier!

> num
Static Error: Undefined name 'num'
> double if;
Incomplete expression

> // Java has 8 primitive data types:
> //    integer types:  int short long byte
> //    floating point types:   double float
> //    and char
> //    and boolean
> 
> // (but there is a corresponding wrapper
> //  class for each primitive type,
> //  and sometimes you need to know that...)
> //  ...and sometimes Java "autoboxes" primitive instances
> //     in wrapper class instances...!
> 
> // class coding standard:
> //     *   for floating point values, use double
> //         unless you have a GOOD reason
> //     *   it is fine to just use int for integers
> //         unless space is tight/or numbers might get
> //         BIG...
> 
> // char - stored as Unicode in Java,
> //    but one of of ways to express char literals is like
> //    C++, in single quotes:
> 
> char initial;
> initial = 'T';
> initial
'T'

> // boolean -- REALLY not an int type in Java!
> //     two boolean literals: true false
> 
> boolean flag;
> flag = true;
> flag
true

> flag = false;
> flag
false

// Java will NOT let you treat a boolean as an int!!

> flag = 1;
Static Error: Bad types in assignment: from int to boolean

> flag = 0;
Static Error: Bad types in assignment: from int to boolean

> flag == false
true

> flag != false
false

> flag == 1
Static Error: Illegal comparison: cannot compare a boolean to a int

> // in Java, it is considered good style to
> //    initialize data fields in the constructor methods
> // in Java, it is a syntax error to not initialize local
> //    variables before they are used!

// (demo'd this by temporarily adding a local variable
//     to GameDie's roll method  and trying to print it to
//     standard output -- inDrJava, that's the Console -- without
//     initializing it first;
// ...and DID get a syntax error;)

// (then initialized that local variable to 785,
//     and showed the effect:)

> GameDie die = new GameDie(4);
> die.roll()
785
1

// Java's operators are quite similar to C++,
//     and includes ++ += etc.

> int quant;
> quant++
0

> quant
1

> ++quant
2

> quant
2

// (now removed that local variable and its being printed
//     from GameDie...!)

> GameDie die = new GameDie(13);
> die.roll()
13

> // named constants in Java are declared using the
> //    keyword final -- (once set, that's their FINAL value)
> // it is also USUALLY the case that named constants are
> //    also declared as static -- one copy no matter how
> //    many class instances are declared;
> 
> //    (class style: if you declare a named constant
> //         NOT static, you'd better have a good,
> //         commented reason why!!!)

// for example:
//     public static final double PI = 3.14159;

// ^ can't demo this in Interactions window --
//     so temporarily added it to GameDie;

> GameDie.PI
3.14159

> // I am removing PI from GameDie now...

// Math class has many public static final named constants!
//    ONE way to express these (anything static) -- with
//    the class name, a period, then that static thing;

> Math.PI
3.141592653589793

> Math.E
2.718281828459045
> 

> // basic operators are a lot like C++
> //   + - * / && || ++ -- 
> //   (and does include modulo operator %)

> 53 % 5
3

> // also == != < > <= >=
> 
> // BUT: you cannot overload operators in Java;

> // AND: be careful, == and != are comparing REFERENCES
> //    in Java, NOT equivalent values, for OBJECTS!!!

> 3 == 3
true

// yes, arithmetic operators are overloaded in Java as in C++ --
//     if both operators are int, you get the int version of
//     that operation:

> (1/3) == 0
true

> 1/3
0

> 1.0/3
0.3333333333333333

> String name = "Clara";
> name == "Clara"
false

// ...why? Because name's "Clara" is a DIFFERENT
//    String object in memory than the "Clara"
//    unnamed String object literal it is being compared to!

> // to compare OBJECTS for equivalent content,
> //    use that object's equals method

> name.equals("Clara")
true

> // really, == for objects means,
> //    "do these objects reference the same memory?"
>
  //     ...and, yes, two Java object variables CAN reference
  //     the same object; aliasing IS possible!

> String moniker = name;
> moniker == name
true
>