LECTURE NOTES CHAPTER 3 STANDARD INPUT - standard output - System.out.println() - System.out is a PrintStream object - calling it's println() method - use Scanner class - package: java.util.Scanner - constructor documentation: Scanner(InputStream source) Constructs a new Scanner that produces values scanned from the specified input stream. - included object System.in is a InputStream object - useful methods for getting input String next() Finds and returns the next complete token from this scanner. int nextInt(int radix) Scans the next token of the input as an int. float nextFloat() Scans the next token of the input as a float. - must put "throws Exception" in header of any method that uses - first prompt user with System.out.println() TAXI PROGRAM - program to calculate cost of cab trip - need to know cab number, cost per mile, distance to destination - problems - if want to calculate another trip, must duplicate code - must make changes in multiple places - more error prone - doesn't follow "single task object" principle - input, sets variables, performs calculations, and output - no data hiding (encapsulation) CLASS DECLARATION class { } - class defines what data and methods belong to this object - every program is made up of at least one class - naming convention - first character capital letter - camel case - modifiers: public - access specifier - always use for now - means any other programmer can use this class INSTANCE DATA ; - storage location present in every object of the class - access specifier: private - always for now - encapsulation - no other programmer can directly access this data - explain dot notation - force access through methods for security - every object of a class has its own set of instance fields OBJECT REFERENCES HERE METHODS () { } - naming convention: same as variable identifiers - modifiers: access specifier: always public for now - return type - value that method evaluates to - primitive - name of a class - void if none - parameters , , ... , - customize method - zero or more - comma delimited list - body: statements executed when method is called CONTROL FLOW AND METHOD CALLS - program control always begins with the first line of the main method - main class: only one class in a program can have a main method - call stack remembers what method we're on and where to go back to - steps for calling a method - new frame allocated on call stack - execute method sequentially until reach end bracket - deallocate frame - resume where left off - ControlFlow/MethodCalls example ControlFlow main 11 MethodCalls method2 L51 ControlFlow main 13 ControlFlow method1 L4 ControlFlow method2 L5 ControlFlow main 15 MethodCalls method1 L47 ControlFlow main 17 CONSTRUCTORS - special kind of method - initialize the fields of an object - can be overloaded just like regular methods - what happens when called - new object of that class is created - instance data is allocated - follows statements (to initialize fields) - returns (evaluates to) an object reference - default initialization of instance data - 0 for all numbers - null for object references - not considered an error by compiler - stylistically, should always do manually in constructor TAXI TRIP 2 - which goals did this accomplish? - modularity - more reuseable - encapsulation - what is still missing? - single task object principle CURRENCY CONVERTER CLASS - write main class to use - given what main class expects, write skeleton for currency converter METHODS, PARAMETERS, AND RETURN TYPES - return statement - syntax: return ; - method will evaluate to this - e.g. public int returnOne() { return 1; } - what happens when a method requiring a parameter is called - new frame pushed onto call stack - space for parameters allocated - arguments copied into parameters - remember, this means sharing for objects - execution - begins at first line inside method - continues sequentially - when reach end bracket or return statement - parameters deallocated - pop frame from call stack - return to where method called in caller - e.g. public int returnInt(int intToReturn) { return intToReturn; } - implement CurrencyConverter LOCAL VARIABLES - allocation occurs when variable is declared - must initialize manually - primitive lasts as long as the method it was declared in exists - object lasts as long as a reference to it exists - e.g. add fee to CurrencyConverter public double fromDollar(double dollar) { double amount, fee; fee = exchangeRate - feeRate; amount = dollar * fee; return amount; } - go through call stack again - show multiple objects doing "same" thing w/ dif args THIS - wherever there is an implicit parameter, use reserved word "this" to refer to it explicitly - compiler automatically does, which is why we can omit it - can be used to clarify naming issues: - compiler first assumes refering to local variable/parameter - use this to specify instance data - add constructor taking exchangeRate to CurrencyConverter public CurrencyConverter(double exchangeRate) { this.exchangeRate = exchangeRate; } - can also refer to other constructors of the same class as "this" from within the class - must be first line in constructor - e.g. String currency; public CurrencyConverter() { exchangeRate = 1; } public CurrencyConverter(String currency) { this(); this.currency = currency; } - have both b/c if don't include default, compiler doesn't include, b/c we have a constructor COMMENTING THE PUBLIC INTERFACE - use Javadoc program to automatically turn comments into HTML documentation - comments must start with /** - write method skeleton, then do Javadoc comments -- Eclipse fills in for you! - provide for public interface - describe purpose: first sentence included in summary - class definition - @author - all methods - @param - @return - look at CurrencyConverter documenation WORKING WITH OBJECTS AS PARAMETERS AND RETURN TYPES - e.g. passing an object as a parameter - e.g. creating an object as a local variable and returning it - Weight, Pet, Kennel, TestKennel