Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: exam revision question help please

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    17
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default exam revision question help please

    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?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,147
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.

    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.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jan 2013
    Posts
    17
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: exam revision question help please

     
    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

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,147
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: exam revision question help please

    Please copy the full text of the error messages and paste it here.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jan 2013
    Posts
    17
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: exam revision question help please

    error: invalid method declaration; return type required
    error: illegal character: \8230

    --- Update ---

    line 10, line 21

  6. #6
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default 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?

Similar Threads

  1. Exam study review question
    By michael305rodri in forum Object Oriented Programming
    Replies: 3
    Last Post: October 17th, 2012, 06:10 PM
  2. Need Help with java revision notes app
    By 06williamsl in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 10th, 2012, 08:53 AM
  3. Java Programming revision
    By BITmixit in forum Java Theory & Questions
    Replies: 4
    Last Post: January 17th, 2012, 11:23 PM
  4. REVISION QUESTIONS!!
    By nav1407 in forum Java Theory & Questions
    Replies: 3
    Last Post: July 20th, 2011, 03:41 AM
  5. Date Of Birth Revision , Class please help
    By youssef22 in forum Object Oriented Programming
    Replies: 2
    Last Post: November 6th, 2010, 02:41 AM