Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 COMP 110 Static Methods and Variables Tabitha Peck M.S. March 24, 2008 MWF 3-3:50 pm Philips 367.

Similar presentations


Presentation on theme: "1 COMP 110 Static Methods and Variables Tabitha Peck M.S. March 24, 2008 MWF 3-3:50 pm Philips 367."— Presentation transcript:

1 1 COMP 110 Static Methods and Variables Tabitha Peck M.S. March 24, 2008 MWF 3-3:50 pm Philips 367

2 2 Announcements No Homework… yet

3 3 Questions?

4 4 Today in COMP 110 Static Methods A little review

5 5 Methods Actions performed by objects –smiley.drawFace(); –this.setColor(“green”); –circumference? –area?

6 6 Static Methods Methods that do not require an object of any kind –Where have you seen static before? –Invoke method with class name –SmileyClass.circumference(3);

7 7 Why no object? What data is in the object? When should you create a static method?

8 8 Writing a static method Add static to the heading public static final double PI = 3.14159; public static double circumference(double radius) { return (PI*(radius + radius)); }

9 9 Writing a static method Cannot do anything that refers to a calling object –Why? public static final double PI = 3.14159; private boolean smile = true; public static double circumference(double radius) { smile = false; return (PI*(radius + radius)); } NO

10 10 Accessing instance variables or methods Create an object public static final double PI = 3.14159; private boolean smile = true; public static double circumference(double radius) { Smiley s = new Smiley(); s.drawMouth(false); return (PI*(radius + radius)); }

11 11 Self-Test Questions Can you invoke a nonstatic method within a static method? Can you invoke a static method within a nonstatic method? Can you reference an instance variable within a static method?

12 12 The Math Class All methods in this class are static –Don’t create a Math object –Invoke methods using class name Math Math.abs Math.max Math.min Math.round –Predefined constants Math.PI Math.E

13 13 Math.round(2.3) Math.round(2.7) Math.floor(2.3) Math.floor(2.7) Math.ceil(2.3) Math.ceil(2.7)

14 14 Type Casting with Math Ceil returns a double –Math.ceil(5.6) return 6.0 What do you do if you want an int? –int num = (int)Math.ceil(5.6);

15 15 Primitive vs. Class Types Primitive –int, float, char, double –Stored in memory as value Class –String, classes you write –Stored in memory as address

16 16 Objects in Memory 5 3.14159 2078 1056 Name: Tom Age: 20 int num float num2 Person p1 Person p2 Name: Jane Age: 23 p2 = p1; 2078 p1.setName(“Joe”); Name: Joe Age: 23 p2.print(); p1.print();

17 17 Methods with objects as arguments How is an object stored in memory? What is passed as the argument? What happens if you modify an object in your method? What about primitive types?

18 18 Wrapper Classes What if you want a primitive type to be modified in your method? Convert a primitive type to an “equivalent” class type –Integer n = new Integer(42); –Integer n = 42; EVERY primitive type has a wrapper class

19 19 Wrapper Classes Constants and Static Methods Numeric Wrapper Classes –Integer.MAX_VALUE; –Double.MIN_VALUE; –Float.parseFloat(“23.4”); –Long.toString(368);

20 20 Class Character toUpperCase toLowerCase isUpperCase isLowerCase isWhiteSpace isLetter isDigit

21 21 Algebra Review / Writing code Write static methods –abs(double n), returns double –ceil(float n), returns double –floor(double n), returns double –max(int n1, int n2), returns int –min(float n1, float n2), returns float –round(float n), returns int

22 22 Wednesday Designing and overloading methods Read 5.3 and 5.4

23 23 Wednesday


Download ppt "1 COMP 110 Static Methods and Variables Tabitha Peck M.S. March 24, 2008 MWF 3-3:50 pm Philips 367."

Similar presentations


Ads by Google