Presentation is loading. Please wait.

Presentation is loading. Please wait.

Overloading a constructor If a constructor is overloaded, then an if statement in the client is needed when creating the object. We have always declared.

Similar presentations


Presentation on theme: "Overloading a constructor If a constructor is overloaded, then an if statement in the client is needed when creating the object. We have always declared."— Presentation transcript:

1 Overloading a constructor If a constructor is overloaded, then an if statement in the client is needed when creating the object. We have always declared objects and given them values on the same line. Sometimes, you must do these 2 things separately. Demo: ConstructorOverloadClass & ConstructorOverloadClient

2 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

3 Add the following methods to DavosClass, and then make necessary additions to the client: An overloaded constructor – it should receive no parameters, and assign the number 42 to the age 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 An overloaded same( ) – receives one word as a parameter, returns true if the word is “bird” convertToDecimal( ) – receives a String parameter (which will actually be an 8-bit binary #), converts this number to decimal, and returns it (as an int). displayGreeting( ) – a private method that just displays “oh hey.” letterGame( ) – receives no parameters, calls the private method displayGreeting( ), creates a random lowercase letter (hint: easier than it seems), then gives the user 10 chances to guess the letter. Returns nothing.


Download ppt "Overloading a constructor If a constructor is overloaded, then an if statement in the client is needed when creating the object. We have always declared."

Similar presentations


Ads by Google