Results 1 to 4 of 4
  1. #1
    janmjeay is offline Member
    Join Date
    Jun 2013
    Posts
    2
    Rep Power
    0

    Default What is wrong in my code

    Hello All,
    I am a Java newbie and learning stuff. I created 2 classes Employee and DoPayroll which worked perfectly.
    So I updated these 2 classes to convert 1 variable into setter/getter method leaving aside everything as it is.

    Code gets compiled but does NOT work, not able to figure out what is wrong and need your help.
    Thank you in advance for your time.

    -----------------------------------------
    Original Employee & DoPayroll class
    -----------------------------------------
    package DummyPackage;

    import static java.lang.System.out;

    public class Employee {
    private String name;
    private String jobTitle;
    // private double amountPaid;

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

    public String getName() {
    return name;
    }

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

    public String getJobTitle() {
    return jobTitle;
    }

    -------------------------------
    And below is DoPayroll class
    --------------------------------
    package DummyPackage;

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

    public class DoPayroll {
    public static void main(String args[])
    throws IOException {
    Scanner diskScanner =
    new Scanner(new File("C://PracticeTutorials//src//DummyPackage//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();
    }
    }

    Output I see is as under -

    Pay to the order of SAMUEL JOSEPH (ANALYST) ***$1,200.00
    Pay to the order of JOHN THORPE (SUPERVISOR) ***$1,300.00
    Pay to the order of LARRY BURRD (SECURITY) ***$1,400.00

    ===========================================
    Changed classes are as under WHICH ARE NOT WORKING

    /*
    * Employee1 differs from Employee class ONLY for Set and Get methods of AmountPaid
    * AmountPaid is made into SetAmountPaid and getAmountPaid - that is the ONLY change
    */

    package DummyPackage;

    import static java.lang.System.out;

    public class Employee1 {
    private String name;
    private String jobTitle;
    private double amountPaid;

    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 setAmountPaid(double amountPaidIn) {
    amountPaid = amountPaidIn;
    }
    public double getAmountPaid() {
    return amountPaid;
    }

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

    -------------------------------
    And DoPayroll1 class is as under
    --------------------------------

    /*
    * DoPayroll1 differs from DoPayroll class ONLY for Set and Get methods of AmountPaid of Employee1 used here
    * in DoPayroll 1 class
    *
    */
    package DummyPackage;

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

    public class DoPayroll1 {
    public static void main(String args[])
    throws IOException {
    Scanner diskScanner =
    new Scanner(new File("C://Users//PracticeTutorials//DummyPackage//EmployeeInfo.txt"));
    for (int empNum = 1; empNum <= 3; empNum++) {
    payOneEmployee(diskScanner);
    }
    }

    static void payOneEmployee(Scanner aScanner) {
    Employee1 anEmployee = new Employee1();
    anEmployee.setName(aScanner.nextLine());
    anEmployee.setJobTitle(aScanner.nextLine());
    anEmployee.setAmountPaid(aScanner.nextDouble());
    aScanner.nextLine();
    }
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,931
    Rep Power
    4

    Default Re: What is wrong in my code

    I believe you forgot to cut the check.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    janmjeay is offline Member
    Join Date
    Jun 2013
    Posts
    2
    Rep Power
    0

    Default Re: What is wrong in my code

    Jim, thanks for your reply.
    I actually had the method cutcheck and I had commented it out since now I use getAmountPaid instead.
    therefore I did not include it above in my question. Code snippet with that commented line is as under -
    But it doesn't help.
    ---------------------

    static void payOneEmployee(Scanner aScanner) {
    Employee1 anEmployee = new Employee1();
    anEmployee.setName(aScanner.nextLine());
    anEmployee.setJobTitle(aScanner.nextLine());
    // anEmployee.cutCheck(aScanner.nextDouble());
    anEmployee.setAmountPaid(aScanner.nextDouble());
    aScanner.nextLine();
    }
    }
    ===============
    If you run the two classes above you will see it doesn't work. Since compiler gives no error I am NOT able to figure out what is going wrong in my code.
    appreciate your help.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,931
    Rep Power
    4

    Default Re: What is wrong in my code

    I made my own data file and it works for me.

    Try changing:

    Java Code:
    Employee1 anEmployee = new Employee1();
    anEmployee.setName(aScanner.nextLine());
    anEmployee.setJobTitle(aScanner.nextLine());
    //  anEmployee.cutCheck(aScanner.nextDouble());
    anEmployee.setAmountPaid(aScanner.nextDouble());
    aScanner.nextLine();
    to

    Java Code:
    Employee1 anEmployee = new Employee1();
    anEmployee.setName(aScanner.nextLine());
    anEmployee.setJobTitle(aScanner.nextLine());
    anEmployee.setAmountPaid(aScanner.nextDouble());
    anEmployee.cutCheck();
    aScanner.nextLine();
    As I said before, in the first code you posted there is no invocation of the cutCheck() method in your DoPayRoll1 class. Or perhaps I am missing something.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. What is wrong with the Code below
    By Bahramudin in forum New To Java
    Replies: 4
    Last Post: 08-16-2012, 07:10 PM
  2. What's wrong with my code?
    By Johanis in forum New To Java
    Replies: 1
    Last Post: 10-30-2011, 12:38 PM
  3. What is wrong with this code?
    By Mythreadings in forum New To Java
    Replies: 38
    Last Post: 11-19-2010, 12:43 AM
  4. What Could be Wrong with This code????
    By Manfizy in forum New To Java
    Replies: 9
    Last Post: 08-22-2010, 11:28 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •