Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Letter.java import java.util.ArrayList; public class Letter { String sender, recipient; ArrayList lines; // The actual contents of the letter, each line stored individually int numLines;
Letter.java
import java.util.ArrayList; public class Letter { String sender, recipient; ArrayListlines; // The actual contents of the letter, each line stored individually int numLines; // number of lines in the letter /** Create a constructor method with 2 parameters, each of type String, one to initialize the sender and one to initialize the recipient. initialize lines to a new array list, initially empty */ //-----------Start below here. To do: approximate lines of code = 4 // //-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions. /** Create a public method void addLine(String line) that adds a new line to the letter. i.e. adds the new line to the array list */ //-----------Start below here. To do: approximate lines of code = 2 // //-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions. /** Create a public method String 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. Add a " " to the end of each line in array list lines */ //-----------Start below here. To do: approximate lines of code = 7 // //-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions. /** * Create a public method getNumberOfLines() that returns the number of lines that were explicitly added to the letter * using the addLines() method */ //-----------Start below here. To do: approximate lines of code = 2 // //-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions. // Create a public method void removeLine(int index) that removes a line from the array list at location index. // Make sure to subtract 1 from the parameter variable index before removing the line from arrayList lines. // Make sure that index-1 is in the range 0 to lines.size()-1 // If it is not in this range, do nothing. //-----------Start below here. To do: approximate lines of code = 3 // //-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions. }
LetterTester.java
public class LetterTester { public static void main(String[] args) { Letter dearJohn = new Letter("Sally","John"); dearJohn.addLine("I'm sorry but it's just not going to work out."); dearJohn.addLine("I'm taking the dog."); dearJohn.addLine("I'm keeping the ring."); System.out.println(dearJohn.getText()); System.out.println(" Letter length " + dearJohn.getNumberOfLines() + " lines"); System.out.println(" Expected: 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 "); System.out.println("Letter length 3 lines"); dearJohn.removeLine(2); System.out.println(dearJohn.getText()); System.out.println(" Letter length " + dearJohn.getNumberOfLines() + " lines"); System.out.println(" Expected: Dear John: I'm sorry but it's just not going to work out. I'm keeping the ring. Sincerely, Sally "); System.out.println("Letter length 2 lines"); dearJohn.removeLine(2); System.out.println(dearJohn.getText()); System.out.println(" Letter length " + dearJohn.getNumberOfLines() + " lines"); System.out.println(" Expected: Dear John: I'm sorry but it's just not going to work out. Sincerely, Sally "); System.out.println("Letter length 1 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