Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-20-2008, 08:05 AM
Member
 
Join Date: Jul 2008
Posts: 24
bri1547 is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-20-2008, 09:47 AM
sukatoa's Avatar
Senior Member
 
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 524
sukatoa is on a distinguished road
Send a message via Yahoo to sukatoa
What's the problem of your code?
__________________
A specific, detailed, simple, well elaborated, and "tested before asking" question may gather more quick replies. hopefully
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-20-2008, 11:13 AM
serjant's Avatar
Member
 
Join Date: Jun 2008
Location: Israel,Tel-Aviv
Posts: 40
serjant is on a distinguished road
Send a message via ICQ to serjant Send a message via Skype™ to serjant
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:

Code:
myEmployee.setName(employeeName); myEmplyee.setRate(hourlyRate); myEmployee.setHoursWorked(hours); myEmployee.setWeeklyPay(weeklyPay);
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.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 07-21-2008, 05:19 AM
Member
 
Join Date: Jul 2008
Posts: 24
bri1547 is on a distinguished road
serjant,

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!

Brian
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to use Java's compression classes to reduce the amount of data sent over a socket Java Tip java.net 0 04-07-2008 08:56 PM
Passing data from one JFrame to another abhiN New To Java 2 03-28-2008 06:39 AM
passing data dynamically abhiN Advanced Java 1 01-22-2008 10:43 AM
passing dynamic data abhiN Web Frameworks 0 01-17-2008 02:16 PM
Passing variable information between classes zen_to_go New To Java 1 10-30-2007 09:09 PM


All times are GMT +3. The time now is 01:27 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org