HI all, I was looking at this program and noticed few things:

import static java.lang.System.out;

public class Employee {
    private String name;
    private String jobTitle;

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

    public String getName() {
        return name;
    }

    public void setJobTitle(String jobTitleIn) {
        jobTitle = jobTitleIn;
    }

    public String getJobTitle() {
        return jobTitle;
    }

    public void cutCheck(double amountPaid) {
        out.printf("Pay to the order of %s ", name);
        out.printf("(%s) ***$", jobTitle);
        out.printf("%,.2f\n", amountPaid);
    }
}




import java.util.Scanner;
import java.io.File;
import java.io.IOException;

class DoPayroll {

    public static void main(String args[])
                                  throws IOException {
        Scanner diskScanner =
            new Scanner(new File("EmployeeInfo.txt"));

        for (int empNum = 1; empNum <= 3; empNum++) {
            payOneEmployee(diskScanner);
        }
    }

    static void payOneEmployee(Scanner aScanner) {
        Employee anEmployee = new Employee();

        anEmployee.setName(aScanner.nextLine());
        anEmployee.setJobTitle(aScanner.nextLine());
        anEmployee.cutCheck(aScanner.nextDouble());
        aScanner.nextLine();
    }
}

And here's the text file

Barry Burd
CEO
5000.00
Harriet Ritter
Captain
7000.00
Your Name Here
Honorary Exec of the Day
10000.00

Right, so first thing: where are the calls to the getters? Surely they are implicitly called, but how? I don't see anywhere

getName()

and

getJobTitle()

?
And don't we need a third setter and getter for the thrid variable, the money?
Then the for loop:

for (int empNum = 1; empNum <= 3; empNum++) {
            payOneEmployee(diskScanner);
        }

We are running this loop 3 times because eachinstance of the new Employee class created has 3 variables associated with it (name, jobTitle and the money?)

thanks

"Right, so first thing: where are the calls to the getters? Surely they are implicitly called, but how? I don't see anywhere."

The class Employee was written with getters and setters, but the author didn't use the getters. The variables "private String name" and "private String jobTitle" have the scope of this method only. Because the main method calls another method within that class, they are able to use these variables.

"And don't we need a third setter and getter for the thrid variable, the money?"

Good programming practice would call for a seperate method for getting and setting the money variable. You should always strive for readablity in your code. The line below looks for the next number with a decimal point in it and records it as the money amount and passes it in as a parameter for the method.

anEmployee.cutCheck(aScanner.nextDouble());

Method "cutCheck" is printing the values of instance variables.
That's what is being displayed in the output when you run the code

out.printf("Pay to the order of %s ", name);
out.printf("(%s) ***$", jobTitle);
out.printf("%,.2f\n", amountPaid);

These variables "name, jobtitle, amountPaid" are all instance varaibles, so they can be direclty accessed in any instance method of the same class without using getter methods.

thanks for the comments guys, really helps. One more question though: if the getters are not used, what's the point of having them in the program? I appreciate it's good programming practice, but they are just a waste of code if not used aren't they?

Thta's not a whole program, it's just one class. That class will be combined with other classes to make a useful program. The other classes will then use the getters to access the Employee info.

thanks, that's the whole program actually (the employee class and the main class) that's why I am a bit baffled

That is the whole program today, but you don't know about tomorrow or who will be using it. One of the biggest mistakes a programmer can make is to assume that their sloppy code won't be used years from now. We are still using some code that was written in the 50's and 60's! Remember Y2K? People said that by the time 2000 rolls around, we won't still be using this code. It may work fine today, that dosen't mean it will work forever. It never hurts to write simple code that you can file away for years and then go back to and dust off and understand with out having to open up a programming book to know what it is doing.

I'd agree with you T-Dogg3030 if it was a big big piece of code, but what's the point of doing that in such a small program? I thought he did it becasue it was actually doing something, but it isn't really

Remember that this code is just one tiny training exercise. In real life there's no such thing as a program as small and useless as this one. In real life the use of getters and setters is an essential part of Java OO programming technique, and it's never too early to start learning and practising their use.

Ok fair enough, thank you all for your contributions!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.