COMP 110 Designing and Overloading Methods - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

COMP 110 Designing and Overloading Methods

Description:

ceil(float n), returns double. floor(double n), returns double. max(int n1, int n2), returns int ... ceil(float n) public static double ceil(float n) return ... – PowerPoint PPT presentation

Number of Views:30
Avg rating:3.0/5.0
Slides: 18
Provided by: michele9
Category:

less

Transcript and Presenter's Notes

Title: COMP 110 Designing and Overloading Methods


1
COMP 110Designing and Overloading Methods
  • Tabitha Peck M.S.
  • March 26, 2008
  • MWF 3-350 pm
  • Philips 367

2
Announcements
3
Questions?
4
Today in COMP 110
  • Math Methods
  • Designing Methods
  • Overloading Methods

5
Math 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

6
abs(double n)
public static double abs(double n) if ( n lt 0
) return ( n -1 ) else return n
7
ceil(float n)
public static double ceil(float n) return
(double) ((int) n 1)
8
min(float n1, float n2)
public static float min(float n1, float n2) if
( n1 lt n2 ) return n1 else return n2
9
Design
  • How would you design a video game?

10
Top-Down DesignDivide and Conquer
  • Big Problem
  • Break problem into smaller subtasks
  • Break big subtasks into smaller subtasks
  • Solve subtasks to solve big problem

11
Testing
  • Subtasks Methods
  • If a subtask doesnt work, your solution isnt
    right
  • Test EVERY method you write

12
Driver programs
  • Simple program (for only you)
  • Call methods with different inputs
  • Positive, negative, zero
  • True and false
  • Strings, characters

13
Bottom-Up Testing
14
Driver Program
  • setMouth(true)
  • setMouth(false)
  • setNoseSize(-3)
  • setNoseSize(2)
  • setNoseSize(10)

15
Overloading
  • Using the same method name for two or more
    methods within the same class
  • Parameter lists are different
  • average(double n1, double n2)
  • average(double n1, double n2, double n3)
  • Java knows what to use based on the number and
    types of the arguments

16
Overloading
  • Change number of arguments
  • Change argument types
  • Cannot change only return type

17
Friday
  • Lab 6
Write a Comment
User Comments (0)
About PowerShow.com