Results 1 to 4 of 4
- 07-20-2008, 07:05 AM #1
Member
- Join Date
- Jul 2008
- Posts
- 24
- Rep Power
- 0
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.in );
System.out.print( "Enter the employee's name. Enter 'stop' to quit:" ); //prompt
employeeName = input.next (); // 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 = input.next (); // 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
- 07-20-2008, 08:47 AM #2
What's the problem of your code?
freedom exists in the world of ideas
- 07-20-2008, 10:13 AM #3
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:
and when you print all this stuff,print it for example as System.out.println(myEmploee.getRate());Java Code:myEmployee.setName(employeeName); myEmplyee.setRate(hourlyRate); myEmployee.setHoursWorked(hours); myEmployee.setWeeklyPay(weeklyPay);
Last edited by serjant; 07-21-2008 at 06:30 AM.
- 07-21-2008, 04:19 AM #4
Member
- Join Date
- Jul 2008
- Posts
- 24
- Rep Power
- 0
Similar Threads
-
How to use Java's compression classes to reduce the amount of data sent over a socket
By Java Tip in forum java.netReplies: 0Last Post: 04-07-2008, 07:56 PM -
Passing data from one JFrame to another
By abhiN in forum New To JavaReplies: 2Last Post: 03-28-2008, 05:39 AM -
passing data dynamically
By abhiN in forum Advanced JavaReplies: 1Last Post: 01-22-2008, 09:43 AM -
passing dynamic data
By abhiN in forum Web FrameworksReplies: 0Last Post: 01-17-2008, 01:16 PM -
Passing variable information between classes
By zen_to_go in forum New To JavaReplies: 1Last Post: 10-30-2007, 08:09 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks