Test Your Knowledge: Exercises
Exercise 8-1. Write a program with an overloaded method for tripling the value of the argument. You don’t need to create a separate class for this; just use static methods right in Tester. One version of the method should triple an int value, and the other version should triple a float value. Call both methods to demonstrate that they work.
Exercise 8-2. Create a Dog class, where the Dog objects have both a weight and a color, hidden from the client. Create a Dog object, then retrieve its color and display it to the user. Ask the user for a weight, and use that input to set the Dog’s weight.
Exercise 8-3. Write a program with just one method that takes an int value, supplied by the user, and returns both double and triple that value. You don’t need a separate class; just put the method in Tester. In Run( ), output the results to the console to make sure it worked.
Exercise 8-4. Modify the program from Exercise 8-3 so that you don’t need to initialize the variables that will hold the doubled and tripled values before calling the method.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access