It's not a secret anymore!*
The moose likes Java in General and the fly likes appointment find (Date d, time t) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "appointment find (Date d, time t)" Watch "appointment find (Date d, time t)" New topic
Author

appointment find (Date d, time t)

Gerald Spica
Ranch Hand

Joined: Jul 28, 2003
Posts: 30
I have an excersize where we needed to define our own Date class, then create an Appointment class and an AppointmentCalendar subclass. I was able to create appointments with int year, int day, int month and other variables. I am confused about how I access the object Date within the appointment class by just using (Date d) and have it carry the int year, int day, int month so I can compare all the dates in the array of appointments and find any that match the same Date.
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1824

Welcome to JavaRanch, Gerald!
I'm afraid that I'm a little bit unclear on where you need help. You state that you can't figure out how to access the Date object from the Appointment object, yes? But you also state that Date is a super class of Appointment. Unless I miss my mark, there is no Date object to reference--it is part of the Appointment object. You simple state "this.getYear()" or some such in the Appointment class and voila, you've just referenced some code in the Date class.
If this isn't your problem, could you please post the code you've got and we will try to take a gander at it and see if we can't enlighten you in your quest for knowledge...
...just please use the [ CODE] and [ /CODE] tags to make the formatting of your code look "pretty"; otherwise, your indents will vanish, and it can be difficult to read. (You can also use the "CODE" button on the message-edit screen to have the computer populate those tags for you...)


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Gerald Spica
Ranch Hand

Joined: Jul 28, 2003
Posts: 30
Thanks for the quick response, that is what I thought I should do as well and that is where my confusion begins. Here is the code for my Appointment class, I am trying this concept here first to limit complexity, and I get
this compile error:
C:\j2sdk1.4.1_03\dev>javac Appointment.java
Appointment.java:159: compareTo(Date) in Appointment cannot be applied to (Appointment)
comparisonResult = aAppt.compareTo(bAppt);
Here is my Appointment Class
Lance Duncan
Ranch Hand

Joined: Jul 28, 2003
Posts: 39
are you working on an on-line matchmaker application?
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1824

Well, first, your Appointment class does ont actually extend your Date class. To do that, you have to declare:

Once you do that, however, you will have "hidden" the member variables in your Date class. (I say this not knowing what the Date class looks like, but rather assuming what it should most likely look like.) I recommend removing all of the member variables / methods that are duplicated between the two -- remember, your Appointment is now a sub-class of Date and dosen't need to bother re-implementing that which Date has already implemented.
Note that you won't be able to access private members of your Date class from your Appointment class, so your toString method will fail to compile. True OOP practice dictate that you use getters and setters instead of referencing the variable names here; that lessens the coupling between the two classes and will reduce the amount of refactoring that you will need to do when/if you redesign your Date class.
I say that true OOP practice dictates this; unless you are (or your instructor is) being picky about this, I would make the member variables protected instead of private. (My decision on when to use getters vs. member variables is based directly on how resuable I want my code to be; unless you plan on placing this functionality in a common library of yours, this sounds pretty much like throw-away code...)
You've got a bit of work to do, but once you do that, your method call should work.
Gerald Spica
Ranch Hand

Joined: Jul 28, 2003
Posts: 30
No I am not working on an on-line matchmaker application
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1873
hi Gerald
u can try changing the declaration
public int compareTo(Date otherDate)
with
public int compareTo(Appointment otherDate)
and see if it works for u the way u want..
i don understand y u declared it to be Date otherDate if u only expect Appointments to be compated w/ each other rather than Dates...
regards
maulin
Gerald Spica
Ranch Hand

Joined: Jul 28, 2003
Posts: 30
Joel McNary
Thanks - the reason that I imported Date instead of extended it is that we also had to make our own Time class. I was confused on how your do an inheritance tree and this has lead to my general confusion.
This is what I have:
Date
Time
Appointment
AppointmentCalendar
I am confused on the notion that Date and Time are in API and typically imported. I created my own, do I say
public class Appointment extends Date, Time /*how do I include time*/
public class AppoinmentCalendar extends Appointment
The rest of what you are saying makes sense to me.
Lance Duncan
Ranch Hand

Joined: Jul 28, 2003
Posts: 39
Originally posted by Gerald Spica:
No I am not working on an on-line matchmaker application

would you like to work on one then?
Gerald Spica
Ranch Hand

Joined: Jul 28, 2003
Posts: 30
Lance,
No thank you.
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1824

Hmm...
Java does not support multiple inheritance. The more I think about this problem, the more is strikes me that you do not want Appointment to be a subclass of Date; you want it to contain a Date (and a Time).
This is the difference between an "is-a" relationship and a "has-a" relationship. An Appointment is not a Date, it has a Date. (OK, bad terminology; in spoken English, you say "an Appointment is a Date," but trust me, it really has-a Date.)
This example:
A Dog is an Animal, but it has a Tail. If you already had a Tail class, you might try to extend it to include all the functionality of a Dog, but it makes little sense to do that.
[EDIT]
Ah...I see that you've discovered this. Further discussion should be done here
[ July 29, 2003: Message edited by: Joel McNary ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: appointment find (Date d, time t)