===== CS 235 - Week 1 Lab - 2021-08-27 ===== ----- TODAY WE WILL: ----- * announcements * compare/contrast #1: hello world in C++ and then Java (lab exercise part 1) * compare/contrast #2: GameDie in C++ and then Java (if time, lab exercise part 2) * prep for next class ===== * please register your Turning license in Canvas by Monday, August 30th! (clicker questions for points will start then) * watch for a class email for Homework 1 is posted * you should be reading: * course syllabus * Core Java text - skim Chapters 1, 2, 3 ===== * POINT: in Java, all code is within a class. * BUT: if a Java class has a method whose header is: public static void main(String[] args) * then that Java class is a JAVA APPLICATION and when you execute that Java application using the java command, it starts execution by executing the main method ===== * POINT: in Java, the names of the files containing code MATTER. * thing 1: Java source code files have suffix .java * thing 2: compiled Java bytecode files have suffix .class * thing 3: a Java source code file can ONLY have ONE class whose visibility is public, it must have at least one such class, and the name of the file MUST be EXACTLY that public class' name, with a .java suffix so, your public class Moo? its source code file MUST be Moo.java * thing 4: CONVENTION! class names shalt start with an uppercase letter, and use CamelCase for "words" within MooBaaLaLaLa (not Moo_baa_la_la_la or mooBaaLaLaLa etc.) ===== * Java has the same comments as C++ // single line /* moo moo moo */ BUT... Java also has a cool related tool named javadoc IF, directly before a class and/or a method, you put a comment using: /** describe the class or method @author Sharon @version 2021-08-27 */ ...then the command javadoc MyClass.java will build fancy HTML doc for your code! * @param paramName param description is good for each parameter in a method * also: @return description of returned value ===== * in Java, put private or public in front of your class header, then class then the class name public class Hello ===== * in Java, one way to print to the screen is to call : System.out.println(aStringExpr); (this ends with a newline, System.out.print(aStringExpr) does NOT) ===== * in Java, compile Java source code to Java bytecode javac DesiredClassName.java // file name with the source code * if your Java class is a Java application (has that main method), java DesiredApplicClassName // class's name, not a file name - no suffix ===== MORE JAVA NAMING CONVENTIONS: * method names (except for constructors) and also variables should start with a lower case letter!!!! and in camelCase after that * named constants are named as in C++: all-uppercase with underscores! ===== NOTE about Java named constants: * use final to indicate an identifier is a constant (so it cannot be changed after it is set * use static if you just want a single instance of that constant for the class (um, you almost always do!!) * and it is you design choice whether it is public or private (is your class providing this constant to the world? Make it public, otherwise make it private) ===== * a class can have have static methods BESIDES main; the classic way to call such a method is to precede it with the class name instead of an object expression The Math class in Java has a static method random that expects no arguments and returns a pseudo-random value in [0, 1) Math.random() // is a call of this static method ===== * (int) will cast the following expression to an int version (double) will cast it to double, etc. (for primitive numeric types at least) ===== * in Java, a public class in the same folder can be seen by used by all other classes in that folder (and packages can be used to extend visibility further) ===== * you call Java constructors a bit differently than C++ -- every Java object variable is a reference to either null or an object of its type GameDie myDie; // myDie's value is actually null right now! GameDie myDie = new GameDie(); // now myDie references a newly-created // (yup, and allocated) 6-sided // GameDie object GameDie anotherDie = new GameDie(12); // and it is fine to do in // 1 statement