1

So, my assignment is to write a Visit class using Doctor and Patient classes (with their own objects) that has a time and a date and a reference to the Doctor and Patient objects its passed so it can return the same "traits" or variables of the Doctor and Patient classes (ie a Doctor object has a name so the Visit object has to keep that Doctor's name and other variables) At first I wrote that Visit class with its own getSpecialty and getName methods making those variables in Doctor and Patient public. The lab instructor wants to have those variables kept private and use the actual methods from Doctor and Patient instead of new methods.

One thing I researched (this is my first time passing class Objects as a reference and using that class's methods) said as long as you had a reference you could say like cbeta.whatevermethod() but I couldn't make it work. Java: How to access methods from another class

I also don't know if he meant for the method to be in the Visit class or the main class that passes Visit the Doctor and Patient object (but if you call the Doctor's and Patient's objects' methods in the main class anyway, there's no point to pass the objects to the Visit class.

I know this is classwork, so if you don't want to answer it for me that's fine, I'll take any help or guidance because I don't fully understand variable scope and class objects and we're not given time to catch up. Thanks so much ^_^

Here's the tester (main class) code:

public class tester
{  
   public static void main(String[] args)
   {  
      Doctor doctorStrange 
            = new Doctor("General Practitioner", 50.0);
            doctorStrange.setName("Doctor Strange");

      Doctor doctorFate
            = new Doctor("Pediatrician");  
            doctorFate.setName("Doctor Fate");

      Doctor doctorLight
            = new Doctor();
            doctorLight.setName("Doctor Light");

      Patient Thor
            = new Patient(42154);
            Thor.setName("Thor");

      Patient Bruce
            = new Patient(67245);
            Bruce.setName("Bruce");

      Patient Clint
            = new Patient();
            Clint.setName("Clint");

      Visit visit1 = new Visit(doctorStrange, Thor, "9:53 AM, 2/27/2014");
      Visit visit2 = new Visit(doctorStrange, Bruce, "4:22 PM, 7/13/2017");
      Visit visit3 = new Visit(doctorFate, Clint, "8:59 AM, 5/05/2015");

      System.out.println("First visit: Doctor name is " + doctorStrange.getName() 
                        + " and Patient name is " + Thor.getName());
}
}

The Doctor class (I can put patient here too if need be)

public class Doctor extends Person
 {
public double visitFee;
public String specialty;
public String name;

public Doctor ()
{
    visitFee = 0.0;
    specialty = "unknown";
}

public Doctor (String type)
{
    specialty = type;
    visitFee = 0.0;
}

public Doctor (double initialFee)

{
    visitFee = initialFee;
    specialty = "unknown";
}

public Doctor (String type, double initialFee)
{
    specialty = type;
    visitFee = initialFee;
}

  public void setName(String newName)
{
    name = newName;
}

public String getSpecialty ()
{
   return specialty;
}

public double getVisitFee ()
{
   return visitFee;
}

public String getName()
{
    return name;
}
 }

And finally, the visit class:

public class Visit { private String timeDate; private Doctor d; private Patient p;

public Visit()
{
  timeDate = "Time and Date of visit unknown";

}

public Visit (Doctor doc, Patient pat, String thetimeDate)
{
  timeDate = thetimeDate;
  d = doc;
  p = pat;
}
}
Community
  • 1
  • 1
user3505195
  • 33
  • 1
  • 1
  • 8
  • I think that your biggest problem is that you may not understand your assignment. So on that note, please clarify it fully for us. What ***specifically*** do your written instructions state you need to have in Visit, and how are you stuck? Your question is not clear, at least not to me. You certainly **do** need to give Visit some methods, and you will likely need to call the methods of Doctor and Patient inside of those Visit methods. – Hovercraft Full Of Eels Sep 11 '14 at 01:52
  • Sorry, I edited the first paragraph to hopefully make it clearer. Essentially, the visit class needs to be passed a reference to the Doctor and Patient classes [my constructor is public Visit (Doctor doc, Patient pat, String thetimeDate)] And I need to be able to use the original methods (like a getName method) from the Doctor or Patient class within my Visit class to return to my main program information like the name. I hope that's clearer. I appreciate your help too :) – user3505195 Sep 11 '14 at 02:16

2 Answers2

0

It sounds like you should be writing some wrapper methods that expose the methods of the Visit class's composite fields.

For example, say you have a class, Dog, that has a name field.

class Dog {
   private String name;

   public Dog(String name) {
      this.name = name;
   }


   public String getName() {
      return name;
   }

   public void setName(String name) {
      this.name = name;
   }  
}

And suppose you have a class Boy, who "owns" a dog -- who has a Dog field. You can give Boy a getDogName() method and have it return the value returned from Dog's getName() method. For example:

class Boy {
   private Dog dog;

   public Boy(Dog dog) {
      this.dog = dog;
   }

   public String getDogName() {
      return dog.getName();
   }
}

It sounds like you need to do something similar for your Visit class, give Visit methods that call and return the results from methods of its composite fields.

Note, I'm using an example that is different from yours since your question is about a homework assignment, but I have faith that you can generalize the concept.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Yeah, this makes a lot of sense and should be more than sufficient, I'll try it now. Thanks so much! For my own understanding, why can the variables still be private? I get that dog in this class is a reference to the Dog object referenced by dog, but it's still outside the Dog class – user3505195 Sep 11 '14 at 02:36
  • @user3505195: almost all variables should be private. This way your class can control visibility of the variable's states with getter and setter methods that can control what can be seen and what can be done. I don't understand what you mean about the dog variable being outside of the Dog class. Most variables are used outside of their own classes. – Hovercraft Full Of Eels Sep 11 '14 at 02:44
  • Nevermind aboout that bit, I confused myself there (I was thinking of the Dog class's methods accessing private variables, but obviously, those methods can access them because they're in the dog class, silly mistake). I do appreciate all the explanaining though and having written the code, I feel I really understand Wrapper methods and variable scope more. I had read in my CSI book (I'm just starting my CSII class) about wrapper methods but when I read that I didn't really have the concept of passing class objects and accessing another class's methods so it didn't make sense at the time – user3505195 Sep 11 '14 at 03:20
0

Your question is a little confusing. But I believe you are asking how to access the doctor/patient functions through the visit class?

If so you need to add some public functions to the visit class first.

Jerry
  • 291
  • 1
  • 6