By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,654 Members | 1,517 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,654 IT Pros & Developers. It's quick & easy.

Help with getting correct output

P: 6
Have a program that is to take a persons name, hours worked, rate of pay, fed and state taxes, and combine them to get a net pay. However, most of the calculations will be done in a separate class file. main file looks like
Expand|Select|Wrap|Line Numbers
  1.  Scanner scanner = new Scanner(System.in);
  2.  
  3.       boolean end = false; // is the input name stop?
  4.             while (end == false) // as long as end is false, proceed
  5.                 {      
  6.                     System.out.print("Enter employee name: ");
  7.                     String name = scanner.nextLine();
  8.  
  9.                       if (name.contentEquals("stop")) 
  10.                     {
  11.                        System.exit(0);
  12.  
  13.                     }
  14.  
  15.         System.out.print("Enter number of hours worked in a week: ");
  16.         double hours = Double.parseDouble(scanner.nextLine());
  17.  
  18.             while (hours < 0) //if a negative number will loop next command
  19.                 {
  20.         System.out.print("Enter a positive number of hours worked:"); // prompt
  21.         hours = Double.parseDouble(scanner.nextLine());
  22.                             }
  23.         System.out.print("Enter hourly pay rate (Do not include dollar sign): ");
  24.         double rate = Double.parseDouble(scanner.nextLine());
  25.  
  26.             while (rate < 0) //if a negative number will loop next command
  27.                 {
  28.                 System.out.print ("Enter a positive hourly rate of pay:");
  29.                     rate = Double.parseDouble(scanner.nextLine());
  30.                             }
  31.  
  32.             /* tax rates should be entered as a decimal point i.e. 20% = .20 */
  33.  
  34.         System.out.print("Enter federal tax withholding rate: ");
  35.             double fedtax = Double.parseDouble(scanner.nextLine());
  36.  
  37.                 System.out.print("Enter state tax withholding rate: ");
  38.                     double statetax = Double.parseDouble(scanner.nextLine());
  39.  
  40.  
  41.     //make object
  42.             Employee emp = new Employee (name,hours,rate);
  43.  
  44.             System.out.printf( "The employee %s" , emp.getName());
  45.             System.out.printf( "'s weekly pay is $%.2f\n", emp.getNetPay());
  46.  
  47.  
  48.         }
  49.     }
  50. }
  51.  
the class code looks like
Expand|Select|Wrap|Line Numbers
  1. public class Employee {
  2.  
  3.     //fields
  4.     String name;
  5.     double rate;
  6.     double hours;
  7.     double gross;
  8.     double fedtax;
  9.     double statetax;
  10.     double deduction;
  11.     double netpay;
  12.  
  13.     // constructor
  14.  
  15.     public Employee(String name, double rate, double hours) {
  16.         this.name = name;
  17.         this.rate = rate;
  18.         this.hours = hours;
  19.  
  20.  
  21.     }
  22.  
  23.         //returns net pay
  24.         public double getNetPay() {
  25.             return gross - deduction;
  26.                     }
  27.  
  28.             public String getName () {
  29.             return name;
  30.         }
  31.  
  32.         public void setName (String name) {
  33.             this.name = name;
  34.         }
  35.  
  36.         public double getHours() {
  37.             return hours;
  38.         }
  39.  
  40.         public void setHours(double hours) {
  41.         this.hours = hours;
  42.     }
  43.  
  44.     public double getRate() {
  45.         return rate;
  46.     }
  47.  
  48.     public void setRate(double rate) {
  49.         this.rate = rate;
  50.     }
  51.  
  52.          public double getGross() {
  53.             return hours*rate;
  54.         }
  55.  
  56.         public void setGross(double gross) {
  57.             this.gross = gross;
  58.         }
  59.  
  60.         public double getFedtax() {
  61.             return fedtax*gross;
  62.         }
  63.  
  64.         public void setFedtax(double fedtax){
  65.             this.fedtax = fedtax;
  66.         }
  67.  
  68.         public double getStatetax() {
  69.             return statetax*gross;
  70.         }
  71.         public void setStatetax(double statetax) {
  72.             this.statetax = statetax;
  73.         }
  74.  
  75.         public double getDeduction() {
  76.             return statetax+fedtax;
  77.         }
  78.  
  79.         public void setDeduction (double deduction) {
  80.             this.deduction = deduction;
  81.                     }
  82.  
  83.  
  84.         }
  85.  
Problem is I keep getting a o.oo for net pay when I should be getting like 296.00.
Sep 27 '12 #1

✓ answered by Rabbit

It's set in the class in the sense that when you want to set the variable, you call a function to set the value and that function resides in the class. The problem is you never call the function.

Basically, the functionality is there for you to set it, you just never use it.

Share this Question
Share on Google+
3 Replies


Rabbit
Expert Mod 10K+
P: 12,421
The problem is that after you instantiate your employee object, you never set your other variables.
Sep 27 '12 #2

P: 6
aren't the other variables set within the employee class? or do they need to be set within the first line of the make object? I've tried to read through java's descriptions, but they didn'r realy help much.
Sep 27 '12 #3

Rabbit
Expert Mod 10K+
P: 12,421
It's set in the class in the sense that when you want to set the variable, you call a function to set the value and that function resides in the class. The problem is you never call the function.

Basically, the functionality is there for you to set it, you just never use it.
Sep 27 '12 #4

Post your reply

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