Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have half of this program figured out but got stuck near the end of #2. Any guidance and help would be greatly appreciated: ____________________________________________________________

I have half of this program figured out but got stuck near the end of #2. Any guidance and help would be greatly appreciated:

____________________________________________________________

Build a multi-class program, each class in its own source code file. All classes should be under the same Project in the IDE.

  1. (Name this classStuPreContact) Find a Contact class in Part 1 of an Assignment4Code.txt.

Next put this code in a class called StuPreContact. Comment in themain() (i.e. put it back in the source code) to test the class before moving to the next exercise. Update each occurrence for the class name.

After that's done, comment out themain() before starting the next exercise.

  1. (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

ThetoString() method should be used from 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);

Use two new classes in this exercise:LocalDateTime andDateTimeFormatter.

import java.time.LocalDateTime;

import java.time.format.DateTimeFormatter;

The main() was commented out in the Contact class earlier.

Set up a simplemain()within this Email class like in the given Contact class. Test the class incrementally. For example, it's possible to just create an empty class and then test it by creating a new object of it in themain(). main() should at least include the following:

1). Contact object with a name and email address (email address may be a fake one)

2). A 2ndContact object with the information of a friend of yours.

3). AnEmail object using the three-parameter constructor: from someone to someone with some subject string.

4). A 2ndEmail object using the four-parameter constructor: from someone to someone with a different subject. The time argument should betheFirstEmailVar.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.

The IDE may issue a warning on theid field as it's never used (assigned but never retrieved). Don't worry about it.

Comment out thismain()

  1. (Name this classStuPreTestEmail) Finally add a TestEmail class with an emptymain(). It will serve as the client program.

Find a staticgenerateEmailList() method in Part 2 of the Assignment4Code.txt for this assignment.Add thegenerateEmailList() method to theTestEmail class. Modify the method following the notes in the text file.

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 such as "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. 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.

The print from step 2 may look like this if thetoString() of the Email class returns a string without an ending newline character:

[Subject: Msg 0

From: Name1 <email1>

To: Name2 <email2>

Time: time0, Subject: Msg 1

From: Name3 <email3>

To: Name6 <email6>

Time: time1, Subject: Msg 2

...

Time: time18, Subject: Msg 19

From: Name7 <email7>

To: Name2 <email2>

Time: time19]

If there is an ending newline character, the print from step 2 may look like this instead. Either way is okay.

[Subject: Msg 0

From: Name1 <email1>

To: Name2 <email2>

Time: time0

, Subject: Msg 1

From: Name3 <email3>

To: Name6 <email6>

Time: time1

, Subject: Msg 2

...

Time: time18

, Subject: Msg 19

From: Name7 <email7>

To: Name2 <email2>

Time: time19]

Here is the text file referred to in the instructions:

//============== // 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", "..put an email address here"); // 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); } */ }

//============== // 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

// generate a slightly different time: // 0~2 months ago // 0~9 hours earlier LocalDateTime emailTime = baseTime.minusMonths(randGen.nextInt(3)).minusHours(randGen.nextInt(10)); String fromName = nameList.get(from); String fromEmail = fromName.replaceFirst(" ", "_") + "@XXXX.com"; // "..0@XXXX.com" kind String toName = nameList.get(to); String toEmail = toName.replaceFirst(" ", "_") + "@XXXX.com"; list.add(new Email(new Contact(fromName, fromEmail), new Contact(toName, toEmail), subject, emailTime) ); // There should be a new Email object and directly add it to list } return list; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

It seems like you are working on building a program that involves creating classes for contacts and emails with the capability to generate a list of e... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Business Communication Essentials a skill based approach

Authors: Courtland L. Bovee, John V. Thill

6th edition

978-0132971324

More Books

Students also viewed these Programming questions

Question

Describe the benefits of studying intersectionality.

Answered: 1 week ago

Question

Identify the most stable compound:

Answered: 1 week ago