1

I wanted to make an elevator system where you send information from the following: elevator (data) -> scheduler (buffer) -> floor (receive) The Floor subsystem and the Elevators are the clients in the system; the Scheduler is the server. when I pressed run some of the issues were:

class elevator is shown below:

package elevator;
import java.util.HashSet;
import java.util.Set;

public class elevator {
    
public enum State {
    MOVING_UP, MOVING_DOWN, STOPPED
}

private int floor;

private State state;

@SuppressWarnings({ })
private Set<Integer> pressedButtons = (Set<Integer>) new HashSet<Integer>();

public elevator() {
    
    state = State.STOPPED;
}

public int getFloor() {
    return floor;
}

public void setFloor(int floor) {
    this.floor = floor;
    pressedButtons.remove(floor);
}

public State getState() {
    return state;
}

public void setState(State s) {
    state = s;  
}
public boolean isMoving() {
    return state == State.MOVING_UP || state == State.MOVING_DOWN;
}

public void buttonPressed(int i) {
    pressedButtons.add(i);
}

public Set<Integer> getButtons() {
    return pressedButtons;
}

public String toString() {
    return  "Floor: " + floor + "\n" + "\t State: " + state + "\n";
}

}

Exception in thread "Thread-0" java.lang.ClassCastException: class elevator.HashSet cannot be cast to class java.util.Set (elevator.HashSet is in unnamed module of loader 'app'; java.util.Set is in module java.base of loader 'bootstrap') at elevator.elevator.(elevator.java:21) at elevator.elevatorExchange.retrieveData(elevatorExchange.java:30) at elevator.elevatorExchange.run(elevatorExchange.java:19) at java.base/java.lang.Thread.run(Thread.java:835)

Thread 1: elevator package elevator;

import java.util.concurrent.BlockingQueue;

public class elevatorExchange implements Runnable{


private BlockingQueue<elevator> messages;

public elevatorExchange(BlockingQueue<elevator> messages) {
    this.messages = messages;
    
}

@Override
public void run() {
    try {
        elevator elevatorData = retrieveData();
        messages.put(elevatorData);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    
}

private elevator retrieveData() throws InterruptedException {
    Thread.sleep(5000);
    elevator elevatorData = new elevator();
    return elevatorData;
}


}

Thread 2: Scheduler. Scheduler is only being used as a communication channel from the Floor thread to the Elevator thread

package scheduler;

import java.util.concurrent.BlockingQueue;

import elevator.elevator;

public class Scheduler implements Runnable {

private BlockingQueue<elevator> messages;

public Scheduler(BlockingQueue<elevator> messages) {
    this.messages = messages;   
}
@Override
public void run() {
    elevator elevatorData = messages.take();

}

Thread 3: The floor (this will receive it from the scheduler). This is the part I'm struggling with the most, I am trying to make sure the data passed down to the floor is from the scheduler and not the elevator, but my IDE keeps making changes to the data type that's running a lot of exceptions.

package floor;
import java.util.concurrent.BlockingQueue;
import elevator.elevator;

public class FloorReceiver implements Runnable{

private BlockingQueue<elevator> messages;

public FloorReceiver(BlockingQueue<elevator> messages) {
    this.messages = messages;
}
@Override
public void run() {
    try {
        System.out.println("waiting for data from elevator");
        elevator elevatorData = messages.take();
        System.out.println("data from elevator" + elevatorData);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }   
}
}

Testing:

package floor;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

import elevator.elevator;
import elevator.elevatorExchange;

public class elevatorToFloorTest {

public static void main(String[] args) {
    BlockingQueue<elevator> messages = new ArrayBlockingQueue<elevator>(1);
    elevatorExchange retriever = new elevatorExchange(messages);
    FloorReceiver receiver = new FloorReceiver(messages);
    
    new Thread(retriever).start();
    new Thread(receiver).start();

}
}
4
  • You seem to have a class elevator.HashSet. Is there some reason you're using this instead of Java's HashSet? Also, you don't show the class elevator, which seems to be where the trouble lies. Commented Feb 6, 2021 at 1:03
  • I had a [HashSet] created before, there's no special reason for not using java's HashSet. the class [elevator] has been added Commented Feb 6, 2021 at 1:54
  • are you aware that the threads will die when they finish executing the methods on run(), right? You didn't implement any loop whatsoever.... or maybe this is intended, dunno.. Commented Feb 6, 2021 at 1:58
  • See my answer below. Commented Feb 6, 2021 at 2:42

1 Answer 1

1

This line is the problem:

private Set<Integer> pressedButtons = (Set<Integer>) new HashSet<Integer>();

The exception message tells us that the HashSet is not a java.util.HashSet but is something you wrote:

 class elevator.HashSet cannot be cast to class java.util.Set

i.e., the HashSet is defined in the elevator package.

Your HashSet is not implementing the Set interface. If it were, then you would not need the cast. Since it isn't, the cast cannot magically make it work.

It seems the compiler would have told you, except you used @SuppressWarnings. (I'm not sure what the empty list does).

On the other hand, the code for the elevator class that you have now posted shows an import for java.util.HashSet, not a homebrew implementation. This cannot produce the error shown. Have you failed to rebuild everything?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.