Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JavaHelp From Person Class, derive Doctor. The Doctor should have a string for a

ID: 3808706 • Letter: J

Question

JavaHelp

From Person Class, derive Doctor. The Doctor should have a string for a specialty, string for gender, string for hospital, int for years of experience and an array list of Person called Patients.

Write a main method to compare years of experience if the specialty is the same. Output all of the information on each doctor including their list of patients.

Person.java:

public class Person {
// Declaring variable
private String name;

// Zero argumented constructor
public Person() {
}

// Parameterized constructor
public Person(String name) {
this.name = name;
}

// Setters and getters
public String getName() {
return name;
}

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

}

Explanation / Answer

import java.util.*;

class Person
{
// Declaring variable
private String name;
// Zero argumented constructor
public Person() {
}
// Parameterized constructor
public Person(String name) {
this.name = name;
}
// Setters and getters
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String toString()
{
return " "+name;
}
}

class Doctor extends Person
{
String speciality;
String gender;
String hospital;
int yearsExperience;
ArrayList<Person> Patients;
  
public Doctor (String speciality,String gender, String hospital,int yearsExperience,ArrayList<Person> Patients)
{
this.speciality = speciality;
this.gender = gender;
this.hospital = hospital;
this.yearsExperience = yearsExperience;
this.Patients = (ArrayList<Person>)Patients.clone();
}
public String getSpeciality()
{
return speciality;
}
public int getExperience()
{
return yearsExperience;
}
  
public String toString()
{
System.out.println(" Patients : ");
for(int i=0;i<Patients.size();i++)
{
System.out.println(Patients.get(i));
}
return " Speciality : "+ speciality +" Gender : "+gender + " Hospital : "+hospital + " Experience : "+yearsExperience;
}
  
}

class Test
{
   public static void main (String[] args)
   {
   Scanner scan = new Scanner(System.in);
  
   Person p1 = new Person("Mary Jones");
   Person p2 = new Person("Christina Joseph");
       ArrayList<Person> patients1 = new ArrayList<Person>();
       patients1.add(p1);
       patients1.add(p2);
      
      
      
       Doctor d1 = new Doctor("Neurologist","Female","XYZ Hospital",10,patients1);
      
      
      
       Person p3 = new Person("John Chris");
   Person p4 = new Person("Tina Hudson");
       ArrayList<Person> patients2 = new ArrayList<Person>();
       patients2.add(p3);
       patients2.add(p4);
  
      
       Doctor d2 = new Doctor("Cardiologist","male","ABC hospital",10,patients2);
      
      
       System.out.println(" Doctor 1 Details :");
       System.out.println(d1.toString());// display details of Doctor d1
      
       System.out.println(" Doctor 2 Details :");
       System.out.println(d2.toString()); //display Doctor d2 details
      
       //compare experience of doctors having same speciality
       if(d1.getSpeciality() == d2.getSpeciality())
       {
       if(d1.getExperience() > d2.getExperience())
       System.out.println("Doctor d1 has more experience than doctor d2");
       else
       System.out.println("Doctor d2 has more experience than doctor d1");
       }
       else
       System.out.println("Doctors have different speciality ");
      
   }
}

Output:

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote