Problem of the Day Why are manhole covers round?
Problem of the Day Why are manhole covers round? No matter how circle rotated, radius is constant… …so only shape that cannot fall into the hole
Announcements Weekly Assignment #1 scores will be ed Also updated on Angel (“refresh” to get latest scores) used for first weeks, better solution soon Similar process for labs (no s for last week) Weekly assignments goal to help students learn Checks that you understand & can apply ideas in code Due on Tuesdays, but problems mapped to lectures Start early Start early enough you can get help; use time wisely
Classes vs. Objects Classes are blueprints describing data type Classes (usually) cannot do anything on their own Objects are instances of a class New objects created (instantiated) using new Fields describe state of an object Object’s behavior represented by methods
Methods Define how objects act, behave, & change Need unique name & parameters (“signature”) Can share name or parameter list, but not both Determines which code used at any method call Methods also define return type not Note that return type not part of signature Unhelpful finding method to call at any line in code
Return Type Methods must specify their return type Primitive or reference type if they will return 1 value double squareIt(float x); int[] primes(int n); Professor getFavoriteTeacher(); Use special return type, void, if no value is returned void goodSoldier(); void doSomethingWithX(int x);
return Examples
Function ends when return is executed Once we hit return, method immediately stops Calling function will resume its execution If code exists after return, method will not compile return Statement
Methods' Parameters
Are Parameters Variables? At start of method, assigned value of argument Works like normal assignment for the parameters type As would expect, primitives copied; references aliased Parameters “live” only during call & die once over Parameter placed in memory at top of frame on stack Variables also found in frame just at lower addresses
Are Parameters Variables? At start of method, assigned value of argument Works like normal assignment for the parameters type As would expect, primitives copied; references aliased Parameters “live” only during call & die once over Parameter placed in memory at top of frame on stack Variables also found in frame just at lower addresses
Locals Variables declared inside a method Must be assigned value before using “Live” only for code block in which declared Declare local in smallest enclosing block Local variable better choice than other variable Helps insure design, algorithm, & code is simple Reduces opportunities for bugs dramatically Added bonus, creates fewer comments to write!
B is for Bad public class BDuck { public static void main(String[] args) { int quackVolume; System.out.println(“Volume is ”+BDuck.getVolume()); BDuck duck = new BDuck(); quackVolume = 11; System.out.println(“Volume is ”+ getVolume()); } public static int getVolume() { return quackVolume; } }
B is for Bad public class BDuck { public static void main(String[] args) { int quackVolume; System.out.println(“Volume is ”+BDuck.getVolume()); BDuck duck = new BDuck(); quackVolume = 11; System.out.println(“Volume is ”+ getVolume()); } public static int getVolume() { return quackVolume; } } This is fine. It specifies the class in which the method is located.
B is for Bad public class BDuck { public static void main(String[] args) { int quackVolume; System.out.println(“Volume is ”+BDuck.getVolume()); BDuck duck = new BDuck(); quackVolume = 11; System.out.println(“Volume is ”+ getVolume()); } public static int getVolume() { return quackVolume; } } This is also fine, since the method is in the same class
B is for Bad public class BDuck { public static void main(String[] args) { int quackVolume; System.out.println(“Volume is ”+BDuck.getVolume()); BDuck duck = new BDuck(); quackVolume = 11; System.out.println(“Volume is ”+ getVolume()); } public static int getVolume() { return quackVolume; } } Compiler error. quackVolume is local to main() & getVolume() is separate method
B is for Bad public class BDuck { public static String duckSpeak() { return “quack”; } } public class UseDuck { public void speak() { System.out.println(BDuck.duckSpeak()); BDuck duck = new BDuck(); System.out.println(duckSpeak()); } }
B is for Bad public class BDuck { public static String duckSpeak() { return “quack”; } } public class UseDuck { public void speak() { System.out.println(BDuck.duckSpeak()); BDuck duck = new BDuck(); System.out.println(duckSpeak()); } } This works. The method is in another class, but we specify which class it is in.
B is for Bad public class BDuck { public static String duckSpeak() { return “quack”; } } public class UseDuck { public void speak() { System.out.println(BDuck.duckSpeak()); BDuck duck = new BDuck(); System.out.println(duckSpeak()); } } Compiler error here, since duckSpeak() is not defined by the UseDuck class
Constructors Special methods called to instantiate object Identical name as class; can have 0 or more params If signatures differ, multiple constructors possible No return type (not even void ) allowed must Arguments in new must match 1 constructor If no constructor defined, implicit one used for class No parameters included in implicit constructor
Tracing With Methods At top of stack add frame for every method call frame is fancy talk for box labeled with method name Inside frame should include method’s locals & params if Implicit this in frame if method not static When method ends, remove frame from stack Lightly crossing out frame easiest way of doing this Process is the same no matter how method ends Goes back to uncrossed out frame at top of stack
Method Trace public class Playa { double static avg(int hits, int pa) { int aB = 3; double retVal = hits/(double)aB; aB = pa; return retVal; } public static void main(String[] args) { Playa h8r = new Playa(); double retVal = avg(10, 100); retVal += avg(0, 10); Playa star = new Playa(); double hits = avg(60, 200); } }
Your Turn Get into your groups and complete activity
For Next Lecture Keep reviewing your Java lessons Should be getting back up-to-speed with Java basics Friday will discuss how fields in an object work How to store values between calls to a method? There are weekly assignment problem on Angel Due before 5PM on Tuesday as will be the normal case Should help finish removing rust from lazy summer WILL NOT MEET Lab WILL NOT MEET tomorrow (sorry!) Two activities will be posted & are due before next lab