Hello, I have no experience experience with Java programming and I spend more time lost than anything. I have made an attempt at my week's assignment but I am stuck. Here is the run down of my assignment for that week "Write a Java application using an Integrated Development Environment (IDE) (or directly from the command line compiler) that calculates the total annual compensation of a salesperson. Consider the following factors:
•A salesperson will earn a fixed salary of $50,000.
•A salesperson will also receive a commission as a sales incentive. Commission is a percentage of the salesperson's annual sales. The current commission is 15% of total sales.
•The total annual compensation is the fixed salary plus the commission earned"

I have also included a zip file of my coding

Here is what I am trying to do in the main class SalesPerson at the bottom where there it says
public double computeCompensation(double salesAmount)
I need to compute the total annual conpensation, that should have a return type, and shouls return the results of a double and the formula I am suppose to use is
baseSalary + commissionRate x salesAmount
I an I have been playing with code to get it to work and I can't figure it out. Can someone please help me with this.

Hi
Yes, people here will help, but not many will bother to download a zip file from an unknown source!

Post your code here (use the code button above the editor) and people will read it.

Im sure u get tons of suggestions to look here or there. But i have to say this guy taught me a great deal. Go to 'newthinktank.com', click on 'videos' in the blue menubar at the top. You'll get a dropdown. Choose 'Java Video Tutorial'. I think it would be well worth your time. I think he has like 90 videos covering multiple aspects of Java. He's my goto guy. His name is Derek Banas. Check him out.

Doug

I had a quick scan over some of the content referenced by NebulaM57. Can't say I was very impressed
Eg "static means that only a class can call for this function to execute" I can guess what he intended, but what he wrote is nonsense.
Eg "java.lang.RuntimeException : exceptions that can be thrown during the normal operation of the Java Virtual Machine. These exceptions are your responsibility as a programmer ... java.lang.Exception : exceptions that are checked for by the java compiler" - completely wrong.

I got the impression that the author covers a vast range of subjects, but inevitably cannot be expert in all of them.

Comments
yet another 'java-guru' exposed :)

Your assignment is a trivial one. Salary ($50K) + (sales * .15) == total compensation. Java is not difficult. Read some manuals/books/tutorials, all available online...

The following code snippet may help you iff you are given the annual salesAmount that you have to apply the 15% on.

There's no need for you to create more then 1 .java files for this problem unless if this task is about using multiple java classes.

public class AnnualCommisionedSalaryComputer
{
    public static void main(String[] args)
    {
        int persornFixedMonthlySalaryAmount = 50000;

        int personAnnualSalesAmount = 100000;

        int personCommisionPercentage = 15;

        // 15% of sales amount.
        double commissionAmount =
            computeCommissionAmount(personAnnualSalesAmount, personCommisionPercentage);

        // annual salary of person
        double personAnnualSalary =
            computeAnnualSalaryAmount(persornFixedMonthlySalaryAmount, commissionAmount);

        System.out.println("user annual salary is : " + personAnnualSalary);
    }

    public static double computeCommissionAmount(double salesAmount, double personCommisionPercentage)
    {
            double commissionAmount = salesAmount * (personCommisionPercentage / 100);
        return commissionAmount;
    }

    public static double computeAnnualSalaryAmount(double monthlySalary, double commisionAmount)
    {
            double anualSalaryNoCommision = monthlySalary * 12;
            double anualSalaryWithCommision = anualSalaryNoCommision + commisionAmount;
        return anualSalaryWithCommision;
    }
}

Edited 7 Months Ago by Nkokhelo

Yes, people here will help, but not many will bother to download a zip file from an unknown source!

James is correct. There are potential pitfalls in downloading a zip file, unzipping it, then compiling and running it. Someone tried that on me one time with a C++ project. The code was fine, but they had a custom Makefile that built the project, but also had a few commands buried in there that attempted to wreak some havoc on my computer. We've all been trained not to mindlessly run .exe files and patch files and jar files and any other executables, but we forget that the IDE will build things for us and the way they build things for us is to run programs like g++ and javac. Stick a few things in a "data" file that are actually command arguments and the unwary person who builds the project can have a really bad day.
That said, I unzipped the file and grabbed the two .java programs and nothing else. Here they are:

// SalesPerson.java

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package salesperson;

/**
 *
 * @author Jenda
 */
public class SalesPerson {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        int salary;
        salary = 50000;

        System.out.print(" A salesperson's annual income is ");
        System.out.print(salary);
        System.out.println(" a year. ");

        commission commissionObject = new commission();
        commission.simplemessag();
    }


    private double baseSalary;
    private double commissioRate;

    public SalesPerson(double commissionRate)
    {
        baseSalary = 50000;
        commissionRate = 0.15;
    }

    /**
     *
     * @param salesAmount
     * @return
     */
    public double computeCompensation(double salesAmount){

        Scanner input = new Scanner (System.in);

        SalesPerson newSalesPerson = new SalesPerson(50000);
    }



// Commission.java  

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package salesperson;

/**
 *
 * @author Jenda
 */
public class Commission {
    public void simplemessage(){
        System.out.println("A salesperson's will also receiva commission as a sales incentive. Commission is a percentage of the salesperson's annual salary.");
    }
}
/**
 * 
 * @param arg
 */
public static void main(String[] arg){

}

OK, where to start? You mention that you are new to Java and are lost. I don't know where you are in your studies, but Java is an Object-Oriented language. In the beginning, some teachers delve right into having you get Java programs to compile and run and they do that by giving you procedural programs that are basically C++ programs without using classes. You learn how to do that, then you start thinking in terms of objects and overall design. In other words, your early programs look nothing like something a Java programmer would write.

I mention this because I don't want to accidentally point out the overall design flaws and just get you even more confused because you haven't gotten that far in your coursework. There are different approaches to learning/teaching. My personal opinion is that you should learn about objects and design right off the bat. Not everyone agrees. Again, hopefully all this doesn't confuse you even more. With all that said, my comments...

You have a SalesPerson class. It contains the fields commissionRate and baseSalary. Both are private. So far, so good. I would expect a constructor in SalesPerson that would take those two values as parameters and assign the fields to be those values.

    public SalesPerson(double baseSalary, double commissionRate)
    {
        this.baseSalary = baseSalary;
        this.commissionRate = commissionRate;
    }

Note the keyword this. I have two doubles passed to this constructor with the same name as my SalesPerson class fields. I need to differentiate these two constructor parameters from the class fields. I do that with the keyword this. Alternatively, I could name my two parameters differently so that they are not the same as my two class fields. In that case, there is no need for the keyword this. Both approaches are fine. See below.

    public SalesPerson(double baseSal, double comRate)
    {
        baseSalary = baseSal;
        commissionRate = comRate;
    }

Now let's look at YOUR constructor

public SalesPerson(double commissionRate)
{
    baseSalary = 50000;
    commissionRate = 0.15;
}

You have ONE parameter, not two. You are assigning baseSalary to equal 50000. Hard-coded. That could be fine if you had more than one constructor, but you only have this one. If baseSalary is always 50000, why bother to make it a "variable"? Second, you are passed a parameter with commisionRate. It is spelled the same as your field, but there is no keyword this, so how are we to differentiate between the parameter and the field. You are passed a parameter called commissionRate and you immediately change its value to 0.15, so why bother passing the parameter. The function above might make sense as a default constructor (you would remove the function parameter), but that's clearly not what you are intending, so I won't explain further. What you need to do is change your constructor to mine (I suggest using the one WITHOUT the keyword this since my guess is that you have not gotten that far in your studies. So you'll have ONE constructor in SalesPerson, as follows:

    public SalesPerson(double baseSal, double comRate)
    {
        baseSalary = baseSal;
        commissionRate = comRate;
    }

Now go through your SalesPerson.java function and look at everywhere you have hardcoded 50000 or 0.15. Replace with baseSalary and commissionRate. The whole point of Java and having a Salesperson object is that you can change the specific values of 50000 and 0.15 with other values. That means they should not be hardcoded.

Now look at this function in SalesPerson:

    public double computeCompensation(double salesAmount){

        Scanner input = new Scanner (System.in);

        SalesPerson newSalesPerson = new SalesPerson(50000);
    }

The function specification:

public double computeCompensation(double salesAmount)

is fine. The function body is not. One, you are creating a Scanner object. That's fine, but there's no reason it should be inside the SalesPerson class. Relocate it into another class. You might as well stick it somewhere in the Commission class. Perhaps in the main function? While we're on the subject of main functions, you have two main functions in two classes. You only need one. I can't see any reason to have one in SalesPerson, especially since you have one in Commission. Delete the main function in SalesPerson.

Now for the line:SalesPerson newSalesPerson = new SalesPerson(50000); Since the constructor was changed, you need to make the line match the constructor. Try this:

SalesPerson newSalesPerson = new SalesPerson(50000, 0.15);

So that line is good, but why is it in the Salesperson member function of computeCompensation? I see no reason for it to be there. Stick it in the main function in the Commission or somewhere else.

The actual computeCompesnsation function is really small.

double computeCompensation(double salesAmount)
{
    return baseSalary + commissionRate * salesAmount;
}

The function CALL will be something like this and it will go after you create your SalesPerson object.

double salesAmount = 80000; // or read the value from the Scanner.
double compensation = newSalesPerson.computeCompensation(salesAmount);

Nkokhelo,

Your function specification:

public static double computeAnnualSalaryAmount(double monthlySalary, double commisionAmount)

does not match the OP's requirements:

public double computeCompensation(double salesAmount)

Generally you are given certain specifications by the professor that you are not allowed to change, not even a little bit. Assuming that is accurate and looking at the OP's original code, this function cannot be a static function. Your program is a procedural program, not an Object Oriented program. As I mentioned in my last post, we don't know the professor's approach, but it appears he is trying to transition to an Object oriented approach. Otherwise the professor would not have given the OP this function specification, no constructors would have been used, etc.

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