tags:

views:

200

answers:

3

Now I have read and read on this but i am just stuck. Any help appreciated. I am looking for hints. The code compiles and runs fine but I don't think the variables are being stored in the employee class and I am not sure i am understanding my use of the constructor right. -Thanks

Requirements: I have completed:

Values are checked to ensure they are positive numbers.

Entering stop as the employee name end program.

Having trouble on

Uses a class to store

  • name
  • hourly rate
  • hours worked

Use a constructor to initialize the employee information and a method within that class to calculate the weekly pay.

Thanks for all the help! FINAL ACCEPTED ASSIGNMENT CODE POSTED BELOW A+

/** Payroll3.java | Due 8/09/09 
** IT 2015 Java Programming | lazfsh | Axia College - University of Phoenix */

import java.util.Scanner;// Import and use scanner

public class Payroll3 {// main method begins
   public static void main( String args[] ) {

   // Initialize
   boolean stop = false;// Variable for exit procedure
   String endProgram = "";// Variable to explain exit procedures blank 1st time
   int version = 3;// Version number

   // Welcome message   
   System.out.printf( 
   "\nWelcome to the Payroll Program v.%d\n", version );//Welcome message      

   while ( stop == false) {// Run program until stop equals true 

      Scanner input = new Scanner( System.in );// new Scanner for CL input
      Employee Employee = new Employee(); // new Employee constructor

      // Prompt for and input name
      System.out.printf( "\nEnter name of the employee%s:", endProgram );
      Employee.setempName(input.nextLine());// Accept input & Store

      if ( Employee.getempName().equals( "stop" )) {// If = end program w/message
         System.out.printf( "\n%s\n%s\n\n", "....", "Thanks for using Payroll Program");
         stop = true;}
      else{// Continue retrieve hourlyRate, hoursWorked & Calculate

         do {// Prompt for and input hourlyRate
            System.out.printf( "\n%s", "Enter hourly rate: $" );
            Employee.sethourlyRate(input.nextFloat());// Accept input & Store
            if ( Employee.gethourlyRate() < 1 ) {// Show error for negative #
               System.out.printf( "%s", "Enter a positive number.");}
            else;{}// Continue
            } while ( Employee.gethourlyRate() < 1 );// End loop if positive number recieved

         do {// Prompt for and input hoursWorked
            System.out.printf( "\n%s", "Enter number of hours worked:" );
            Employee.sethoursWorked(input.nextFloat());// Accept input & Store
            if ( Employee.gethoursWorked() < 1 ) {// Show error for negative #
               System.out.printf( "%s", "Enter a positive number.");}
            else;{}// Continue
            } while ( Employee.gethoursWorked() < 1 );// End loop if positive number recieved

         // Display formatted results
         System.out.printf( "\n%s's weekly pay is $%,.2f\nHourly rate ($%,.2f) multiplied by hours worked (%.0f)\n\n...Ready for next employee.\n\n",
         Employee.getempName(), Employee.getweeklyPay(), Employee.gethourlyRate(), Employee.gethoursWorked() );

// Debug Line         Employee.showVariables();         

         }// end retrieve hourlyRate, hoursWorked & Calculate

         endProgram = ( ", \n(Or type \"stop\" to end the program)" );//explain exit procedure on second empName prompt

      }// ends program when stop equals true

   }// end method main

}//end class Payroll3

Employee class

/** Employee Class | Initializes and storeds data for employee
      Also provides method to calculate weekly pay.
*/

// Import statements
import java.util.Scanner;// Import and use scanner


public class Employee {//Begin Employee class
   Scanner input = new Scanner( System.in );// new Scanner for CL input

   // Declare instance variables
   String empName; // Declare name as string
   float hourlyRate; // Declare hourlyRate as float
   float hoursWorked; // Declare hoursWorked as float

   // constructor initializes employee information
   public Employee() { // Initialize (clear) instance variables here
      empName = "";
      hourlyRate = 0; 
      hoursWorked = 0; 
      } // end constructor

   // Begin methods
   public void setempName(String empName) {// public method to set the employee name 
      this.empName = empName;}// end method setempName

   public String getempName() {// public method to get the employee name
      return empName;}// end method getempName

   public void sethourlyRate(float hourlyRate) {// public method to set the hourly Rate
      this.hourlyRate = hourlyRate;}// end method set hourly Rate

   public float gethourlyRate() {// public method to retrieve the hourly Rate
      return hourlyRate;} // end method get hourly Rate

   public void sethoursWorked(float hoursWorked) {// public method to set the hours Worked
      this.hoursWorked = hoursWorked;} // end method set hours Worked

   public float gethoursWorked() {// public method to retrieve the hours Worked
      return hoursWorked;} // end method get hours Worked

   public float getweeklyPay() {// public method to retrieve weekly Pay
      return ( hoursWorked * hourlyRate );}// end method get weeklyPay

   /* Test line
   public void showVariables(){
   System.out.printf( "empName=%s, hourlyRate=%s, hoursWorked=%s", this.empName, this.hourlyRate, this.hoursWorked );} */

   } // end class Employee
+2  A: 

Here are a couple hints:

Use a constructor to initialize the employee information

Looking at the provided code, there is a single constructor which takes no arguments and will initialize the fields of the object to zero.

Perhaps the "constructor to initialize the employee information" means that the constructor should be able to accept values which the Employee object should initlialize its fields to.

The Java Tutorials has a page on Providing Constructors for Your Classes with examples which should help in creating such constructor.

... and a method within that class to calculate the weekly pay

This seems to say that there should be a method that is only visible to itself and not available from others in order to calculate the value for the weeklyPay field.

Again, from The Java Tutorials, Controlling Access to Members of a Class discusses show to change the visibility of methods.

Reading the contents of the assignment, it seems like taking a look at the Lesson: Classes and Objects of The Java Tutorials may be of use, as it provides some good examples and explanations on the concepts of object-oriented programming.

coobird
+1  A: 

You'll notice that you are asked to make a class that stores the name, the hourly rate, and the hours worked, while you are asked to make a method that calculates the weekly pay. In other words, you should not have a weeklyPay instance variable. Whenever the user asks for the weekly pay (by means of your getWeeklyPay() method), you calculate and return it directly, without storing it in an instance variable.

Then, in order to actually use that result, you'll need to change this:

Employee.getweeklyPay();// Calculate weeklyPay ( hoursWorked * hourlyRate )
weeklyPay = ( hoursWorked * hourlyRate );

into something like this:

weeklyPay = employee.getWeeklyPay();

If you don't actually assign the result of your method to some variable, you can't use the result.

John Calsbeek
+2  A: 

In addition to the fact that you haven't used the constructor to set your variables, as mentioned by others. The setter methods are performing no-ops. Which is why you aren't getting the results you expect. You are setting the local var to itself and not to the member var.

You need to either change your local var names, change the member var names, or change the setters to use the 'this' keyword

 public void sethoursWorked(float hoursWorked)
 { 
    this.hoursWorked = hoursWorked;
 }
James R