Download presentation
Presentation is loading. Please wait.
Published byFlorence Barrett Modified over 8 years ago
1
Problem of the Day Why are manhole covers round?
2
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
4
Announcements Weekly Assignment #1 scores will be emailed Also updated on Angel (“refresh” to get latest scores) E-mail used for first weeks, better solution soon Similar process for labs (no e-mails 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
5
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
6
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
7
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);
8
return Examples
14
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
15
Methods' Parameters
16
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
17
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
18
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!
19
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; } }
20
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.
21
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
22
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
23
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()); } }
24
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.
25
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
26
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
27
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
28
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); } }
29
Your Turn Get into your groups and complete activity
30
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.