// SLIGHTLY-CLEANED-UP (and partially reconstructed!) // version of DrJava Interactions // window from CS 235 - Week 2 Lab, 2015-09-02 Welcome to DrJava. Working directory is /Users/smtuttle/humboldt/f15cs235/235labs/235lab02 > double value = 3; > value 3.0 > int quant = 3.1; Static Error: Bad types in assignment: from double to int > // you CAN often cast numeric values (and sometimes > // others) using the syntax: > // > // (desiredType) expression > int quant = (int) 3.1; > quant 3 // NOTE that casting TRUNCATES the fractional part rather // than rounding it... > quant = (int) 3.9; > quant 3 // ...you CAN use Math class's round function to get a // rounded result instead, and cast that to an int; > quant = (int) Math.round(3.9); > quant 4 // remember: CANNOT cast a boolean to an int! > boolean flag = true; > flag true > flag = (boolean) 0; Static Error: Bad types in cast: from int to boolean > // as of Java 5, Java has enumerated types (as does C++) > enum Size { SMALL, MEDIUM, LARGE, X_LARGE }; > Size myShirt = Size.MEDIUM; > myShirt MEDIUM > myShirt = null; > new String("George") "George" > "George" "George" > // String is a class in package java.lang > // with MANY lovely methods! > // and String instances are IMMUTABLE -- > // they CANNOT be changed! > // (but a String variable can reference a new String > // object, no problem!!) > // (see StringBuilder, since Java 5, for the preferred > // mutable-Stringlike being...) > String moniker; > moniker null > moniker = "DJ"; > moniker = "DJ" + " is in CS 235"; > // here, moniker is referencing a NEW String instance > // that is the result of concatenating these two Strings > moniker "DJ is in CS 235" > moniker.substring(0, 2) "DJ" > moniker "DJ is in CS 235" // remember that + is overridden such that if EITHER of // its operators is of type String, it does string // concatenation instead of numeric addition > 3 + 5 8 > 3 + "5" "35" // System.out's method println ends printing out a newline, and // method print does not! > System.out.println("Moo"); Moo > System.out.print("Moo"); Moo> System.out.println(3); 3 // playing with standard INPUT -- // we'd like to use packet java.util's Scanner // class, WITHOUT having to call it java.util.Scanner: > import java.util.*; > Scanner in = new Scanner(System.in); > System.out.println("What is your name?"); What is your name? > String name = in.nextLine(); [DrJava Input Box] // we typed in Thomas > name "Thomas" > int value = in.nextInt(); [DrJava Input Box] // we typed in 7 > value 7 > value = in.nextInt(); [DrJava Input Box] // we typed in moo java.util.InputMismatchException at java.util.Scanner.throwFor(Scanner.java:864) at java.util.Scanner.next(Scanner.java:1485) at java.util.Scanner.nextInt(Scanner.java:2117) at java.util.Scanner.nextInt(Scanner.java:2076) // Java now has a C-style printf method, for formatted output -- // see course text Chapter 3 for more details on this! > System.out.printf("%.3f", 3.1); 3.100> System.out.printf("%.3f\n", 3.456789); 3.457 // Java's "classic" control structures are MUCH like those in // C++, EXCEPT conditions MUST be true-boolean expressions! > if (true) System.out.println("Moo"); Moo > if (1) System.out.println("Moo"); Static Error: Condition must be boolean > int x = 3; > while (x < 6) { System.out.println(x); x++; } 3 4 5 > for (int val=7; val < 10; val++) { System.out.println(val); } 7 8 9 > // And the Java switch is a lot like C++'s, > // except you CAN switch on String *literals* > // starting with Java 7 > // (so: ints, enumerated types, chars, and String literals; > // I *think* that's the list...) // arrays are fairly similar to those in C++, // except they ARE objects, with a lovely // (public?!) length data field giving the // number of elements in that array -- useful!! > int[] weights; > weights null > weights = new int[5]; > weights { 0, 0, 0, 0, 0 } > GameDie[] rolls; > rolls null > rolls = new GameDie[4]; > rolls { null, null, null, null } > rolls[0] = new GameDie(12); > rolls { GameDie@e42572f, null, null, null } > rolls[1] = new GameDie(6); > rolls { GameDie@e42572f, GameDie@47de7053, null, null } > rolls[0].roll() 9 > rolls[0].roll() 2 > rolls[0].getNumRolls() 2 // in Java, thou shalt NOT go outside an array's bounds! > rolls[4] java.lang.ArrayIndexOutOfBoundsException > weights { 0, 0, 0, 0, 0 } > // again -- NOTE every array has its size in its length > // data field (a RARE public data field!) > for (int i=0; i < weights.length; i++) { weights[i] = 7; } > weights { 7, 7, 7, 7, 7 } > // enhanced for loop: > // for (variable: collection) > // statement > for (int element: weights) { System.out.println(element); } 7 7 7 7 7 // remember: DON'T call a method on a null reference...! > GameDie quad; > quad.roll() java.lang.NullPointerException > quad = new GameDie(4); > quad.roll() 4 // [now we wrote TryMe.java, and played with it, // with BOTH styles of for-loops!] > java TryMe moo oink baa moo oink baa > java TryMe moo baa oink moo baa oink > java TryMe // and now we wrote TryMe2.java, and played with IT; > java TryMe2 moo moo > java TryMe2 moo 2 3.4 moo 2 3.4 > java TryMe2 "moo baa" oink moo baa oink // Integer and Double wrapper classes have lovely methods // for trying to parse *appropriate* String instances // into int, double values! > Integer.parseInt("34") 34 > Double.parseDouble("3.4") 3.4 > Integer.parseInt("moo") java.lang.NumberFormatException: For input string: "moo" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.lang.Integer.parseInt(Integer.java:580) at java.lang.Integer.parseInt(Integer.java:615) >