Results 1 to 4 of 4
  1. #1
    bri1547 is offline Member
    Join Date
    Jul 2008
    Rep Power

    Default Need help passing data between classes

    I am working on an assignment for a java programming class and have a logic error that I can't figure out. I will post the code to both of the classes at the bottom. The first class asks the user to input an employee's name, rate of pay, and hours worked. These variables are passed to another class where the weekly pay is calculated. Ideally, the weekly pay will be returned in a printf statement. What is happening is that I get $0.00 no matter what I put in for hours and pay rate. Can anyone help?

    import java.util.Scanner; // scanner will be used to capture input from command screen

    public class Payroll3
    public static void main( String args [] )
    String employeeName; // Employee's name
    double hours; // number of hours worked
    double hourlyRate; // employee's hourly rate of pay
    double weeklyPay; //calculation of employee's weekly pay

    //create scanner to obtain input from command window
    Scanner input = new Scanner( );

    System.out.print( "Enter the employee's name. Enter 'stop' to quit:" ); //prompt
    employeeName = (); // read employee name

    while ( !employeeName.equalsIgnoreCase ("stop") )
    System.out.print( "Enter the number of hours the employee worked: " ); // prompt
    hours = input.nextDouble (); // read number of hours

    while ( hours < 0 ) // verify a positive value for hours worked
    System.out.print ( "Please enter a positive value for hours worked:" );// error message
    hours = input.nextDouble (); // new value for hours

    System.out.print( "Enter the employee's hourly rate of pay: $" ); // prompt
    hourlyRate = input.nextDouble (); // read employee rate of pay

    while (hourlyRate < 0 ) // verify a positive value for hourly rate
    System.out.print( "Please enter a positive value for hourly rate of pay:" );// error message
    hourlyRate = input.nextDouble ();// new value for hourly rate

    Employee myEmployee = new Employee( employeeName, hourlyRate, hours );

    System.out.printf( "Employee %s", employeeName ); // display employee name
    System.out.printf( " earned $%.2f\n", myEmployee.getWeeklyPay() ); // display weekly pay

    System.out.println(); // print a blank line to separate employees

    System.out.print( "Enter the employee's name. Enter 'stop' to quit:" ); //prompt
    employeeName = (); // read employee name


    System.out.println(); // print a blank line between employee's and exit message

    System.out.println( "Thank you for using the Payroll Program!" ); // exit message

    } // end method main

    } // end class Payroll2

    public class Employee // Class Employee to hold variables and calculate pay
    String name; // instance variable for employee name
    double rate; // instance variable for pay rate
    double hoursWorked; // instance variable for the number of hours worked

    public Employee( String name, double rate, double hoursWorked ) // create constructor
    name = null; // pass employee's name
    rate = 2; // pass rate of pay
    hoursWorked = 2; // pass number of hours worked
    } // end constructor

    public void setName ( String employeeName ) // method to set employee name
    name = employeeName; // store employee name
    } // end method setName

    public String getName() // method to retrieve employee name
    return name;
    } // end method getName

    public void setRate ( double hourlyRate ) // method to set rate of pay
    rate = hourlyRate; // store rate of pay
    } // end method setRate

    public double getRate() // method to retrieve rat of pay
    return rate;
    } // end method getRate

    public void setHoursWorked ( double hours ) // method to set number of hour worked
    hoursWorked = hours; // store number of hours worked
    } // end method setHoursWorked

    public double getHoursWorked() // method to retrieve number of hours worked
    return hoursWorked;
    } // end method getHoursWorked

    public double getWeeklyPay() // method to retrieve weekly pay
    return hoursWorked * rate;
    } // end method getWeeklyPay

    } // end class Employee

  2. #2
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Cebu City, Philippines
    Rep Power


    What's the problem of your code?
    freedom exists in the world of ideas

  3. #3
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Rep Power


    Yes you have an error here,if so ,you should use your set and get methods which were declared in Employee class.In main clas add those lines before printing all the variables:

    Java Code:
    and when you print all this stuff,print it for example as System.out.println(myEmploee.getRate());
    Last edited by serjant; 07-21-2008 at 07:30 AM.

  4. #4
    bri1547 is offline Member
    Join Date
    Jul 2008
    Rep Power



    Thanks for your help! I really, really appreciate it! As far as printing the name and weekly pay, the assignment calls specifically for the use of printf when printing the line. Again, thank you so much for your help!


Similar Threads

  1. Replies: 0
    Last Post: 04-07-2008, 08:56 PM
  2. Passing data from one JFrame to another
    By abhiN in forum New To Java
    Replies: 2
    Last Post: 03-28-2008, 06:39 AM
  3. passing data dynamically
    By abhiN in forum Advanced Java
    Replies: 1
    Last Post: 01-22-2008, 10:43 AM
  4. passing dynamic data
    By abhiN in forum Web Frameworks
    Replies: 0
    Last Post: 01-17-2008, 02:16 PM
  5. Passing variable information between classes
    By zen_to_go in forum New To Java
    Replies: 1
    Last Post: 10-30-2007, 09:09 PM

Posting Permissions

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