I say urgent because I have to finish it pretty much tonight or maybe very early tomorrow morning. But I am moving along decently on the project, so it's not like nothing is getting done.

Basically I have a problem with a class I am in the process of completing, in which one method display(), needs to use the variables defined in another method calculateNetMonthly(). The problem is that calculateNetMonthly is not recognizing these variables. Which leads me to believe that I am somehow defining them wrong. I'm not sure how but I suspect it is obvious to those more experienced in java.

As you can see I tried a concatenation in the display() method. Doing so generates a compiler error which says cannot find symbol, basically the variable is invalid. Why?

Also any help in figuring out how to get a date from the user in getInput() would be useful. :)

The code is as follows

/*
 * FullTimeEmployee.java
 *
 * CSCE 155 Fall 2008
 * Assignment 1
 * @author
 * @version
 */

// import statements
import java.io.*;
import java.util.*;

/**
 * Used to calculate the pay of the full time employees
 * 
 * This class allows the application class to calculate the pay of the 
 * full time employees by gathering information, performing calculations
 * and displaying results.
 * <p>
 * 
 * @author
 * @version
 */

public class FullTimeEmployee {
    
    // -------------------------------------------------------------------------
    // You may add more data members to the following to describe a Casual 
	// Employee.  You may need to remove some data members as well.
    // -------------------------------------------------------------------------

	//------------------Declare Constants---------------------------------------
    
    /**
     * Represents the percentage of the federal tax
     */
    private final double FEDERAL_TAX_PERCENT = 0.15; 
    
    /**
     * Represents the percentage of the health insurance deduction
     */
    private final double HEALTH_INSURANCE_PERCENT = 0.05;   

    /**
     * Represents the percentage of the Retirement Savings deduction
     */
    private final double RETIREMENT_SAVINGS_PERCENT = 0.12;   

    /**
     * Represents the percentage of the state tax
     */
    private final double STATE_TAX_PERCENT = 0.08;   
    
    //-------------Declare private variables-----------------------------------
    
	 /**
     * Represents the name for the employee
     */
    private String 	employeeID;
    /**
     * Represents the name for the employee
     */
    private String 	employeeName; 	
    /**
     * Represents the amount in dollars of the salary for the employee
     */
    private int 	monthlySalary; 	
    /**
     * Represents the date of employment
     */
    private Date 	startDate; 		
        
    /**
     * Constructor used to create this object.  Responsible for setting
     * all of this object's information to their corresponding default values
     */
    public FullTimeEmployee(){
        this.employeeName = "";
        
    }//end of constructor
    
    
    //-------------------------------------------------------------------------
    //Please complete the following get and set methods
    //Some may need to be added or removed.
    //-------------------------------------------------------------------------

    /**
     * Returns the employee's name from this object 
     * @return <code>String</code> Name of the Employee represented in object
     */
	public String getEmployeeName() {
		return employeeName;
	}

    /**
     * Sets the employee name represented by this object
     * @param employeeName the name of the employee
     */
	public void setEmployeeName(String employeeName) {
		this.employeeName = employeeName;
	}

    //--------------------------------------------------------------------------
    //Please complete the code and docmentation for the following methods
    //--------------------------------------------------------------------------

    // --------------------------------------------------------------------------
    // In the following, we will just have some "simulated" methods since you do
    // do not have enough background to actually implement the following
    // behavior.  Our calculation is simply to printout a statement "performing"
    // an action.  You need to perform the actual calculations for this type
	// of employee.
    // Hint!
    // 1. Complete the getInput method.  You will need to prompt the user for 
	//    all of the information related to the employee.  Use the provided
	//    readInteger and readString methods within your code to do the actual
	//    reading or information from the keyboard.  Store all of the 
	//    information appropriately.
    // 2. Complete the calculateNetMonthly method.  You will need to take some
	//    of the information you have read from the getInput method and do the 
	//    appropriate calculations.  You may need to break down this method 
	//    into several seperate methods and/or determine how to store the 
	//    calculated information.
    // 3. Complete the display method.  You need to print out the information
	//    that was input or calculated as needed for this employee type.
    // --------------------------------------------------------------------------
    public void calculateNetMonthly(){
	 
        double deductionFederalTax;
		  double deductionStateTax;
		  double deductionRetirementSavings;
		  double deductionHealthInsurance;
		  double totalDeduction;
		  double netMonthlySalary;
		  
		  deductionFederalTax = (monthlySalary * FEDERAL_TAX_PERCENT);
		  deductionStateTax = (monthlySalary * STATE_TAX_PERCENT);
		  deductionRetirementSavings = (monthlySalary * RETIREMENT_SAVINGS_PERCENT);
		  deductionHealthInsurance = (monthlySalary * HEALTH_INSURANCE_PERCENT);
		  totalDeduction = (deductionFederalTax + deductionStateTax + deductionRetirementSavings + deductionHealthInsurance); 
		  netMonthlySalary = (monthlySalary - totalDeduction);
        
    }//end of calculateNetMonthly
    
    public void display(){
        System.out.println("Calculating Pay for Employee: " + employeeName +" employeeID: " + employeeID);
        System.out.println("Deduction for Federal Income Tax (15%): " + deductFederalTax);
		  System.out.println("Deduction for State Income Tax (8%): $");
		  System.out.println("Deduction for Retirement Savings (12%): $");
		  System.out.println("Deduction for Health Insurance (5%): $");
		  System.out.println("Total deductions from employee salary: $");
		  System.out.println("The net monthly salary of the employee is: $");
        
    }//end of display
    
    public void getInput(){
         System.out.println("Please enter the Employee ID:");
	 employeeID = readString();
	 System.out.println("Please enter the Employee Name:");
	 employeeName = readString();
	 System.out.println("Please enter the date of the employement period:");
	 
	 System.out.println("Please enter the gross salary of employee (in dollars):");
	 monthlySalary = readInteger();
       
    }//end of getInput
    
    
    /**
     * This method reads an integer and returns it to the caller of the method.
     * @return <code>int</code> integer read from keyboard
     */
    private int readInteger()  {
        
        int temp = 0;
        
        Scanner scanner = new Scanner(System.in);
        
        try {
            temp = scanner.nextInt();  // read integer
        }  catch (InputMismatchException ex) {
            System.out.println(ex);
        }
        
        return temp;
        
    }  // end readInteger
    
    /**
     * This method reads a string and returns it to the caller of the method.
     * @return <code>String</code> String read from keyboard
     */
    private String readString()  {
        
        String userInput = "";
        
        Scanner scanner = new Scanner(System.in);
        
        userInput = scanner.nextLine();
        
        return userInput;
        
    }  // end readString
    
}//end of class FullTimeEmployee

I assume this is a typo?

System.out.println("Deduction for Federal Income Tax (15%): " + deductFederalTax);

Should it be deductionFederalTax ? If it is, it's still not going to work because deductionFederalTax is a local variable in the calculateNetMonthly() function and therefore you can't use it in the display () function. Is deductionFederalTax perhaps supposed to be a member variable in the FullTimeEmployee class? If so, it would be accessible to all the functions and you would declare it not in the calculateNetMonthly() function, but rather with the other class variables at the top outside of any functions.

As far as asking for the date in getInput , note that the Date class is mostly deprecated so it looks like you're supposed to use Calendar and GregorianCalendar instead. I'm a little confused because they seem to have deprecated most of the methods, but I don't see where they tell you not to use the class itself.

http://java.sun.com/j2se/1.5.0/docs/api/java/util/Date.html
http://java.sun.com/j2se/1.5.0/docs/api/java/util/Calendar.html
http://java.sun.com/j2se/1.5.0/docs/api/java/util/GregorianCalendar.html

Regardless, ask the user for the month, day, and year, then set the Date/Calendar using what the user types in using the set functions. That's how I'd do it. You already have your readString and readInt functions, so use them, then use the set commands from Calendar .

First you are referencing a non-existent variable in display(). You've declared double deductionFederalTax; in the calculateNetMonthly() yet you are using deductFederalTax in the display() method.
However so, the scope of deductionFederalTax variable is only valid in the calculateNetMonthly() method and cannot be reffered to in the display() method. So, declare deductionFederalTax as a class variable.Also, i propose you do so for the variables in the calculateNetMonthly() method since it seems you will need to reference them in the display() method in the subsequent unconcatenated statements.

The code for deductionFederalTax is as follows:

import java.io.*;
import java.util.*;

    /**
     * Used to calculate the pay of the full time employees
     * 
     * This class allows the application class to calculate the pay of the 
     * full time employees by gathering information, performing calculations
     * and displaying results.
     * <p>
     * 
     * @author
     * @version
     */

    public class FullTimeEmployee {

        // -------------------------------------------------------------------------
        // You may add more data members to the following to describe a Casual 
        // Employee.  You may need to remove some data members as well.
        // -------------------------------------------------------------------------

        //------------------Declare Constants---------------------------------------

        /**
         * Represents the percentage of the federal tax
         */
        private final double FEDERAL_TAX_PERCENT = 0.15; 

        /**
         * Represents the percentage of the health insurance deduction
         */
        private final double HEALTH_INSURANCE_PERCENT = 0.05;   

        /**
         * Represents the percentage of the Retirement Savings deduction
         */
        private final double RETIREMENT_SAVINGS_PERCENT = 0.12;   

        /**
         * Represents the percentage of the state tax
         */
        private final double STATE_TAX_PERCENT = 0.08;   

        //-------------Declare private variables-----------------------------------

         /**
         * Represents the name for the employee
         */
        private String  employeeID;
        /**
         * Represents the name for the employee
         */
        private String  employeeName;   
        /**
         * Represents the amount in dollars of the salary for the employee
         */
        private int     monthlySalary;  
        /**
         * Represents the date of employment
         */
        private Date    startDate;  

       private double deductionFederalTax;

        /**
         * Constructor used to create this object.  Responsible for setting
         * all of this object's information to their corresponding default values
         */
        public FullTimeEmployee(){
            this.employeeName = "";

        }//end of constructor


        //-------------------------------------------------------------------------
        //Please complete the following get and set methods
        //Some may need to be added or removed.
        //-------------------------------------------------------------------------

        /**
         * Returns the employee's name from this object 
         * @return <code>String</code> Name of the Employee represented in object
         */
        public String getEmployeeName() {
            return employeeName;
        }

        /**
         * Sets the employee name represented by this object
         * @param employeeName the name of the employee
         */
        public void setEmployeeName(String employeeName) {
            this.employeeName = employeeName;
        }

        //--------------------------------------------------------------------------
        //Please complete the code and docmentation for the following methods
        //--------------------------------------------------------------------------

        // --------------------------------------------------------------------------
        // In the following, we will just have some "simulated" methods since you do
        // do not have enough background to actually implement the following
        // behavior.  Our calculation is simply to printout a statement "performing"
        // an action.  You need to perform the actual calculations for this type
        // of employee.
        // Hint!
        // 1. Complete the getInput method.  You will need to prompt the user for 
        //    all of the information related to the employee.  Use the provided
        //    readInteger and readString methods within your code to do the actual
        //    reading or information from the keyboard.  Store all of the 
        //    information appropriately.
        // 2. Complete the calculateNetMonthly method.  You will need to take some
        //    of the information you have read from the getInput method and do the 
        //    appropriate calculations.  You may need to break down this method 
        //    into several seperate methods and/or determine how to store the 
        //    calculated information.
        // 3. Complete the display method.  You need to print out the information
        //    that was input or calculated as needed for this employee type.
        // --------------------------------------------------------------------------
        public void calculateNetMonthly(){

              //double deductionFederalTax;
              double deductionStateTax;
              double deductionRetirementSavings;
              double deductionHealthInsurance;
              double totalDeduction;
              double netMonthlySalary;

              deductionFederalTax = (monthlySalary * FEDERAL_TAX_PERCENT);
              deductionStateTax = (monthlySalary * STATE_TAX_PERCENT);
              deductionRetirementSavings = (monthlySalary * RETIREMENT_SAVINGS_PERCENT);
              deductionHealthInsurance = (monthlySalary * HEALTH_INSURANCE_PERCENT);
              totalDeduction = (deductionFederalTax + deductionStateTax + deductionRetirementSavings + deductionHealthInsurance); 
              netMonthlySalary = (monthlySalary - totalDeduction);

        }//end of calculateNetMonthly

        public void display(){
            System.out.println("Calculating Pay for Employee: " + employeeName +" employeeID: " + employeeID);
            System.out.println("Deduction for Federal Income Tax (15%): " + deductionFederalTax);
            System.out.println("Deduction for State Income Tax (8%): $");
            System.out.println("Deduction for Retirement Savings (12%): $");
            System.out.println("Deduction for Health Insurance (5%): $");
            System.out.println("Total deductions from employee salary: $");
            System.out.println("The net monthly salary of the employee is: $");

        }//end of display

        public void getInput(){
             System.out.println("Please enter the Employee ID:");
         employeeID = readString();
         System.out.println("Please enter the Employee Name:");
         employeeName = readString();
         System.out.println("Please enter the date of the employement period:");

         System.out.println("Please enter the gross salary of employee (in dollars):");
         monthlySalary = readInteger();

        }//end of getInput


        /**
         * This method reads an integer and returns it to the caller of the method.
         * @return <code>int</code> integer read from keyboard
         */
        private int readInteger()  {

            int temp = 0;

            Scanner scanner = new Scanner(System.in);

            try {
                temp = scanner.nextInt();  // read integer
            }  catch (InputMismatchException ex) {
                System.out.println(ex);
            }

            return temp;

        }  // end readInteger

        /**
         * This method reads a string and returns it to the caller of the method.
         * @return <code>String</code> String read from keyboard
         */
        private String readString()  {

            String userInput = "";

            Scanner scanner = new Scanner(System.in);

            userInput = scanner.nextLine();

            return userInput;

        }  // end readString

    //}//end of class FullTimeEmployee


    public static void main(String[] args) {
        FullTimeEmployee ft=new FullTimeEmployee();
        ft.getInput();
        ft.calculateNetMonthly();
        ft.display();

    }


}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

This article has been dead for over six months. Start a new discussion instead.