Question
Always be sure to fully read the assigned textbook sections each session before attempting the programming assignments. Complete exercise 3.54 on page 126 in your
Always be sure to fully read the assigned textbook sections each session before attempting the programming assignments.
Complete exercise 3.54 on page 126 in your Objects First with Java textbook.
Submit MailClient.java and MailServer.java and MaileItem.java files.
To do this assignment, open the "clock-display" project in BlueJ.
This can be found in your downloaded book projects folder at projects\chapter03\clock-display
---------------------------------
Exercise 3.54
--------------------------------
Add a subject line for an email e-mail to mail item in the mail-system project. Make sure printing messages also prints the subjectline. Modify the mail client accordingly.
Please see below the two files provided in the files provided in the "projects\chapter03\clock-display" folder.
ClockDisplay.java
/** * The ClockDisplay class implements a digital clock display for a * European-style 24 hour clock. The clock shows hours and minutes. The * range of the clock is 00:00 (midnight) to 23:59 (one minute before * midnight). * * The clock display receives "ticks" (via the timeTick method) every minute * and reacts by incrementing the display. This is done in the usual clock * fashion: the hour increments when the minutes roll over to zero. * * @author Michael Klling and David J. Barnes * @version 2016.02.29 */ public class ClockDisplay { private NumberDisplay hours; private NumberDisplay minutes; private String displayString; // simulates the actual display /** * Constructor for ClockDisplay objects. This constructor * creates a new clock set at 00:00. */ public ClockDisplay() { hours = new NumberDisplay(24); minutes = new NumberDisplay(60); updateDisplay(); }
/** * Constructor for ClockDisplay objects. This constructor * creates a new clock set at the time specified by the * parameters. */ public ClockDisplay(int hour, int minute) { hours = new NumberDisplay(24); minutes = new NumberDisplay(60); setTime(hour, minute); }
/** * This method should get called once every minute - it makes * the clock display go one minute forward. */ public void timeTick() { minutes.increment(); if(minutes.getValue() == 0) { // it just rolled over! hours.increment(); } updateDisplay(); }
/** * Set the time of the display to the specified hour and * minute. */ public void setTime(int hour, int minute) { hours.setValue(hour); minutes.setValue(minute); updateDisplay(); }
/** * Return the current time of this display in the format HH:MM. */ public String getTime() { return displayString; } /** * Update the internal string that represents the display. */ private void updateDisplay() { displayString = hours.getDisplayValue() + ":" + minutes.getDisplayValue(); } }
`````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````
And
NumberDisplay.Java
/** * The NumberDisplay class represents a digital number display that can hold * values from zero to a given limit. The limit can be specified when creating * the display. The values range from zero (inclusive) to limit-1. If used, * for example, for the seconds on a digital clock, the limit would be 60, * resulting in display values from 0 to 59. When incremented, the display * automatically rolls over to zero when reaching the limit. * * @author Michael Klling and David J. Barnes * @version 2016.02.29 */ public class NumberDisplay { private int limit; private int value;
/** * Constructor for objects of class NumberDisplay. * Set the limit at which the display rolls over. */ public NumberDisplay(int rollOverLimit) { limit = rollOverLimit; value = 0; }
/** * Return the current value. */ public int getValue() { return value; }
/** * Return the display value (that is, the current value as a two-digit * String. If the value is less than ten, it will be padded with a leading * zero). */ public String getDisplayValue() { if(value < 10) { return "0" + value; } else { return "" + value; } }
/** * Set the value of the display to the new specified value. If the new * value is less than zero or over the limit, do nothing. */ public void setValue(int replacementValue) { if((replacementValue >= 0) && (replacementValue < limit)) { value = replacementValue; } }
/** * Increment the display value by one, rolling over to zero if the * limit is reached. */ public void increment() { value = (value + 1) % limit; } }
Blow is the "MailClient.java", "MailItem.java", and "MailServer.java":
MailClient.java
```````````````````````
/** * A class to model a simple email client. The client is run by a * particular user, and sends and retrieves mail via a particular server. * * @author David J. Barnes and Michael Klling * @version 2016.02.29 */ public class MailClient { // The server used for sending and receiving. private MailServer server; // The user running this client. private String user;
/** * Create a mail client run by user and attached to the given server. */ public MailClient(MailServer server, String user) { this.server = server; this.user = user; }
/** * Return the next mail item (if any) for this user. */ public MailItem getNextMailItem() { return server.getNextMailItem(user); }
/** * Print the next mail item (if any) for this user to the text * terminal. */ public void printNextMailItem() { MailItem item = server.getNextMailItem(user); if(item == null) { System.out.println("No new mail."); } else { item.print(); } }
/** * Send the given message to the given recipient via * the attached mail server. * @param to The intended recipient. * @param message The text of the message to be sent. */ public void sendMailItem(String to, String message) { MailItem item = new MailItem(user, to, message); server.post(item); } }
MailItem.java
``````````````````````
/** * A class to model a simple mail item. The item has sender and recipient * addresses and a message string. * * @author David J. Barnes and Michael Klling * @version 2016.02.29 */ public class MailItem { // The sender of the item. private String from; // The intended recipient. private String to; // The text of the message. private String message;
/** * Create a mail item from sender to the given recipient, * containing the given message. * @param from The sender of this item. * @param to The intended recipient of this item. * @param message The text of the message to be sent. */ public MailItem(String from, String to, String message) { this.from = from; this.to = to; this.message = message; }
/** * @return The sender of this message. */ public String getFrom() { return from; }
/** * @return The intended recipient of this message. */ public String getTo() { return to; }
/** * @return The text of the message. */ public String getMessage() { return message; }
/** * Print this mail message to the text terminal. */ public void print() { System.out.println("From: " + from); System.out.println("To: " + to); System.out.println("Message: " + message); } }
MailServer.java
``````````````````````````
import java.util.ArrayList; import java.util.List; import java.util.Iterator;
/** * A simple model of a mail server. The server is able to receive * mail items for storage, and deliver them to clients on demand. * * @author David J. Barnes and Michael Klling * @version 2016.02.29 */ public class MailServer { // Storage for the arbitrary number of mail items to be stored // on the server. private List
/** * Construct a mail server. */ public MailServer() { items = new ArrayList<>(); }
/** * Return how many mail items are waiting for a user. * @param who The user to check for. * @return How many items are waiting. */ public int howManyMailItems(String who) { int count = 0; for(MailItem item : items) { if(item.getTo().equals(who)) { count++; } } return count; }
/** * Return the next mail item for a user or null if there * are none. * @param who The user requesting their next item. * @return The user's next item. */ public MailItem getNextMailItem(String who) { Iterator
/** * Add the given mail item to the message list. * @param item The mail item to be stored on the server. */ public void post(MailItem item) { items.add(item); } }
Please help
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