Why would you use an inner class instead of regular class -


Q.1. Why would you use an inner class instead of regular class?

Q.2. What does this code print? Why is it polymorphic?
DataSet data = new DataSet();
data.add(new BankAccount(1000));
data.add(new Coin(0.1, "dime"));
System.out.println(data.getAverage());

Q.3. Can you convert a superclass reference into a subclass reference? A subclass reference into a superclass reference? If so, give examples. If not, explain why not.

Q.4. Implement a superclass Person. Make two classes, Student and Instructor that inherit from Person. A person has a name and a year of birth. A student has a major, and an instructor has a salary. Write the class declarations, the constructors, and the methods toString for all classes. Supply a test program that tests these classes and methods.

Q.5. Implement the compare method of the following class RectangleComparator. The method compares two rectangles. The method should return:
A positive integer if the area of the first rectangle is larger than the area of the second rectangle
A negative integer if the area of the first rectangle is smaller than the area of the second rectangle
0 if the two rectangles have the same area.
import java.util.Comparator;
import java.awt.Rectangle;
public class RectangleComparator implements Comparator
`{
/**
Compares two Rectangle objects.
@param r1 the first rectangle
@param r2 the second rectangle
@return 1 if the area of the first rectangle is larger than the area of
the second rectangle, -1 if the area of the first rectangle is smaller than the area of the second rectangle or 0 if the two
rectangles have the same area
*/
public int compare(Rectangle r1, Rectangle r2)
{
...
}
}

Solution Preview :

Prepared by a verified Expert
Dissertation: Why would you use an inner class instead of regular class -
Reference No:- TGS02538482

Now Priced at $10 (50% Discount)

Recommended (90%)

Rated (4.3/5)