user3956566
user3956566

Reputation:

Ignoring values passed in as parameters

I asked this question here (thinking I would help people) Creating an unnecessary getter and unearthed a huge area of ignorance I have.

In this answer it was pointed out to me I had a fatal flaw in my code and I quote for ease:


"This is wrong:

public Patient(final String ptNo, final String ptName,
        final String procDate, final int procType, final String injury,
        final String drName) throws IOException
{
    Patient.ptNo = getPtNo();
    Patient.ptName = getPtName();
    Patient.procDate = getProcDate();
    Patient.procType = getProcType();
    Patient.injury = getPtNotes();
    Patient.drName = getDrName();
}

As you're completely ignoring all values passed in as parameters. Instead do:

public Patient(final String ptNo, final String ptName,
        final String procDate, final int procType, final String injury,
        final String drName) throws IOException
{
    Patient.ptNo = ptNo;
    Patient.ptName = ptName;
    Patient.procDate = procDate;
    Patient.procType = procType;
    Patient.injury = injury;
    Patient.drName = drName;
}

Where here you're setting your class's fields with the parameter values."


What I don't understand, is why the values are being ignored. I call separate methods for eg:

public static String getPtName()
{
    System.out.print("Enter patient name: \n");
    try
    {
        ptName = stdin.readLine();
    } catch (IOException e)
    {
        System.out.println("Error! Enter a valid option.");
        getPtName();
    }
    return ptName;
}

So I thought that was the same, in a longer way of writing the second block of code.

Can someone please explain to me, why it is different?


edit The assignment requirements from uni.

C) Provide a constructor for the class that accepts the patient number (a String), patient name (a String), procedure date (a String in the format dd/mm/yy), procedure type (an int), the injury description (a String) and the doctor name of the physician who is administering the patient’s treatment.

This constructor should initialise the instance variables with the corresponding parameter values that have been passed in - it should also initialise the patient notes instance variable to the injury description that was passed in initially and initialise the patient status instance variable to ‘S’ (indicating that the new patient has had a procedure Scheduled).

public Patient (String patientNo, String patientName, 
                 String procedureDate, int procedureType,
                 String injuryDescription, String doctorName)

D) Implement accessors for the patient number, patient name, procedure date, patient notes and doctor name instance variables.

Upvotes: 0

Views: 731

Answers (2)

Adam Liss
Adam Liss

Reputation: 48280

In the first example:

public Patient(final String ptNo, final String ptName,
               final String procDate, final int procType,
               final String injury, final String drName)
               throws IOException

The constructor never uses the variables that are passed in. That is, they don't appear anywhere within the body of the function.

It's particularly confusing because the parameters to the function have exactly the same names as the class variables, but Patient.ptNo is not the same variable as the parameter ptNo. (Actually, Patient.ptNo should be this.ptNo, because it belongs to this particular instance of the class. Patient.ptNo would refer to a single value that's common to all objects of type Patient.) When you write the corrected form

this.ptNo = ptNo;

you're setting the class variable to the same value as the parameter variable. In fact, in this case, you must qualify the class variables, otherwise the parameter variables will be used instead.

Some programmers follow conventions, such as adding a prefix or suffix to the names of all class variables, to make it impossible for these name collisions to occur. So, for example, you might have

this.c_ptNo = ptNo;

or just

c_ptNo = ptNo;

where the c_ prefix indicates a class variable.

Upvotes: 3

user1231232141214124
user1231232141214124

Reputation: 1349

    public Patient(final String ptNo, final String ptName,
                   final String procDate, final int procType, final String injury,
                   final String drName) throws IOException

You are ignoring all of the values passed into the constructor. If you want the user to manually enter the values the way you have it can be done with a parameterless constructor.

public Patient() {
    /*your code*/
}

You original code requires all that information to be passed in when the Patient object is created.

That being said, you shouldn't be accepting user input in a constructor call. You should just make standard getters and setters to deal with those fields.

Patient.<whatever> should just be this.<whatever>

Upvotes: 2

Related Questions