Title: Ch 5 Static Methods
1Ch 5 Static Methods
Qi ZhangOct 15
COMP 110 Introduction to Programming
2Methods
- Actions performed by objects
- smiley.drawFace()
- Statistics.getTotalPoints.
-
3Static 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)
4Why no object?
- What data is in the object?
- When should you create a static method?
5Writing 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))
6Writing 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
7Accessing instance variables or methods
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))
8Self-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?
9The Math Class
- All methods in this class are static
- Dont create a Math object
- Invoke methods using class name Math
- Math.abs
- Math.max
- Math.min
- Math.round
- Predefined constants
- Math.PI
- Math.E
10- Math.round(2.3)
- Math.round(2.7)
- Math.floor(2.3)
- Math.floor(2.7)
- Math.ceil(2.3)
- Math.ceil(2.7)
11Type 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)
12Primitive vs. Class Types
- Primitive
- int, float, char, double
- Stored in memory as value
- Class
- String, classes you write
- Stored in memory as address
13Objects in Memory
5
int num
p2 p1
float num2
3.14159
2078
Person p1
p1.setName(Joe)
1056
2078
Person p2
p2.print() p1.print()
Name Tom Age 20
Name Jane Age 23
Name Joe Age 23
14Methods 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?
15Wrapper 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
16Wrapper ClassesConstants and Static Methods
- Numeric Wrapper Classes
- Integer.MAX_VALUE
- Double.MIN_VALUE
- Float.parseFloat(23.4)
- Long.toString(368)
17Class Character
- toUpperCase
- toLowerCase
- isUpperCase
- isLowerCase
- isWhiteSpace
- isLetter
- isDigit
18Algebra 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
19Wednesday
- Designing and overloading methods
- Read 5.3 and 5.4
20Wednesday
21Todays lecture
- Write a bank account class for Harry Potter
- Review worksheet (Ch.4)
21
22Example run
- Would you like to make a transaction?
y/nyWould you like to make a deposit (d),
withdrawal (w), or inquire (i)?dHow many
galleons, sickles, and knuts would you like to
deposit?Galleons 5Sickles 6Knuts 4 - Would you like to make a transaction?
y/nyWould you like to make a deposit (d),
withdrawal (w), or inquire (i)?wHow many
galleons, sickles, and knuts would you like to
withdrawal?Galleons 4Sickles 0Knuts 0Would
you like to make a transaction? y/nyWould you
like to make a deposit (d), withdrawal (w), or
inquire (i)?iYou now have 1 galleons6
sickles4 knuts
22
23UML Diagram - Student
Class Name Student Name
(String) Class Year (int) GPA (double) Major
(String) Credits (int) getMajor() String
increaseYear() void updateGPA(double grade,
int credit) void printData() void
24Private Instance Variables
- What are the instance variables for our bank
account program? - What do we need to keep track of?
24
25UML Diagram - Account
Class Name Account - galleons (int) -
sickles (int) - knuts (int)
26UML Diagram - Account
Class Name Account - galleons (int) -
sickles (int) - knuts (int) all accessors
all mutators deposit (int g, int s, int k)
void withdraw (int g, int s, int k) void
inquire() void
27Beginning of Account class
- public class Account private int
galleons 0 private int sickles 0
private int knuts 0
27
28Accessor and Mutator Methods
- getGalleons int
- getSickles int
- getKnuts int
- setGalleons(int)
- setSickles(int)
- setKnuts(int)
28
29Deposit
- public void deposit(int g, int s, int k)
- galleonsg sickless
knutsk -
- public static void main(String args)
- Account potter new Account()
- .
- .
- .
- potter.deposit(5, 7, 9)
29
30Withdrawal
- public void withdraw(int g, int s, int k)
-
- if((g gt galleons) (s gt sickles) (k gt
knuts)) System.out.println("You do not
have enough money") else
galleons - g sickles - s
knuts - k
public static void main(String args)
Account potter new Account() . . .
potter.withdraw(5, 7, 9)
31Inquire
- public void inquire()
System.out.println("You now have " galleons
" galleons " sickles " sickles " knuts "
knuts")
- public static void main(String args)
- Account potter new Account()
- .
- .
- .
- potter.inquire()
31
32Review Worksheet
32