EXERCISES
We will build a multi-class program, each class in its ownsource code file. All classes from this assignment should becreated under the same Project in your IDE.
Next put this code in a class calledStuPreContact. You may comment in the main() (i.e. put it back inthe source code) to test your class before moving to the nextexercise. You need to update each occurrence of the class name.
After you’re done, comment out themain() before starting the next exercise.
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
Subject: subject-str
From:from-contact-in-a-string
To:to-contact-in-a-string
Time:timestamp-in-a-string
Be sure to use the toString() methodof the StuPreContact class.
Use this segment of code to convertthe timeStamp data member (a LocalDateTime object) 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 thisexercise: LocalDateTime and DateTimeFormatter.
import java.time.LocalDateTime;
importjava.time.format.DateTimeFormatter;
The instructions given above should beenough, but feel free to research those two classes.
https://docs.oracle.com/en/java/javase/ ... eTime.html
https://docs.oracle.com/en/java/javase/ ... atter.html
You should have commented out themain() in the Contact class.
Set up a simple main() within thisEmail class like in the given Contact class. Test your classincrementally while you build it. For example, it’s possible tojust create an empty class and then test it by creating a newobject of it in the main(). Your main() should at leastinclude the following:
1). Create a Contact object with yourname and email address (email address may be a fake one, but thename should be yours).
2). Create a 2nd Contactobject with the information of a friend of yours.
3). Create an Email object using thethree-parameter constructor: from you to your friend with somesubject string.
4). Create a 2nd Emailobject using the four-parameter constructor: from your friend toyou with a different subject. The time argument should beyourFirstEmailVar.getTimeStamp().plusHours(1) which means1 hour after the first email message. plusHours() is a method ofLocalDateTime.
5). Print out the 1st emailobject.
6). Print out the 2nd emailobject.
Your IDE may issue a warning on the idfield as it’s never used (assigned but never retrieved). Don’tworry about it.
Take a screenshot of the execution ofyour program and include it in the assignment report document.
Comment out this main() after you’redone.
Find a static generateEmailList()method in Part 2 of the Assignment4Code.txt for this assignment.Add the generateEmailList() method to your TestEmail class. Be sureto modify the method following the notes in the text file.
Add the following to the main() ofTestEmail:
1). Call generateEmailList() withargument 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 addressassociated with the from field of the 1st object (atindex 0) stored in the returned list. Hint: first retrieve theobject at index 0 of the result list, which is an Email object.Next call getXX() on this Email object to get a Contact object.Finally call getXXX() on this Contact object. Those may be done inone statement with chained method calls or in multiple statements.
4). Print out that email address in amessage like “Search for email messages from/tothat_email_address:”.
5). Search the returned list for thatemail address. Find and print any email from or to that address.You need to loop through the returned list, retrieve the emailaddress from the from field and to field of each object, andcompare them with the email address from step 3. This step shouldat least print out the very first email object in the list, sincethe email address is taken out of the first email object’s fromfield.
The print from step 2 may look likethis if the toString() of your Email class returns a string withoutan ending newline character:
[Subject: Msg 0
From: Name1<email1>
To: Name2<email2>
Time: time0, Subject: Msg1
From: Name3<email3>
To: Name6<email6>
Time: time1, Subject: Msg2
…
Time: time18, Subject: Msg19
From: Name7<email7>
To: Name2<email2>
Time:time19]
If there is an ending newlinecharacter, your 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]
EXERCISES We will build a multi-class program, each class in its own source code file. All classes from this assignment
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am