6 Replies - 208 Views - Last Post: 09 March 2013 - 02:49 AM Rate Topic: -----

#1 DragonWood  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 05-March 13

Calling Methods - What am I doing wrong?

Posted 08 March 2013 - 06:33 PM

Hello,

My name is Dragon, I am a student of Java and yes, technically this was an assignment. However, I am not turning this one in, I have re-written another one for the assignment. However, I am sure the reason this one didn't work was something simple, I would like to know what it was so I can do better next time.

The idea was to make an array of sales people, assign an amount of sales to each and then to compare two of them and tell the person with fewer sales how many they needed to reach the other person.

What I wanted to do was to put in a random number generator with minimum and maximum limits that would simply assign a number at random to each person you asked for, then use that value to do everything else. It doesn't matter if the sales person you ask for is actually in the list, the main point is the math and to be able to use methods.

I can get the random number generator to work, I can get it to send that value to the commissionCalculator method to determine if they get a raise in commission. I can send that value to the comparisonCalculator to determine who has more sales and what the difference is.

I can't get it to call that number and display the results on the screen.

I appreciate any help I can get.


import java.util.Arrays;            //Imports the Arrays class
import java.util.Scanner;           //Imports the Scanner class
import java.text.NumberFormat;      //Imports the NumberFormat class to format to currency

public class DragonWoodPRG420Week4
{

    static Scanner sc = new Scanner(System.in);
    
                
    public static void main(String[] args)
    {
         String totalSalesStr;           //Declares the string variable for the sales input
         String firstPerson;             //Sets the variable for user input
         String secondPerson;            //Sets the variable for user input
         
         NumberFormat annualTotal = NumberFormat.getCurrencyInstance();
         
         double firstPersonSales = commissionCalculator();
         double secondPersonSales = commissionCalculator();
         
         double totalSalesDifference = comparisonCalculator();
         
        System.out.println("The possible sales people are: \n");
        
        String[] salesPeople = {"Jack Dickerhoff", "Joseph Montana", "Magan Sander-Gaines", "Sean Wiles", "Dragon Wood"};
        for (String salesPerson : salesPeople)
        {
            System.out.println(salesPerson);
        }
       
        System.out.println("\nPlease enter the name of the first sales person: ");
        firstPerson = sc.nextLine();
        System.out.println("\nThe total annual sales for this person is: " + annualTotal.format(firstPersonSales) +".");
        
        System.out.println("\nPlease enter the name of the second sales person: ");
        secondPerson = sc.nextLine();
        System.out.println("\nThe total annual sales for this person is: " + annualTotal.format(secondPersonSales) +".");
        
    }
    
    public static double commissionCalculator()
    {
        double salesTarget = 156000.00;    //Sets the sales target to $156,000.00
        double salesThresh = 0.7;          //Sets the sales threshold to 70%
        double accelFactor = 1.5;          //Sets the acceleration factor to 1.5
        double fixedSalary = 50000.00;      //Sets annaul salary amount
        double commissionRate = 0.12;       //Sets the commission rate to 12%
        double totalPay;                    //Declares the variable for the total pay outputs
        double totalCommission;             //Declares the variable for the commission outputs
        double minSales = 100000;      //Sets the minimum number of sales
        double maxSales = 200000;      //Sets the maximum number of sales
        double totalSales = getTotalSales(minSales, maxSales);
                       
        if (totalSales >= (salesTarget * salesThresh))
        {
            if (totalSales > salesTarget)
            {
            totalCommission = ((commissionRate * totalSales) * accelFactor);
            totalPay = totalCommission + fixedSalary;
            return totalPay;
            }
            else
            {
            totalCommission = commissionRate * totalSales;
            totalPay = totalCommission + fixedSalary;
            return totalPay;
            }
        }
        else
        {
            totalPay = fixedSalary;
            return totalPay;
        }
    }
    
    public static double getTotalSales(double minSales, double maxSales)
    {
        return (double)(Math.random() * (maxSales - minSales + 1)) + minSales;
    }
    
    public static double comparisonCalculator(double firstPersonSales, double secondPersonSales)
    {
        double salesFirst = firstPersonSales;
        double salesSecond = secondPersonSales;
        double salesDifference;
        
        if (salesFirst != salesSecond)
        {
            if (salesFirst > salesSecond)
            {
                salesDifference = salesFirst - salesSecond;
                return salesDifference;
            }
            else
            {
                salesDifference = salesSecond - salesFirst;
                return salesDifference;
            }
        }
        else
        {
            salesDifference = 0;
            return salesDifference;
        }
    }
    
}




Thanks,

Dragon Wood

Is This A Good Question/Topic? 0
  • +

Replies To: Calling Methods - What am I doing wrong?

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10186
  • View blog
  • Posts: 37,604
  • Joined: 27-December 08

Re: Calling Methods - What am I doing wrong?

Posted 08 March 2013 - 06:58 PM

Quote

I can't get it to call that number and display the results on the screen.

Your code won't compile. You aren't passing anything to comparisonCalculator().
Was This Post Helpful? 0
  • +
  • -

#3 DragonWood  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 05-March 13

Re: Calling Methods - What am I doing wrong?

Posted 08 March 2013 - 08:00 PM

Thanks for the reply, but I'm still confused.

I thought I was sending the getTotalSales() random number to the commissionCalculator() method.

Then that is supposed to be sending the results to the firstPersonSales and secondPersonSales variables so the results can be displayed before they are comparred.

Those in turn are supposed to be used by the comparisonCalculator() method to determine which is higher.

Then that is supposed to be sent to the totalSalesDifference variable so it can be displayed.

But I'm getting an error on that variable that says the method cannot be applied to the variable because it's the wrong type, but it says it needs to be a double, and that is what it is.

It looks to me like everything is sending and receiving fine, except the return from the comparisonCalculator() method.
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10186
  • View blog
  • Posts: 37,604
  • Joined: 27-December 08

Re: Calling Methods - What am I doing wrong?

Posted 08 March 2013 - 08:07 PM

Look here: double totalSalesDifference = comparisonCalculator();. You define comparisonCalculator() to accept two doubles: public static double comparisonCalculator(double firstPersonSales, double secondPersonSales), yet you invoke it and don't pass it anything. You need to invoke comparisonCalculator(firstPersonSales, secondPersonSales).
Was This Post Helpful? 0
  • +
  • -

#5 DragonWood  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 05-March 13

Re: Calling Methods - What am I doing wrong?

Posted 08 March 2013 - 08:47 PM

I don't know how I missed that. I thought it would be something simple though.

Why didn't I have to do that with the variables from the commissionCalculator() method though?
Was This Post Helpful? 0
  • +
  • -

#6 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2194
  • View blog
  • Posts: 5,222
  • Joined: 10-September 10

Re: Calling Methods - What am I doing wrong?

Posted 09 March 2013 - 02:11 AM

View PostDragonWood, on 08 March 2013 - 10:47 PM, said:

Why didn't I have to do that with the variables from the commissionCalculator() method though?

The method commissionCalculator() doesn't accept any parameters, and the variables it needs/uses are all local. Typically that method would accept many of those parameters so that the value calculated by the method is NOT specific to a single case.
Was This Post Helpful? 0
  • +
  • -

#7 DragonWood  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 05-March 13

Re: Calling Methods - What am I doing wrong?

Posted 09 March 2013 - 02:49 AM

Ok, that makes sense.

Thanks.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1