===== CS 235 - Week 2 Lecture - 2021-08-30 ===== ===== TODAY WE WILL ===== * announcements * continuing Java basics for C++ programmers * prep for next class ----- Announcements ----- * Reading: (from "Core Java" course text) * Skimming Chapters 1-3 * Chapter 3 - Section 3.10 - Arrays * Chapter 5 - Section 5.3 and Chapter 9 - Subsection 9.3.2 - ArrayList class (analogue to C++ vector class) * Chapter 3 - Subsection 3.7.3 - File input and output * CLICKER QUESTIONS for POINTS start today! * You should would be working on Homework 1 * jshell -- Java's REPL, read-evaluate-print-loop!!!!!! * jshell -- Java's REPL, read-evaluate-print-loop!!!!!! * NOTE: forgot to mention during class: you can EXIT a jshell session by typing the control key and d at the same time! (that's often "abbreviated" as ^D) ===== * Java programming language - you write Java classes, applications, servlets, and components are written * Java platform - the predefined set of Java classes that exist on every Java installation * sometimes these are called the "core Java APIs" * API - application programming interface ===== * Java is PICKY about its file names, as discussed last week! * And Java has very-accepted naming CONVENTIONS -- (style, not syntax) * class names start upper case letter, then CamelCase * often "noun"-y * method and data field andvariable names start with a lower-case letter, then camelCase * EXCEPT for CONSTRUCTOR METHODS!!! like C++, constructor method name IS the name of the class!! * method names are often "verb"-y * "classic" selectors - typically named getDesiredAttrib (like GameDie's getCurrTop) * "classic" modifiers - typically named setDesiredAttrib (we'll see setText in some graphical elements later, for example) * variable names are often "noun"-y * named constants - name like in C++ common practice, all-uppercase, with underscores MAX_SAFE_TEMP ===== * basic declarations are MOSTLY like you know from C++: desiredType varName; desiredType varName = desiredExpr; (Java also has a var option now -- cute nod to JavaScript (?), feel free to use if you'd like.) ===== * Java data types: * has eight primitive types <- variables of these types are NOT objects! These types are NOT classes! * ...and the rest are classes! * 4 of the primitive types are integer types: int short long byte * unlike C++, the ranges of these are part of the Java *language* definition * 2 are floating-point number types float double * float: 7 significant decimal digits * double: 14 (I think) significant decimal digits * double is double-precision floating point * unless you have a good reason, use double instead of float * character type char Unicode! see course text for more!! 'a' is a char, Unicode CAN add some compexity... * Boolean-type boolean <---- NOT integer-based...! ===== NOTE this! it is an ERROR to treat a boolean like an int in Java! ===== boolean literals in Java: true false a separate and non-numeric type! can't set a boolean variable to an int, control structures that expect a boolean expression will not accept a non-boolean expression in its place! ===== * reminder: named constants use final instead of const in their declarations, and typically make it static (one copy only) when it is a data field named constant (and you decide if it is visible to the user-world by making it either public or private) ===== * operators -- a LOT of overlap between C++ and Java + - * / % modulo ++ -- += -= *= /= && short-circuit boolean and [NOT the keyword and ] || short-circuit boolen or [NOT the keyword or ] == and != also -- BUT!!! == for primitive type values is what you expect -- do they have equivalent values? == for OBJECTS (any instance of a class), you need to be CAREFUL; == for objects is asking if the references of those two objects are the same -- do they reference the same OBJECT in memory; ==== ASIDE - FUN FACT - * in the jshell, you can type in a class to use it! if it exists in the current directory, you can bring in its code using /open and the source code file name If I have GameDie.java in the directory where I started jshell, then /open GameDie.java ...and now I can use GameDie in that jshell ===== ...SO many classes -- if you would like to test if two instances are EQUIVALENT in value -- declare an equals method String has this! "die".equals("die") ===== * a public static member of class -- either a data field or method -- can be accessed in a number of ways, the most classic is to precede it by the name of the class it is a member of; So, the Math class has a method sqrt that is static, and I can call it using Math.sqrt(34) ===== * operator precedence chart for Java is Table 3.4 on p. 61 of course text ---but use parentheses to make operation meanings clear, please!! ===== * String class in Java * yes, "double quoted thing" IS a String OBJECT * String has MANY methods! see the Java API, class String * weird bit: String instances are IMMUTABLE once you have created a String instance, you CANNOT change what is in it! * this is amazingly not a problem that often -- when it is, use a StringBuffer instead of a String ===== Simple I/O!!! * we've seen System.out.println(a_str_expr); * here's some simple interactive input: Scanner is a class in package java.util (sometimes to use a java.util class, you need to import it: import java.util.*; // imports all the classes in that package ) // attach a Scanner object to standard input Scanner myInny =- new Scanner(System.in); // to read something in, of a given type, // you use the appropriate next method -- // nextLine nextInt nextDouble and more! ===== * Arrays in Java! an array in Java IS AN OBJECT. so it has the ability to remember its size!!!!! int[] a; // a is a reference to an int array int b[]; // b " a = new int[100]; // a now references an int array object of size 100 b = new int[3]; // b " 3 * also note: you can use the length DATA FIELD to get the number of elements in an array in jshell: a.length == 100 b.length == 3 * you CANNOT reference outside of an array's bounds in Java -- you'll get an Exception! ===== * what, you want something like a C++ vector in Java? Try the Java ArrayList class! ArrayList<desiredType> name; ArrayList<desiredType> name = new ArrayList<desiredType>(); ArrayList has methods such as: add size get remove