1 comp 110 static methods and variables tabitha peck m.s. march 24, 2008 mwf 3-3:50 pm philips 367
Post on 20-Dec-2015
214 views
TRANSCRIPT
5
Methods
• Actions performed by objects– smiley.drawFace();– this.setColor(“green”);– circumference?– area?
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);
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
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
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
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
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
• 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
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
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
Objects in Memory
5
3.14159
2078
1056
Name: TomAge: 20
int numfloat num2
Person p1
Person p2
Name: JaneAge: 23
p2 = p1;
2078p1.setName(“Joe”);
Name: JoeAge: 23
p2.print();p1.print();
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
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
Wrapper ClassesConstants and Static Methods• Numeric Wrapper Classes
– Integer.MAX_VALUE;– Double.MIN_VALUE;– Float.parseFloat(“23.4”);– Long.toString(368);
20
Class Character
• toUpperCase
• toLowerCase
• isUpperCase
• isLowerCase
• isWhiteSpace
• isLetter
• isDigit
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