170 likes | 289 Vues
In this COMP 110 class, we explore essential methods in Java, including mathematical operations such as abs, ceil, floor, max, min, and round. We also cover effective design strategies for software, like top-down and bottom-up approaches. Learn how to tackle problems by breaking them down into smaller tasks and testing each method meticulously. Overloading techniques are also examined, showing how the same method name can serve different purposes through varied parameter lists. Enhance your programming skills with practical examples and driver programs.
E N D
COMP 110Designing and Overloading Methods Tabitha Peck M.S. March 26, 2008 MWF 3-3:50 pm Philips 367
Today in COMP 110 • Math Methods • Designing Methods • Overloading Methods
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
abs(double n) public static double abs(double n) { if ( n < 0 ) return ( n * -1 ); else return n; }
ceil(float n) public static double ceil(float n) { return (double) ((int) n + 1); }
min(float n1, float n2) public static float min(float n1, float n2) { if ( n1 < n2 ) return n1; else return n2; }
Design • How would you design a video game?
Top-Down DesignDivide and Conquer • Big Problem • Break problem into smaller subtasks • Break big subtasks into smaller subtasks • Solve subtasks to solve big problem
Testing • Subtasks = Methods • If a subtask doesn’t work, your solution isn’t right • Test EVERY method you write
Driver programs • Simple program (for only you) • Call methods with different inputs • Positive, negative, zero • True and false • Strings, characters
Driver Program • setMouth(true); • setMouth(false); • setNoseSize(-3); • setNoseSize(2); • setNoseSize(10);
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
Overloading • Change number of arguments • Change argument types • Cannot change only return type
Friday • Lab 6