******************************************************************************* 0) Last time- numerical types, conversion, operators, expressions 1) Administrative - hw back - codelab ch3 due next week - hw: read 4.4 and 4.7, next person presents 2) Split in groups and go over 3.4-3.7 3) Highlights of material A) String: a class name (class names are capitalized by convention) * strings are sequences of characters * there are string literals, but no primitive type for strings - class is like a wrapper for a literal, with helpful methods * constructed like any object: String s = new String("Frodo"); - there's a shortcut form: String s = "Frodo"; (only strings) * when you "add" strings, it's really concatenation - s + " Baggins" -> "Frodo Baggins" * wrapper classes - parallel wrappers for primitive types - Integer, Long, Double, Float (all very similar) - can create an object to wrap: Integer i = new Integer(5); - can just use static methods: int x= Integer.parseInt("26"); B) String/number conversion * when's the only time it happens automatically? - concatenation: "test" + 1 -> "test1" - why is "test" + 1 + 2 -> "test12"? how do you get "test3"? * explicit number->string: String s = Integer.toString(5); -> "5" - how could you force it w/o the call? s = "" + 5; * explicit string->number: int i = Integer.parseInt("5"); -> 5 - what if you send the wrong kind of parameter? runtime error * use JOptionPane to input a double with prompt "Dollars: " String input = JOptionPane.showInputDialog(null,"Dollars: "); double c = Double.parseDouble(input); * write it without the intermediate String variable, in one line double c= Double.parseDouble(JOptionPane.showInputDialog(..); * if there are 1.21 Euros/dollar, output "Euros: " and the amount JOptionPane.showMessageDialog(null, "Euros: "+1.21*c); C) Number formatting: library class DecimalFormat * above: 1.98*1.21 -> 2.3958, will print with all four places * write the code to print with only two DecimalFormat df = new DecimalFormat("0.00"); JOptionPane.showMessageDialog(null,"Euros: "+df.format(1.21*c)); * making an object, passing constructor a parameter, calling method D) Standard output: what you've seen already * anatomy of the call "System.out.print();" - class name (capitalized) - static data member of type PrintStream (prints to black) - method call on that object * difference b/w print() and println()? * how can you print "They say "Hello"" on the screen? System.out.print("They say \"Hello\""); * escape characters: \n, \t, \", \', \\ E) Standard input: complicated, but better to look at than JOptionPanes * requires classes in the package "java.io", so import? java.io.*; * what type of object reads a line? BufferedReader BufferedReader buf = new BufferedReader(...); * what does it need as a parameter? object of InputStreamReader insert: (new InputStreamReader(...)) * and what does THAT need? System.in (static InputStream object) insert: (System.in) * it's a big mess, so get the idea and then just copy and paste it * now how do we read a line? String s = buf.readLine(); - no prompt, as in the window, so probably print one first * what could go wrong with this call? - IOException: put "throws IOException" in method header F) Math class: library class with helpful functions, all static * call with just the class name, w/o making a Math object * look at methods on pg 114 * write code to calculate x = quadratic equation, given a, b, c double x = (-b + Math.sqrt(Math.pow(b,2) - 4*a*c)) / 2 / a; * write code to generate a random int r between 0 and 9 int r = Math.floor(Math.random() * 10); 4) Exercise: write a main class with just the main method - prompt for user's height in inches - convert it to centimeters (2.54 cm/in) and round off (don't truncate) - print out a message with the converted value * type this on the overhead and run it, edit if necessary * go over commenting and blocking of the code 5) Summary - string/number conversions - number formatting - standard input and output - using Math class *******************************************************************************