Results 1 to 12 of 12
  1. #1
    booter4429 is offline Member
    Join Date
    Jul 2008
    Posts
    20
    Rep Power
    0

    Default Can Someone Help Me?

    Hello all,

    I'm new to Java and need some help. I am trying to Modify the Payroll Program so that it uses a class to store and retrieve the employee's
    name, the hourly rate, and the number of hours worked. Use a constructor to initialize the
    employee information, and a method within that class to calculate the weekly pay. Once
    stop is entered as the employee name, the application should terminate. Make sure the
    program maintains all the functionality required in previous assignments and your source
    code is readable and well documented. Use feedback you have received from the
    instructor to make any needed modifications.


    This is what I have so far.


    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */

    package javaapplicationweek4b;

    import java.util.Scanner;

    /**
    *
    * @author A
    */
    public class Week4b {

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {
    // TODO code application logic here
    boolean stop = false;

    while (!stop)
    {


    Scanner input = new Scanner(System.in);

    double HourlyRate=0.0;
    double HoursWorked=0.0;




    System.out.print("Employee Name or stop to end program:");
    String EmpName = input.nextLine();

    if (EmpName.equals("stop"))
    {
    System.out.println( "Program End." );

    stop = true;
    }

    else


    System.out.print("Number of Hours Worked:");
    HoursWorked=input.nextDouble();

    while (HoursWorked <= 0)
    {
    System.out.print("Hours Worked must have a positive value. Please reenter:");


    HoursWorked = input.nextFloat();
    }
    System.out.print("Hourly Pay Rate:");
    HourlyRate=input.nextDouble();

    while (HourlyRate <=0)
    {
    System.out.print( "Hourly rate must be a positive value. Please reenter:");


    HourlyRate = input.nextFloat();
    }





    Employee2 employee =
    new Employee2(EmpName, HourlyRate , HoursWorked);

    System.out.printf( "Employee Name: %s, Weekly pay is $%,.2f\n",
    employee.empName, employee.getWeeklyPay() );

    }


    }

    }
    class Employee2
    {
    String empName;
    double hoursWorked;
    double payRate;
    double Total;

    public Employee2(String name, double HoursWorked, double HourlyRate)
    {
    this.empName = name;
    this.hoursWorked = HoursWorked;
    this.payRate = HourlyRate;
    computeWeeklyPay();
    }

    private void computeWeeklyPay()
    {
    Total = hoursWorked * payRate;
    }

    public double getWeeklyPay()
    {
    return Total;

    }
    }

  2. #2
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default place to start

    in between where is says input next Double and print HoursWorked = next double is where you need to do some calculations. The way we do that would be

    float anAmount = HourlyRate.toFloat();

    That just gets you a value that you can add, subtract, multiply and divide. Did your instructor show you how to do that? Is this a classroom instructed course? Have you read any basic Java books or know how to write the simplest Java program? This is not a complex program but looks like that if you have never seen Java before.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,387
    Rep Power
    25

    Default

    What do you need help on?
    Compiler errors?
    Runtime errors?
    Wrong output? - Please show what you get and how you want it changed.

  4. #4
    booter4429 is offline Member
    Join Date
    Jul 2008
    Posts
    20
    Rep Power
    0

    Default

    It Just not working the way I think it need too.?? I can't get it to retrieve name or anything else.??

    I have no idea what I'm doing wrong....

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,387
    Rep Power
    25

    Default

    It Just not working the way I think it need too.
    You'll have to explain. Copy the output and add comments to it to show how you think it should work.
    Where is it trying to retrieve name? Add comments to the code at the point something like: // NOT getting name here

  6. #6
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    import java.util.Scanner;
    
    public class W4Rx
    {
        public static void main(String[] args)
        {
            Scanner input = new Scanner(System.in);
            boolean stop = false;
    
            while (!stop)
            {
                double HourlyRate=0.0;
                double HoursWorked=0.0;
    
                System.out.print("Employee Name or stop to end program:");
                String EmpName = input.nextLine();
    
                if (EmpName.equals("stop")) 
                {
                    System.out.println( "Program End." );
    
                    stop = true;
                }
                else
                {
                    System.out.print("Number of Hours Worked:");
                    HoursWorked = Double.parseDouble(input.nextLine());
    
                    while (HoursWorked <= 0)
                    {
                        System.out.print("Hours Worked must have a " +
                                         "positive value. Please reenter:");
    
                        HoursWorked = Double.parseDouble(input.nextLine());
                    }
    
                    System.out.print("Hourly Pay Rate:");
                    HourlyRate = Double.parseDouble(input.nextLine());
    
                    while (HourlyRate <=0)
                    {
                        System.out.print("Hourly rate must be a " +
                                         "positive value. Please reenter:");
    
                        HourlyRate = Double.parseDouble(input.nextLine()); 
                    }
    
                    Employee2 employee =
                        new Employee2(EmpName, HourlyRate , HoursWorked);
    
                    System.out.printf( "Employee Name: %s, Weekly pay is $%,.2f\n",
                                        employee.empName, employee.getWeeklyPay() );
                }
            }
        }
    }

  7. #7
    booter4429 is offline Member
    Join Date
    Jul 2008
    Posts
    20
    Rep Power
    0

    Default

    But how do I retrieve data for the employee?

  8. #8
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    But how do I retrieve data for the employee?
    The way you did here
    Java Code:
    System.out.printf( "Employee Name: %s, Weekly pay is $%,.2f\n",
                        employee.empName, employee.getWeeklyPay() );
    by accessing fields and methods of the Employee2 class.

  9. #9
    booter4429 is offline Member
    Join Date
    Jul 2008
    Posts
    20
    Rep Power
    0

    Default

    But if I run the application and input the name it doesn't retrieve anything.

  10. #10
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default rewrite?

    Quote Originally Posted by booter4429 View Post
    But if I run the application and input the name it doesn't retrieve anything.
    Hardwired put some code up, did you study the code and see what he did and where? This may be too much code for you to navigate. If so, ask us. Probably you need to trace the flow of data from main( String[] args) to where you use it and see how he did it compared to how you did it.

    He used a Scanner, you probably have not put that in your code yet.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  11. #11
    booter4429 is offline Member
    Join Date
    Jul 2008
    Posts
    20
    Rep Power
    0

    Default

    HoursWorked = Double.parseDouble(input.nextLine());

    HourlyRate = Double.parseDouble(input.nextLine());

    This is the only thing that is new. And if I use that I still can't get it to retrieve.??

  12. #12
    booter4429 is offline Member
    Join Date
    Jul 2008
    Posts
    20
    Rep Power
    0

    Arrow

    Quote Originally Posted by Nicholas Jordan View Post
    Hardwired put some code up, did you study the code and see what he did and where? This may be too much code for you to navigate. If so, ask us. Probably you need to trace the flow of data from main( String[] args) to where you use it and see how he did it compared to how you did it.

    He used a Scanner, you probably have not put that in your code yet.
    And yes I have used a Scanner but I think I'm lost. LOL

    This is going to be a fun night.:)

Posting Permissions

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