Java Software Solutions, 8e, Global Edition Exercise Solutions, Ch. 7
Chapter 7 Exercise Solutions
EX 7.1. Write a method called product that accepts four integer parameters and returns their
product as an integer.
}
EX 7.2. Overload the product method of Exercise 7.1 such that if five integers are provided
as parameters, the method returns the product of all five.
EX 7.3. Overload the product method of Exercise 7.1 to accept six integer parameters and return
their product.
}
EX 7.4. Write a method called doubleConcat that takes a String and an integer as parameters.
Return a String that consists of the string parameter concatenated with itself twice the
number times, where number is an integer parameter. For example, if the parameter
values are “hello” and 1, the return value should be “hellohello”. Return the original
string if the integer parameter is less than 1.
}
EX 7.5. Overload the doubleConcat method from Exercise 7.4 such that if the integer parameter
is not provided, the method returns the string itself without concatenation. For example, if
the string is “hello”, the return string is also “hello”.
Java Software Solutions, 8th Edition Exercise Solutions, Ch. 7
EX 7.6. Write a method called drawCircle that draws a circle based on the method’s
parameters: a Graphics object through which to draw the circle, two integer
values representing the (x, y) coordinates of the center of the circle, another
integer that represents the circle’s radius, and a Color object that defines the
EX 7.7. Overload the drawCircle method of Exercise 7.6 such that if the Color parameter
is not provided, the circle’s color will default to black.
public void drawCircle(Graphics page, int x, int y,
EX 7.8. Overload the drawCircle method of Exercise 7.6 such that if the radius is not
provided, a random radius in the range 10 to 100 (inclusive) will be used.
public void drawCircle(Graphics page, int x, int y,
EX 7.9. Overload the drawCircle method of Exercise 7.6 such that if both the color and
the radius of the circle are not provided, the color will default to red and the radius
will default to 40.
EX 7.10. Discuss the manner in which Java passes parameters to a method. Is this
technique consistent between primitive types and objects? Explain.
Java Software Solutions, 8th Edition Exercise Solutions, Ch. 7
EX 7.11. Explain why a static method cannot refer to an instance variable.
A static method is invoked through a class rather than through an object of the class.
No object of the class needs to be instantiated in order to invoke a static method. If no
EX 7.12. Can a class implement two interfaces that each contains the same method
signature? Explain.
Yes. The class which implements an interface provides method implementations for
EX 7.13. Create an interface called Visible that includes two methods:
makeVisible and makeInvisible. Both methods should take no parameters and
should return a boolean result. Describe how a class might implement this
interface.
EX 7.14. Draw a UML class diagram that shows the relationships among the
elements of the previous exercise.
EX 7.15. Imagine a game in which some game elements can be broken by the player
and others can’t. Create an interface called Breakable that has a method called
Java Software Solutions, 8th Edition Exercise Solutions, Ch. 7
break that takes no parameters and another called broken that returns a boolean
result indicating whether the object is currently broken.
public interface Breakable
EX 7.16. Create an interface called VCR that has methods that represent the standard
operations on a video cassette recorder (play, stop, etc.). Define the method
signatures any way you desire. Describe how a class might implement this
interface.
public interface VCR
EX 7.17. Draw a UML class diagram that shows the relationships among the
elements of the Exercise 7.16.
Java Software Solutions, 8th Edition Exercise Solutions, Ch. 7
EX 7.19. What visual effect would result by changing the horizontal and vertical
gaps on the border layout used in the LayoutDemo program? Make the change to
test your answer.
The panel used to display the buttons has a green background, but no green is visible
EX 7.20. Write the lines of code that will define a compound border using three
borders. Use a line border on the inner edge, an etched border on the outer edge,
and a raised bevel border in between.
//create borders
Border line = BorderFactory.createLineBorder(Color.blue);
Java Software Solutions, 8th Edition Exercise Solutions, Ch. 7