Results 1 to 10 of 10
  1. #1
    DavidEvans is offline Member
    Join Date
    Mar 2010
    Posts
    5
    Rep Power
    0

    Default 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.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,000
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by DavidEvans View Post
    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;
    	}
    Shouldn't that be: team[i].getSales(); not team[0].getSales() ...

    kind regards,

    Jos

  3. #3
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

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

    Java Code:
    for (i = [B]0[/B]; i < team.length; i++);
    			sumOfSales = (sumOfSales + team[[B]i[/B]].getSales());

  4. #4
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    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:
    Java 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:
    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]
    ...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="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-
    Last edited by gcalvin; 04-21-2010 at 08:49 PM.

  5. #5
    DavidEvans is offline Member
    Join Date
    Mar 2010
    Posts
    5
    Rep Power
    0

    Default 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!)

    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;
     	}
    btw Is there a way to mark this particular post as solved in the forum?

  6. #6
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Quote Originally Posted by DavidEvans View Post
    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:
    Java Code:
    			team[i] = new SalesAssociate(name, sales);
    Quote Originally Posted by DavidEvans View Post
    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-
    Last edited by gcalvin; 04-21-2010 at 08:42 PM. Reason: Bad reading

  7. #7
    DavidEvans is offline Member
    Join Date
    Mar 2010
    Posts
    5
    Rep Power
    0

    Default

    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

  8. #8
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    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-

  9. #9
    DavidEvans is offline Member
    Join Date
    Mar 2010
    Posts
    5
    Rep Power
    0

    Default

    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).

  10. #10
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    You also have a superfluous instance variable:
    Java Code:
    	int size = 0;
    And I would think some more about your second constructor. Is there a reason you're not doing this?
    Java Code:
    	public SalesReport(SalesAssociate a[])
    	{
    		team = a;
    	}
    -Gary-

Similar Threads

  1. Problem With Dimension of Jasper Report
    By Prashant.surwade in forum Advanced Java
    Replies: 3
    Last Post: 09-19-2009, 02:29 PM
  2. Problem in running jasper report in servlet
    By khadaree in forum Java Servlet
    Replies: 1
    Last Post: 07-16-2009, 09:35 AM
  3. Problem with a report
    By sanduta in forum NetBeans
    Replies: 1
    Last Post: 06-26-2008, 02:14 PM
  4. Averages of user inputed values (Need Help)
    By Zebra in forum New To Java
    Replies: 2
    Last Post: 04-16-2008, 01:51 PM
  5. Replies: 4
    Last Post: 04-16-2008, 01:26 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •