2 Replies - 305 Views - Last Post: 19 October 2013 - 07:47 PM Rate Topic: -----

#1 bmep08  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 19-October 13

LinkedQueue Java Program Problem

Posted 19 October 2013 - 06:05 PM

import java.util.*;

 public class Patient {

String description;

int arriveTime;

int leaveTime;

String dentalWork;

int waitTime;

Patient next;

}

public class Dentist {

Patient firstPatient;


public void insertPatient(Patient p) {

if (firstPatient == null) {

firstPatient = p;

firstPatient.next = null;

return;

}

Patient temp = firstPatient;

Patient prev = null;

while (p.arriveTime > temp.arriveTime && temp != null) {

prev = temp;

temp = temp.next;

}

if (temp == null) {

prev.next = p;

p.next = null;

}

}


void popPatient() {

if (firstPatient != null) {

Patient temp = firstPatient;

firstPatient = temp.next;

temp = null;

}

}


int addTime(int time, int minutes) {

int hh = time/100;

int min = time%100;

if ((min + minutes) % 60 > 0) { hh+= (min+minutes)%60; min = (min+minutes)/60; }

else { min = min + minutes;}

return (hh*100 + min);

}


int getTimeDiff(int time1, int time2) {

int hh1 = time1/100 ; int mm1 = time1%100;

int hh2 = time2/100 ; int mm2 = time2%100;

if (hh1 > hh2 || (hh1 == hh2 && mm1 > mm2)) {

return ((hh1-hh2)*60 + (mm1-mm2));

}

else return 0;

}


public void createLog() {

int lastLeaveTime = firstPatient.arriveTime;

int numPatient = 1;

System.out.println("Name Description Arrival Time Dental Work Wait Time Leave Time");

while(firstPatient != null) {

int serviceTime = 0;

if (firstPatient.dentalWork.equals("Cleaning")) {

serviceTime = 30;

} else if (firstPatient.dentalWork.equals("Checkup")) {

serviceTime = 15;

} else if (firstPatient.dentalWork.equals("ToothExtract")) {

serviceTime = 30;

} else if (firstPatient.dentalWork.equals("Braces")) {

serviceTime = 45;

}

firstPatient.waitTime = getTimeDiff(lastLeaveTime,firstPatient.arriveTime);

firstPatient.leaveTime = addTime(firstPatient.arriveTime, firstPatient.waitTime + serviceTime);

System.out.println("Patient "+numPatient+" - "+firstPatient.description+" "+firstPatient.arriveTime+" "+ firstPatient.dentalWork+" "+firstPatient.waitTime+ " minutes "+firstPatient.leaveTime);

numPatient++;

firstPatient = firstPatient.next;

}

}
 public static void main(String [ ] args) { 
  Dentist dentist = new Dentist(); Patient p; 
  p = new Patient(); p.description = "Emmy Lawyer"; 
  p.arriveTime = 1000; p.dentalWork = "Cleaning"; dentist.insertPatient(p); 
  p = new Patient(); 
  p.description = "Jack Carpenter"; p.arriveTime = 1010; p.dentalWork = "Checkup"; dentist.insertPatient(p); 
  p = new Patient(); p.description = "Janet Engineer"; p.arriveTime = 1040; p.dentalWork = "Braces"; 
dentist.insertPatient(p); dentist.createLog(); } 
} 


The description below is what the above code is suppose to be about (just so anyone who helps me understands), but I'm not sure if I did it right because I am getting a NullPointerException when I run it.

I'm suppose to create a LinkedQueue program that is a simulation of a Dentist.
I'm suppose to create a patient class that declares the description of customer, arrival time and what work needs to be done to the patient.
Ex. Emma Lawyer(Description) 10:00 (Arrive Time) Cleaning (method)
Next, I created a Dentist Class. There is only ONE dentist so:
-The Dentist reads in a patient in order of time arrived.
-If Queue is not empty, than the patient is fetched and dental work is done in the following way:
If the patients service needs : Cleaning (30 min), Checkup (15 Minute), ToothExtract(30), Braces(45)
Each patient comes to the dentist with an arrival time and leave time is computed after getting their dental work done; however leave time = waiting time + service time (which is the numbers in the dental work); waiting time is computed using the previous patientís leave time minus the current patientís arrival time.

Is This A Good Question/Topic? 0
  • +

Replies To: LinkedQueue Java Program Problem

#2 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3640
  • View blog
  • Posts: 12,614
  • Joined: 12-December 12

Re: LinkedQueue Java Program Problem

Posted 19 October 2013 - 07:34 PM

What line in your posted code generates the error message?
Was This Post Helpful? 0
  • +
  • -

#3 bmep08  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 19-October 13

Re: LinkedQueue Java Program Problem

Posted 19 October 2013 - 07:47 PM

View Postandrewsw, on 19 October 2013 - 07:34 PM, said:

What line in your posted code generates the error message?


This is what generates when I try to run it:
java.lang.NullPointerException
at Dentist.insertPatient(Dentist.java:24)
at Dentist.main(Dentist.java:138)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)

View Postbmep08, on 19 October 2013 - 07:45 PM, said:

View Postandrewsw, on 19 October 2013 - 07:34 PM, said:

What line in your posted code generates the error message?


I put the Patient class and Dentist class together when I posted the code. I'm not exactly sure which line since its together.

This is what generates when I try to run it:
java.lang.NullPointerException
at Dentist.insertPatient(Dentist.java:24)
at Dentist.main(Dentist.java:138)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1