new)implements keyword to implementWhen a class implements an interface, it has an “is-a” relationship with the interface
You are building a simple notification system.
Different notification types all need to be sent and described, but the details of how they work differ.
We will use an interface to define the shared contract.
Create an interface called Notification with the following method signatures:
String getRecipient() — returns who the notification is going toString getMessage() — returns the body of the notificationvoid send() — prints a formatted line simulating sending the notificationInterface methods are implicitly public because they are a public contract for external use
Class EmailNotification
recipientEmail (String), subject (String), body (String)getRecipient() returns the email addressgetMessage() returns the subject and body combined: “Subject: … | Body: …”send() prints something like: [EMAIL] To: alice@example.com | Subject: Meeting at 3pmpublic class EmailNotification implements Notification {
private String recipientEmail;
private String subject;
private String body;
public EmailNotification(String email, String subject, String body) {
this.recipientEmail = email;
this.subject = subject;
this.body = body;
}
@Override
public String getRecipient() {
return recipientEmail;
}
@Override
public String getMessage() {
return "Subject: " + subject + " | Body: " + body ;
}
@Override
public void send() {
System.out.print("[EMAIL] To: " + recipientEmail);
System.out.println(" | Subject: " + subject);
}
}Class TextNotification
phoneNumber (String), message (String)getRecipient() returns the phone numbergetMessage() returns the message textsend() prints something like: [TEXT] To: 555-1234 | Message: Don’t forget the meeting!public class TextNotification implements Notification {
private String phoneNumber;
private String message;
public TextNotification(String phoneNumber, String message) {
this.phoneNumber = phoneNumber;
if (message.length() > 160) {
System.out.println("Message longer than 160, truncated");
message = message.substring(0, 159);
}
this.message = message;
}
@Override
public String getRecipient() {
return phoneNumber;
}
@Override
public String getMessage() {
return message;
}
@Override
public void send() {
System.out.print("[TEXT] To: " + phoneNumber);
System.out.println(" | Message: " + message);
}
}In a main method, create the following array:
Notification[] inbox = {
new EmailNotification("alice@example.com", "Meeting at 3pm", "Please join the Zoom link."),
new TextNotification("555-1234", "Don't forget the meeting!"),
new EmailNotification("bob@example.com", "Homework due", "Project 2 is due Friday."),
new TextNotification("555-5678", "Your package has been delivered.")
};
Loop over the array and call send() on each notification, then print a count of how many notifications were sent.
Write a static method (outside of main) with this signature:
public static void sendToRecipient(Notification[] notifications, String recipient)
It should call send() only on notifications whose getRecipient() matches the given recipient. Call it from main to send only Alice’s notifications.
Submit your solution files to gradescope:
EmailNotification[] and TextNotification[] — instead?Notification[] parameter. Could you pass it an array that contains a mix of EmailNotification and TextNotification objects? Why?Comparable interfaceNumbers and Strings are easy to compare.
100 smaller, equal, or larger than 50?apple smaller, equal, or larger than kiwi?What if we have notifications or chairs? How to we compare chairs?
Comparable interfaceComparable<T>int compareTo(T o)
x.compareTo(y) should return 0 if x.equals(y) is trueComparable interfaceComparable interface supports generic typescompareTo() is applied only to data of a compatible typecompareTo() are detected at compile timeCheck how the String class implements Comparable
Create a Chair class that implements Comparable<T>
import java.util.Arrays;
Chair[] chairs = new Chair[]{
new Chair(10, 15),
new Chair(4, 10),
new Chair(10, 5),
new Chair(1, 5)
};
Arrays.sort(chairs); // sorting based on the compareTo methodSubmit your Chair.java file to gradescope
public class Chair implements Comparable<Chair> {
private int width;
private int depth;
public Chair(int width, int depth) {
this.width = width;
this.depth = depth;
}
public int getDepth() {
return depth;
}
public int getWidth() {
return width;
}
public int getArea() {
return width * depth;
}
@Override
public int compareTo(Chair o) {
if (o.getArea() == this.getArea()) return 0;
if (o.getArea() < this.getArea()) return 1;
return -1;
}
@Override
public String toString() {
return width + " " + depth;
}
}How would we implement compareTo(Notification other) in our Notification project so that our Notification[] array would still work?
implements in an interfaceObject (which contains toString() and equals()) was an interface rather than a class, how would things change?