LECTURE NOTES CHAPTERS 4, 9.6, AND 9.7 USES OF CLASSES - datatype - tester - application STATIC METHODS - book 9.6 - motivation - encapsulate computation that involves only numbers (which can't invoke methods) - methods that don't make sense for an object - examples from lecture? - stack trace examples - so far, instance methods operate on a particular instance of a class - static/class method: not invoked on an object - declare with the static modifier - example - call: use the name of the class instead of the name of the object - example - main must be static because there are no objects when the program starts - minimize use of static methods STATIC FIELDS - book 9.7 - aka class field - motivation - store values outside a particular object - single value that is the same for the entire class - declare - with the static modifier - always private - example - initialze when declare - example - created when class is loaded - every method of a class can access static fields of the same class - example - minimize use of static fields NUMBER TYPES - every value is either an object reference or a primitive - p. 105 table of primitive types, usages, and range of values - byte 1 byte -2^8, 2^8 - 1 - short 2 bytes -2^16, 2^16 - 1 - int 4 bytes -2^32, 2^32 - 1 - long 8 bytes -2^64, 2^64 - 1 - float 4 bytes +- 10^38, 7 sig digits - double 8 bytes +- 10^308, 15 sig digits - char 2 bytes Unicode characters - boolean 1 bit true/false - default initialization for boolean is false - generally use int for integers - overflow: result of computation exceeds range for number type - value truncated to fit - result is wrong - no warning - example - long - correct int overflow - if overflows, use BigInteger - floating point - use double b/c float has low precision - rounding error - can't always convert exactly - binary number system can't represent decimal fractions exactly - must use BigDecimal for real financial applications - casting - can assign integers to floating point values - example - cannot assign floating point values to integers - example - explicitly convert between types - only required from more precise to less precise - tell compiler that agree to information loss - synatx: () expression - (int) truncates floating point part - example - use Math.round to round numbers - static long round(double x) - static int round(float x) CONSTANTS - numerical constants - values that do not change - have special significance - use symbolic names for all values - declare a constant - final modifier - if try to change, get compile-time error - naming convention - all capital letters - use underscores to separate words - example declaration - need constant in several methods - declare with instance data - also declare static - declare public because cannot be modified - examples: double Math.PI, double Math.E - don't use numeric constants in your code w/out explanation - choose descriptive variable names: important for sharing code ASSIGNMENT OPERATORS - = - what does it do? - what is it called? - example - increment operator - ++ - increases the value of its operand by one - example - decrement operator - -- - decreases the value of its operand by one - example - combining assignment and arithmetic - a op= b - equivalent to a = a op b - example ARITHMETIC OPERATIONS AND MATHEMATICAL FUNCTIONS - addition operator? - subtraction operator? - multiplication operator? - precedence? - unary ops first - mult/div before add/sub - parentheses? (evaluated first) - division operator: / - division - works as expected if at least one operand is floating point - example - when both operands are integers, the result is a truncated integer - example - modulo: remainder of an integer division - example - x^n - Math class - static double pow(double x, doublen) - for squares, more efficient to write out - example - square root - Math class - static double sqrt(double x) - example - Math methods - p. 120 - textbook - whitespace - put spaces around all binary operators - put a space after every Java keyword CALLING STATIC METHODS - review (ask them): - what's a static method? - where is it defined? - how do we call one? - when reading other people's code, how can we tell if they're calling a static or an instance method? STRINGS - string: sequence of characters - enclosing quotation marks - used by Java to specify - not part of string - example - object - empty string - no characters - written "" - concatenation - put strings together to form a longer string - + operator - provided at least one argument is a string - forces other argument to type string - example - reduce number of System.out.print instructions - example - convert a string to an int - Integer static int parseInt(String x) - example - convert a string to a float - Double static double parseDouble(String x) - example - index - refer to a character by its position in the string - String indices start at 0 - String methods - length - number of characters in it - String int length() - example - String substring(int startPosition, int pastEnd) - returns the String - from index startPosition - up to but not including index pastEnd - example - String substring(int startPosition) - including startPosition through the end of the implicit parameter - example - the last two methods are... "overloaded" - unreasonable parameters cause program to terminate with error message - others in documentation; I may not cover READING INPUT - review - what object? - use System.in object - what class? - use java.util.Scanner class - how? - pass System.in to the Scanner constructor - methods? - int nextInt() method lets you read the next int - example - double nextDouble() method lets you read the next double - example - String nextLine() gets all of the input as a String - example - String next() gets the next word (terminated by white space) - example - example program w/out prompt -- what's wrong? - when does input go to program? - input is sent to your program when user hits Enter key