Question
We will build a multi-class program, each class in its own source code file. All classes from this assignment should be created under the same
We will build a multi-class program, each class in its own source code file. All classes from this assignment should be created under the same Project in your IDE.
1. Update this code following the steps below.
//============== // Part 1 //============== /** * Please add proper file prolog comment */ public class Contact { private String name; private String emailAddress; // Constructs a Contact object with null name and email address public Contact() {} // Constructs a Contact object with the specified name and email address // Assume emailAddress represents a valid email address public Contact(String name, String emailAddress) { this.name = name; this.emailAddress = emailAddress; } // Sets email address // Assume emailAddress represents a valid email address public void setEmailAddress(String emailAddress) { this.emailAddress = emailAddress; } // Sets contact name // Assume emailAddress represents a valid email address public void setName(String name) { this.name = name; } // Gets email address of the calling object public String getEmailAddress() { return emailAddress; } // Gets name of the calling object public String getName() { return name; } // Returns a string representation of a contact in the format of: // name public String toString() { return name + " <" + emailAddress + ">"; } /* // unit testing public static void main(String[] args) { Contact p1 = new Contact(); Contact p2 = new Contact("John Doe", "..e@gmail.com"); // toString() of a class will be invoked when its object is // used in print/ln/f or concatenated with a string System.out.println(p1); System.out.println(p2); } */ }
- Draw a UML class diagram for the given Contact class.
- Next put this code in a class called StuPreContact. You may comment in themain() (i.e. put it back in the source code) to test your class before moving to the next exercise. You need to update each occurrence of the class name.
- After you're done, comment out themain() before starting the next exercise.
2. (Name this classStuPreEmail) Add an Email class to your project based on the following specification:
StuPreEmail |
- idGen : int = 1000 - id : int - from : StuPreContact - to : StuPreContact - subject : String - timeStamp : LocalDateTime |
+ StuPreEmail() + StuPreEmail(from : StuPreContact, to : StuPreContact, subject : String) + StuPreEmail(from : StuPreContact, to : StuPreContact, subject : String, time : LocalDateTime) + getFrom() : StuPreContact + getTo() : StuPreContact + getSubject() : String + getTimeStamp() : LocalDateTime + setFrom(from : StuPreContact) : void + setTo(to : StuPreContact) : void + setSubject(subject : String) : void + toString() : String |
- The static memberidGen is used to generate a unique id for each new email object. Its current value should be used when creating a new email object and then incremented (to prepare for the next object). Hint: it needs to be used and updated in each constructor.
- Default constructor: constructs an email object with a unique id based onidGen. Use null value for the remaining data members.
- The three-parameter constructor: constructs an email object with a unique id based onidGen, and the specified from, to, and subject fields. The timestamp field will be set to the current local time:timeStamp = LocalDateTime.now();
- The four-parameter constructor: constructs an email object with a unique id based onidGen and the specified from, to, subject, and time fields
- getXXX() and setXXX() methods follow the general convention of such methods.
- toString(): returns a string representation of the calling Email object in this format:
Subject: subject-str
From: from-contact-in-a-string
To: to-contact-in-a-string
Time: timestamp-in-a-string
Be sure to use thetoString() method of theStuPreContact class.
Use this segment of code to convert thetimeStamp data member (aLocalDateTimeobject) to a string:
// format: Three-letter-month-name, dd, yyyy, hh:mm AM-or-PM
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("LLL dd, yyyy, hh:mm a");
String timeStr = timeStamp.format(formatter);
You will use two new classes in this exercise:LocalDateTime andDateTimeFormatter.
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
You should have commented out themain() in the Contact class.
Set up a simplemain()within this Email class like in the given Contact class. Test your class incrementally while you build it. For example, it's possible to just create an empty class and then test it by creating a new object of it in themain(). Yourmain() should at least include the following:
1). Add aContact object with your name and with email a..w@example.com.
2). Add a 2ndContact object with the information of a friend of yours.
3). Add anEmail object using the three-parameter constructor: from you to your friend with some subject string.
4). Add a 2ndEmail object using the four-parameter constructor: from your friend to you with a different subject. The time argument should beyourFirstEmailVar.getTimeStamp().plusHours(1) which means 1 hour after the first email message. plusHours() is a method of LocalDateTime.
5). Print out the 1st email object.
6). Print out the 2nd email object.
Your IDE may issue a warning on theid field as it's never used (assigned but never retrieved). Don't worry about it.
Take a screenshot of the execution of your program and include it in the assignment report document.
Comment out thismain() after you're done.
3. (Name this classStuPreTestEmail) Finally add a TestEmail class with an emptymain(). It will serve as the client program.
Find a staticgenerateEmailList() method in thefollowing code below. Add thegenerateEmailList() method to your TestEmail class. Be sure to modify the method following the notes in the text file.
//============== // Part 2 //============== Note A: Need: import java.time.LocalDateTime; import java.util.ArrayList; import java.util.Random; Note B: Need to update: - each occurence of "Email" to your StuPreEmail class name - each occurence of "Contact" to your StuPreContact class name // Returns a list of Email with the specified size. // Email objects will be generated by code public static ArrayList generateEmailList(int size) { final int NUM_OF_CONTACT = 10; ArrayList nameList = new ArrayList<>(); for (int i=0; i list = new ArrayList<>(); for (int i=0; i
Add the following to themain() of TestEmail:
1). CallgenerateEmailList()with argument 20. Save the returned result in a proper local variable. Hint: it should be an ArrayList of some type.
2). Print out the returned list.
3). Retrieve the email address associated with the from field of the 1st object (at index 0) stored in the returned list. Hint: first retrieve the object at index 0 of the result list, which is anEmail object. Next call getXX() on thisEmail object to get aContact object. Finally call getXXX() on thisContact object. Those may be done in one statement with chained method calls or in multiple statements.
4). Print out that email address in a message like "Search for email messages from/to that_email_address:".
5). Search the returned list for that email address. Find and print any email from or to that address. You need to loop through the returned list, retrieve the email address from the from field and to field of each object, and compare them with the email address from step 3. This step should at least print out the very first email object in the list, since the email address is taken out of the first email object's from field.
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