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

Java programming. Need help fixing my code. It is almost done, but the construct

ID: 3903050 • Letter: J

Question

Java programming.

Need help fixing my code. It is almost done, but the constructor for doctor and surgeon is not working well, the name parameter specifically.

Also, need change BasicDoctor to an interface with the program. Five classes, Person (abstract), Patient, Doctor, Surgeon, BasicDoctor(interface).

-BasicDoctor at the bottem of question.

//Person.java
/**
* The model of a person who has a name and a health number
* that cannot be changed.
*/
public abstract class Person
{
/**
* The name of the person.
*/
private String pname;
/**
* The health number of the person.
*/
private int healthNum;
private int age;
private String address;
/**
* Initialize an instance with the given name and health number.
*
* @param pName the person's name
* @param pNumber the person's health number
*/
public Person(String pname, int age, String address) {
this.pname = pname;
this.age = age;
this.address = address;
}
/**
* Return the name of the person.
*
* @return the name of the person
*/
public String getName()
{
return pname;
}
/**
* Return the health number of the person.
*
* @return the health number of the person
*/
public int getHealthNumber()
{
return healthNum;
}
/**
* Change the name of the person.
*
* @param newName the name of the person
*/
public void setName(String newName)
{
pname = newName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
/**
* Return a string representation of the person.
*
* @return a string representation of the person
*/
@Override
public String toString() {
return "Name: "+pname+" Age: "+age+" Address: "+address;
}
}
/**
* A method to test the Person class.
*/

//Patient.java
import java.util.LinkedList;
public class Patient extends Person{
String bedReference;
LinkedList<Doctor> doctors;
public Patient(String pName, int pNumber, String pAddress) {
super(pName, pNumber, pAddress);
this.doctors = new LinkedList<>();
}
public String getBedReference() {
return bedReference;
}
public void setBedReference(String bedReference) {
this.bedReference = bedReference;
}
public LinkedList<Doctor> getDoctors() {
return doctors;
}
public void setDoctors(LinkedList<Doctor> doctors) {
this.doctors = doctors;
}
@Override
public String toString() {
String text="";
if(bedReference!=null) {
text+="Patient: "+this.getName()+" Health number: "+this.getHealthNumber()+" Bed reference: "+this.getBedReference();
if(!doctors.isEmpty()) {
text+=" Doctors: ";
for (Person person : doctors) {
text+=" "+person.getName()+" ";
}
}
} else {
text+="Patient: "+this.getName()+" Health number: "+this.getHealthNumber();
if(!doctors.isEmpty()) {
text+=" Doctors: ";
for (Person person : doctors) {
text+=" "+person.getName()+" ";
}
}
}
return text;
}
public static void main(String[] args) {
Patient patient =new Patient("Aabid", 90, "Saskatoon");
Doctor doctor=new Doctor("Hilal", 22,"Canada",10,"Oncology");
Doctor doctor1=new Doctor("Nasir", 22,"Canada",10,"Oncology");
Doctor doctor2=new Doctor("Aadil", 22,"Canada",10,"Oncology");
Doctor doctor3=new Doctor("Saima", 22,"Canada",10,"Oncology");
LinkedList<Doctor> docList=new LinkedList<>();
docList.add(doctor);
docList.add(doctor1);
docList.add(doctor2);
docList.add(doctor3);
patient.setDoctors(docList);
patient.setBedReference("34");
System.out.println(patient);
}
}

// Doctor.java
import java.util.LinkedList;
import java.util.List;
public class Doctor extends Person implements BasicDoctor{
String department;
int experience;
public List<Person> patients;
public Doctor(String name, int age, String address, int experience, String department) {
super(name, age, address);
this.experience = experience;
patients=new LinkedList<>();
this.department=department;
}
public List<Person> getPatients() {
return patients;
}
public void addPatient(Person patient) {
this.patients.add(patient);
}
public void setExperience(int experience) {
this.experience = experience;
}
// @Override
//public String getName() {
//return //name;
//}
public int getExperience() {
return experience;
}
@Override
public Person checkUp() throws Exception {
if(this.patients.isEmpty())
throw new Exception("No patients to be checked up");
return this.patients.remove(0);
}
@Override
public String toString() {
return "Doctor: "+name+" Age: "+getAge()+" Address: "+getAddress()+" Designation: "+department+" With experience of: "+experience;
}
}

// Surgeon.java
import java.util.List;
public class Surgeon extends Doctor{
public Surgeon(String name, int age, String address, int experience, String department) {
super(name, age, address, experience, department);
}
@Override
public String toString() {
return "Surgeon: "+name+" Age: "+getAge()+" Address: "+getAddress()+" Designation: "+department+" With experience of: "+experience;
}
}

// BasicDoctor.java

// TO EDIT

/**

* A very simple model of a doctor who has a name.

*/

public class BasicDoctor

{

/**

* The name of the doctor.

*/

private String name;

/**

* Initialize an instance of BasicDoctor with the given name.

*

* @param name the name of the doctor

*/

public BasicDoctor(String name)

{

this.name = name;

}

/**

* Return the name of the doctor

*

* @return the name of the doctor

*/

public String getName()

{

return name;

}

/**

* Return a string representation of the doctor

*

* @return a string representation of the doctor

*/

public String toString()

{

return " Name: " + name + " ";

}

/**

* A method to test the BasicDoctor class.

*/

public static void main(String[] args)

{

int numErrors = 0;

BasicDoctor d = new BasicDoctor("Joe");

System.out.println("BasicDoctor Joe is " + d + " ");

if (! d.getName().equals("Joe"))

{

System.out.println("The constructor or getName failed");

numErrors++;

}

d = new BasicDoctor("Mary");

System.out.println("BasicDoctor Mary is " + d + " ");

if (! d.getName().equals("Mary"))

{

System.out.println("The constructor or getName failed");

numErrors++;

}

System.out.println("The number of errors found is " + numErrors);

}

}

Modifications to BasicDoctor class; Even though BasicDoctor exists, we have no basic doctors in the hospital. Thus we want to disallow an instantiation of a BasicDoctor. Also, this class should NOT inherit from Person. A BasicDoctor is not a person. Doctor and Surgeon are Persons however. Thus this class is to be altered to an Interface.

Explanation / Answer

Solution:

//Person.java

/**

* The model of a person who has a name and a health number

* that cannot be changed.

*/

public abstract class Person

{

/**

* The name of the person.

*/

private String pname;

/**

* The health number of the person.

*/

private int healthNum;

private int age;

private String address;

/**

* Initialize an instance with the given name and health number.

*

* @param pName the person's name

* @param pNumber the person's health number

*/

public Person(String pname, int age, String address) {

this.pname = pname;

this.age = age;

this.address = address;

}

/**

* Return the name of the person.

*

* @return the name of the person

*/

public String getName()

{

return pname;

}

/**

* Return the health number of the person.

*

* @return the health number of the person

*/

public int getHealthNumber()

{

return healthNum;

}

/**

* Change the name of the person.

*

* @param newName the name of the person

*/

public void setName(String newName)

{

pname = newName;

}

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

public String getAddress() {

return address;

}

public void setAddress(String address) {

this.address = address;

}

/**

* Return a string representation of the person.

*

* @return a string representation of the person

*/

@Override

public String toString() {

return "Name: "+pname+" Age: "+age+" Address: "+address;

}

public Person checkUp() throws Exception {

// TODO Auto-generated method stub

return null;

}

}

/**

* A method to test the Person class.

*/

//Patient.java

import java.util.LinkedList;

public class Patient extends Person{

String bedReference;

LinkedList<Doctor> doctors;

public Patient(String pName, int pNumber, String pAddress) {

super(pName, pNumber, pAddress);

this.doctors = new LinkedList<>();

}

public String getBedReference() {

return bedReference;

}

public void setBedReference(String bedReference) {

this.bedReference = bedReference;

}

public LinkedList<Doctor> getDoctors() {

return doctors;

}

public void setDoctors(LinkedList<Doctor> doctors) {

this.doctors = doctors;

}

@Override

public String toString() {

String text="";

if(bedReference!=null) {

text+="Patient: "+this.getName()+" Health number: "+this.getHealthNumber()+" Bed reference: "+this.getBedReference();

if(!doctors.isEmpty()) {

text+=" Doctors: ";

for (Person person : doctors) {

text+=" "+person.getName()+" ";

}

}

} else {

text+="Patient: "+this.getName()+" Health number: "+this.getHealthNumber();

if(!doctors.isEmpty()) {

text+=" Doctors: ";

for (Person person : doctors) {

text+=" "+person.getName()+" ";

}

}

}

return text;

}

public static void main(String[] args) {

Patient patient =new Patient("Aabid", 90, "Saskatoon");

Doctor doctor=new Doctor("Hilal", 22,"Canada",10,"Oncology");

Doctor doctor1=new Doctor("Nasir", 22,"Canada",10,"Oncology");

Doctor doctor2=new Doctor("Aadil", 22,"Canada",10,"Oncology");

Doctor doctor3=new Doctor("Saima", 22,"Canada",10,"Oncology");

LinkedList<Doctor> docList=new LinkedList<>();

docList.add(doctor);

docList.add(doctor1);

docList.add(doctor2);

docList.add(doctor3);

patient.setDoctors(docList);

patient.setBedReference("34");

System.out.println(patient);

}

}

// Doctor.java

import java.util.LinkedList;

import java.util.List;

public class Doctor extends Person{

String department;

int experience;

public List<Person> patients;

public Doctor(String name, int age, String address, int experience, String department) {

super(name, age, address);

this.experience = experience;

patients=new LinkedList<>();

this.department=department;

}

public List<Person> getPatients() {

return patients;

}

public void addPatient(Person patient) {

this.patients.add(patient);

}

public void setExperience(int experience) {

this.experience = experience;

}

// @Override

//public String getName() {

//return //name;

//}

public int getExperience() {

return experience;

}

@Override

public Person checkUp() throws Exception {

if(this.patients.isEmpty())

throw new Exception("No patients to be checked up");

return this.patients.remove(0);

}

@Override

public String toString() {

return "Doctor: "+getName()+" Age: "+getAge()+" Address: "+getAddress()+" Designation: "+department+" With experience of: "+experience;

}

}

// Surgeon.java

import java.util.List;

public class Surgeon extends Doctor{

public Surgeon(String name, int age, String address, int experience, String department) {

super(name, age, address, experience, department);

}

@Override

public String toString() {

return "Surgeon: "+getName()+" Age: "+getAge()+" Address: "+getAddress()+" Designation: "+department+" With experience of: "+experience;

}

}

// BasicDoctor.java

// TO EDIT

/**

* A very simple model of a doctor who has a name.

*/

public class BasicDoctor

{

/**

* The name of the doctor.

*/

private String name;

/**

* Initialize an instance of BasicDoctor with the given name.

*

* @param name the name of the doctor

*/

public BasicDoctor(String name)

{

this.name = name;

}

/**

* Return the name of the doctor

*

* @return the name of the doctor

*/

public String getName()

{

return name;

}

/**

* Return a string representation of the doctor

*

* @return a string representation of the doctor

*/

public String toString()

{

return " Name: " + name + " ";

}

/**

* A method to test the BasicDoctor class.

*/

public static void main(String[] args)

{

int numErrors = 0;

BasicDoctor d = new BasicDoctor("Joe");

System.out.println("BasicDoctor Joe is " + d + " ");

if (! d.getName().equals("Joe"))

{

System.out.println("The constructor or getName failed");

numErrors++;

}

d = new BasicDoctor("Mary");

System.out.println("BasicDoctor Mary is " + d + " ");

if (! d.getName().equals("Mary"))

{

System.out.println("The constructor or getName failed");

numErrors++;

}

System.out.println("The number of errors found is " + numErrors);

}

}

Sample Run:

BasicDoctor Joe is
Name: Joe


BasicDoctor Mary is
Name: Mary


The number of errors found is 0

Note: BasicDoctor is not a class, it's a interface. I need the whole question to solve it. But as far your constructor is concerned I have fixed it and above is the output for the same.

Before giving negative feedback please do discuss in comment section. If you liked it then dont forget to gives a thumbs up.

After Investigation:

Below codes may help you.

//Person.java

/**

* The model of a person who has a name and a health number

* that cannot be changed.

*/

public abstract class Person

{

/**

* The name of the person.

*/

private String pname;

/**

* The health number of the person.

*/

private int healthNum;

private int age;

private String address;

/**

* Initialize an instance with the given name and health number.

*

* @param pName the person's name

* @param pNumber the person's health number

*/

public Person(String pname, int age, String address) {

this.pname = pname;

this.age = age;

this.address = address;

}

/**

* Return the name of the person.

*

* @return the name of the person

*/

public String getName()

{

return pname;

}

/**

* Return the health number of the person.

*

* @return the health number of the person

*/

public int getHealthNumber()

{

return healthNum;

}

/**

* Change the name of the person.

*

* @param newName the name of the person

*/

public void setName(String newName)

{

pname = newName;

}

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

public String getAddress() {

return address;

}

public void setAddress(String address) {

this.address = address;

}

/**

* Return a string representation of the person.

*

* @return a string representation of the person

*/

@Override

public String toString() {

return "Name: "+pname+" Age: "+age+" Address: "+address;

}

public Person checkUp() throws Exception {

// TODO Auto-generated method stub

return null;

}

}

//Patient.java

import java.util.LinkedList;

public class Patient extends Person{

String bedReference;

LinkedList<Doctor> doctors;

//Parameterized Constructor

public Patient(String pName, int pNumber, String pAddress) {

super(pName, pNumber, pAddress);

this.doctors = new LinkedList<>();

}

//Getters and Setters

public String getBedReference() {

return bedReference;

}

public void setBedReference(String bedReference) {

this.bedReference = bedReference;

}

public LinkedList<Doctor> getDoctors() {

return doctors;

}

public void setDoctors(LinkedList<Doctor> doctors) {

this.doctors = doctors;

}

//Displaying the Patient details

@Override

public String toString() {

String text="";

if(bedReference!=null) {

text+="Patient: "+this.getName()+" Health number: "+this.getHealthNumber()+" Bed reference: "+this.getBedReference();

if(!doctors.isEmpty()) {

text+=" Doctors: ";

for (Person person : doctors) {

text+=" "+person.getName()+" ";

}

}

} else {

text+="Patient: "+this.getName()+" Health number: "+this.getHealthNumber();

if(!doctors.isEmpty()) {

text+=" Doctors: ";

for (Person person : doctors) {

text+=" "+person.getName()+" ";

}

}

}

return text;

}

//Driver Program

public static void main(String[] args) {

Patient patient =new Patient("Aabid", 90, "Saskatoon");

Doctor doctor=new Doctor("Hilal", 22,"Canada",10,"Oncology");

Doctor doctor1=new Doctor("Nasir", 22,"Canada",10,"Oncology");

Doctor doctor2=new Doctor("Aadil", 22,"Canada",10,"Oncology");

Doctor doctor3=new Doctor("Saima", 22,"Canada",10,"Oncology");

LinkedList<Doctor> docList=new LinkedList<>();

docList.add(doctor);

docList.add(doctor1);

docList.add(doctor2);

docList.add(doctor3);

patient.setDoctors(docList);

patient.setBedReference("34");

System.out.println(patient);

}

}

// Doctor.java

import java.util.LinkedList;

import java.util.List;

public class Doctor extends Person implements BasicDoctor{

String department;

int experience;

public List<Person> patients;

//Parameterized constructor of Doctor class

public Doctor(String name, int age, String address, int experience, String department) {

super(name, age, address);

this.experience = experience;

patients=new LinkedList<>();

this.department=department;

}

//Getters and Setters

public List<Person> getPatients() {

return patients;

}

public void addPatient(Person patient) {

this.patients.add(patient);

}

public void setExperience(int experience) {

this.experience = experience;

}

public int getExperience() {

return experience;

}

//Implementing the checkup method

@Override

public Person checkUp() throws Exception {

if(this.patients.isEmpty())

throw new Exception("No patients to be checked up");

return this.patients.remove(0);

}

//Overrding the toString method to display the information.

@Override

public String toString() {

return "Doctor: "+getName()+" Age: "+getAge()+" Address: "+getAddress()+" Designation: "+department+" With experience of: "+experience;

}

}

// Surgeon.java

import java.util.List;

public class Surgeon extends Doctor{

//Parameterized constructor of Doctor class

public Surgeon(String name, int age, String address, int experience, String department) {

super(name, age, address, experience, department);

}

@Override

public String toString() {

return "Surgeon: "+getName()+" Age: "+getAge()+" Address: "+getAddress()+" Designation: "+department+" With experience of: "+experience;

}

}

public interface BasicDoctor {

public Person checkUp() throws Exception;

}

Sample Run:

Patient: Aabid Health number: 0 Bed reference: 34

Doctors:

Hilal

Nasir

Aadil

Saima

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