470,810 Members | 1,205 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,810 developers. It's quick & easy.

help on simple problem

I need to modify the following 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. For some
reason Icant seem to grasp this simple task. Can some plese help me
out on this? Thanks

import java.util.Scanner;


public class Payroll


{


public static void main(String[] args)
{


// Variable Declaration
double payrate; // Hourly Rate of employee
double hours; // Hours Worked by employee
double grosspay; // Weekly Pay for employee
Scanner keyboard;


Scanner input = new Scanner( System.in );


// Get employee's name
System.out.printf( "Enter the employee's name(Enter stop when
finished):");


String name = input.next();
while(!name.equals("stop"))


{ // Start of name while loop


// Get hourly rate of employee
System.out.printf( "Enter the employee's hourly pay rate: " );
payrate = input.nextDouble();
while (payrate<=0)
{ // start payrate while loop
System.out.printf("\nPayrate must be positive, try again:");
payrate = input.nextDouble();
} // end of payrate while loop


// Get hours worked for the pay period
System.out.printf( "Enter the number of hours worked in this pay
period: ");
hours = input.nextDouble();
while ( hours <= 0 )
{ // start the hours while loop
System.out.printf( "\nSorry you need to enter a positive
number for the Hours:");
hours = input.nextDouble();
} // end hours while loop


// This calculates the gross pay.
grosspay = hours * payrate;


// Display results and reshows what you have entered
System.out.printf( "\nemployee: %s", name );
System.out.printf( "\nhourly rate: $%.2f", payrate );
System.out.printf( "\nhours worked: $%.2f", hours );
System.out.printf( "\nweekly paycheck: $%.2f\n",grosspay);


System.out.printf( "\n\n\nEnter another employee's name (Enter
stop when finished):");
name = input.next();


}// End of name while loop


}


}
Oct 20 '06 #1
6 2079
r035198x
13,262 8TB
I need to modify the following 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. For some
reason Icant seem to grasp this simple task. Can some plese help me
out on this? Thanks

import java.util.Scanner;


public class Payroll


{


public static void main(String[] args)
{


// Variable Declaration
double payrate; // Hourly Rate of employee
double hours; // Hours Worked by employee
double grosspay; // Weekly Pay for employee
Scanner keyboard;


Scanner input = new Scanner( System.in );


// Get employee's name
System.out.printf( "Enter the employee's name(Enter stop when
finished):");


String name = input.next();
while(!name.equals("stop"))


{ // Start of name while loop


// Get hourly rate of employee
System.out.printf( "Enter the employee's hourly pay rate: " );
payrate = input.nextDouble();
while (payrate<=0)
{ // start payrate while loop
System.out.printf("\nPayrate must be positive, try again:");
payrate = input.nextDouble();
} // end of payrate while loop


// Get hours worked for the pay period
System.out.printf( "Enter the number of hours worked in this pay
period: ");
hours = input.nextDouble();
while ( hours <= 0 )
{ // start the hours while loop
System.out.printf( "\nSorry you need to enter a positive
number for the Hours:");
hours = input.nextDouble();
} // end hours while loop


// This calculates the gross pay.
grosspay = hours * payrate;


// Display results and reshows what you have entered
System.out.printf( "\nemployee: %s", name );
System.out.printf( "\nhourly rate: $%.2f", payrate );
System.out.printf( "\nhours worked: $%.2f", hours );
System.out.printf( "\nweekly paycheck: $%.2f\n",grosspay);


System.out.printf( "\n\n\nEnter another employee's name (Enter
stop when finished):");
name = input.next();


}// End of name while loop


}


}

Expand|Select|Wrap|Line Numbers
  1. public class Employee {
  2.     String name;
  3.     double payRate; // Hourly Rate of employee
  4.     double hours; // Hours Worked by employee
  5.     double grossPay; // Weekly Pay for employee
  6.  
  7.     public Employee () {
  8.         name = "";
  9.         payRate = 0.0;
  10.         hours = 0.0;
  11.         grossPay = 0.0;
  12.     }
  13.  
  14.     public Employee (String name, double payRate, double hours) {
  15.         this.name = name;
  16.         this.payRate = payRate;
  17.         this.hours = hours;
  18.  
  19.     }
  20.  
  21.     public void calculateGross() {
  22.         grossPay = hours * payRate;
  23.     }
  24.  
  25.     public void setName(String name) {
  26.         this.name = name;
  27.     }
  28.     public void setName(double hours) {
  29.             this.hours = hours;
  30.     }
  31. }
Take it away from here
Oct 21 '06 #2
Expand|Select|Wrap|Line Numbers
  1. public class Employee {
  2.     String name;
  3.     double payRate; // Hourly Rate of employee
  4.     double hours; // Hours Worked by employee
  5.     double grossPay; // Weekly Pay for employee
  6.  
  7.     public Employee () {
  8.         name = "";
  9.         payRate = 0.0;
  10.         hours = 0.0;
  11.         grossPay = 0.0;
  12.     }
  13.  
  14.     public Employee (String name, double payRate, double hours) {
  15.         this.name = name;
  16.         this.payRate = payRate;
  17.         this.hours = hours;
  18.  
  19.     }
  20.  
  21.     public void calculateGross() {
  22.         grossPay = hours * payRate;
  23.     }
  24.  
  25.     public void setName(String name) {
  26.         this.name = name;
  27.     }
  28.     public void setName(double hours) {
  29.             this.hours = hours;
  30.     }
  31. }
Take it away from here
I did this but there is still somthing that I can not get right
Expand|Select|Wrap|Line Numbers
  1.  public class Payroll3
  2.  { // start Payroll3
  3.      public static void main( String args[])
  4.      {
  5.          Employee n = new Employee();
  6.  
  7.          n.showName();
  8.          n.showpayRate();
  9.          n.showHours();
  10.      }
  11.  } // end Payroll3
What am I missing?
Oct 21 '06 #3
r035198x
13,262 8TB
I did this but there is still somthing that I can not get right
Expand|Select|Wrap|Line Numbers
  1.  public class Payroll3
  2.  { // start Payroll3
  3.      public static void main( String args[])
  4.      {
  5.          Employee n = new Employee();
  6.  
  7.          n.showName();
  8.          n.showpayRate();
  9.          n.showHours();
  10.      }
  11.  } // end Payroll3
What am I missing?
You were supposed to add some methods to the Employee class that I gave you first. Did you define methods showName(), showpayRate() and showHours() in the Employee class?
Oct 23 '06 #4
You were supposed to add some methods to the Employee class that I gave you first. Did you define methods showName(), showpayRate() and showHours() in the Employee class?
Now I fell like an idiot, yes that’s what I forgot. Thanks
Oct 23 '06 #5
I can't seem to find what is wrong with this. I know a lot is wrong
by the amounts of errors that are popping up but I can't seem to find
a way to fix them. Can some one please help with my stupid mistakes?
Thanks
Expand|Select|Wrap|Line Numbers
  1.  import java.util.Scanner; 
  2.  class Employee 
  3.  
  4.  
  5.    private double payrate; 
  6.    private double hoursworked; 
  7.    private String name = "blanknow"; 
  8.  
  9.  
  10.  public Employee () 
  11.  { 
  12.   payrate = 0.0; 
  13.   hours = 0.0; 
  14.  } 
  15.  
  16.  
  17.  public void setpayrate(double payrate) 
  18.  { 
  19.   payrate = rate; 
  20.  } 
  21.  
  22.  
  23.   public double getpayrate() 
  24.    { 
  25.       return payrate; 
  26.    } 
  27.    { System.out.printf ( "enter employees payrate"); 
  28.      while (payrate<=0) 
  29.     System.out.printf("\nPayrate must be positive, try again:"); 
  30.         get.payrate = payrate; 
  31.  } 
  32.   public void sethoursworked(double hours) 
  33.    { 
  34.       hoursworked = hours; 
  35.    } 
  36.      public double gethoursworked() 
  37.    { 
  38.       return hoursworked; 
  39.    } 
  40.    { 
  41.      get.hours = hours; 
  42.  System.out.printf( "Enter the number of hours worked in this pay 
  43. period: "); 
  44.         while ( hours <= 0 ) 
  45.     System.out.printf( "\nSorry you need to enter a positive number for 
  46. the Hours:"); 
  47.         get.hours = hours; 
  48.      } 
  49.       public void setname(String name) 
  50.  { 
  51.   get.name = name; 
  52.  System.out.printf( "Enter the employee's name, untill Stop entered"); 
  53.      while (! get.name.equals("Stop")); 
  54.     } 
  55.  public double calculateweeklypay() 
  56.    { 
  57.       return payrate * hoursworked; 
  58.    } 
  59.  
  60.  
  61.  
  62.  
  63. ****************************************** 
  64.  public class Payroll3 
  65.  { // start Payroll3 
  66.  
  67.   public static void main( String args[]) 
  68.   { 
  69.   Scannerinput = new Scanner( System.in ); 
  70.  
  71.  
  72.     Employee myEmployee = new Employee(); 
  73.  
  74.  
  75.    System.out.printf("Employee Payroll calculator\n"); 
  76.  
  77.  
  78.          System.out.print("Enter Employee Pay Rate: "); 
  79.          myEmployee.setpayrate(input.nextDouble()); 
  80.  
  81.  
  82.          System.out.print( "Enter Hours Worked: "); 
  83.          myEmployee.sethoursworked(input.nextDouble()); 
  84.  
  85.  
  86.          System.out.printf("Employee earned " + 
  87. myEmployee.calculateweeklypay() ); 
  88.  
  89.  
  90.   } 
  91.  } // end Payroll3 
  92.  
Oct 25 '06 #6
r035198x
13,262 8TB
You need to revisit the tutorials on getters and setters

Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner;
  2.  class Employee {
  3.      private double payrate;
  4.      private double hoursworked;
  5.      private String name = "blanknow";
  6.      public Employee () {
  7.         payrate = 0.0;
  8.           hoursworked = 0.0;//hours or hoursworked?
  9.      }
  10.      public void setpayrate(double rate)// what's coming into the method is rate
  11.     {
  12.         payrate = rate;
  13.      }
  14.      public double getpayrate()
  15.     {
  16.         return payrate;
  17.        }
  18.  
  19.        /*What is all this supposed to be?
  20.    { System.out.printf ( "enter employees payrate");
  21.      while (payrate<=0)
  22.     System.out.printf("\nPayrate must be positive, try again:");
  23.         get.payrate = payrate;
  24.  }*/
  25.   public void sethoursworked(double hours)
  26.    {
  27.       hoursworked = hours;
  28.    }
  29.      public double gethoursworked()
  30.    {
  31.       return hoursworked;
  32.    }
  33.    public void setName(String s) {
  34.        name = s;
  35.    }
  36.  
  37.    /*What is all this supposed to be?
  38.  
  39.    {
  40.      get.hours = hours;
  41.  System.out.printf( "Enter the number of hours worked in this pay period: ");
  42.         while ( hours <= 0 )
  43.     System.out.printf( "\nSorry you need to enter a positive number for the Hours:");
  44.         get.hours = hours;
  45.      }
  46.       public void setname(String name)
  47.  {
  48.   get.name = name;
  49.  System.out.printf( "Enter the employee's name, untill Stop entered");
  50.      while (! get.name.equals("Stop"));
  51.     }*/
  52.  public double calculateweeklypay()
  53.    {
  54.       return payrate * hoursworked;
  55.    }
  56.  
  57.  
  58. }
  59.  
  60.  
  61. //******************************************
  62.  public class Payroll3
  63.  { // start Payroll3
  64.  
  65.   public static void main( String args[])
  66.   {
  67.   Scanner input = new Scanner(System.in);
  68.  
  69.  
  70.     Employee myEmployee = new Employee();
  71.  
  72.  
  73.    System.out.printf("Employee Payroll calculator\n");
  74.  
  75.  
  76.          System.out.print("Enter Employee Pay Rate: ");
  77.          myEmployee.setpayrate(input.nextDouble());
  78.  
  79.  
  80.          System.out.print( "Enter Hours Worked: ");
  81.          myEmployee.sethoursworked(input.nextDouble());
  82.  
  83.  
  84.          System.out.printf("Employee earned " + myEmployee.calculateweeklypay());
  85.  
  86.  
  87.   }
  88.  } // end Payroll3 
Oct 25 '06 #7

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Steve K | last post: by
2 posts views Thread by Vitali Gontsharuk | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.