简体   繁体   中英

Need some assistance/suggestions with short java code

Ok so I just posted a question and it was way too long so nobody fully read it or tried to help so I deleted that and I'm going to make this question much more specific and less of a rambling story. I would just like some help troubleshooting a class in a java program.

Here's what the class (This is one of 6 classes needed for this assignment) should do:

"Write a fully documented class named Queue that is a subclass of Vector (or any class of your choice). Your queue class should define operations for isEmpty, enqueue, dequeue and size*, along with a constructor that defines an empty queue. Read through the Java API documentation to find out which methods act the same as enqueue and dequeue. Remember that your queue will hold items of type Object. *the "size" method simply returns the number of elements in the queue"

Here's what I have written so far for this class:

import java.util.Vector;

    public class Queue extends Vector {

Vector<Object> line;

public Queue() {

}

public boolean isEmpty() {
    if (line.isEmpty())
        return true;
    else
        return false;
}

public void enqueue(Customer customer) {
    line.addElement(customer);
}

public void dequeue() {
    line.removeElementAt(0);
}

public int size() {
    return elementCount;
}

}

This is in the grading rubric:

Queue class defined correctly as a subclass of Vector by using "extends" and by using the available Vector methods (Queue class should not have an instance of a Vector inside it). Any other class may be used instead of Vector. [10 points]

I have seen the vector api so please do not simply link me to it (no offense) because I'm still having trouble understanding this. Any help on what to do is greatly appreciated.

You are requested to write a new class called Queue

  1. with the methods isEmpty , enqueue , dequeue and size
  2. your class should extend Vector (or something else that is like a Vector )
  3. your class should use the methods of the extended class to implement the four required methods.

For example:

public class Queue extends Vector{

    // here you are implementing isEmpty
    public boolean isEmpty() {
        // ... by delegating to the method with the same name in the Vector class
        return super.isEmpty();
    }

    // ...

}

As documented, you should not have an instance of Vector in your class.

As Jakub says, you will not have to implement methods that have the same name as the method in Vector (so go ahead and remove that isEmpty method). Where you will have to do some translation is, for example:

public void enqueue(Object o) {
    // what method(s) could you use to simulate an enqueue of this object? 
    // Maybe `add(o)`?
}

Perhaps you would find it helpful to think of your new class, Queue in this case, as a wrapper or skin around the class you are extending. If you were to create Queue as a subclass of Vector and do nothing else with it, it would still have all of the state and functionality of a Vector . By adding the line instance variable, you essentially have two vectors -- one that you inherit and another one that you have added as extra state.

Once you think of your new class as inheriting all of the functionality of the Vector class, it may be easier to see that

  1. You may have some of the functions you need already, and
  2. Your new methods will call Vector functions to operate on the state you already have.

(I am deliberately addressing the conceptual issues here, not the syntactic ones, since that seems to be the real point of your question.)

Well, if your Queue can extend any class of your choice, you could just have extended LinkedList, which already has the Queue functionality implemented. You would only have to declare those methods that does not have the same name (eg dequeue() and poll()).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM