Consider the following program fragment that defines a self‐referential class:
public class DrinkOrders { public String drinkDescription; public DrinkOrders next; public DrinkOrders(String d) { drinkDescription=d; next=null; } public addDrink(String d) { DrinkOrders o,c; o=new DrinkOrders(d); c=this; while (c != null) { c=c.next; } c.next=o; } … public String toString() { return drinkDescription + toString(); } } public class Harness { public static void main(String []args) { DrinkOrders myRound; myRound=new DrinkOrders("Beer"); System.out.println(myRound.toString()); /* ### */ myRound.addDrink("Wine"); System.out.println(myRound.toString()); } }
a. What will be displayed on the screen if the above program is compiled and run?
Why?
b. Assume that any compiler/logic errors that arose in part (a) above are corrected
and, on execution, the annotated line (###) is reached without error. What
would the output of the final println() method call be? Why?