Question

please write code in java, assignment is due soon Thanks. public interface Named The Named interface...

please write code in java, assignment is due soon Thanks.

public interface Named

The Named interface is a simple interface which applies to anything which has a name. The only thing required is the following method:

  • String name() which returns the name of the instance.

public interface MessageSink, It includes:

void deliver(Named sender, String message) delivers the specified message from the given sender.

public interface Sharable, which is Named, It includes no method.

public abstract class GeneralCapability It incudes:

  • The getter and setter getOwner and setOwner, for the Named participant who owns the user interface.
  • The getter getSharable, for the Sharable currently associated with the Capability.
  • @Override public void linkSharable(Sharable s) creates an association between the input Sharable and this object, if the Sharable is a compatible type. If it is not a compatible type (which recognizeSharable will tell us), then it should throw a SharableException with the message "cannot handle Sharable NAME", where NAME is the name of the Sharable. Likewise, if the Capabilitiy has already been associated with a different Sharable, then it should throw a SharableException with the message "Capability is already linked to a Sharable". Otherwise, we would create the association with the Sharable. What do we mean when we say create an association? Maybe it would help to think of this as an ordinary setter method with a few extra checks built-into it.
  • @Override public void unlinkSharable(Sharable s) will disassociate the current Sharable from the object (sets it to null); unless the input is either null or some completely different Sharable, in which case it would throw a SharableException with the message "non-matching Sharable".

public class Chat, which is Sharable, It includes following method

  • public void send(Named sender, String message) delivers the specified message to all of the MessageSinks in the list, in order.
  • public void addSink(MessageSink s) adds a MessageSink to the list of sinks.
  • public boolean removeSink(MessageSink s) removes the specified MessageSink from the list of sinks, returning true if the sink was found and removed, and false otherwise.
  • @Override public String name() returns the string "chat".

public class ChatHandler, which is a GeneralCapability and provides MessageSink

It must have the following methods:

  • public Chat getChat() returns the Chat object associated with this capability, or null if there isn't an association yet.
  • @Override public boolean recognizeSharable(Sharable s) returns true only if the Sharable is a Chat.
  • @Override public void linkSharable(Sharable s) in addition to linking the Sharable as in the GeneralCapability class, this method should register the ChatHandler as a MessageSink for the Chat feed by using its addSink method.
  • @Override public void unlinkSharable(Sharable s) in addition to unlinking the Sharable as in the GeneralCapability class, this method should deregister the ChatHandler as a MessageSink from the Chat feed by using its removeSink method.
  • public void send(String message) if the Chat is associated, then this method will send the specified message to the Chat feed from the current user.
  • @Override public void deliver(Named sender, String message) will print out the incoming message in the format "USER: MESSAGE" (including a newline).
0 0
Add a comment Improve this question Transcribed image text
Answer #1

ANSWER :

Java codes to copy:-

package interface_problem;

public interface Named {
//Returns the name of the instance(in the implemented class)
String name();
}

package interface_problem;

public interface Sharable extends Named{

}

package interface_problem;

public interface MessageSink {
//Delivers the specified message from the given sender
void deliver(Named sender, String message);
}

package interface_problem;

public abstract class GeneralCapability {
//Named participant who owns the user interface
private Named owner;
//Sharable currently associated with the Capability
private Sharable sharable;
  
  
public Named getOwner() {
return owner;
}
  
public void setOwner(Named owner) {
this.owner = owner;
}
  
public Sharable getSharable() {
return sharable;
}
  
//Creates an association between the input Sharable and this object,following constraints
public void linkSharable(Sharable s) throws SharableException
{
//Checking if the Sharable is a compatible type
if (this.recognizeSharable(s))
this.sharable = s;
//Checking if the Capabilitiy has already been associated with a different Sharable
else if(this.sharable != null)
throw new SharableException("Capability is already linked to a Sharable");
//If it is not a compatible type
else
throw new SharableException("cannot handle Sharable " + s.name());
  
}
//To disassociate the current Sharable from the object
public void unlinkSharable(Sharable s) throws SharableException
{
if(s != null && (s instanceof Chat))
this.sharable = null;
//If the input is either null or some completely different Sharable
else
throw new SharableException("non-matching Sharable");
  
}
//Implementation in child class
public abstract boolean recognizeSharable(Sharable s);
  
  

}

package interface_problem;

public class ChatHandler extends GeneralCapability implements MessageSink{
  
//Chat object which is null
private Chat obj;
//Returns chat object associated with this capability, or null if there isn't an association yet.
public Chat getChat()
{
return obj;
}
  
public void linkSharable(Sharable s) throws SharableException
{
super.linkSharable(s);
//Registers the ChatHandler as a MessageSink for the Chat feed.
((Chat)getSharable()).addSink(this);
}
  
public void unlinkSharable(Sharable s) throws SharableException
{
super.unlinkSharable(s);
//Deregister the ChatHandler as a MessageSink from the Chat feed
obj.removeSink(this);
}
  
//Checks if the Sharable is a Chat
public boolean recognizeSharable(Sharable s)
{
return s instanceof Chat;
}
  
public void send(String message)
{
//If the Chat is associated
if(getSharable() != null)
//send the specified message to the Chat feed from the current user.
((Chat)getSharable()).send(getOwner(), message);
}
//Print out the incoming message in the format "USER: MESSAGE" (including a newline)
public void deliver(Named sender, String message)
{
System.out.println(sender.name() + ":" + message + "\n");
}
}

package interface_problem;

import java.util.ArrayList;
import java.util.List;

public class Chat implements Sharable{
  
//List of sink
private List<MessageSink> sink_list = new ArrayList<>();
  
//Delivers the specified message to all of the MessageSinks in the list, in order(as list
//maintains insertion order
public void send(Named sender, String message)
{
for(MessageSink each:sink_list)
{
each.deliver(sender, message);
}
}
//Adds a MessageSink to the list of sinks
public void addSink(MessageSink s)
{
sink_list.add(s);
}
  
// Removes the specified MessageSink from the list of sinks,
//returning true if the sink was found and removed, and false otherwise.
public boolean removeSink(MessageSink s) {
return sink_list.remove(s);
}
//Returns the string "chat".
@Override
public String name() {
return "chat";
}
  
}

package interface_problem;

public class SharableException extends Exception {
  
public SharableException(String message)
{
super(message);
}
}

package interface_problem;

public class Demo {

public static void main(String[] args) {
  
//Creating chat and chathandler instances
Chat chat = new Chat();
  
ChatHandler handler = new ChatHandler();
  
//Linking the chat with the handler
try {
handler.linkSharable(chat);
} catch (SharableException e) {
System.out.println(e.getMessage());
  
}
//Setting the owner of the sharable
handler.setOwner(chat);
  
handler.send("Hello");
  
//Handling exceptions by deliberately setting chat instance to null
chat = null;
  
try {
handler.unlinkSharable(chat);
handler.linkSharable(chat);
} catch (SharableException e) {
  
System.out.println(e.getMessage());
}
  
  
}

}


Screenshot of a sample output:-

chat:Hello non-matching Sharable Picked up _JAVA_OPTIONS: -Xmx512M

Add a comment
Know the answer?
Add Answer to:
please write code in java, assignment is due soon Thanks. public interface Named The Named interface...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • please write code in java and comment. thanks. the program is about interface . Implement the...

    please write code in java and comment. thanks. the program is about interface . Implement the basics of Fitness and types of Fitness: Aerobic. Implement specific Fitness types such as Swimming, Cycling, Fitness Task: public interface Fitness (10pts) This will be used as a starting point for deriving any specific Fitness type. Every fitness exercise type has one or more muscle group it affects. Therefor a Fitness has the following abstarct method (Note that all methods in an interface are...

  • I Need UML Design for the following Java code: Colorable interface: public interface Colorable { //a...

    I Need UML Design for the following Java code: Colorable interface: public interface Colorable { //a void method named howToColor(). void howToColor(); } GeometricObject class: public class GeometricObject { } Sqaure class: public class Square extends GeometricObject implements Colorable{ //side variable of Square double side;    //Implementing howToColor() @Override public void howToColor() { System.out.println("Color all four sides."); }    //setter and getter methods for Square public void setSide(double side) { this.side = side; }    public double getSide() { return...

  • q2: Write a public class named MythMouseListener that implements the Mouselistener interface. This class will have...

    q2: Write a public class named MythMouseListener that implements the Mouselistener interface. This class will have a public constructor that takes a JTextArea and a JLabel as parameters and stores these in instance variables. Override the mouseEntered method to . display the text from the JTextArea on the JLabel in all upper case letters. Then, override the mouseReleased method to display the text from the JTextArea on the JLabel in all lower case letters. The other three methods from the...

  • JAVA PROGRAMMING In this final review lab from our intro course, use the Name class you...

    JAVA PROGRAMMING In this final review lab from our intro course, use the Name class you created in the previous lab. In this lab, create a Student class with the following class variable: Student name: Name (Note: Name is a datatype) Student Id: String Create the getter and setter methods. In addition to these methods, create a toString() method, which overrides the object class toString() method. This override method prints the current value of any of this class object. Complete...

  • QUESTION 5 (15 Marks) Inheritance and Polymorphism Design a Ship class that has the following members:...

    QUESTION 5 (15 Marks) Inheritance and Polymorphism Design a Ship class that has the following members: • A private String data field named name for the name of the ship. • A private String data field named yearBuilt for the year that the ship was built. • A constructor that creates a ship with the specified name and the specified year that the ship was built. • Appropriate getter and setter methods. A toString method that overrides the toString method...

  • Code using Java What is an interface? Complete the following code: interface IExample{ public void print();...

    Code using Java What is an interface? Complete the following code: interface IExample{ public void print(); } class Example1 implements IExample{ . . . } // Driver class public class Questions { public static void main(String[] args) { . . . } } To have this message when you execute it: Implementation of an interface

  • In a file named LLBag.java, write a class called LLBag that implements the Bag interface using...

    In a file named LLBag.java, write a class called LLBag that implements the Bag interface using a linked list instead of an array. You may use a linked list with or without a dummy head node. Bag interface code: /* * Bag.java * * Computer Science 112, Boston University */ /* * An interface for a Bag ADT. */ public interface Bag { /*    * adds the specified item to the Bag. Returns true on success    * and...

  • Java -Create an interface and implement it In the murach.db package, create an interface named IProductDB....

    Java -Create an interface and implement it In the murach.db package, create an interface named IProductDB. This interface should specify this abstract method: public abstract Product get(String productCode); Modify the ProductDB class so it implements the IProductDB interface. Write the code for the new ‘get’ method. Then remove the getProductByCode method. In the Main class, modify the code so it works with the new ProductDB class. This code should create an instance of the IProductDB interface like this: IProductDB db...

  • In Java. Please use the provided code Homework 5 Code: public class Hw05Source { public static...

    In Java. Please use the provided code Homework 5 Code: public class Hw05Source { public static void main(String[] args) { String[] equations ={"Divide 100.0 50.0", "Add 25.0 92.0", "Subtract 225.0 17.0", "Multiply 11.0 3.0"}; CalculateHelper helper= new CalculateHelper(); for (int i = 0;i { helper.process(equations[i]); helper.calculate(); System.out.println(helper); } } } //========================================== public class MathEquation { double leftValue; double rightValue; double result; char opCode='a'; private MathEquation(){ } public MathEquation(char opCode) { this(); this.opCode = opCode; } public MathEquation(char opCode,double leftVal,double rightValue){...

  • Suppose we have the following Java Interface: public interface Measurable {    double getMeasure(); // An...

    Suppose we have the following Java Interface: public interface Measurable {    double getMeasure(); // An abstract method    static double average(Measurable[] objects) { // A static method    double sum = 0;    for (Measurable obj : objects) {    sum = sum + obj.getMeasure();    }    if (objects.length > 0) { return sum / objects.length; }    else { return 0; }    } } Write a class, called Person, that has two instance variables, name (as...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT