Inheritance Review
Inheritance Review
int x = 10;
int y = 20;
/* missing code */
Which of the following code segments can be used to replace /* missing code */ so that the value 20 will be
printed?
(A) Android a = new Android(x);
a.setServoCount(y);
System.out.println(a.getServoCount());
B. super(the_author, the_title);
super(the_subject);
C. subject = the_subject;
super(the_author, the_title);
D. super(the_author, the_title);
subject = the_subject;
A I only
B II only
C III only
D I and II only
E I, II and III
Free response
This question involves the design of an interface, writing a class that implements the interface, and writing a
method that uses the interface.
(a) A number group represents a group of integers defined in some way. It could be empty, or it could
contain one or more integers.
Write an interface named NumberGroup that represents a group of integers. The interface should have
a single contains method that determines if a given integer is in the group. For example,
- if group1 is of type NumberGroup, and it contains all numbers between -5 and 3
o then group1.contains(-5) would return true.
o but group1.contains(5) would return false.
- Write the complete NumberGroup interface. It must have exactly one method. (Hint: Do not
overthink this, there is not a lot of code.)
(b) A range represents a number group that contains all (and only) the integers between a minimum value
and a maximum value, inclusive.
Write the Range class, which implements NumberGroup. The Range class represents the group
of int values that range from a given minimum value up through a given maximum value, inclusive.
For example, the declaration
Write the complete Range class. Include all necessary instance variables and methods as well as a
constructor that takes two int parameters. The first parameter represents the minimum value, and the
second parameter represents the maximum value of the range. You may assume that the minimum is less
than or equal to the maximum.
(c) The MultipleGroups class represents a collection of NumberGroup objects and is a
NumberGroup. The MultipleGroups class stores the number groups in the instance variable
groupList which is initialized in the constructor.
Write the MultipleGroups method contains. The method takes an integer and returns true if and only
if the integer is contained in one or more of the number groups in groupList.
Code segments and a portion of the MulitpleGroups class is shown below. Your method should produce
the desired output from the sample code segments.
/** returns true if at least one of the number groups contains num */
public boolean contains(int num)
{ /** to be implemented in part (c) */ }
}
System.out.println(multiple1.contains(2)); // true
System.out.println(multiple1.contains(9)); // false
System.out.println(multiple1.contains(6)); // true
/** returns true if at least one of the number groups contains num */
public boolean contains(int num)
{