2

Basically, I want to do something like this:

patientReference = ((Patient) TreatmentRoomQueue).peekFront();

But my IDE claims the types are inconvertible. (required: Patient; found: TreatmentRoomQueue<Patient>). You can see that I'm attempting to cast the Node as a Patient object and that I am attempting to call my peekFront method that should return the first node in the list. It is however apparent that this is illegal. My syntax could be wrong, or maybe I'm just approaching this the wrong way.

My motivation behind this is that I have a program for running a (fictional) Emergency Room. I need to get someone from my TreatmentRoomQueue, discharge them, and then, if someone is in my WaitingRoomQueue, move them to the Treatment Room. I am not using the Java built in LinkedList class, I am using my own linked list (which I have used previously and know it works).

I could just say screw it and used an array instead of a linked list, but since linked lists are a bit harder for me to understand, I think there's more to be learned from a linked list implementation.

Any pointers, code snippets, advice, or whatever would be greatly appreciated! Thanks for reading.

I declare TreatmentRoomQueue in my Main class this way:

TreatmentRoomQueue<Patient> TreatmentRoomQueue = new TreatmentRoomQueue();

Here's the code for TreatmentRoomQueue:

public class TreatmentRoomQueue <ClassType> 
{
 private Node frontQueueNodeRef = null;
 private Node backQueueNodeRef = null;
 private int counter = 0;
 private class Node 
 {
 private ClassType classTypeObjectRef;
 private Node nextNodeRef;
 Node(ClassType newClassTypeObjectRef)
 {
 classTypeObjectRef = newClassTypeObjectRef;
 }
 }
 private Node peekFront()
 {
 return frontQueueNodeRef;
 }
 public void enqueue(ClassType enqueueObjectRef) 
 { 
 Node queueNodeRef = new Node(enqueueObjectRef);
 if (frontQueueNodeRef == null)
 {
 frontQueueNodeRef = backQueueNodeRef = queueNodeRef;
 }
 else {
 backQueueNodeRef.nextNodeRef = queueNodeRef;
 backQueueNodeRef = queueNodeRef;
 counter++;
 }
 }
 public ClassType dequeue() 
 {
 if ( frontQueueNodeRef == null )
 {
 return null;
 } else {
 ClassType firstClassTypeObjectRef = frontQueueNodeRef.classTypeObjectRef;
 frontQueueNodeRef = frontQueueNodeRef.nextNodeRef;
 counter--;
 return firstClassTypeObjectRef;
 }
 }
 public boolean isFull()
 {
 return false;
 }
 public boolean isEmpty()
 {
 return frontQueueNodeRef == null;
 } 
}
asked Oct 14, 2012 at 1:05
1
  • 1
    List as in the interface doesn't have a peekFront() method. We'd have to see the implementation of that. Commented Oct 14, 2012 at 1:06

2 Answers 2

4

First, you are casting the list, not the element that you get from it. All you need to do is moving the parentheses to the right place:

patientReference = (Patient) (TreatmentRoomQueue.peekFront());

The way you placed the parentheses, Java tried casting TreatmentRoomQueue to Patient first, and after that attempted to call the peekFront() method on the Patient object.

Next, you need to make a public method for looking at the front element of your queue: replace your

private Node peekFront()

method with

public ClassType peekFront()
{
 return frontQueueNodeRef.classTypeObjectRef;
}

Since classTypeObjectRef is private, you should make it public to make it available to peekFront.

answered Oct 14, 2012 at 1:07
4
  • My initial thought was that this was a doubly-linked list and each node had .peekFront() method that returned a reference to the element in front of it. Commented Oct 14, 2012 at 1:11
  • Thank you, but I'm still getting the same error with that snippet of code, along with some other errors. Firstly, peekFront() has private access in TreatmentRoomQueue (thought I know how to fix this! :) ), secondly, the inconvertible type issue is still there (required: Patient; found: TreatMentRoomQueue<Patient>.Node. Next, there's an illegal start of type. And lastly, it can't fin the symbol variable TreatmentRoomQueue in class patient. It is obvious that I have something horrendously wrong. :[ Commented Oct 14, 2012 at 1:11
  • I am using my own implementation of a linked list queue in TreatmentRoomQueue (and WaitingRoomQueue). If it's helpful I could edit my original post with the code for one of them (they are the same just with different names). Commented Oct 14, 2012 at 1:15
  • @instago Absolutely - please provide the declaration of the TreatmentRoomQueue variable, and its class as well. Commented Oct 14, 2012 at 1:19
1

Any pointers, code snippets, advice, or whatever would be greatly appreciated!

Since you asked, here is my advice / whatever:

A variable called TreatmentRoomQueue is a CODING STANDARD VIOLATION in all sensible Java coding standards. You have used the class identifier pattern for a variable. A variable identifier should have the form treatmentRoomQueue (unless it is a static final constant ... in which case it should be TREATMENT_ROOM_QUEUE).

Ordinarily this is a small thing, but in this case you have compounded the problem:

 TreatmentRoomQueue<Patient> TreatmentRoomQueue = new TreatmentRoomQueue();

Now you have a variable name and a type name that are identical ... but name different things!!! The JLS specifies how an identifier should be interpreted (i.e. whether it should be read by the compiler as a variable name or as a class name) ... but normal human Java programmers typically don't understand these rules, and are liable to read them the wrong way.

Indeed, this might have been the cause of some of the confusing compilation errors that you saw!

(Please don't think that I'm saying that ordinary programmers should understand the JLS's identifier disambiguation rules. What I'm saying is that you should follow accepted coding standards so that normal human programmers are able to read your code without falling into semantic mantraps!)

answered Oct 14, 2012 at 1:44

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.