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;
}
}