Results 1 to 5 of 5

Thread: Please critique

  1. #1
    jim01 is offline Member
    Join Date
    Sep 2010
    Location
    Southwest Missouri
    Posts
    97
    Rep Power
    0

    Default Please critique

    This is my very first attempt at writing a java program. It works, but I would appreciate it if those of you with experience could critique my code. I am only on chapter 2 of Liang, so I can only do so much at the moment. Any advice would be appreciated.

    /* PayrollStatement.java
    * Jim01
    * 9/22/2010
    * This program creates a payroll statement using dialog boxes
    */

    import javax.swing.JOptionPane;

    public class PayrollStatement {

    public static void main(String [] args) {

    //Enter Employee's Name string variable
    String enterEmployeeName = JOptionPane.showInputDialog (
    "Enter Employee Name (e.g., Smith)");

    //Enter the number of hours worked in a week string variable
    String enterHoursWorkedWeekString = JOptionPane.showInputDialog (
    "Enter number of hours worked in a week (e.g., 10)");

    //Convert String to double
    double enterHoursWorkedWeek =
    Double.parseDouble(enterHoursWorkedWeekString);

    //Enter Hourly Pay Rate string variable
    String enterHourlyPayRateString = JOptionPane.showInputDialog (
    "Enter Hourly Pay Rate (e.g., 6.75)");

    //Convert String to double
    double enterHourlyPayRate =
    Double.parseDouble(enterHourlyPayRateString);

    //Enter Federal Tax Withholding Rate string variable
    String enterFederalTaxWithholdingString = JOptionPane.showInputDialog (
    "Enter Federal Tax Withholding Rate (e.g., 20 for 20%)");

    //Convert String to double
    double enterFederalTaxWithholding =
    Double.parseDouble(enterFederalTaxWithholdingStrin g);

    //Enter State Tax withholding Rate string variable
    String enterStateTaxWithholdingString = JOptionPane.showInputDialog (
    "Enter State Tax Withholding Rate (e.g., 9 for 9%)");

    //Convert String to double
    double enterStateTaxWithholding =
    Double.parseDouble(enterStateTaxWithholdingString) ;

    //Declare Gross Pay variable
    double GrossPay = enterHoursWorkedWeek * enterHourlyPayRate;

    //Declare Federal Withholding Deductions amount variable
    double FederalWithholdingDeductions = (enterFederalTaxWithholding /
    100) * GrossPay;

    //Declare State Withholding deductions amount variable
    double StateWithholdingDeductions = (enterStateTaxWithholding / 100) *
    GrossPay;

    //Declare Total Deductions variable
    double TotalDeductions = FederalWithholdingDeductions +
    StateWithholdingDeductions;

    //Declare Net Pay variable
    double NetPay = GrossPay - TotalDeductions;

    //Display Results
    String output = "Employee Name" + enterEmployeeName;
    JOptionPane.showMessageDialog(null,"Employee Name: " +
    enterEmployeeName
    + "\nNumber of Hours Worked: " + enterHoursWorkedWeekString
    + "\nHourly Pay Rate: $ " + enterHourlyPayRate + "\nFederal Tax
    Rate: "
    + (enterFederalTaxWithholding / 100) + "\nState Tax Rate: "
    + (enterStateTaxWithholding / 100) + "\nGross Pay: $ " + GrossPay
    + "\nDeductions: " + "\n Federal Withholding: "
    + FederalWithholdingDeductions + "\n State Withholding: "
    + StateWithholdingDeductions + "\n Total Deductions: "
    + TotalDeductions + "\nNetPay: " + NetPay);

    }

    }

  2. #2
    crikey is offline Senior Member
    Join Date
    Jul 2010
    Posts
    125
    Rep Power
    0

    Default

    This is great start :)
    Adding lots of comments on what you are processing is an excellent idea, especially when you get further into your book, as more complex code requires extra logic. So my hat down to you :). Have you learned about methods yet? If not, getting to them will help shorten code loads, so you don't have to repeat something over and over. Nonetheless, this looks great! Keep it up!

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default

    When you post code in the future, please put it in code tags. It makes it more readable.
    Info here: Java Forums - BB Code List

  4. #4
    jim01 is offline Member
    Join Date
    Sep 2010
    Location
    Southwest Missouri
    Posts
    97
    Rep Power
    0

    Default

    Quote Originally Posted by crikey View Post
    This is great start :)
    Adding lots of comments on what you are processing is an excellent idea, especially when you get further into your book, as more complex code requires extra logic. So my hat down to you :). Have you learned about methods yet? If not, getting to them will help shorten code loads, so you don't have to repeat something over and over. Nonetheless, this looks great! Keep it up!
    No we haven't went as far as methods yet. I know main and that's it. Thank you for your input. I thought I might be way off base in how I presented it. My professor wants us to separate everything into either variables, input, processing, or output, but when I look at his examples, it's mass confusion and doesn't look anything like what's in the book or other written programs I've seen. I tried it his way and ended up getting lost. Doing it one step at a time with comments was much easier for me. Now that it's written I guess I'll go back and cut and paste everything into the format he wants.

    Once again, thank you for your input. Sorry about the formatting issue. I was unaware of the code tags.

  5. #5
    jim01 is offline Member
    Join Date
    Sep 2010
    Location
    Southwest Missouri
    Posts
    97
    Rep Power
    0

Similar Threads

  1. Critique Java Game: Help Me Improve
    By gretty in forum New To Java
    Replies: 1
    Last Post: 07-15-2010, 04:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •