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

Dani AI

Generated

A couple of clarifications that tie together the good points from , , and . Getters are not called implicitly in Java. In your snippet, cutCheck is an instance method of Employee, so it can read the private fields directly; there is no need to call getName() or getJobTitle() from inside the same class. Also, small correction: amountPaid is not an instance variable in your example; it is just a parameter passed to cutCheck, so there is no state to get/set for it unless you decide to store it as a field.

On the “why have getters if they are not used” question: they are mainly for other classes to read Employee data without exposing fields. Even in a small exercise, practicing encapsulation pays off when code grows. If these values never change after construction, consider a constructor and making fields final, and keep setters only where mutation is required. For money, avoid double; currency amounts are better modeled with BigDecimal to sidestep rounding surprises.

The loop runs three times simply because it is hard-coded to 3, not because an employee has three attributes. A more robust approach is to drive the loop from the file data and validate records, which also prevents NoSuchElementException when the file is short or malformed. For example:

try (Scanner sc = new Scanner(new File("EmployeeInfo.txt"))) {
  while (sc.hasNextLine()) {
    String name = sc.nextLine().trim();
    if (!sc.hasNextLine()) break;
    String title = sc.nextLine().trim();
    if (!sc.hasNextLine()) break;
    BigDecimal amount = new BigDecimal(sc.nextLine().trim());
    Employee e = new Employee();
    e.setName(name);
    e.setJobTitle(title);
    e.printPaycheck(amount); // or reuse your cutCheck method
  }
}

This keeps I/O resilient and your Employee API clean, while still letting other classes use the getters when they need the data.

Recommended Answers

All 9 Replies

"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.