Possible Solutions for the Midterm Examination Computer Science 345 1. This program is basically an expanded version of a program that was a sample in class late last week. There is one tricky part: the class lettuce does not have an explicit constructor. Instead a method Cheese ( ) is defined in that class, but never used. Otherwise, the question is just a careful application of the initialization rules presented in class, and explained in the textbook. Here is the output: Meal The TIme default constructor would be called next, but it probably does not produce any output. Lunch PortableLunch GrainFood Bread These last two for the top variable contained in Sandwich GrainFood Bread These last two for the bottom variable DairlyFood Cheese For the c variable Sandwich 2. There are many needed changes. These include the need to import java. applet.Applet, and java.awt.*, the need to extend the Applet class in the main class, the need to use an init ( ), and a paint ( Graphics g) method, the removal of the main method ( not actually necessary, but it is never used by the applet), and the replacement of all calls to println with uses of the drawString method of the Graphics object, g. 3. I will use indentation to place derived classes under their base class. Meal Lunch PortableLunch Sandwich GrainFood Bread DairyFood Cheese Lettuce Meal, GrainFood, DairyFood and Lettuce could be placed under Object from which they all inherit implicitly, if you wish. 4. Lettuce ( String k) { kind = k; } Then change the use of Lettuce in main to new Lettuce("Romaine") 5. Add this line to the Sandwich class: Meal m Then add these lines within the main method of the Sandwich class: m = new Meal ( ); m = new Lunch ( ); m = new PortableLunch ( ); m = new Sandwich ( ) ; This is an example of upcasting. 6. Class variables are indicated by the modifier Static. Just place Static in front of each variable declaration. 7. Constants are indicated by the final modifier. Just place final in front of each variable declaration. Then after the variable name put new Class-name ( ) 8. Meal [ ] [ ] meals = { { new Sandwich(), new Lunch( ) }, { new PortableLunch ( ), new Sandwich( ) }}; Each element in the array must be assigned a new object of the Meal class or of one of the classes that can be upcasted to Meal. 9. Each of the classes in this program contains executable code. Thus none of the classes can be an interface. 10. Add to the Lettuce class something like: public void lettuceType ( ) { System.out.println (kind); } Then add a line to the main method l.lettuceType( ); 11. Notice that only the erase method is ever called. The output is: Triangle erase Square erase Circle erase Square erase This is an example of polymorphism. It uses a slightly modified version of a sample program explained in class and in the textbook. 12. An abstract class cannot itself have any objects. Shape and perhaps Shapes are the only classes that do not have objects.