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.

Here is my code so far. I need help creating the class that does what the question asks. Can anyone help me?


import java.util.Scanner; // program uses class Scanner


// Payroll program that calculates an employee's weekly pay.

public class PayApp

{

// main method begins execution of Java application
public static void main(String args[])

{

// create Scanner to obtain input from command window
Scanner input = new Scanner( System.in );

double hourlyRate = 0.0; //employee's payrate

double hoursWorked = 0.0; //employee's total hours

double pay; // total of hourlyRate and hoursWorked

String name = ""; //sets variable name

System.out.print( "Enter employee's name - Enter stop to end: " ); //prompt

System.out.flush(); //Flushes the buffer.

name = input.nextLine(); //read employee's name


while ( !name.equals( "stop" ) ) //ends program after entering stop for the employee name
{

System.out.print( "Enter hourly rate: " ); //prompt
hourlyRate = input.nextDouble(); //reads first number
System.out.flush(); //Flushes buffer.
while ( hourlyRate < 0.0 ) //starts while loop to make sure a positive number is entered.

{
System.out.print( "Input must be a positive number. Please Re-enter hourly rate: " ); //prompt
hourlyRate = input.nextDouble(); //reads first number
input.nextLine(); //prompt user for input
}
System.out.print( "Please Enter Hours Worked: " ); //prompt
hoursWorked = input.nextDouble(); //reads second number
input.nextLine(); //prompt user for input

while ( hoursWorked < 0.0 ) //Checks that a positive (valid) number is entered.
{//starts while loop

System.out.print( "Input must be a positive number. Please Re-enter Hours Worked: " ); //prompt
hoursWorked = input.nextDouble(); //read second number from user
input.nextLine(); //prompt user for input
}

pay = hourlyRate * hoursWorked; //calculates pay

System.out.printf( "Employee: %s\n", name ); //display employee's name
System.out.printf( "Total Pay for this period is $%.2f\n", pay ); //display total pay for the period


} //ends while loop


} //end method main

}//end class PayApp

Use code tags and it'll be easier to read:

[code]

// paste your code here

[/code]
or

[code=JAVA] // paste your code here

[/code]

The second of the two above ends up looking like this (after formatting through NetBeans):

import java.util.Scanner; // program uses class Scanner
// Payroll program that calculates an employee's weekly pay.
public class PayApp 
{
    // main method begins execution of Java application
    public static void main(String args[]) 
    {
        // create Scanner to obtain input from command window
        Scanner input = new Scanner(System.in);
        double hourlyRate = 0.0; //employee's payrate
        double hoursWorked = 0.0; //employee's total hours
        double pay; // total of hourlyRate and hoursWorked
        String name = ""; //sets variable name
        System.out.print("Enter employee's name - Enter stop to end: "); //prompt
        System.out.flush(); //Flushes the buffer.
        name = input.nextLine(); //read employee's name

        while (!name.equals("stop")) //ends program after entering stop for the employee name
        {
            System.out.print("Enter hourly rate: "); //prompt
            hourlyRate = input.nextDouble(); //reads first number
            System.out.flush(); //Flushes buffer.
            while (hourlyRate < 0.0) //starts while loop to make sure a positive number is entered.
            {
                System.out.print("Input must be a positive number. Please Re-enter hourly rate: "); //prompt
                hourlyRate = input.nextDouble(); //reads first number
                input.nextLine(); //prompt user for input
            }
            System.out.print("Please Enter Hours Worked: "); //prompt
            hoursWorked = input.nextDouble(); //reads second number
            input.nextLine(); //prompt user for input

            while (hoursWorked < 0.0) //Checks that a positive (valid) number is entered.
            {//starts while loop

                System.out.print("Input must be a positive number. Please Re-enter Hours Worked: "); //prompt
                hoursWorked = input.nextDouble(); //read second number from user
                input.nextLine(); //prompt user for input
            }

            pay = hourlyRate * hoursWorked; //calculates pay

            System.out.printf("Employee: %s\n", name); //display employee's name
            System.out.printf("Total Pay for this period is $%.2f\n", pay); //display total pay for the period
        } //ends while loop
    } //end method main
}//end class PayApp

As to your question itself, looks like you need to add some "Get" functions to your class. These functions should be public not private. An example would be:

public double GetHourlyRate ()
{
     return hourlyRate;
}

Note that the function takes no parameters, is public, and the data type of the return is the data type of the data member in question. As for the constructor, that will also be a public function within the class. It will have no return type.

public PayApp (double hourlyrate)
{
     hourlyRate = hourlyrate;
}

You have more data members to assign so it'll be longer and take more parameters, but that's the general idea.

Modify the Payroll Program so that it uses a class to store and retrieve the employee's data

since I do not detect a class Employee in your post, I take you have just given us your assignment as it was handed out to you along with the question: can you please make it for me?

the class you need help creating, is an Object of the type Employee. you know the name of the type, you know the data it's supposed to hold and I have no doubt that your class has already covered the subject of 'creating, initializing and using Objects', otherwise you wouldn't have been given this assignment.

my advice would be to start working for yourself as fast as possible, since even if you find someone willing to make your tasks for you, you'll still need to know how to do it for yourself on your exams.

since I do not detect a class Employee in your post, I take you have just given us your assignment as it was handed out to you along with the question: can you please make it for me?

the class you need help creating, is an Object of the type Employee. you know the name of the type, you know the data it's supposed to hold and I have no doubt that your class has already covered the subject of 'creating, initializing and using Objects', otherwise you wouldn't have been given this assignment.

my advice would be to start working for yourself as fast as possible, since even if you find someone willing to make your tasks for you, you'll still need to know how to do it for yourself on your exams.

Actually I do know how to do an object of the type of Employee, but am having trouble figuring out where to put it. And there is no need to be rude to me, I was under the impression that this form was to receive help if it was needed or a least an idea of a direction. I know if you are extremely experienced that this must be mundain but I am not that experienced and am learning as quickly as possible, both with NetBeans and JCreator.
Thank you for your input.

Thank you so much for the advice and the help, this makes since to me know. Again thank you it isn't easy finding help.

Okay I got irritated that I could not get the program to work the right way so I started over. I got this program to work, could someone just make sure it works the right way? It looks good to me, but I am new, what do I know.

package payroll3;

//Payroll3

import java.util.Scanner;

public class Main {

    // main begin Java application
    public static void main (String[] args) {
        Employee emp = new Employee();
        Scanner input = new Scanner(System.in);

        double payRate;
        double hoursWorked;
        double earnedPay;

        while (true)
        {
            System.out.println("Enter Employee firstName:");
            emp.setName(input.next());
            System.out.println ("Enter Employee lastName: ");
            emp.setName(input.next());

            if (!emp.getName().equalsIgnoreCase("Stop"))
            {

                    System.out.print ("Enter Employee payRate: $"); //prompt
                    emp.setpayRate(input.nextDouble());

                    while (emp.getpayRate() <= 0) 
                    {

System.out.println ("Invalid amount, setpayRate must be positive");
System.out.print ("Please enter valid payrate: $");emp.setpayRate(input.nextDouble());
  // Loop until valid number is entered for payrate: while (setpayRate < 0.0)
                    } // end while

                    System.out.print ("Enter number hours worked: ");
                    emp.sethoursWorked(input.nextDouble());
                    while (emp.gethoursWorked() <= 0 )
                    {

        System.out.println ("Invalid amount, hoursWorked must be positive");
                        System.out.print ("Please enter valid hourWorked: ");
                        emp.sethoursWorked(input.nextDouble());
                    }

  System.out.printf("%s's weekly earned pay is: $%.2f\n", 
          emp.getName(), emp.getpayRate()*emp.gethoursWorked ());
            } 
            else
                break;
        } // end while (true)

    } // end main

}//end class Payroll3


//  Class Employee
class Employee
{
    public String name;
    public double hourlyRate;
    public double hoursWorked;

    public Employee ()
    {
        //implicit call to object constructor occurs here
        name = "";
        hourlyRate = 0.0;
        hoursWorked = 0.0;
    }

    // Three argument initialization constructor


public Employee(String employeenameIn, double hourlyRateInDollarsIn, double hoursWorkedInWeekIn) {
        //implicit call to object constructor occurs here
        name = employeenameIn;
        hourlyRate = hourlyRateInDollarsIn;
        hoursWorked = hoursWorkedInWeekIn;
    } // end three argument intialization constructor

    public void setName (String name) {
        this.name = name;
    }

    public String getName () {
        return name;
    }

    public void setpayRate (double payRate) {
        this.hourlyRate = payRate;
    }

    public double getpayRate () {
        return hourlyRate;
    }

    public void sethoursWorked (double hours) {
        this.hoursWorked = hours;
    }

    public double gethoursWorked () {
        return hoursWorked;
    }

    public double getearnedPay () {
        return hourlyRate * hoursWorked;
    }
} // end class Employee

Close on the code tags, but not quite:

[code=JAVA] // paste code here

[/code]
There is a Preview button you can press to see what your post will look like so if you have a typo you can fix it before it's published. Here's your code. I haven't had time to look at it, but it makes more sense that you have an Employee class and you're now storing that info in the Employee class rather than the PayApp class. I hadn't thought of that originally in my first post, but making a NEW class as you have is better that incorporating it all into one class so keep Employee. Is there a reason you decided to change PayApp to Main?

Haven't had time to look at the code itself yet, but will.

package payroll3;

//Payroll3
import java.util.Scanner;

public class Main
{

// main begin Java application
    public static void main(String[] args)
    {
        Employee emp = new Employee();
        Scanner input = new Scanner(System.in);

        double payRate;
        double hoursWorked;
        double earnedPay;

        while (true)
        {
            System.out.println("Enter Employee firstName:");
            emp.setName(input.next());
            System.out.println("Enter Employee lastName: ");
            emp.setName(input.next());

            if (!emp.getName().equalsIgnoreCase("Stop"))
            {

                System.out.print("Enter Employee payRate: $"); //prompt
                emp.setpayRate(input.nextDouble());

                while (emp.getpayRate() <= 0)
                {

                    System.out.println("Invalid amount, setpayRate must be positive");
                    System.out.print("Please enter valid payrate: $");
                    emp.setpayRate(input.nextDouble());
// Loop until valid number is entered for payrate: while (setpayRate < 0.0)
                } // end while

                System.out.print("Enter number hours worked: ");
                emp.sethoursWorked(input.nextDouble());
                while (emp.gethoursWorked() <= 0)
                {

                    System.out.println("Invalid amount, hoursWorked must be positive");
                    System.out.print("Please enter valid hourWorked: ");
                    emp.sethoursWorked(input.nextDouble());
                }

                System.out.printf("%s's weekly earned pay is: $%.2f\n",
                        emp.getName(), emp.getpayRate() * emp.gethoursWorked());
            }
            else
            {
                break;
            }
        } // end while (true)

    } // end main
}//end class Payroll3
// Class Employee
class Employee
{

    public String name;
    public double hourlyRate;
    public double hoursWorked;

    public Employee()
    {
//implicit call to object constructor occurs here
        name = "";
        hourlyRate = 0.0;
        hoursWorked = 0.0;
    }

// Three argument initialization constructor
    public Employee(String employeenameIn, double hourlyRateInDollarsIn, double hoursWorkedInWeekIn)
    {
//implicit call to object constructor occurs here
        name = employeenameIn;
        hourlyRate = hourlyRateInDollarsIn;
        hoursWorked = hoursWorkedInWeekIn;
    } // end three argument intialization constructor

    public void setName(String name)
    {
        this.name = name;
    }

    public String getName()
    {
        return name;
    }

    public void setpayRate(double payRate)
    {
        this.hourlyRate = payRate;
    }

    public double getpayRate()
    {
        return hourlyRate;
    }

    public void sethoursWorked(double hours)
    {
        this.hoursWorked = hours;
    }

    public double gethoursWorked()
    {
        return hoursWorked;
    }

    public double getearnedPay()
    {
        return hourlyRate * hoursWorked;
    }
} // end class Employee

Every time I type PayApp where Main is, I get an error. I know it is something that I am missing but the system automatically gives it "Main" and I have tried everything. So i was gonna get me JAVA for Dummies book out and see what I have missed or forgot. But for now i left it that ways so I could run the program and see if it works.

When you make a new package in NetBeans, generally the default is to create a class called "Main" and put a "main" function in the "Main" class. The file that the "Main" class is stored in is called "Main.java". This is the default in NetBeans. If you want to call it something different, uncheck that box that creates "Main" when creating the new package. You are likely getting the error because if you change the name of "Main" to "PayApp", it is still stored in a file called "Main.java". You'd have a public class called "PayApp" inside a file called "Main.java", which you can't have.

Now that you have already created it, here is something you can do. Add a new file to your package called PayApp.java. Copy everything from Main.java and paste it into the new PayApp.java file. Then delete Main.java from the package. Finally, in PayApp.java and any other files, change the word "Main" to "PayApp".

In the future, just be careful of what boxes are being checked when creating the new package and uncheck them as needed.

See I knew it was something simple that I had over looked. I have tried everything and never even paid attention to that box. I have pasted and changed everything. Nice, thank you.

See I knew it was something simple that I had over looked. I have tried everything and never even paid attention to that box. I have pasted and changed everything. Nice, thank you.

You're welcome. Actually I made a slight mistake in my last post. The defaults that you have to uncheck are when you create a new project, not a new package.

package payroll3;

//Payroll3
import java.util.Scanner;

public class Main
{

// main begin Java application
    public static void main(String[] args)
    {
        Employee emp = new Employee();
        Scanner input = new Scanner(System.in);

        double payRate;
        double hoursWorked;
        double earnedPay;

        while (true)
        {
            System.out.println("Enter Employee firstName:");
            emp.setName(input.next());
            System.out.println("Enter Employee lastName: ");
            emp.setName(input.next());

            if (!emp.getName().equalsIgnoreCase("Stop"))
            {

                System.out.print("Enter Employee payRate: $"); //prompt
                emp.setpayRate(input.nextDouble());

                while (emp.getpayRate() <= 0)
                {

                    System.out.println("Invalid amount, setpayRate must be positive");
                    System.out.print("Please enter valid payrate: $");
                    emp.setpayRate(input.nextDouble());
// Loop until valid number is entered for payrate: while (setpayRate < 0.0)
                } // end while

                System.out.print("Enter number hours worked: ");
                emp.sethoursWorked(input.nextDouble());
                while (emp.gethoursWorked() <= 0)
                {

                    System.out.println("Invalid amount, hoursWorked must be positive");
                    System.out.print("Please enter valid hourWorked: ");
                    emp.sethoursWorked(input.nextDouble());
                }

                System.out.printf("%s's weekly earned pay is: $%.2f\n",
                        emp.getName(), emp.getpayRate() * emp.gethoursWorked());
            }
            else
            {
                break;
            }
        } // end while (true)

    } // end main
}//end class Payroll3
// Class Employee
class Employee
{

    public String name;
    public double hourlyRate;
    public double hoursWorked;

    public Employee()
    {
//implicit call to object constructor occurs here
        name = "";
        hourlyRate = 0.0;
        hoursWorked = 0.0;
    }

// Three argument initialization constructor
    public Employee(String employeenameIn, double hourlyRateInDollarsIn, double hoursWorkedInWeekIn)
    {
//implicit call to object constructor occurs here
        name = employeenameIn;
        hourlyRate = hourlyRateInDollarsIn;
        hoursWorked = hoursWorkedInWeekIn;
    } // end three argument intialization constructor

    public void setName(String name)
    {
        this.name = name;
    }

    public String getName()
    {
        return name;
    }

    public void setpayRate(double payRate)
    {
        this.hourlyRate = payRate;
    }

    public double getpayRate()
    {
        return hourlyRate;
    }

    public void sethoursWorked(double hours)
    {
        this.hoursWorked = hours;
    }

    public double gethoursWorked()
    {
        return hoursWorked;
    }

    public double getearnedPay()
    {
        return hourlyRate * hoursWorked;
    }
} // end class Employee

Line 81 - this comment is not true. The comment on line 72 is true.
Lines 15 - 17 - Are you using these variables anywhere? Their is no harm keeping them in but you appear to never use them.
Lines 21 - 24 - Lines 23 and 24 write over the information you collected and stored, so there is no point in asking for the first name.

Lines 66 - 68 - Make these variables private. The whole idea of using public "Get" and "Set" functions is to control access to the private data members. When you make the data members of a class public, you actually don't even need to use "Get" and "Set" functions.

The first part of the program required lines 15-17. I just kept them so Instructor could see them. I fixed the items you listed and ran program. It is very nice and the flow seems better. Thank you for your time and effort. Will let you know the grade I get.

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.