Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
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-26-2008, 08:27 PM
Member
 
Join Date: Jul 2008
Posts: 20
booter4429 is on a distinguished road
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;

}
}
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-26-2008, 09:06 PM
Nicholas Jordan's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Southwest
Posts: 781
Nicholas Jordan is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-26-2008, 09:58 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
Norm is on a distinguished road
What do you need help on?
Compiler errors?
Runtime errors?
Wrong output? - Please show what you get and how you want it changed.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 07-27-2008, 12:21 AM
Member
 
Join Date: Jul 2008
Posts: 20
booter4429 is on a distinguished road
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....
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 07-27-2008, 12:55 AM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
Norm is on a distinguished road
Quote:
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
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 07-27-2008, 01:09 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
Code:
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() ); } } } }
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 07-27-2008, 01:32 AM
Member
 
Join Date: Jul 2008
Posts: 20
booter4429 is on a distinguished road
But how do I retrieve data for the employee?
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 07-27-2008, 02:06 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
But how do I retrieve data for the employee?
The way you did here
Code:
System.out.printf( "Employee Name: %s, Weekly pay is $%,.2f\n", employee.empName, employee.getWeeklyPay() );
by accessing fields and methods of the Employee2 class.
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 07-27-2008, 02:48 AM
Member
 
Join Date: Jul 2008
Posts: 20
booter4429 is on a distinguished road
But if I run the application and input the name it doesn't retrieve anything.
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 07-27-2008, 02:51 AM
Nicholas Jordan's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Southwest
Posts: 781
Nicholas Jordan is on a distinguished road
rewrite?
Quote:
Originally Posted by booter4429 View Post
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
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 07-27-2008, 03:00 AM
Member
 
Join Date: Jul 2008
Posts: 20
booter4429 is on a distinguished road
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.??
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 07-27-2008, 03:04 AM
Member
 
Join Date: Jul 2008
Posts: 20
booter4429 is on a distinguished road
Quote:
Originally Posted by Nicholas Jordan View Post
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.
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



All times are GMT +3. The time now is 02:30 AM.


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