Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Trouble Calling Method

  1. #1
    Junior Member
    Join Date
    Dec 2017
    Posts
    18
    Thanks
    13
    Thanked 1 Time in 1 Post

    Default Trouble Calling Method

    Hey guys, thank you for the help with previous posts. Right now I am attempting to call a method getComm and getAnnComp but am slightly confused with the variable I should place before the method call. Here is the method, I highlighted my issue with all caps

     Scanner input=new Scanner(System.in);
            System.out.print("\nTotal Commission and Annual Sales Calculator\nThis "
                    + "program will calculate Total Compensation based"
                    + " upon\nthe percentage met of the $120,000 Annual Sales Target."
                    + "\nThis program will also output a table of possible Total"
                    + " Compensations\nin increments of $5,000 up to 1.5 times your inputted Annual Sales\n\n"
                    + "Please enter your Total Annual Sales: ");
     
     
             //Check for Correct Entry Data
             try { 
                BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 
                String line = in.readLine().trim(); 
     
                //Check for Null Entry
                   if (line == null || line.length() == 0) { 
                      System.out.println("You did not enter a value"); 
                      return; 
                } 
     
                   //Declare Annual Sales and Replace Commas/Dollar Signs
                    annualSales = Double.parseDouble(line.replaceAll(",", "").replaceAll("\\$", ""));
     
                //Check for numerical value
            }   catch (NumberFormatException exception) { 
                     System.out.println("Error: Not a valid sales amount."); 
                     return; 
     
     
                //Check for IOException
            }   catch (IOException exception) { 
                     System.out.println("Error: " + exception.getLocalizedMessage()); 
                     return; 
            } 
     
             //Declare Sales
            spsales=new Salesperson(annualSales);
     
            //Output Total Commission
            System.out.println("\nYour Total Commission for the year is: $" +
                    spsales.getComm()); //HERE IS MY PROBLEM
     
            //Output Total Compensation
            System.out.println("Your Total Compensation for the year is: $"+
                    String.format("%.2f", spsales.getAnnComp())+ "+\n");  //HERE IS ALSO MY PROBLEM
     
    ...etc assume code closes correctly

    the corresponding document is

    package salesperson_part1;
     
    public class Salesperson {
     
       //Variable Definition
         //Declares and initalizes fixed salary.
        private final double Fix_Sal = 50000;
        //Declares and initalizes commission.
        private final double Comm = 7.5;
        //Declares and initalizes acceleration factor.
        private final double Accel_Factor = 1.25;
        //Declares and initializes sales target.
        double target = 120000;         
        //Declares and initializes sales incentive threshold.
        double thresh = .80;
        String spName;    //holds the salesperson's name
        double annSales;   // Holds value for annual sales
        double commRate;  //holds calculated commission rate.  
        double commEarned;  //holds calculated commission earned.   
        double totalAnnComp; //Holds calculated total annual commission
     
        //Default Constructor  
     
        //Default Constructor
        public Salesperson()
        {
            spName = "Unknown";
            annSales = 0.0;
        }
     
        ////parameterized constructor
        public Salesperson(String name, double sales)
        {
            spName = name;
            annSales = sales;
        }
     
        Salesperson(double annualSales) {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }
     
        //The setName method will set the name of salesperson
        public void setName(String name)
        {
            spName = name;
        }
     
        //The getName method will rueturn the name of salesperson
        public String getName()
        {
            return spName;
        }
     
        //The setSales method will set the annual sales
        public void setSales(double sales)
        {
            annSales = sales;
        }
     
        //The getSales method returns the value stored in annualSales
        public double getSales()
        {
            return annSales;
        }
     
        //The getComm method will calculate and return commission earned
        public double getComm()
        {
        //Check if sale are greater than or equal to 80% of target. 
        if (annSales >= (target * thresh))
        {
                if (annSales > target) //Checks if annual sales exceed target.
                {   
                //Gets commission rate.
                    commRate = (Comm * Accel_Factor)/100;
            commEarned = commRate * annSales;
                }
                else
                {
            commRate = Comm/100;
            commEarned = commRate * annSales;
                }
        }
        else
            {
            commRate = 0;
            commEarned = 0;
        }
        return commEarned;
        }
     
        /*
         * The getAnnComp method will calculate and return the total 
         * annual compensation.
         */ 
        public double getAnnComp ()
        {
        totalAnnComp = Fix_Sal + commEarned;
        return totalAnnComp;
        }
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Trouble Calling Method

    confused with the variable I should place before the method call.
    Can you explain what is confusing?
    What do you mean by "before the method call"?
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    Deployment (January 9th, 2018)

  4. #3
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Trouble Calling Method

    Do you get any runtime exception? If so, what is it?

  5. The Following User Says Thank You to pbrockway2 For This Useful Post:

    Deployment (January 9th, 2018)

  6. #4
    Junior Member
    Join Date
    Dec 2017
    Posts
    18
    Thanks
    13
    Thanked 1 Time in 1 Post

    Default Re: Trouble Calling Method

    I decided to use a different method figuratively and literally as I could not figure out why spsales could not be passed to GetComm(). With that being said I used

    //Output Total Commission
            System.out.println("\nYour Total Commission for the year is: $" +
                    Salesperson.CommEarned);

    and was able to collect the variable efficiently

    Thanks again!

    - Deployment

  7. #5
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Trouble Calling Method

    I'm glad you've got it sorted out.

    ---

    The other thread is at double-cannot-dereferenced

    ---

    Going back to your original question (top of this thread) you had

    spsales=new Salesperson(annualSales);
     
    //Output Total Commission
    System.out.println("\nYour Total Commission for the year is: $" +
        spsales.getComm()); //HERE IS MY PROBLEM
     
    //Output Total Compensation
    System.out.println("Your Total Compensation for the year is: $"+
        String.format("%.2f", spsales.getAnnComp())+ "+\n");  //HERE IS ALSO MY PROBLEM

    Note the line "spsales=new Salesperson(annualSales);" because in the other class you define that as

    Salesperson(double annualSales) {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    So I would have thought saying "new SalesPerson()" would throw an exception and give the error message. That's why I was asking about runtime exceptions.

  8. The Following User Says Thank You to pbrockway2 For This Useful Post:

    Deployment (January 9th, 2018)

  9. #6
    Junior Member
    Join Date
    Dec 2017
    Posts
    18
    Thanks
    13
    Thanked 1 Time in 1 Post

    Default Re: Trouble Calling Method

    Quote Originally Posted by pbrockway2 View Post
    I'm glad you've got it sorted out.

    ---

    The other thread is at double-cannot-dereferenced

    ---

    Going back to your original question (top of this thread) you had

    spsales=new Salesperson(annualSales);
     
    //Output Total Commission
    System.out.println("\nYour Total Commission for the year is: $" +
        spsales.getComm()); //HERE IS MY PROBLEM
     
    //Output Total Compensation
    System.out.println("Your Total Compensation for the year is: $"+
        String.format("%.2f", spsales.getAnnComp())+ "+\n");  //HERE IS ALSO MY PROBLEM

    Note the line "spsales=new Salesperson(annualSales);" because in the other class you define that as

    Salesperson(double annualSales) {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    So I would have thought saying "new SalesPerson()" would throw an exception and give the error message. That's why I was asking about runtime exceptions.
    Ah, I understand. I edited that out and corrected after posting. Thanks again for your help with everything.

    As some background, Im in a 5 week course in which this program took an evolution of complexity each week. Some things are learned as we work, and it means a lot to have a source for help when needed. I look back at my questions and code from Week 2 and laugh at it's messy format. I'd love to share with you the final program and its evolution if you want to give it a look, I'm proud even though it may seem very basic to you haha! I won't post it here as someone may copy and paste it when Googling for help, but if you want to shoot me a PM, skype, or discord I'd be willing to send it!

    Thanks again, and this concludes the programming for this class. Java is done for my major however I do plan on continuing to expand knowledge in the language

    - Deployment

Similar Threads

  1. Method not calling?
    By NTWolf1220 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 20th, 2013, 12:04 PM
  2. calling this method
    By antnas in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 2nd, 2012, 01:32 PM
  3. Calling another method on my code
    By Caliichick in forum Java Theory & Questions
    Replies: 3
    Last Post: June 12th, 2012, 05:01 PM
  4. Calling method cant get the parameters right
    By frozen java in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 22nd, 2011, 02:23 PM
  5. [SOLVED] method calling
    By javapenguin in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 4th, 2010, 01:43 AM