help on simple problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • porky008
    New Member
    • Oct 2006
    • 20

    #1

    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.Scann er;


    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.prin tf( "Enter the employee's name(Enter stop when
    finished):");


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


    { // Start of name while loop


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


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


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


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


    System.out.prin tf( "\n\n\nEnte r another employee's name (Enter
    stop when finished):");
    name = input.next();


    }// End of name while loop


    }


    }
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by porky008
    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.Scann er;


    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.prin tf( "Enter the employee's name(Enter stop when
    finished):");


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


    { // Start of name while loop


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


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


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


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


    System.out.prin tf( "\n\n\nEnte r another employee's name (Enter
    stop when finished):");
    name = input.next();


    }// End of name while loop


    }


    }

    Code:
    public class Employee {
    	String name;
    	double payRate; // Hourly Rate of employee
    	double hours; // Hours Worked by employee
    	double grossPay; // Weekly Pay for employee
    
    	public Employee () {
    		name = "";
    		payRate = 0.0;
    		hours = 0.0;
    		grossPay = 0.0;
    	}
    
    	public Employee (String name, double payRate, double hours) {
    		this.name = name;
    		this.payRate = payRate;
    		this.hours = hours;
    
    	}
    
    	public void calculateGross() {
    		grossPay = hours * payRate;
    	}
    
    	public void setName(String name) {
    		this.name = name;
    	}
    	public void setName(double hours) {
    			this.hours = hours;
    	}
    }
    Take it away from here

    Comment

    • porky008
      New Member
      • Oct 2006
      • 20

      #3
      Originally posted by r035198x
      Code:
      public class Employee {
      	String name;
      	double payRate; // Hourly Rate of employee
      	double hours; // Hours Worked by employee
      	double grossPay; // Weekly Pay for employee
      
      	public Employee () {
      		name = "";
      		payRate = 0.0;
      		hours = 0.0;
      		grossPay = 0.0;
      	}
      
      	public Employee (String name, double payRate, double hours) {
      		this.name = name;
      		this.payRate = payRate;
      		this.hours = hours;
      
      	}
      
      	public void calculateGross() {
      		grossPay = hours * payRate;
      	}
      
      	public void setName(String name) {
      		this.name = name;
      	}
      	public void setName(double hours) {
      			this.hours = hours;
      	}
      }
      Take it away from here
      I did this but there is still somthing that I can not get right
      Code:
       public class Payroll3
       { // start Payroll3
       	public static void main( String args[])
       	{
       		Employee n = new Employee();
       		
       		n.showName();
       		n.showpayRate();
       		n.showHours();
       	}
       } // end Payroll3
      What am I missing?

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by porky008
        I did this but there is still somthing that I can not get right
        Code:
         public class Payroll3
         { // start Payroll3
         	public static void main( String args[])
         	{
         		Employee n = new Employee();
         		
         		n.showName();
         		n.showpayRate();
         		n.showHours();
         	}
         } // 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?

        Comment

        • porky008
          New Member
          • Oct 2006
          • 20

          #5
          Originally posted by r035198x
          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

          Comment

          • porky008
            New Member
            • Oct 2006
            • 20

            #6
            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
            Code:
             import java.util.Scanner; 
             class Employee 
            { 
            
            
               private double payrate; 
               private double hoursworked; 
               private String name = "blanknow"; 
            
            
             public Employee () 
             { 
              payrate = 0.0; 
              hours = 0.0; 
             } 
            
            
             public void setpayrate(double payrate) 
             { 
              payrate = rate; 
             } 
            
            
              public double getpayrate() 
               { 
                  return payrate; 
               } 
               { System.out.printf ( "enter employees payrate"); 
                 while (payrate<=0) 
                System.out.printf("\nPayrate must be positive, try again:"); 
                    get.payrate = payrate; 
             } 
              public void sethoursworked(double hours) 
               { 
                  hoursworked = hours; 
               } 
                 public double gethoursworked() 
               { 
                  return hoursworked; 
               } 
               { 
                 get.hours = hours; 
             System.out.printf( "Enter the number of hours worked in this pay 
            period: "); 
                    while ( hours <= 0 ) 
                System.out.printf( "\nSorry you need to enter a positive number for 
            the Hours:"); 
                    get.hours = hours; 
                 } 
                  public void setname(String name) 
             { 
              get.name = name; 
             System.out.printf( "Enter the employee's name, untill Stop entered"); 
                 while (! get.name.equals("Stop")); 
                } 
             public double calculateweeklypay() 
               { 
                  return payrate * hoursworked; 
               } 
            
            
            } 
            
            
            ****************************************** 
             public class Payroll3 
             { // start Payroll3 
            
              public static void main( String args[]) 
              { 
              Scannerinput = new Scanner( System.in ); 
            
            
                Employee myEmployee = new Employee(); 
            
            
               System.out.printf("Employee Payroll calculator\n"); 
            
            
                     System.out.print("Enter Employee Pay Rate: "); 
                     myEmployee.setpayrate(input.nextDouble()); 
            
            
                     System.out.print( "Enter Hours Worked: "); 
                     myEmployee.sethoursworked(input.nextDouble()); 
            
            
                     System.out.printf("Employee earned " + 
            myEmployee.calculateweeklypay() ); 
            
            
              } 
             } // end Payroll3

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              You need to revisit the tutorials on getters and setters

              Code:
              import java.util.Scanner;
               class Employee {
              	 private double payrate;
                   private double hoursworked;
                   private String name = "blanknow";
               	public Employee () {
              		payrate = 0.0;
                		hoursworked = 0.0;//hours or hoursworked?
               	}
               	public void setpayrate(double rate)// what's coming into the method is rate
                  {
              		payrate = rate;
               	}
               	public double getpayrate()
                  {
              		return payrate;
                 	}
              
                 	/*What is all this supposed to be?
                 { System.out.printf ( "enter employees payrate");
                   while (payrate<=0)
                  System.out.printf("\nPayrate must be positive, try again:");
                      get.payrate = payrate;
               }*/
                public void sethoursworked(double hours)
                 {
                    hoursworked = hours;
                 }
                   public double gethoursworked()
                 {
                    return hoursworked;
                 }
                 public void setName(String s) {
              	   name = s;
                 }
              
                 /*What is all this supposed to be?
              
                 {
                   get.hours = hours;
               System.out.printf( "Enter the number of hours worked in this pay period: ");
                      while ( hours <= 0 )
                  System.out.printf( "\nSorry you need to enter a positive number for the Hours:");
                      get.hours = hours;
                   }
                    public void setname(String name)
               {
                get.name = name;
               System.out.printf( "Enter the employee's name, untill Stop entered");
                   while (! get.name.equals("Stop"));
                  }*/
               public double calculateweeklypay()
                 {
                    return payrate * hoursworked;
                 }
              
              
              }
              
              
              //******************************************
               public class Payroll3
               { // start Payroll3
              
                public static void main( String args[])
                {
                Scanner input = new Scanner(System.in);
              
              
                  Employee myEmployee = new Employee();
              
              
                 System.out.printf("Employee Payroll calculator\n");
              
              
                       System.out.print("Enter Employee Pay Rate: ");
                       myEmployee.setpayrate(input.nextDouble());
              
              
                       System.out.print( "Enter Hours Worked: ");
                       myEmployee.sethoursworked(input.nextDouble());
              
              
                       System.out.printf("Employee earned " + myEmployee.calculateweeklypay());
              
              
                }
               } // end Payroll3

              Comment

              Working...