Help with getting correct output

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rwgwiccan
    New Member
    • Sep 2012
    • 6

    Help with getting correct output

    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
    Code:
     Scanner scanner = new Scanner(System.in);
    
          boolean end = false; // is the input name stop?
                while (end == false) // as long as end is false, proceed
                    {      
                        System.out.print("Enter employee name: ");
                        String name = scanner.nextLine();
    
                          if (name.contentEquals("stop")) 
                        {
                           System.exit(0);
    
                        }
    
            System.out.print("Enter number of hours worked in a week: ");
            double hours = Double.parseDouble(scanner.nextLine());
            
                while (hours < 0) //if a negative number will loop next command
                    {
            System.out.print("Enter a positive number of hours worked:"); // prompt
            hours = Double.parseDouble(scanner.nextLine());
                                }
            System.out.print("Enter hourly pay rate (Do not include dollar sign): ");
            double rate = Double.parseDouble(scanner.nextLine());
            
                while (rate < 0) //if a negative number will loop next command
                    {
                    System.out.print ("Enter a positive hourly rate of pay:");
                        rate = Double.parseDouble(scanner.nextLine());
                                }
                
                /* tax rates should be entered as a decimal point i.e. 20% = .20 */
            
            System.out.print("Enter federal tax withholding rate: ");
                double fedtax = Double.parseDouble(scanner.nextLine());
            
                    System.out.print("Enter state tax withholding rate: ");
                        double statetax = Double.parseDouble(scanner.nextLine());
          
         
        //make object
                Employee emp = new Employee (name,hours,rate);
                
                System.out.printf( "The employee %s" , emp.getName());
                System.out.printf( "'s weekly pay is $%.2f\n", emp.getNetPay());
                
              
            }
        }
    }
    the class code looks like
    Code:
    public class Employee {
        
        //fields
        String name;
        double rate;
        double hours;
        double gross;
        double fedtax;
        double statetax;
        double deduction;
        double netpay;
       
    	// constructor
    	
        public Employee(String name, double rate, double hours) {
    		this.name = name;
    		this.rate = rate;
    		this.hours = hours;
                   
                   
    	}
            
            //returns net pay
            public double getNetPay() {
                return gross - deduction;
                        }
            
                public String getName () {
                return name;
            }
            
            public void setName (String name) {
                this.name = name;
            }
            
            public double getHours() {
                return hours;
            }
            
            public void setHours(double hours) {
    		this.hours = hours;
    	}
    
    	public double getRate() {
    		return rate;
    	}
    
    	public void setRate(double rate) {
    		this.rate = rate;
    	}
            
             public double getGross() {
                return hours*rate;
            }
            
            public void setGross(double gross) {
                this.gross = gross;
            }
            
            public double getFedtax() {
                return fedtax*gross;
            }
            
            public void setFedtax(double fedtax){
                this.fedtax = fedtax;
            }
            
            public double getStatetax() {
                return statetax*gross;
            }
            public void setStatetax(double statetax) {
                this.statetax = statetax;
            }
            
            public double getDeduction() {
                return statetax+fedtax;
            }
            
            public void setDeduction (double deduction) {
                this.deduction = deduction;
                        }
                  
           
            }
    Problem is I keep getting a o.oo for net pay when I should be getting like 296.00.
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    The problem is that after you instantiate your employee object, you never set your other variables.

    Comment

    • rwgwiccan
      New Member
      • Sep 2012
      • 6

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

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

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

        Comment

        Working...