Results 1 to 10 of 10
- 04-21-2010, 07:23 PM #1
Member
- Join Date
- Mar 2010
- Posts
- 5
- Rep Power
- 0
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.
Thanks in advance for any help you can provide!!
David
Average calculation (Note: the values 'avgSales' and 'sumOfSales' are declared at the beginning of the code (see entire code below))
Java 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
Java 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"); } } }Last edited by DavidEvans; 04-21-2010 at 07:27 PM.
- 04-21-2010, 07:34 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
- 04-21-2010, 07:34 PM #3
Senior Member
- Join Date
- Mar 2010
- Posts
- 266
- Rep Power
- 4
I bet what you wanted isJava Code:for (i = 1; i < team.length; i++); sumOfSales = (sumOfSales + team[0].getSales());
Java Code:for (i = [B]0[/B]; i < team.length; i++); sumOfSales = (sumOfSales + team[[B]i[/B]].getSales());
- 04-21-2010, 08:26 PM #4
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
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:
This should be:Java Code:public SalesReport(SalesAssociate a[]) { team = new SalesAssociate[a.length]; for (int i = 0; i < team.length; i++) team[i] = a[i]; }
...or even better...Java 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:Java 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]
...and drive yourself crazy wondering why it doesn't work.Java 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]
-Gary-Last edited by gcalvin; 04-21-2010 at 08:49 PM.
- 04-21-2010, 08:35 PM #5
Member
- Join Date
- Mar 2010
- Posts
- 5
- Rep Power
- 0
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!)
btw Is there a way to mark this particular post as solved in the forum?Java 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; }
- 04-21-2010, 08:39 PM #6
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
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:
Check out the Thread Tools link toward the top of the page.Java Code:team[i] = new SalesAssociate(name, sales);
-Gary-Last edited by gcalvin; 04-21-2010 at 08:42 PM. Reason: Bad reading
- 04-21-2010, 08:44 PM #7
Member
- Join Date
- Mar 2010
- Posts
- 5
- Rep Power
- 0
Happy to say they are, though I'm not going to lie by saying I understand exactly how or why.Do you mean to say that your nameAndSales() method is working as is? I don't believe it.
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 #8
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
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 #9
Member
- Join Date
- Mar 2010
- Posts
- 5
- Rep Power
- 0
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 #10
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
Similar Threads
-
Problem With Dimension of Jasper Report
By Prashant.surwade in forum Advanced JavaReplies: 3Last Post: 09-19-2009, 02:29 PM -
Problem in running jasper report in servlet
By khadaree in forum Java ServletReplies: 1Last Post: 07-16-2009, 09:35 AM -
Problem with a report
By sanduta in forum NetBeansReplies: 1Last Post: 06-26-2008, 02:14 PM -
Averages of user inputed values (Need Help)
By Zebra in forum New To JavaReplies: 2Last Post: 04-16-2008, 01:51 PM -
[SOLVED] Integers (averages and remainders)...need help
By Zebra in forum New To JavaReplies: 4Last Post: 04-16-2008, 01:26 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks