• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

boolean remove(Appointment appt)

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to implement a boolean remove(Appointment appt) method in the AppointmentCalendar class I wrote below. In the main method I am calling the method with apptCal.remove(appt[0]); but I cannot get the class to compile, I am not sure that the comparison in my method is legal.
for (i=0;i<nAppointments;i++)
if(appt == appt[i])
I get a compile error that states: "array required, but Appointment found"
I have not seen this compile error before. What I am trying to test is if the Appoinment [] appt has an appointment appt[0] if true set appt[0] to null.

 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following excerpt from your program illustrates the problem:

You've used the same name "appt" for both the static array variable and the method parameter. Inside remove(), the parameter name "appt" hides the static variable "appt", so whenever you refer to "appt", it means the parameter; since the parameter is not an array, you get an error.
The best local fix for this would be to rename the member variable something that indicates what it actually is: "appointments" or "appointmentList" or something. The ugliest, quickest fix would be to rewrite that one erroneous line of code so that the compiler knows what you mean:

Now, while I'm looking at this: why not use an ArrayList to hold the appointment list -- why use an array at all? The ArrayList would do a better job of handling a growing/shrinking list.
Why is the class variable "appt" static, since it's initialized in a constructor?
Why is "temp" a class variable? Since it's just a scratch variable, why isn't it just local in the method that uses it?
 
Gerald Spica
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks I realized the mistake on the naming crossover,
here is what I put together. I would have loved to use an arrayList
but the excersize called for creating an array whose size could be increased.
Why is the class variable "appt" static, since it's initialized in a constructor?
trying to dodge a compile error, I changed it.
Why is "temp" a class variable? Since it's just a scratch variable, why isn't it just local in the method that uses it?
Good point.
I saw the reference to your book, I am having a hard time understanding the API descriptions, can you reccomend a resource that give code examples vs. written explanations.
Thanks again.
 
reply
    Bookmark Topic Watch Topic
  • New Topic