exam revision question help please
Consider the following program fragment that defines a self‐referential class:
Code :
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?
Re: exam revision question help please
Please edit your post and fix the formatting for the code. Statements need to be indented to show the logic nesting level.
All statements should NOT start in the first column.
Quote:
What will be displayed on the screen if the above program is compiled and run
What happens when you compile and run the code? It seems silly to talk about it when it is so easy to do.
Re: exam revision question help please
Code :
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());
}
}
I tried to compile but cannot thanks
Re: exam revision question help please
Please copy the full text of the error messages and paste it here.
Re: exam revision question help please
error: invalid method declaration; return type required
error: illegal character: \8230
--- Update ---
line 10, line 21
Re: exam revision question help please
Which lines are line 10 and 21?
Check on line 10: is your method declared correctly? Does it state on its declaration line that it will return anything?
Check on line 21: do you have some characters in your file that simply don't belong there?