1,105,310 Community Members

setters, getters and input from file

Member Avatar
Violet_82
Veteran Poster
1,002 posts since Jan 2010
Reputation Points: 14 [?]
Q&As Helped to Solve: 6 [?]
Skill Endorsements: 8 [?]
 
0
 

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

Member Avatar
T-Dogg3030
Light Poster
39 posts since Nov 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 5 [?]
Skill Endorsements: 0 [?]
 
0
 

"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());

Member Avatar
subramanya.vl
Junior Poster in Training
82 posts since Oct 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 10 [?]
Skill Endorsements: 3 [?]
 
0
 

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.

Member Avatar
Violet_82
Veteran Poster
1,002 posts since Jan 2010
Reputation Points: 14 [?]
Q&As Helped to Solve: 6 [?]
Skill Endorsements: 8 [?]
 
0
 

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?

Member Avatar
JamesCherrill
... trying to help
10,362 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,749 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
0
 

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.

Member Avatar
Violet_82
Veteran Poster
1,002 posts since Jan 2010
Reputation Points: 14 [?]
Q&As Helped to Solve: 6 [?]
Skill Endorsements: 8 [?]
 
0
 

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

Member Avatar
T-Dogg3030
Light Poster
39 posts since Nov 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 5 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
Violet_82
Veteran Poster
1,002 posts since Jan 2010
Reputation Points: 14 [?]
Q&As Helped to Solve: 6 [?]
Skill Endorsements: 8 [?]
 
0
 

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

Member Avatar
JamesCherrill
... trying to help
10,362 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,749 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
0
 

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.

Member Avatar
Violet_82
Veteran Poster
1,002 posts since Jan 2010
Reputation Points: 14 [?]
Q&As Helped to Solve: 6 [?]
Skill Endorsements: 8 [?]
 
0
 

Ok fair enough, thank you all for your contributions!

Question Answered as of 1 Year Ago by JamesCherrill, T-Dogg3030 and subramanya.vl
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article