0

I have created a class and import my interfaces but how to do the rest, what should I do?

Here I have anything but it doesn't work I mean ClassCastExeption doesn't work

code example :

import java.util.LinkedList;
import java.util.Queue;

import com.revmetrix.code_test.linkify_queue.ProcessingQueue;
import com.revmetrix.code_test.linkify_queue.ProcessingQueueFactory;

public class Solution {

    ProcessingQueue newQueue;
    ProcessingQueueFactory runFactory;

    Solution() {
        ProcessingQueueFactoryClass runFactory = new ProcessingQueueFactoryClass();

        ProcessingQueue newQueue = runFactory.createQueue();

    }

    /**Your ProcessingQueueFactory must contain two methods: one for creating new
     * queues and one for cleaning up after them. In `createQueue`, just create a
     * new ProcessingQueue, performing any necessary initialization of the queue
     * before it is returned. `createQueue` will be called multiple times during our
     * automated tests. In `stopQueue`, perform any cleanup required for a queue
     * created by your `createQueue` method. (E.g., stop threads, if necessary for
     * your solution.) `stopQueue` will be called once for each queue created with
     * `createQueue`.
     */
    class ProcessingQueueFactoryClass implements ProcessingQueueFactory {

        public void stopQueue(ProcessingQueue p) {

        }


        // TODO encok burda sikinti var, queue yaratacam ama Proccesing Queue donderiyor bu
        // asil eleman ekleyecegim queue yi ProcessingQueue nin icinde mi yaratcam?
        // ProcessingQueue bi interface bu arada, bunu implement eden class ProcessingQueueClass yazdim 
        // onun icinde queue olsun dedim yine gormuyor zalim queue olarak 
        //bi loop var ProcessingQueue ile ProcessingQueueFactory arasinda, anlamadim!
        public ProcessingQueue createQueue() {



            Queue<String> newQueue = new LinkedList<String>();

            return (ProcessingQueue) newQueue;
        }

    }

    /**
     * Your ProcessingQueue implementation will receive unprocessed textual data
     * from multiple concurrent producers through its `offer` method. Your queue
     * must provide a transformed version of the data via its `poll` method. The
     * transformation is described below. As a queue, the data received from
     * `poll` must be FIFO (first-in, first-out) with respect to calls to
     * `offer`; i.e., the first items in should also be the first items out. If
     * data is available, `poll` must remove it from queue and return it. If no
     * data is available, then `poll` must return null.
     */
    class ProcessingQueueClass implements ProcessingQueue {

        ProcessingQueueFactoryClass openFactory = new ProcessingQueueFactoryClass();
        ProcessingQueue newQueue = openFactory.createQueue();

        /**The linkify transformation should find raw URLs prefixed with "http(s)://" in
         * the input text and convert them to HTML links. For example,
         * http://www.example.com becomes <a href="http://www.example.com">www.example.com</a>. Do not include the scheme
         * (http(s)://) in the anchor text. Any URLs that are already within HTML links
         * should not be modified. You can assume the input text contains multiple words
         * separated by white space (i.e. spaces, new lines) or punctuations (commas,
         * periods, etc.)
         */
        public String linkifyTransformation(String s){
            // System.out.println(s);
            String[] splitArray = s.split(" ");
            String transformedString = "";

            for (int i = 0; i < splitArray.length; ++i) {
                if (splitArray[i].startsWith("https://")) {
                    transformedString += "<a href=\"" + splitArray[i] + "\">"
                            + splitArray[i].substring(8) + "</a> ";
                } else if (splitArray[i].startsWith("http://")) {
                    transformedString += "<a href=\"" + splitArray[i] + "\">"
                            + splitArray[i].substring(7) + "</a> ";
                } else {
                    transformedString += splitArray[i] + " ";
                }
            }

            //System.out.println(transformedString);            
            return transformedString;
        }

        public boolean offer(String s) {

            //returns transformedString 
            linkifyTransformation(s);
            // we need to add the transformedString to our Queue, Where should we create?;

            //how to return true or false?
            return false;
        }

        public String poll() {

            return "";
        }

    }

    public static void main(String[] args) {
//      String s = "The quick http://www.brown.com/fox 
//                 jumps https://over.com the lazy dog foo www.bar.com 
//                 is <a href=\"http://myfavorite.com\">my favorite</a> "
//                 + "These aren't the droids you're looking for.";

        //Solution sol = new Solution();
        //ProcessingQueueClass pqs = sol.new ProcessingQueueClass();
        //pqs.linkifyTransformation(s);


}

}

Here is some details which might help.

Revmetrix Linkify Queue Coding Problem the input and output of string data with a transformation. This queue must correctly handle concurrent operations with reasonable performance. Implement ProcessingQueue and ProcessingQueueFactory from the JAR. Your ProcessingQueue implementation will receive unprocessed textual data from multiple concurrent producers through its offer method. Your queue must provide a transformed version of the data via its poll method. As a queue, the data received from poll must be FIFO (first-in, first-out) with respect to calls to offer;

Ada
  • 1
  • 3
  • I need help, a little help, not warning, no it is not duplicate because I focus on two interfaces and I import them from a jar file but how to fill the latter? – Ada Feb 06 '16 at 10:19
  • What exactly is the problem you are facing in the above situation ? – rootExplorr Feb 06 '16 at 10:53

1 Answers1

0

In general, when you implement an interface in java you need to implement it's methods and define the body for the methods defined in that interface.

If you don't want to implement all of that interface's methods bodies, you would change your class into an abstract class and mark that methods as abstract too.

I don't know how familiar you are with interfaces and classes in java, but I think reading some tutorials like http://tutorials.jenkov.com/java/interfaces.html would be helpful.

[EDIT after Question Changed]

The ClassCastException is happening here:

return (ProcessingQueue) newQueue;

because you illegally cast an instance of Queue<String> to an interface of type ProcessingQueue. This cast can not happen because Queue<String> or none of it's parents didn't implement ProcessingQueue interface.

But due to your assignment specification, your implementation is not right.

You should implement one class to implement ProcessingQueue and another to implement ProcessingQueueFactory (Til now you did this part).

You should not create instances of ProcessingQueueFactoryClass and ProcessingQueue in your ProcessingQueueClass class, so remove these two line:

ProcessingQueueFactoryClass openFactory = new ProcessingQueueFactoryClass();
ProcessingQueue newQueue = openFactory.createQueue();

Also you should keep the Queue of linkified Strings in your ProcessingQueue class. So move the following line from public ProcessingQueue createQueue() method to ProcessingQueue class:

Queue<String> newQueue = new LinkedList<String>();

to

class ProcessingQueueClass implements ProcessingQueue {
    Queue<String> newQueue = new LinkedList<String>();
    ...
}

In your public boolean offer(String s) you should get the result of linkifyTransformation(s); and add it to the newQueue and in the public String poll() method you remove and then return the first item of newQueue to maintain the FIFO paradigm.

Now about the ProcessingQueueFactoryClass: I this class you have two simple methods. The work of createQueue() method is very simple: Just create and instance of ProcessingQueueClass class and return it.

And the other method stopQueue(ProcessingQueue p): This method gets an instance of ProcessingQueue as an argument. Because your class ProcessingQueueClass implemented ProcessingQueue, the runtime type of input argument would be of type ProcessingQueueClass. And because you have a Queue<String> member variable in your ProcessingQueueClass class, the purpose of this method is to clear the Queue<String> newQueue items and release it.

Hope this would be helpful,

Good Luck.

STaefi
  • 4,297
  • 1
  • 25
  • 43
  • Thank you very much, I want to implement two interface in java so I need to implement it's methods and define the body for the methods defined in that interface. – Ada Feb 06 '16 at 11:08
  • Your welcome, also I strongly recommend to read that tutorial carefully. If you've found this post helpful, you can mark it as the answer by checking the green check mark in left hand side of this post. – STaefi Feb 06 '16 at 11:42
  • Can you check again, your solution force me to do much but still there are errors. – Ada Feb 07 '16 at 18:59
  • @Ada: please provide some info about what is exactly the problem? Is there an exception? Provide the code you are running and also the exception stacktrace. Now your main method is all commented and we don't have your library for that interfaces to run your codes. So please provide more exact information. – STaefi Feb 07 '16 at 20:22
  • Thank you very much can I get in touch? – Ada Feb 08 '16 at 21:21
  • We are already in touch :-) see the edit. – STaefi Feb 09 '16 at 07:10