Ch 5 Static Methods - PowerPoint PPT Presentation

1 / 32
About This Presentation
Title:

Ch 5 Static Methods

Description:

Write a bank account class for Harry Potter. Review worksheet (Ch.4) 21. Example run ... Account potter = new Account(); potter.deposit(5, 7, 9) ... – PowerPoint PPT presentation

Number of Views:43
Avg rating:3.0/5.0
Slides: 33
Provided by: qizh
Category:
Tags: methods | potter | static

less

Transcript and Presenter's Notes

Title: Ch 5 Static Methods


1
Ch 5 Static Methods
Qi ZhangOct 15
COMP 110 Introduction to Programming
2
Methods
  • Actions performed by objects
  • smiley.drawFace()
  • Statistics.getTotalPoints.

3
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)

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

5
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))
6
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
7
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))
8
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?

9
The 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)

11
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)

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

13
Objects 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
14
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?

15
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

16
Wrapper ClassesConstants and Static Methods
  • Numeric Wrapper Classes
  • Integer.MAX_VALUE
  • Double.MIN_VALUE
  • Float.parseFloat(23.4)
  • Long.toString(368)

17
Class Character
  • toUpperCase
  • toLowerCase
  • isUpperCase
  • isLowerCase
  • isWhiteSpace
  • isLetter
  • isDigit

18
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

19
Wednesday
  • Designing and overloading methods
  • Read 5.3 and 5.4

20
Wednesday
21
Todays lecture
  • Write a bank account class for Harry Potter
  • Review worksheet (Ch.4)

21
22
Example 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
23
UML 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
24
Private Instance Variables
  • What are the instance variables for our bank
    account program?
  • What do we need to keep track of?

24
25
UML Diagram - Account
Class Name Account - galleons (int) -
sickles (int) - knuts (int)
26
UML 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
27
Beginning of Account class
  • public class Account private int
    galleons 0 private int sickles 0
    private int knuts 0

27
28
Accessor and Mutator Methods
  • getGalleons int
  • getSickles int
  • getKnuts int
  • setGalleons(int)
  • setSickles(int)
  • setKnuts(int)

28
29
Deposit
  • 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
30
Withdrawal
  • 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)
31
Inquire
  • 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
32
Review Worksheet
32
Write a Comment
User Comments (0)
About PowerShow.com