Answered step by step
Verified Expert Solution
Question
1 Approved Answer
public class Letter 1/-----------Start below here. To do: approximate lines of code = 16 Create 3 instance variables of type String. One for * the
public class Letter 1/-----------Start below here. To do: approximate lines of code = 16 Create 3 instance variables of type String. One for * the sender of the letter, one for the recipient, one for the * text of the letter. Create a 4th instance variable of type int * representing the number of lines in the text of the letter. private String sender; private String recipient; private String text; private int num_lines; Create a constructor method with 2 parameters, each of type String, one to initialize the sender and one to initialize the recipient. Set the text to the emtpy string. Set the number of lines to public Letter (String sender, String receipient) this.sender= sender; this.recipient= recipient; text= null; num_lines= 0; Create a public method addLine(String line) that concatenates the string " " to the instance variable text you created above, then concatenates the given String parameter line to the instance variable text that you created above. Increment the number of lines variable by 1. " public void addLine(String line) line= " "+ text+" num_lines= num_lines+1; Create a public method getText() that returns a String containing "Dear " followed by the recipient name followed by : " followed by the text of the letter followed by " Sincerely, " followed by the sender name public String getText() return "Dear "+this.recipient+ * Create a public method getNumberOfLines() that returns the value of the number of lines instance variable Letter dear John = new Letter("Sally","John"); dear John.addLine("I'm sorry but it's just not going to work out."); dear John.addLine("I'm taking the dog."); dear John.addLine("I'm keeping the ring."); * Expected Output: * Dear John: * I'm sorry but it's just not going to work out. * I'm taking the dog. * I'm keeping the ring. Sincerely, * Sally Letter length 3 lines
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started