Download presentation
Presentation is loading. Please wait.
Published byArnold Egeland Modified over 5 years ago
1
Agenda Warmup Lesson 2.8 (Overloading constructors, etc) Independent Practice (2.8 Assignments) (Time Permitting) Lesson 2.9 Closure Activity Students will be able to: Understand how to overload a constructor Create a method that contains a loop in which a value is returned Understand the concept of encapsulation, and how it affects code See how today's lesson fits into the unit and the course as a whole
2
Warmup What is the point of a toString() method?
2) How is a toString() method called? 3) How is a constructor called? 4) What does a constructor return? 5) What does encapsulation have to do with methods? 7) What is the term for using helper methods to make a big method more manageable? 8) int num = ‘Q’ % 5; // is this valid code? 9) List 9 countries that begin with L; New Jersey is not one of them.
3
Overloading a constructor
If a constructor is overloaded, then an if statement in the client is needed when creating the object. So far, we have always declared an object and given it a value on the same line. Sometimes, you must do these 2 things separately. Demo: ConstructorOverloadClass & ConstructorOverloadClient
4
Using a return statement to break a loop
Although we learned previously that you usually want to avoid putting a return statement inside a loop, (as well as inside an if statement), sometimes it is necessary / more efficient to do so. When you do this, the return will cause a break from the loop. Demo: ReturnInsideLoopDemo
5
What changes could be made that would result in 6 displaying?
It is important to see how method can and cannot affect the value of variables: See demos MethodChangeDemo and MethodChangeDemoClient What changes could be made that would result in 6 displaying?
6
Add the following methods to Mar18Class, and then make necessary additions to Mar18Client:
An overloaded constructor – it should receive no parameters, assign the number 42 to the age variable, and “jelly” to the donut variable. An overloaded abs( ) – receives two numbers as parameters, only returns the absolute value of the smaller number. same( ) – receives 2 words as parameters, returns true if they are the same word (case sensitive) An overloaded same( ) – receives one word as a parameter, returns true if the word is “frog”
7
displayGreeting( ) – a private method that just displays “hi.”
letterGame( ) – receives no parameters calls the helper method displayGreeting( ) creates a random lowercase letter (not as difficult as it sounds) gives the user 10 chances to guess the letter If letter is correctly guessed, displays how many guesses were made If the correct letter is not guessed, shows the correct letter after 10 guesses, displays which letter was the closest guess in terms of Ascii value (hint: use an already existing method from this class!) returns nothing newGame( ) – create your own game. It should be fairly complex. You might deal cards from your Card( ) class, or roll dice from your Die( ) class, or do something completely different. It’s up to you whether to send parameters or not.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.