473,396 Members | 1,866 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Check Code to ensure correctness

Modify payroll program so that it uses a class to store and retrive the employee's name, 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 code is readable and well documented.

This is what I have done is this correct, as per the parameters above:

Expand|Select|Wrap|Line Numbers
  1. import java.util.ArrayList;
  2. import java.util.Scanner; // program uses class Scanner
  3.  
  4. class EmployeeData {
  5.     EmployeeData(String newName, float newHourlyRate, float newHoursWorked) {
  6.         name = newName;  hourlyRate = newHourlyRate;  hoursWorked = newHoursWorked;
  7.     }
  8.  
  9.     public String getName() { return name; }
  10.     public float getWeeklyPay() { return hourlyRate * hoursWorked; }
  11.  
  12.     private String name;
  13.     private float hourlyRate, hoursWorked;
  14. } // end of EmployeeData class
  15.  
  16. public class payroll4a
  17. {
  18.    private String EmployeeData; // employee information for this Payroll
  19.  
  20.     private static int ArrayList;
  21.  
  22.     private static int i;
  23.  
  24.    // main method begins execution of java application
  25.    public static void main( String args[] ) 
  26.    {
  27.        ArrayList employees = new ArrayList (); // employee information to be stored for all employees
  28.  
  29.            boolean stop = false; // This flag will control whether you exit the loop below
  30.   // Loop until user types "stop" as the employee name:
  31.          while (!stop)
  32.  
  33.                 {     
  34.             // create scanner to obtain input from command window
  35.             Scanner input = new Scanner ( System.in );
  36.  
  37.             System.out.println();  // outputs a blank line
  38.             System.out.println( "Please enter the employee name or 'stop' to end program: " ); // prompt
  39.             String empName = input.nextLine(); // read employee name
  40.  
  41.             if ( empName.compareTo("stop") == 0) // Check whether user indicated to stop program
  42.             {
  43.                 System.out.println( "Program ended." );
  44.                 stop = true;
  45.             }
  46.           else
  47.           {
  48.        // User did not indicate to stop, so continue reading info for this iteration:
  49.                 EmployeeData employee;
  50.                 float hourlyRate; // first number to multiply
  51.                 float hoursWorked; // second number to multiply
  52.                 float weeklyPay; // product of hourlyRate and hoursWorked
  53.            // If we are at the end of input then NoSuchElement;
  54.     // If there is still input left then InputMismatch
  55.     {
  56.  
  57.                 System.out.println( "Please enter hourly rate: $" ); // prompt
  58.                 hourlyRate = input.nextFloat(); // read hourly rate from user
  59.                 if (hourlyRate <= 0) // prompt until a positive value is entered
  60.  
  61.                 {
  62.                     System.out.println( "Hourly rate must be a positive value. " +
  63.                        "Please enter the hourly rate again: $" ); // prompt for positive value for hourly rate
  64.                     hourlyRate = input.nextFloat(); // read hourly rate again
  65.  
  66.                 }
  67.  
  68.               System.out.println( "Please enter hours worked: " ); // prompt
  69.               hoursWorked = input.nextFloat(); // read number of hours worked from user
  70.               while (hoursWorked <= 0) // prompt until a positive value is entered
  71.               {
  72.                    System.out.println( "Hours worked must be a positive value. " +
  73.                   "Please enter the hours worked again: " ); // prompt for positive value for hours worked
  74.                   hoursWorked = input.nextFloat(); // read hours worked again
  75.               }
  76.  
  77.              employee = new EmployeeData(empName, hourlyRate, hoursWorked) {
  78.              ///weeklyPay = hourlyRate * hoursWorked; // multiply
  79.  
  80.                            };
  81.               System.out.print( employee.getName() ); // display employee name
  82.               System.out.printf( "'s weekly pay is: $%,.2f\n", employee.getWeeklyPay() );  // display weekly pay
  83.  
  84.  
  85.  
  86.  
  87.  
  88.         }
  89.       }
  90.  
  91.    } // end method main
  92.  
  93. }} // end class Payroll4a
Feb 8 '08 #1
5 3017
Laharl
849 Expert 512MB
Is there any place in particular you're having problems? Is your code not doing what you want it to? Skimming it, I don't see anything that screams "WRONG!", though you never use employees anywhere. Also, if you're working in Java 1.5 or later (you should be, 1.6 is recommended), use an ArrayList<EmployeeData> so that you don't have to cast values accessed to EmployeeData.
Feb 8 '08 #2
Yes,
As per the instructions store and retrieve name, hourly rate and the number of hours worked. Use a constructor to intialize the employee information.
Does what I have written fit? It does compile and work........ Just want to ensure I have this done correctly

Thank You
Hammer45
Feb 8 '08 #3
Laharl
849 Expert 512MB
It looks fine to me, though if you're going to declare an ArrayList to store employee data, you should put employee data into the list.

Your constructor is fine as it is.
Feb 8 '08 #4
BigDaddyLH
1,216 Expert 1GB
If you are using a recent version of Java (>=5), you will want to use generics with collection classes:

http://java.sun.com/docs/books/tutor...ons/index.html
Feb 8 '08 #5
Thank you both for your guidance.



V/R
Hammer
Feb 9 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: RAD | last post by:
I am working with an evaluation copy of SQL Server 2000 for the first time; my DB experience lies with MS Access. I have a simple table in SQL Server (tblCompany) that has a field called...
0
by: Ryan Stewart | last post by:
In some situations, I'd like to put some content within a box with a header. No real problem there. But what if I want the header to have something on the left hand side as well as on the right,...
245
by: Vortex Soft | last post by:
http://www.junglecreatures.com/ Try it and tell me what's happenning in the Microsoft Corporation. Notes: VB, C# are CLS compliant
18
by: Joel Hedlund | last post by:
Hi! The question of type checking/enforcing has bothered me for a while, and since this newsgroup has a wealth of competence subscribed to it, I figured this would be a great way of learning...
19
Frinavale
by: Frinavale | last post by:
Filtering user input is extremely important for web programming. If input is left unfiltered users can input malicious code that can cripple your website. This article will explain how to make...
0
by: Thamizh | last post by:
Hi All, I am writing an Axis web service in Java. It has been used by .Net client. 1. Is there any way to debug the web service when it requested by .net client? 2. Is there any way to...
173
by: Marty James | last post by:
Howdy, I was reflecting recently on malloc. Obviously, for tiny allocations like 20 bytes to strcpy a filename or something, there's no point putting in a check on the return value of malloc....
24
by: David | last post by:
Hi list. What strategies do you use to ensure correctness of new code? Specifically, if you've just written 100 new lines of Python code, then: 1) How do you test the new code? 2) How do...
4
by: David | last post by:
Hi list. Do test-driven development or behaviour-driven development advocate how to do higher-level testing than unit testing? types of testing: unit integration system
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.