comp 110 designing and overloading methods luv kohli november 3, 2008 mwf 2-2:50 pm sitterson 014
DESCRIPTION
Questions? 3TRANSCRIPT
![Page 1: COMP 110 Designing and overloading methods Luv Kohli November 3, 2008 MWF 2-2:50 pm Sitterson 014](https://reader036.vdocuments.us/reader036/viewer/2022062401/5a4d1b4d7f8b9ab0599a61d8/html5/thumbnails/1.jpg)
COMP 110COMP 110Designing and overloading methodsDesigning and overloading methods
Luv KohliNovember 3, 2008
MWF 2-2:50 pmSitterson 014
![Page 2: COMP 110 Designing and overloading methods Luv Kohli November 3, 2008 MWF 2-2:50 pm Sitterson 014](https://reader036.vdocuments.us/reader036/viewer/2022062401/5a4d1b4d7f8b9ab0599a61d8/html5/thumbnails/2.jpg)
AnnouncementsAnnouncementsLab 6 due Friday, November 7, 2pm
2
![Page 3: COMP 110 Designing and overloading methods Luv Kohli November 3, 2008 MWF 2-2:50 pm Sitterson 014](https://reader036.vdocuments.us/reader036/viewer/2022062401/5a4d1b4d7f8b9ab0599a61d8/html5/thumbnails/3.jpg)
Questions?Questions?
3
![Page 4: COMP 110 Designing and overloading methods Luv Kohli November 3, 2008 MWF 2-2:50 pm Sitterson 014](https://reader036.vdocuments.us/reader036/viewer/2022062401/5a4d1b4d7f8b9ab0599a61d8/html5/thumbnails/4.jpg)
Today in COMP 110Today in COMP 110Designing methods
Overloading methods
4
![Page 5: COMP 110 Designing and overloading methods Luv Kohli November 3, 2008 MWF 2-2:50 pm Sitterson 014](https://reader036.vdocuments.us/reader036/viewer/2022062401/5a4d1b4d7f8b9ab0599a61d8/html5/thumbnails/5.jpg)
TetrisTetris
5
![Page 6: COMP 110 Designing and overloading methods Luv Kohli November 3, 2008 MWF 2-2:50 pm Sitterson 014](https://reader036.vdocuments.us/reader036/viewer/2022062401/5a4d1b4d7f8b9ab0599a61d8/html5/thumbnails/6.jpg)
Get into small groupsGet into small groupsDecide what high-level tasks are required
for Tetris gameplay to work
Assume the graphical display code is taken care of for you
6
![Page 7: COMP 110 Designing and overloading methods Luv Kohli November 3, 2008 MWF 2-2:50 pm Sitterson 014](https://reader036.vdocuments.us/reader036/viewer/2022062401/5a4d1b4d7f8b9ab0599a61d8/html5/thumbnails/7.jpg)
Tetris high-level gameplay tasksTetris high-level gameplay tasksChoose a random tetromino to give the
userUser-controlled tetromino manipulationGame-controlled tetromino manipulation
(automatically falling)Remove full horizontal lines of blocksIncrease user score, level, and speed of
falling blocksCheck if game is over
7
![Page 8: COMP 110 Designing and overloading methods Luv Kohli November 3, 2008 MWF 2-2:50 pm Sitterson 014](https://reader036.vdocuments.us/reader036/viewer/2022062401/5a4d1b4d7f8b9ab0599a61d8/html5/thumbnails/8.jpg)
User-controlled tetromino manipulationUser-controlled tetromino manipulation
High-level task: manipulate tetromino based on user input
How can a tetromino be manipulated?◦Move◦Rotate
8
![Page 9: COMP 110 Designing and overloading methods Luv Kohli November 3, 2008 MWF 2-2:50 pm Sitterson 014](https://reader036.vdocuments.us/reader036/viewer/2022062401/5a4d1b4d7f8b9ab0599a61d8/html5/thumbnails/9.jpg)
Moving a tetrominoMoving a tetrominoHow?
Subtasks◦Move left◦Move right◦Move down
9
![Page 10: COMP 110 Designing and overloading methods Luv Kohli November 3, 2008 MWF 2-2:50 pm Sitterson 014](https://reader036.vdocuments.us/reader036/viewer/2022062401/5a4d1b4d7f8b9ab0599a61d8/html5/thumbnails/10.jpg)
Rotating a tetrominoRotating a tetrominoSubtasks◦Rotate left◦Rotate right
10
![Page 11: COMP 110 Designing and overloading methods Luv Kohli November 3, 2008 MWF 2-2:50 pm Sitterson 014](https://reader036.vdocuments.us/reader036/viewer/2022062401/5a4d1b4d7f8b9ab0599a61d8/html5/thumbnails/11.jpg)
Imagine a Tetromino classImagine a Tetromino classpublic class Tetromino{ private int x; private int y; // some other stuff describing this Tetromino’s shape
public void moveLeft() { x--; }
public void moveRight() { x++; }
public void moveDown() { y--; }}
11
+y
+x
![Page 12: COMP 110 Designing and overloading methods Luv Kohli November 3, 2008 MWF 2-2:50 pm Sitterson 014](https://reader036.vdocuments.us/reader036/viewer/2022062401/5a4d1b4d7f8b9ab0599a61d8/html5/thumbnails/12.jpg)
Top-down designTop-down designDivide and conquer
Start with a big problemDecompose problem into smaller subtasksDecompose big subtasks into even smaller
subtasksSolve subtasks to solve big problem
12
![Page 13: COMP 110 Designing and overloading methods Luv Kohli November 3, 2008 MWF 2-2:50 pm Sitterson 014](https://reader036.vdocuments.us/reader036/viewer/2022062401/5a4d1b4d7f8b9ab0599a61d8/html5/thumbnails/13.jpg)
Game-controlled tetromino manipulationGame-controlled tetromino manipulation
How can we implement automatically falling tetrominoes?
What are we trying to do at a high level?◦Every n amount of time, make a tetromino
move down one space◦Need a timer
13
![Page 14: COMP 110 Designing and overloading methods Luv Kohli November 3, 2008 MWF 2-2:50 pm Sitterson 014](https://reader036.vdocuments.us/reader036/viewer/2022062401/5a4d1b4d7f8b9ab0599a61d8/html5/thumbnails/14.jpg)
Using the Tetromino class in a game loopUsing the Tetromino class in a game loop
public class TetrisGame{ private Tetromino userTetr;
// gameUpdate() is called once per game loop public void gameUpdate() { // ...do some stuff here // check user input, assume userTetr has been properly // instantiated if (userInput == LEFT) userTetr.moveLeft(); else if (userInput == RIGHT) userTetr.moveRight(); else if (userInput == DOWN) userTetr.moveDown(); applyAutoFalling(userTetr);
// do some other stuff here }}
14
![Page 15: COMP 110 Designing and overloading methods Luv Kohli November 3, 2008 MWF 2-2:50 pm Sitterson 014](https://reader036.vdocuments.us/reader036/viewer/2022062401/5a4d1b4d7f8b9ab0599a61d8/html5/thumbnails/15.jpg)
applyAutoFalling methodapplyAutoFalling method
public void applyAutoFalling(Tetromino tetr){ double timeSinceLastAutoFall = // some code to figure out the time since the last
fall if (timeSinceLastAutoFall > 0.5) { tetr.moveDown(); }}
15
![Page 16: COMP 110 Designing and overloading methods Luv Kohli November 3, 2008 MWF 2-2:50 pm Sitterson 014](https://reader036.vdocuments.us/reader036/viewer/2022062401/5a4d1b4d7f8b9ab0599a61d8/html5/thumbnails/16.jpg)
What if we see this behavior?What if we see this behavior?Imagine that we have run the game◦A new tetromino appears◦The user does not provide any input◦The tetromino does not automatically fall, it
simply stays where it is
What could the problem be?
16
![Page 17: COMP 110 Designing and overloading methods Luv Kohli November 3, 2008 MWF 2-2:50 pm Sitterson 014](https://reader036.vdocuments.us/reader036/viewer/2022062401/5a4d1b4d7f8b9ab0599a61d8/html5/thumbnails/17.jpg)
Let’s check applyAutoFallingLet’s check applyAutoFallingpublic void applyAutoFalling(Tetromino tetr){ double timeSinceLastAutoFall = // some code to figure out the time since the last
fall if (timeSinceLastAutoFall > 0.5) { tetr.moveDown(); }}
What if we had this code? double timeSinceLastAutoFall = 0.0;
17
![Page 18: COMP 110 Designing and overloading methods Luv Kohli November 3, 2008 MWF 2-2:50 pm Sitterson 014](https://reader036.vdocuments.us/reader036/viewer/2022062401/5a4d1b4d7f8b9ab0599a61d8/html5/thumbnails/18.jpg)
The problem could be elsewhereThe problem could be elsewhereWhat if we had this code inside the class
Tetromino?
public void moveDown() { y = y; }
The moveDown() method does not do what it is supposed to
18
![Page 19: COMP 110 Designing and overloading methods Luv Kohli November 3, 2008 MWF 2-2:50 pm Sitterson 014](https://reader036.vdocuments.us/reader036/viewer/2022062401/5a4d1b4d7f8b9ab0599a61d8/html5/thumbnails/19.jpg)
TestingTestingIf a subtask (method) does not work,
your solution is incorrectTest EVERY method you write
19
![Page 20: COMP 110 Designing and overloading methods Luv Kohli November 3, 2008 MWF 2-2:50 pm Sitterson 014](https://reader036.vdocuments.us/reader036/viewer/2022062401/5a4d1b4d7f8b9ab0599a61d8/html5/thumbnails/20.jpg)
Bottom-up testingBottom-up testingHow do we determine if the error is in
applyAutoFalling or moveDown?
Test each method individually◦ If method A calls method B, fully test method
B before testing method A◦ In this case, fully test moveDown before
testing applyAutoFalling
20
![Page 21: COMP 110 Designing and overloading methods Luv Kohli November 3, 2008 MWF 2-2:50 pm Sitterson 014](https://reader036.vdocuments.us/reader036/viewer/2022062401/5a4d1b4d7f8b9ab0599a61d8/html5/thumbnails/21.jpg)
Driver programsDriver programsSimple program for only you to test with◦Run by you, not your user
Call methods with different inputs◦Test cases, edge conditions
Positive, negative, zero true, false Strings, characters
MathUtils example in jGRASP
21
![Page 22: COMP 110 Designing and overloading methods Luv Kohli November 3, 2008 MWF 2-2:50 pm Sitterson 014](https://reader036.vdocuments.us/reader036/viewer/2022062401/5a4d1b4d7f8b9ab0599a61d8/html5/thumbnails/22.jpg)
OverloadingOverloadingUsing the same method name for two or more
methods within the same class
We have seen this for constructors already
Parameter lists must be different◦ public double average(double n1, double n2)◦ public double average(double n1, double n2, double n3)
Java knows what to use based on the number and types of the arguments
22
![Page 23: COMP 110 Designing and overloading methods Luv Kohli November 3, 2008 MWF 2-2:50 pm Sitterson 014](https://reader036.vdocuments.us/reader036/viewer/2022062401/5a4d1b4d7f8b9ab0599a61d8/html5/thumbnails/23.jpg)
Method Method signaturesignatureA method’s name and the number and
types of its parameters
signature does NOT include return type
Cannot have two methods with the same signature in the same class
23
![Page 24: COMP 110 Designing and overloading methods Luv Kohli November 3, 2008 MWF 2-2:50 pm Sitterson 014](https://reader036.vdocuments.us/reader036/viewer/2022062401/5a4d1b4d7f8b9ab0599a61d8/html5/thumbnails/24.jpg)
GotchaGotchaOverloading and automatic type conversion
Imagine we have this constructor defined: public Pet(double initialWeight)
We create a Pet like this: Pet myPet = new Pet(35);
What happens?
24
![Page 25: COMP 110 Designing and overloading methods Luv Kohli November 3, 2008 MWF 2-2:50 pm Sitterson 014](https://reader036.vdocuments.us/reader036/viewer/2022062401/5a4d1b4d7f8b9ab0599a61d8/html5/thumbnails/25.jpg)
GotchaGotchaImagine we have these two constructors
defined: public Pet(int initialAge) public Pet(double initialWeight)
We create a Pet like this: Pet myPet = new Pet(35);
What happens?◦We create a pet with age 35, instead of weight 35.0
25
![Page 26: COMP 110 Designing and overloading methods Luv Kohli November 3, 2008 MWF 2-2:50 pm Sitterson 014](https://reader036.vdocuments.us/reader036/viewer/2022062401/5a4d1b4d7f8b9ab0599a61d8/html5/thumbnails/26.jpg)
WednesdayWednesdayArrays
26