Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with Inheritance 1

Status
Not open for further replies.

jeak

Technical User
Oct 9, 2002
16
0
0
US
The following is a definition of a class named Doctor that I created whose objects are records for a clinic’s doctors. This class is derived from the Person class I have also included below. The Doctor record has the doctor’s name (Inherited from the Person class), specialty (i.e. “Pediatrician”, “General Practitioner”), and office visit fee. I want to be sure my class has a reasonable complement of constructors and accessor methods and is accurate since I am new at this. Do you think it meets these requirements? Also, I have an error (not an expression statement) at the line that says: super(); Does anyone know what I am doing wrong there?

Thanks!

BASE CLASS:

public class Person
{
private String name;


public Person()
{
name = "No name yet.";
}

public Person(String initialName)
{
name = initialName;
}

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

public String getName()
{
return name;
}

public void writeOutput()
{
System.out.println("Name: " + name);
}

public boolean sameName(Person otherPerson)
{
return (this.name.equalsIgnoreCase(otherPerson.name));
}

}


DERIVED CLASS:

public class Doctor extends Person
{
private string specialty;

private double officeVisitFee;

public Doctor()
{
super();
specialty;
officeVisitFee = 0;//Indicating no number yet
}

public Doctor(String initialName, String specialty, double initialOfficeVisitFee)
{
super(initialName);
specialty = initialSpecialty;
officeVisitFee = initialOfficeVisitFee;
}

public void reset(String newName, String specialty, double newOfficeVisitFee)
{
setName(newName);
specialty = newSpecialty;
officeVisitFee = newOfficeVisitFee;
}

public double getOfficeVisitFee()
{
return officeVisitFee;
}
public void setOfficeVisitFee(double newOfficeVisitFee)
{
officeVisitFee = newOfficeVisitFee;
}

public void writeOutput()
{
System.out.println("Name: " + getName());
System.out.println("Specialty: " + getSpecialty());
System.out.println("Office Visit Fee : " + officeVisitFee);
}

public boolean equals(Doctor otherDoctor)
{
return (this.sameName(otherDoctor)
&& (this.officeVisitFee == otherDoctor.officeVisitFee));
}

}
 
Concerning the error: How about the line after that says: specialty;
all by itself?
--- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
For my next trick I will pull a hat out of a rabbit (if you think thats bad you should see how the pigeon feels...) :p
 
Sorry about that. I made some corrections to the code and posted it below.


public class Doctor extends Person
{
private string specialty;

private double officeVisitFee;

public Doctor()
{
super();
specialty();
officeVisitFee = 0;//Indicating no number yet
}

public Doctor(String initialName, String specialty, double initialOfficeVisitFee)
{
super(initialName);
specialty = initialSpecialty;
officeVisitFee = initialOfficeVisitFee;
}

public void reset(String newName, String specialty, double newOfficeVisitFee)
{
setName(newName);
specialty = newSpecialty;
officeVisitFee = newOfficeVisitFee;
}
public string getSpecialty()
{
return specialty;
}

public double getOfficeVisitFee()
{
return officeVisitFee;
}

public void setSpecialty(string newSpecialty)
{
specialty = newSpecialty;
}

public void setOfficeVisitFee(double newOfficeVisitFee)
{
officeVisitFee = newOfficeVisitFee;
}

public void writeOutput()
{
System.out.println("Name: " + getName());
System.out.println("Specialty: " + Specialty());
System.out.println("Office Visit Fee : " + officeVisitFee);
}

public boolean equals(Doctor otherDoctor)
{
return (this.sameName(otherDoctor)
&& (this.officeVisitFee == otherDoctor.officeVisitFee)
&& (this.specialty == otherDoctor.specialty));
}

}
 
Now when I try to run the program, I am getting the following errors. I'm not sure what's wrong...

"Doctor.java": Error #: 300 : class string not found in class Doctor at line 38, column 12
"Doctor.java": Error #: 300 : class string not found in class Doctor at line 48, column 30
"Doctor.java": Error #: 300 : method specialty() not found in class Doctor at line 21, column 9
"Doctor.java": Error #: 300 : variable initialSpecialty not found in class Doctor at line 28, column 21
"Doctor.java": Error #: 300 : variable newSpecialty not found in class Doctor at line 35, column 21
"Doctor.java": Error #: 300 : variable specialty not found in class Doctor at line 40, column 16
"Doctor.java": Error #: 300 : variable specialty not found in class Doctor at line 50, column 9
"Doctor.java": Error #: 300 : method Specialty() not found in class Doctor at line 61, column 44
"Doctor.java": Error #: 300 : variable specialty not found in class Doctor at line 69, column 23
"Doctor.java": Error #: 300 : variable specialty not found in class Doctor at line 69, column 48
 
String needs to be upper case, it is an object, not a primitive. I'll take a look and se what I can find.
-Tarwn --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
For my next trick I will pull a hat out of a rabbit (if you think thats bad you should see how the pigeon feels...) :p
 
The rest of your errors are pretty self explanatory:
It is still assuming that specialty is method on line 21 because you have placed it on a line by itself with parantheses around it. Either use it as a variable by assigning a value to it, or remove the line.
On lines 28 and 35 you have some inconsistencies with your variable names. As a note, it is generally a bad idea to name a variable in a method or constructor definition to be the same as a global variable (in this case specialty) you need to change the specialty in the constructor to something else (perhaps initialSpecialty?) and ditto for your reset function (line 50)
Line 61 is again seeing a supposed method name without a method definition. Only methods have () after them. try removing them and lowercasing the S.

And that should be all the problems. In the future try to remember that only primitive variables are lower case (int, long, double, float) everything else is an object, and by practice, uppercased. Also remember that if the debugger tells you it can't find a variable or method that you need to check that line to see why it can't find the variable or method in question.

And lastly, while I did debug this for you once, it is generally not a good idea to post code that needs only minor debugging, posting a few lines that have a problem that you couldn't find yourself, or a large amount of code to support a small piece of code is one thing. Most people won't debug code for you, I just happened to be rather full from dinner and looking for something to do.

-Tarwn --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
For my next trick I will pull a hat out of a rabbit (if you think thats bad you should see how the pigeon feels...) :p
 
Thank you for all your help Tarwn!!! :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top