We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,787 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

setters, getters and input from file

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

4
Contributors
9
Replies
2 Days
Discussion Span
5 Months Ago
Last Updated
10
Views
Question
Answered
Violet_82
Master Poster
793 posts since Jan 2010
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 1

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

T-Dogg3030
Light Poster
39 posts since Nov 2009
Reputation Points: 10
Solved Threads: 5
Skill Endorsements: 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.

subramanya.vl
Junior Poster in Training
81 posts since Oct 2012
Reputation Points: 0
Solved Threads: 10
Skill Endorsements: 1

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?

Violet_82
Master Poster
793 posts since Jan 2010
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 1

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.

JamesCherrill
... trying to help
Moderator
8,512 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,455
Skill Endorsements: 30

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

Violet_82
Master Poster
793 posts since Jan 2010
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 1

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.

T-Dogg3030
Light Poster
39 posts since Nov 2009
Reputation Points: 10
Solved Threads: 5
Skill Endorsements: 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

Violet_82
Master Poster
793 posts since Jan 2010
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 1

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.

JamesCherrill
... trying to help
Moderator
8,512 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,455
Skill Endorsements: 30

Ok fair enough, thank you all for your contributions!

Violet_82
Master Poster
793 posts since Jan 2010
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 1
Question Answered as of 5 Months Ago by T-Dogg3030, JamesCherrill and subramanya.vl

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.3332 seconds using 2.76MB