CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Aug 2009
    Posts
    8

    Local Variables and If statments: N00b style

    Hi; first off, I'd like to mention that I'm quite new to Java, this being the third thing I've ever programmed! The program I'm writing is supposed to calculate someone's salary after tax and National Insurance has been deducted. I'm supposed to be using local variables in the methods for calculating the values for pay after NI and pay after tax, which is fine, except that when it comes to the third method where I calculate the final salary after both have been taken, I don't know how to use the values from the other methods in this third method.

    Also, I am trying to use an if statement to ask the user if they want more information concerning the amounts taken. If the user answers the question with Yes, then they are to be given more info. If they answer No, then the program should end. Instead of running, I get error messages saying ") expected", but I have no idea where :<

    Code:
    /**
     * put class description in here
     * @author 
     * @version 
     */
    public class Tax
    {
       
    float salary; 
    float taxAmount;
    float NIAmount;
    
    //float tax;
    
        /**
         * Constructor to be placed here
         * 
         * IMPORTANT NOTE when initialising floats in BlueJ you must put the letter 'f' at the end to indicate it 
         * is a float. no other primitive variables require such idintificaition, just floats.
         * for example
         *  TaxRate= 0.20f; 
         */
        
        Tax()
        {
          float taxAmount = 0.2f;
          float NIAmount = 0.15f;
          float salary = 0f;
        }
          
        
        /**
         * the main method to allow the program to be run from outside of BlueJ
         */ 
        public static void main(String [] args)
        {
            //This will create a new instance of the Tax class and run the constructor
           Tax taxSalary = new Tax();
             
          // call to the director class
           taxSalary.director(); 
        }
        
        /**
         * director mehtod here 
         * this method will be in charge of calling all the other methods within the program    
         */ 
        
        public void director()
        {
             getSalary();
             taxSalary();
             nationalInsurance();
             extraInfo();
             //salaryAfterBoth();        
        }
          
        
        
        /**
         * a method that will get the user input 
         * needs a name and of course salary will need to be declared somewhere.
         * 
         * the code inside is already written for you and shows how to use the Genio class to gain access to user input
         * an IMPORTANT thing to note here is that BlueJ requires you to have a print statement out to screen before it will 
         * accept any user input!!!!
         */ 
        
        public void getSalary()
        {
             //get the users salary
            System.out.print("Enter your salary  £");
            salary=Genio.getFloat();
        }
           
     
        public void taxSalary()
        {
            float salaryAfterTax;
            float amountTaxed;
            salaryAfterTax = salary - (salary*taxAmount);
            amountTaxed = salary - salaryAfterTax;
            System.out.println("Your salary after tax is £" + salaryAfterTax + ".");
        }
        
        public void nationalInsurance()
        {
            float salaryAfterNI;
            float amountForNI;
            salaryAfterNI = salary - (salary*NIAmount);
            amountForNI = salary - salaryAfterNI;
            System.out.println("Your salary after National Insurance is £" + salaryAfterNI + ".");
        }
        
        /*public void salaryAfterBoth()
        {
            float finalSalary;
            finalSalary = ((salary - amountTaxed) - amountForNI);
            System.out.println("Your final salary is £" + finalSalary + ".");
        }*/
        
        public void extraInfo()
        {
            String moreInfo;
            String yes = "Yes";
            String no = "No";
            System.out.println();
            System.out.println("Would you like to see how much money was taken in taxes and National Insurance?");
            moreInfo=Genio.getString();
            if(moreInfo=String yes){
                System.out.println("Amount taken in Taxes: £" + amountTaxed + ".");
                System.out.printnl("Amount taken for National Insurance: £" + amountForNI + ".");
            }
            else if(moreInfo="no"){
                break;
            }
        }
        
        /**
         * 
         * The above methods are all required, here are some suggestions of others that you may want to consider using in this program.
         *
         * a method that takes a single float parameter of salary
         * and returns a float value for the amount of tax taken for that salary
         * 
         * 
         * a method that takes a single float parameter of salary
         * and returns a float value for the amount of National Insurance taken for that salary
         * 
         * 
         * 
         * 
         */
       
    }
    I'm not sure if you guys need to see the code for the Genio class that I was given (not sure if it's something all Java programmers use or whatever), but I'll post it anyway. I was told not to touch this code or change, but just in case you need to see it to work out where I've gone wrong.

    Code:
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    
    /**
     * General purpose class to enable simple coding of programs requiring data input at 
     * a text based interface.
     * 
     * The class is under development, so where unexpected exceptions occur, please could
     * you report them to me at [email protected]
     * 
     * @author Peter Gregor
     * @version 1.2 16th September 2003
     */
    
        
    public class Genio 
    {
    
    
        /**
         * Constructor for objects of class genio, but nothing needing constructed!
         */
        public Genio()
        {
        }
    
         
        /** 
         * getStr()  is a private method which safely returns a string for use
         * by the public methods getString() and getCharacter() in the class.
         * 
         * @return String for further processing withing the class
         */
        
        private static String getStr() 
        {
            String inputLine = "";
            BufferedReader reader = 
                new BufferedReader(new InputStreamReader(System.in));
            try 
            {
                inputLine = reader.readLine();
            }
            
            catch(Exception exc) 
            {
                System.out.println ("There was an error during reading: "
                                    + exc.getMessage());
            }
            return inputLine;
        }
        
        /** 
         * getInteger() returns an integer value. Exception handling is used to trap
         * invalid data - including floating point numbers, non-numeric characters
         * and no data. In the event of an exception, the user is prompted to enter
         * the correct data in the correct format.
         * 
         * @return validated int value 
         */
        public static int getInteger()
        {
            int temp=0;
            boolean OK = false;
            
            BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
            do 
            {
                try
                {
                    temp = Integer.parseInt(keyboard.readLine());
                    OK = true;
                }
    
                catch (Exception eRef)
                {
                    if (eRef instanceof NumberFormatException) 
                    {
                        System.out.print("Integer value needed: ");
                    }
                    else
                    {
                        System.out.println("Please report this error: "+eRef.toString());
                    }
                }
              
            } while(OK == false);
            return(temp);
         }
         
        /** 
         * getFloat() returns a floating point value. Exception handling is used to trap
         * invalid data - including non-numeric characters and no data.
         * In the event of an exception (normally no data or alpha), the user is prompted to enter
         * data in the correct format
         * 
         * @return validated float value
         */        
        public static float getFloat()
        {
            float temp=0;
            boolean OK = false;
    
            BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
            do 
            {
                try
                {
                    temp = Float.parseFloat(keyboard.readLine());
                    OK = true;
                }
    
    
                catch (Exception eRef)
                {
                    if (eRef instanceof NumberFormatException) 
                    {
                        System.out.print("Number needed: ");
                    } 
                    else
                    {
                        System.out.println("Please report this error: "+eRef.toString());
                    }
                }
               
            } while(OK == false);
            
            return(temp);
         }
         
        /** 
         * getDouble() returns a double precision floating point value. 
         * Exception handling is used to trap invalid data - including non-numeric
         * characters and no data.
         * In the event of an exception, the user is prompted to enter
         * data in the correct format
         * 
         * @return validated double precision value
         */        
        public static double getDouble()
        {
            double temp=0;
            boolean OK = false;
            BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
            do 
            {
                try
                {
                    temp = Double.parseDouble(keyboard.readLine());
                    OK = true;
                }
    
                catch (Exception eRef)
                {
                    if (eRef instanceof NumberFormatException) 
                    {
                        System.out.print("Number needed: ");
                    }
                    else
                    {
                        System.out.println("Please report this error: "+eRef.toString());
                    }
                }
               
            } while(OK == false);
            
            return(temp);
         }
    
        /** 
         * getCharacter() returns a character from the keyboard. It does this by 
         * reading a string then taking the first character read. Subsequent characters
         * are discarded without raising an exception.
         * The method checks to ensure a character has been entered, and prompts 
         * if it has not.
         * 
         * @return validated character value
         */
            
         public static char getCharacter()
         {
             String tempStr="";
             char temp=' ';
             boolean OK = false;
             do 
             {
                 try
                 {
                     tempStr = getStr();
                     temp = tempStr.charAt(0);
                     OK = true;
                 }
    
                 catch (Exception eRef)
                 {
                     if (eRef instanceof StringIndexOutOfBoundsException)
                     {
                         // means nothing was entered so prompt ...
                         System.out.print("Enter a character: ");
                     }            
                     else 
                     {
                         System.out.println("Please report this error: "+eRef.toString());
                     }
                 }
               
             } while(OK == false);
            
             return(temp);
         }
         
         /** 
          * getString() returns a String entered at the keyboard.
          * @return String value
          */
         
         public static String getString()
         {
            String temp="";
            try
            {
                temp = getStr();
            }
            catch (Exception eRef)
            {
                System.out.println("Please report this error: "+eRef.toString());
            }
            return(temp);
         }
         
    }
    Thanks in advance for any help given!

  2. #2
    Join Date
    Feb 2008
    Posts
    966

    Re: Local Variables and If statments: N00b style

    You can have the taxSalary() and nationalInsurance() return values to your director method. These values will be local to the director method. You can then pass these values to the salaryAfterBoth() method: salaryAfterBoth(float taxsal,float nisal) , or whatever.

    As far as the Genio class, I think most people probably use the nifty Scanner class that came with Java 1.5

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured