|
|
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.
|
|

07-26-2008, 08:27 PM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 20
|
|
|
Can Someone Help Me?
Hello all,
I'm new to Java and need some help. I am trying to Modify the Payroll 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. Once
stop is entered as the employee name, the application should terminate. Make sure the
program maintains all the functionality required in previous assignments and your source
code is readable and well documented. Use feedback you have received from the
instructor to make any needed modifications.
This is what I have so far.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplicationweek4b;
import java.util.Scanner;
/**
*
* @author A
*/
public class Week4b {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
boolean stop = false;
while (!stop)
{
Scanner input = new Scanner(System.in);
double HourlyRate=0.0;
double HoursWorked=0.0;
System.out.print("Employee Name or stop to end program:");
String EmpName = input.nextLine();
if (EmpName.equals("stop"))
{
System.out.println( "Program End." );
stop = true;
}
else
System.out.print("Number of Hours Worked:");
HoursWorked=input.nextDouble();
while (HoursWorked <= 0)
{
System.out.print("Hours Worked must have a positive value. Please reenter:");
HoursWorked = input.nextFloat();
}
System.out.print("Hourly Pay Rate:");
HourlyRate=input.nextDouble();
while (HourlyRate <=0)
{
System.out.print( "Hourly rate must be a positive value. Please reenter:");
HourlyRate = input.nextFloat();
}
Employee2 employee =
new Employee2(EmpName, HourlyRate , HoursWorked);
System.out.printf( "Employee Name: %s, Weekly pay is $%,.2f\n",
employee.empName, employee.getWeeklyPay() );
}
}
}
class Employee2
{
String empName;
double hoursWorked;
double payRate;
double Total;
public Employee2(String name, double HoursWorked, double HourlyRate)
{
this.empName = name;
this.hoursWorked = HoursWorked;
this.payRate = HourlyRate;
computeWeeklyPay();
}
private void computeWeeklyPay()
{
Total = hoursWorked * payRate;
}
public double getWeeklyPay()
{
return Total;
}
}
|
|

07-26-2008, 09:06 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: Southwest
Posts: 781
|
|
|
place to start
in between where is says input next Double and print HoursWorked = next double is where you need to do some calculations. The way we do that would be
float anAmount = HourlyRate.toFloat();
That just gets you a value that you can add, subtract, multiply and divide. Did your instructor show you how to do that? Is this a classroom instructed course? Have you read any basic Java books or know how to write the simplest Java program? This is not a complex program but looks like that if you have never seen Java before.
__________________
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. .
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
|
|

07-26-2008, 09:58 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
|
|
|
What do you need help on?
Compiler errors?
Runtime errors?
Wrong output? - Please show what you get and how you want it changed.
|
|

07-27-2008, 12:21 AM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 20
|
|
|
It Just not working the way I think it need too.?? I can't get it to retrieve name or anything else.??
I have no idea what I'm doing wrong....
|
|

07-27-2008, 12:55 AM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
|
|
It Just not working the way I think it need too.
You'll have to explain. Copy the output and add comments to it to show how you think it should work.
Where is it trying to retrieve name? Add comments to the code at the point something like: // NOT getting name here
|
|

07-27-2008, 01:09 AM
|
|
Senior Member
|
|
Join Date: Jul 2007
Posts: 1,222
|
|
import java.util.Scanner;
public class W4Rx
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
boolean stop = false;
while (!stop)
{
double HourlyRate=0.0;
double HoursWorked=0.0;
System.out.print("Employee Name or stop to end program:");
String EmpName = input.nextLine();
if (EmpName.equals("stop"))
{
System.out.println( "Program End." );
stop = true;
}
else
{
System.out.print("Number of Hours Worked:");
HoursWorked = Double.parseDouble(input.nextLine());
while (HoursWorked <= 0)
{
System.out.print("Hours Worked must have a " +
"positive value. Please reenter:");
HoursWorked = Double.parseDouble(input.nextLine());
}
System.out.print("Hourly Pay Rate:");
HourlyRate = Double.parseDouble(input.nextLine());
while (HourlyRate <=0)
{
System.out.print("Hourly rate must be a " +
"positive value. Please reenter:");
HourlyRate = Double.parseDouble(input.nextLine());
}
Employee2 employee =
new Employee2(EmpName, HourlyRate , HoursWorked);
System.out.printf( "Employee Name: %s, Weekly pay is $%,.2f\n",
employee.empName, employee.getWeeklyPay() );
}
}
}
}
|
|

07-27-2008, 01:32 AM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 20
|
|
|
But how do I retrieve data for the employee?
|
|

07-27-2008, 02:06 AM
|
|
Senior Member
|
|
Join Date: Jul 2007
Posts: 1,222
|
|
But how do I retrieve data for the employee?
The way you did here
System.out.printf( "Employee Name: %s, Weekly pay is $%,.2f\n",
employee.empName, employee.getWeeklyPay() );
by accessing fields and methods of the Employee2 class.
|
|

07-27-2008, 02:48 AM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 20
|
|
|
But if I run the application and input the name it doesn't retrieve anything.
|
|

07-27-2008, 02:51 AM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: Southwest
Posts: 781
|
|
|
rewrite?
Originally Posted by booter4429
But if I run the application and input the name it doesn't retrieve anything.
Hardwired put some code up, did you study the code and see what he did and where? This may be too much code for you to navigate. If so, ask us. Probably you need to trace the flow of data from main( String[] args) to where you use it and see how he did it compared to how you did it.
He used a Scanner, you probably have not put that in your code yet.
__________________
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. .
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
|
|

07-27-2008, 03:00 AM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 20
|
|
|
HoursWorked = Double.parseDouble(input.nextLine());
HourlyRate = Double.parseDouble(input.nextLine());
This is the only thing that is new. And if I use that I still can't get it to retrieve.??
|
|

07-27-2008, 03:04 AM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 20
|
|
Originally Posted by Nicholas Jordan
Hardwired put some code up, did you study the code and see what he did and where? This may be too much code for you to navigate. If so, ask us. Probably you need to trace the flow of data from main( String[] args) to where you use it and see how he did it compared to how you did it.
He used a Scanner, you probably have not put that in your code yet.
And yes I have used a Scanner but I think I'm lost. LOL
This is going to be a fun night. 
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|