Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 5 Part 1 COSI 11a 2006-11-06 Prepared by Ross Shaull.

Similar presentations


Presentation on theme: "Chapter 5 Part 1 COSI 11a 2006-11-06 Prepared by Ross Shaull."— Presentation transcript:

1 Chapter 5 Part 1 COSI 11a 2006-11-06 Prepared by Ross Shaull

2 Objectives for the Week Review methods and parameters Finish Chapter 5 –Constructors –Packages –Testing and the top-down/bottom-up techniques Write some interesting code together this week

3 Methods A method is an action that a class can take. They can be static methods or object methods. Who remembers the difference?

4 Method Parameters A parameter is like a variable declared in the body of a method, but you decide what value it gets by passing a value into the parameter when you call the method. This is on page 246.

5 Method Parameter Example public class TestClass { public doStuff(String s) { System.out.println(s.toUpperCase()) } public TestClassDriver { public static void main(String[] args) { TestClass testClass = new TestClass(); testClass.doStuff( ); } “hello world!”

6 Method Parameter Example public class TestClass { public doStuff(String s) { String s = ; System.out.println(s.toUpperCase()) } public TestClassDriver { public static void main(String[] args) { TestClass testClass = new TestClass(); testClass.doStuff( ); } “hello world!” “hello world!” Don’t actually write this, it is just an Illustration Behind the scenes, this local variable is created for you

7 Static Methods Methods that don’t have access to instance variables. Call them directly from class (i.e. Math.round). Write them when you want some functionality to be available without instantiating an object.

8 Constructors We’ve seen them in two recitations What does one look like? Notice: no return type! public ClassName(type1 param1, …, type2 param2) { // initialize stuff here in // body of constructor }

9 Constructors are methods? Special methods with no return type! You can’t call them directly, only with the “new” operator. But, they do have parameters like other methods. Also like other methods, they can be overloaded (who remembers what that means?)

10 Default Constructors 1 Simply a constructor with no parameters. Useful for specifying default values for instance variables.

11 Default Constructors 2 Sometimes there are no reasonable defaults for an instance variable, in that case don’t include a default parameter. It’s okay to omit the default constructor if you feel that your class requires parameters to the constructor. Example: a Person class; are there instance variables that wouldn’t have a sensible default?

12 Default Constructors 3 What will happen if someone tries to construct an instance of your class using a constructor with no arguments, AND you have no default constructor? See page 379.

13 Calling a constructor Scanner s = new Scanner(System.in); String s = “Hello World!”; Counter counter = new Counter();

14 Different from an initializer method? Remember when we wrote initializers on quizzes and the midterm? Is a constructor different from one of these methods? Yes, an initializer (or setter) is not called automatically, so you must always remember to call it after instantiation, making your class less safe and more difficult to use.

15 Code Interlude A return to the Counter class

16 Gotcha: Information Leakage Objects are always passed/returned by reference. If your class has an instance variable of non- primitive type, then returning it from a getter will allow users of your class to modify a private variable. Who can explain why? Who can tell me why this won’t matter with String objects?

17 Gotcha: Information Leakage Private instance variables that are of object type (instead of primitive type) are stored as references, so returning the reference out of a getter method allows a malicious user to make unauthorized changes inside your object. String objects are an exception to this rule because they are immutable (this means literally that they have no mutator methods, you cannot change the contents of a String, only create new Strings.

18 Another Code Interlude The CadetClass example from page 381.

19 Code Conclusion Lets start writing a word guessing game It will play a bit like “hangman” We will write it together today, Wednesday, and Thursday It will use many of the techniques and tools we’ve been learning up to now, including: –String manipulation –Loops and branching –Multiple interacting classes –Methods with parameters –Static methods –Constructors –Bottom-up and top-down testing techniques


Download ppt "Chapter 5 Part 1 COSI 11a 2006-11-06 Prepared by Ross Shaull."

Similar presentations


Ads by Google