# Problem with Sums and Averages in Sales Report

• 04-21-2010, 07:23 PM
DavidEvans
Problem with Sums and Averages in Sales Report
Hi people,

I'm having a problem with a Computer Science assignment that should be really simple, except I can't seem to figure it out.

Our assignment is to write a program to generate sales reports for a company's employees which works by the user inputting the names and sales figures for a set number of employees into an array and then outputs the names and sales figure for each employee together with a statement whether each individual employees sales are above or below the average (of all the sales made / number of employees). I hope that's clear! :)

I have the program working perfectly up to the point where the average sales are calculated and I believe that the problem lies in the method I am using to determine the total of all the sales values (when I make the program output this value it is always incorrect). I have pasted the portion of the code that is malfunctioning below in the hope that someone out there can let me know what I'm doing wrong. I have then included the code for the entire class separately incase the problem goes deeper.

David

Average calculation (Note: the values 'avgSales' and 'sumOfSales' are declared at the beginning of the code (see entire code below))

Code:

``` public double avgSale()         {                 //Average sale method                 int i;                                 for (i = 1; i < team.length; i++);                         sumOfSales = (sumOfSales + team[0].getSales());                                   avgSales = (sumOfSales / team.length);                                 return avgSales;         }```

Entire Code

Code:

``` import java.util.Scanner; public class SalesReport {         private double largestSale;         private double avgSales;         private double sumOfSales;         private SalesAssociate team[];                 int size = 0;                 public SalesReport(int size)         {                 team = new SalesAssociate[size];         }         public SalesReport(SalesAssociate a[])         {                 team = new SalesAssociate[a.length];                         for (int i = 0; i < team.length; i++)                   team[i] = a[i];         }         public void nameAndSales()         {                                 Scanner get = new Scanner(System.in);                 int i;                                 for (i = 0; i < team.length; i++)                           {                                                 System.out.printf("please enter %d th associate name: \n", i+1);                                   String name = get.next();                           System.out.printf("please enter %d th associate sale \n", i+1);                                 double sales = get.nextDouble();                                                 team[i] = new SalesAssociate(name, sales);                         }         }           public double largestSale()         {                 int i;                 //double largestSale;                                         largestSale = team[0].getSales();                                 for (i = 1; i < team.length; i++);                           if (largestSale < team[1].getSales())                                   largestSale = team[1].getSales();                                 return largestSale;         }         public double avgSale()         {                 //Average sale method                 int i;                                 for (i = 1; i < team.length; i++);                         sumOfSales = (sumOfSales + team[0].getSales());                                   avgSales = (sumOfSales / team.length);                                 return avgSales;         }         public void Display()         {                 System.out.printf("Largest Sales is: %s \n", largestSale);                 System.out.printf("Sum of all Sales is: %s \n", sumOfSales);                 System.out.printf("Average Sales per associate is: %s \n", avgSales);                                                 for (int i = 0; i < team.length; i++)                 {                           System.out.printf("Name: %s \n", team[i].getName());                           System.out.printf("Sales: %s \n", team[i].getSales());                                 if (team[i].getSales() > avgSales)                                         System.out.println("performs better than average");                                 else                                         System.out.println("performs worse than average");                 }         } }```
• 04-21-2010, 07:34 PM
JosAH
Quote:

Originally Posted by DavidEvans
Code:

``` public double avgSale()         {                 //Average sale method                 int i;                                 for (i = 1; i < team.length; i++);                         sumOfSales = (sumOfSales + team[0].getSales());                                   avgSales = (sumOfSales / team.length);                                 return avgSales;         }```

Shouldn't that be: team[i].getSales(); not team[0].getSales() ...

kind regards,

Jos
• 04-21-2010, 07:34 PM
iluxa
Code:

```for (i = 1; i < team.length; i++);                         sumOfSales = (sumOfSales + team[0].getSales());```
I bet what you wanted is

Code:

```for (i = [B]0[/B]; i < team.length; i++);                         sumOfSales = (sumOfSales + team[[B]i[/B]].getSales());```
• 04-21-2010, 08:26 PM
gcalvin
You have the same issue in your largestSale() method as well.

This may be premature optimization, but wouldn't it make more sense to update sumOfSales and largestSale in your entry loop? Also, your nameAndSales() method is wrong. You need to be filling your team[] array, right? That means each time through your entry loop you need to create a new SalesAssociate object, add it to your array, and assign values to its name and sales properties. Otherwise your Display() method (bad name for that method, btw) has nothing to do.
EDIT: I missed a line in the source. See my post below.

You have some other problems in your code, but I'll let you find them, or at least ask other questions. Thank you for using CODE tags and for formatting your code well. The formatting should actually help you find some of your other problems. Here's a hint:
Code:

```        public SalesReport(SalesAssociate a[])         {                 team = new SalesAssociate[a.length];                         for (int i = 0; i < team.length; i++)                   team[i] = a[i];         }```
This should be:
Code:

```[COLOR="Blue"]        public SalesReport(SalesAssociate a[])         {                 team = new SalesAssociate[a.length];                 for (int i = 0; i < team.length; i++)                           team[i] = a[i];         } [/COLOR]```
...or even better...
Code:

```[COLOR="Blue"]        public SalesReport(SalesAssociate a[])         {                 team = new SalesAssociate[a.length];                 for (int i = 0; i < team.length; i++) {                           team[i] = a[i];                 }         } [/COLOR]```
If you don't make it a habit to use that style, then someday you will do something like this:
Code:

```[COLOR="Red"]        public SalesReport(SalesAssociate a[])         {                 team = new SalesAssociate[a.length];                 for (int i = 0; i < team.length; i++)                           team[i] = a[i];                           totalSales += team[i].sales;         } [/COLOR]```
...and drive yourself crazy wondering why it doesn't work.

-Gary-
• 04-21-2010, 08:35 PM
DavidEvans
Problem Solved!
Hey again,

I think I solved it by making the following changes to the 'largestSale' and 'averageSale' methods (the rest of the code stays pretty much the same, but thanks for your advice Gary!)

Code:

```         public double largestSale() // Largest sale method         {                         largestSale = team[0].getSales();                         for (int i=1;i<team.length;i++)                                 largestSale = team[i].getSales();                         return largestSale;         }         public double avgSale() // Average sale method         {                                 for(int i = 0; i < team.length; i++)                         avgSales += team[i].getSales()/team.length;                 return avgSales;         }```
btw Is there a way to mark this particular post as solved in the forum?
• 04-21-2010, 08:39 PM
gcalvin
Quote:

Originally Posted by DavidEvans
Hey again,

I think I solved it by making the following changes to the 'largestSale' and 'averageSale' methods (the rest of the code stays pretty much the same, but thanks for your advice Gary!)

Do you mean to say that your nameAndSales() method is working as is? I don't believe it.

EDIT: My bad. I missed this line:
Code:

`                        team[i] = new SalesAssociate(name, sales);`
Quote:

Originally Posted by DavidEvans
btw Is there a way to mark this particular post as solved in the forum?

Check out the Thread Tools link toward the top of the page.

-Gary-
• 04-21-2010, 08:44 PM
DavidEvans
Quote:

Do you mean to say that your nameAndSales() method is working as is? I don't believe it.
Happy to say they are, though I'm not going to lie by saying I understand exactly how or why.

If you would like I can post the complete code (there is another class and the main class so what you've seen here isn't the complete program), and you can try it out.

David
• 04-21-2010, 08:47 PM
gcalvin
You still should fix your indentation before turning in your assignment. I'd mark off some points for improper indentation if I were grading it. :-)

-Gary-
• 04-21-2010, 08:52 PM
DavidEvans
lol, thanks! I am in the process of tidying it up and putting comments in. Pasting it into the message posts here doesn't seem to help the formatting (I noticed a few extra tabs appearing where there weren't any in the actual code, strangely).
• 04-21-2010, 08:57 PM
gcalvin
You also have a superfluous instance variable:
Code:

`        int size = 0;`
And I would think some more about your second constructor. Is there a reason you're not doing this?
Code:

```        public SalesReport(SalesAssociate a[])         {                 team = a;         }```
-Gary-