Docstoc

Slide 1 - Department of Computer Science_ UNC-Chapel Hill

Document Sample
Slide 1 - Department of Computer Science_ UNC-Chapel Hill Powered By Docstoc
					Ch 5 Static Methods
Qi Zhang
Oct 15

COMP 110: Introduction to Programming

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

Methods
Actions performed by objects
smiley.drawFace(); Statistics.getTotalPoints.

2

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

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

3

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

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

4

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

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)); }

5

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

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

Why?

6

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

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)); }

7

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

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?

8

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

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

9

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

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

10

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

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

11

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

Primitive vs. Class Types
Primitive
int, float, char, double Stored in memory as value

Class
String, classes you write Stored in memory as address

12

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

Objects in Memory
int num float num2 Person p1 5 3.14159 2078 2078 1056 Name: Tom Age: 20 Name: Jane Joe Age: 23

p2 = p1; p1.setName(“Joe”); p2.print(); p1.print();

Person p2

13

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

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?
14 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

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
15 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

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

16

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

Class Character
toUpperCase toLowerCase isUpperCase isLowerCase isWhiteSpace isLetter isDigit
17 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

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

18

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

Wednesday
Designing and overloading methods Read 5.3 and 5.4

19

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

Wednesday

20

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

Today’s lecture
Write a bank account class for Harry Potter Review worksheet (Ch.4)

21

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

21

Example run
Would you like to make a transaction? y/n y Would you like to make a deposit (d), withdrawal (w), or inquire (i)? d How many galleons, sickles, and knuts would you like to deposit? Galleons: 5 Sickles: 6 Knuts: 4 Would you like to make a transaction? y/n y Would you like to make a deposit (d), withdrawal (w), or inquire (i)? w How many galleons, sickles, and knuts would you like to withdrawal? Galleons: 4 Sickles: 0 Knuts: 0 Would you like to make a transaction? y/n y Would you like to make a deposit (d), withdrawal (w), or inquire (i)? i You now have: 1 galleons 6 sickles 4 knuts

22

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

22

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

23

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

Private Instance Variables
What are the instance variables for our bank account program?
What do we need to keep track of?

24

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

24

UML Diagram - Account
Class Name: Account - galleons (int) - sickles (int) - knuts (int)

25

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

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

26

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

Beginning of Account class
public class Account { private int galleons = 0; private int sickles = 0; private int knuts = 0;

27

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

27

Accessor and Mutator Methods
getGalleons: int getSickles: int getKnuts: int setGalleons(int) setSickles(int) setKnuts(int)

28

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

28

Deposit
public static void main(String[] args) { Account potter = new Account(); . . . potter.deposit(5, 7, 9); public void deposit(int g, int s, int k) { galleons+=g; sickles+=s; knuts+=k; }

29

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

29

Withdrawal
public static void main(String[] args) { Account potter = new Account(); . . . potter.withdraw(5, 7, 9); public void withdraw(int g, int s, int k) { if((g > galleons) || (s > sickles) || (k > knuts)) { System.out.println("You do not have enough money"); } else { galleons -= g; sickles -= s; knuts -= k; } }

30

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

Inquire
public static void main(String[] args) { public void inquire() { System.out.println("You now have: " + galleons + " galleons " + sickles + " sickles " + knuts + " knuts"); }

Account potter = new Account(); . . . potter.inquire();

31

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

31

Review Worksheet

32

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

32


				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:3
posted:10/25/2009
language:English
pages:32