Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Develop an application that inputs a name and an address, and prints out a form

ID: 3644395 • Letter: D

Question

Develop an application that inputs a name and an address, and prints out a form letter.
The letter should begin with the address, including the person's full name. Then it should
use the salutation "Dear <firstname>, "where<firstname> is the person's first name.
Within the letter, the text should address the person by his or her first name at least once.
You can use the Name class to represent the name, and develop your own class to represent the address.
You get to make up the content of the form letter, which can be as serious or silly as you like.

Explanation / Answer

import java.util.*; import java.io.*; class Crowd { // Constructors public Crowd() { // Create default Vector object to hold people people = new Vector(); } public Crowd(int numPersons) { // Create Vector object to hold people with given capacity people = new Vector(numPersons); } // Add a person to the crowd public boolean add(Person someone) { return people.add(someone); // Use the Vector method to add } //Sort the people public void sort() { Collection.sort(people); } // Get the person at a given index Person get(int index) { return (Person)people.get(index); } // Get number of persons in crowd public int size() { return people.size(); } // Get people store capacity public int capacity() { return people.capacity(); } // Get an iterator for the crowd public Iterator iterator() { return people.iterator(); } // Person store - only accessible through methods of this class private Vector people; } package tryvectorandsort; import java.io.*; import java.util.*; public class TryVectorAndSort { public static void main(String[] args) { Person aPerson; // A person object Crowd filmCast = new Crowd(); // Populate the crowd for( ; ; ) // Indefinite loop { aPerson = readPerson(); // Read in a film star if(aPerson == null) // If null obtained... break; // We are done... filmCast.add(aPerson); // Otherwise, add to the cast } int count = filmCast.size(); System.out.println("You added "+count + (count == 1 ? " person": " people")+ " to the cast. "); filmCast.sort(); //sort the cast // Show who is in the cast using an iterator Iterator thisLot = filmCast.iterator(); // Obtain an iterator while(thisLot.hasNext()) // Output all elements System.out.println( thisLot.next() ); } // Read a person from the keyboard static public Person readPerson() { FormattedInput in = new FormattedInput(); // Read in the first name and remove blanks front and back System.out.println( " Enter first name or ! to end:"); String firstName = in.stringRead().trim(); // Read and trim a string if(firstName.charAt(0) == '!') // Check for ! entered return null; // If so, we are done... // Read in the surname, also trimming blanks System.out.println("Enter surname:"); String surname = in.stringRead().trim(); // Read and trim a string return new Person(firstName,surname); } } package tryvectorandsort; import java.io.*; import java.util.*; public class Person implements Comparable { // Constructor public Person(String firstName, String surname) { this.firstName = firstName; this.surname = surname; } public String toString() { return firstName + " " + surname; } //Compare Person objects public int compareTo(Object person) { int result = surname.compareTo(((Person)person).surname); return result == 0 ? firstName.compareTo(((Person)person).firstName) : result; } private String firstName; // First name of person private String surname; // Second name of person } package tryvectorandsort; import java.io.*; import java.util.*; public class FormattedInput { // Method to read an int value public int intRead() { try { for(int i = 0; i < 5; i++) { if(tokenizer.nextToken()==tokenizer.TT_NUMBER) return (int)tokenizer.nval; // Value is numeric, so return as int else { System.out.println("Incorrect input: " + tokenizer.sval + " Re-enter an integer"); continue; // Retry the read operation } } System.out.println("Five failures reading an int value" + " - program terminated"); System.exit(1); // End the program return 0; } catch(IOException e) // Error reading in nextToken() { System.out.println(e); // Output the error System.exit(1); // End the program return 0; } } // Read a string public String stringRead() { try { for(int i = 0; i < 5; i++) { int tokenType = tokenizer.nextToken(); // Read a token if(tokenType==tokenizer.TT_WORD || tokenType == '"') // Type is a string return tokenizer.sval; // so return it else if(tokenType == '!') // Non-alpha returned as type return "!"; // so return end string else { System.out.println( "Incorrect input. Re-enter a string between double quotes"); continue; // Retry the read operation } } System.out.println("Five failures reading a string" + " - program terminated"); System.exit(1); // End the program return null; } catch(IOException e) // Error reading in nextToken() { System.out.println(e); // Output the error System.exit(1); // End the program return null; } } //Object to tokenize input from the standard input stream private StreamTokenizer tokenizer = new StreamTokenizer( new InputStreamReader(System.in)); }