8 Replies - 1785 Views - Last Post: 05 November 2013 - 01:40 PM Rate Topic: -----

#1 swim_5318  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 84
  • Joined: 05-December 11

a class for authoring a simple letter

Posted 13 December 2011 - 01:28 AM

Provide a class for authoring a simple letter. In the constructor, supply the names of the sender and the recipient
I am getting a null in the return. It's right before the body.

/**
   This class models a simple letter.
 */
public class Letter
{
   private String sender;
   private String recipient;
   private String body;

   /**
      Constructs a letter with a given sender and recipient.
      @param from the sender
      @param to the recipient 
   */
	
  public Letter(String from, String to)
   {
      this.sender = from;
	  this.recipient = to;
   }
   
   /**
      Adds a line to the body of this letter. 
   */   
   public void addLine(String line)
   {
     body = body + line + "\n"; 
   }
   
   /**
      Gets the text of this letter.
   */
   public String getText() 
   {
    String text = "Dear " + recipient + ":\n\n";
	text += body + "\nSincerely,\n\n" + sender + ".";
	return text; 
   }
}

public class LetterPrinter
{
	public static void main(String[] args) 
	{
		Letter letter = new Letter("Mary", "John");
		letter.addLine("I'm sorry we must part.");
		letter.addLine("I wish you all the best.");
		System.out.print(letter.getText());
	}	
}


Is This A Good Question/Topic? 0
  • +

Replies To: a class for authoring a simple letter

#2 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,020
  • Joined: 12-October 11

Re: a class for authoring a simple letter

Posted 13 December 2011 - 01:34 AM

What do you mean right before the body? Paste your error exactly please so we know where to look.
Was This Post Helpful? 0
  • +
  • -

#3 swim_5318  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 84
  • Joined: 05-December 11

Re: a class for authoring a simple letter

Posted 13 December 2011 - 01:35 AM

Running Main Program

Dear John:

nullI'm sorry we must part.
I wish you all the best.

Sincerely,

Mary.
Was This Post Helpful? 0
  • +
  • -

#4 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,020
  • Joined: 12-October 11

Re: a class for authoring a simple letter

Posted 13 December 2011 - 01:39 AM

You never initialize body in your constructor, so when you say body = body + ..., The very first time you are saying body = "null" + ... Just initialize body to "" in the constructor.
Was This Post Helpful? 0
  • +
  • -

#5 swim_5318  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 84
  • Joined: 05-December 11

Re: a class for authoring a simple letter

Posted 13 December 2011 - 01:47 AM

Is this what you mean by initialize?
String body = "";

This is what I got when I did that....

Dear John:

null
Sincerely,

Mary.

I am sorry... I am really new.

This post has been edited by swim_5318: 13 December 2011 - 01:48 AM

Was This Post Helpful? 0
  • +
  • -

#6 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,020
  • Joined: 12-October 11

Re: a class for authoring a simple letter

Posted 13 December 2011 - 01:50 AM

That is declare AND initialize. How can you not know that with the beautiful code you've written? Just initialize it in the Letter constructor: this.body = "";

This post has been edited by Sheph: 13 December 2011 - 01:50 AM

Was This Post Helpful? 0
  • +
  • -

#7 swim_5318  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 84
  • Joined: 05-December 11

Re: a class for authoring a simple letter

Posted 13 December 2011 - 02:20 AM

My teacher does not go over any terms....I barely know any terms. Thanks though for explaining that. I just try and follow her examples. I feel like there is so much little details that I have missed. :unsure:
Was This Post Helpful? 0
  • +
  • -

#8 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,020
  • Joined: 12-October 11

Re: a class for authoring a simple letter

Posted 13 December 2011 - 02:26 AM

Well the code is very neat and readable and that is worth a lot. So good job. The rest of it will come. If you have good habits now it'll pay off.
Was This Post Helpful? 0
  • +
  • -

#9 ChristophAnderson  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 05-November 13

Re: a class for authoring a simple letter

Posted 05 November 2013 - 01:40 PM

public class Letter {
	private String p_sender;
	private String p_recipient;
	private String p_line ="";
	
	public Letter(String from, String to){
		this.p_sender = from;
		this.p_recipient = to;
		p_line = "";
	}
	
	public void addLine(String line){
		p_line = p_line + "\n" + line;
	}
	
	public String getText(){
		String tmp;
		tmp = "Dear " + p_recipient + ":" + "\n";
		tmp = tmp + p_line + "\n\n";
		tmp = tmp + "Sincerely, " + "\n\n";
		tmp = tmp + p_sender;
		return tmp;
	}
}





public class LetterPrinter {
	public static void main (String[] args){
		
		Letter l1 = new Letter ("Mary", "John");
		l1.addLine("I am sorry, we must part.");
		l1.addLine("I wish you all the best.");
		
		System.out.println(l1.getText());
	}
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1