-5

Hi been stuck on figuring out the math coding on how to find the average horsepower of a specific year? If anyone can help me code this that would be great. I can also provide more info on the ArrayList or Class if you like, just comment.

Here is the method requirements:

public double getAverageHorsepowerOfYear(int modelYear)

  • returns the average horsepower of all Lamborghini objects that match the modelYear specified as the parameter.
  • 0.0 is returned if no Lamborghini cars match the model year specified
  • the value returned by this method MUST be a decimal number (10 / 3 = 3.3333334, not 3).

Here is my method:

public double getAverageHorsepowerOfYear(int modelYear)
{
    double avgHP = 0.0;


    for(Lamborghini l : inventory){
        if(l.getModelYear() == modelYear){
            avgHP = avgHP/l.getHorsepower();
            avgHP++;
        } 
    }
    return avgHP;
}

I have a feeling that this is incorrect so if anyone can help me with this that would be greatly appreciated. Thanks in advance.

Tre
  • 37
  • 8
  • Please find a sample program where it produces the wrong result, if you think it's wrong. – djechlin Nov 25 '15 at 20:35
  • 4
    You _do_ know the formula to find the average, right? – tobias_k Nov 25 '15 at 20:35
  • @tobias_k no please enlighten me that is why I am asking the question and it states I am stuck with figuring out the math please read question in full. – Tre Nov 25 '15 at 20:40
  • 1
    google is your friend. this site is for programming help not basic math help – redFIVE Nov 25 '15 at 20:40
  • @djechlin it's the reason why I am asking the question to see if anyone can confirm if it right!!! please read question in full before commenting! – Tre Nov 25 '15 at 20:44
  • @Tre I did. Please read StackOverflow posting guidelines before posting :) – djechlin Nov 25 '15 at 20:46
  • @redFIVE I asked if anyone knew how to code the math for it – Tre Nov 25 '15 at 20:58
  • 1
    Did you even bother researching yourself? Clearly not. http://stackoverflow.com/questions/7008189/calculate-average-in-java – redFIVE Nov 25 '15 at 21:06
  • @redFIVE hey thanks I actually did research this for a long time but couldn't find anything this was my last resort and I got a answer below it was just a question don't know why everyone has got to police stackoverflow like chill. Some people that post on here are just beginners and the reason why they ask questions is to get help not to get trolled!!! So everyone just needs to chill out. Not everyone is a pro coder. Otherwise why would there be websites like these! – Tre Nov 25 '15 at 21:13
  • 2
    I literally googled "calculate average java" and the first results were questions already asked and answered on this very site. Are you seriously trying to tell me you researched this and couldn't come up with that? If you think this is trolling then the real world is going to chew you up and spit you out. – redFIVE Nov 25 '15 at 21:13
  • @redFIVE seriously nope! Nope but I will be certain to contact you first before I do a google search. Thanks again – Tre Nov 25 '15 at 21:17

3 Answers3

0

You calculation for the average is all wrong. You have to find the sum of the horsepower, then divide it by the total number of cars matching your criterion. What you do instead is... divide the running average by the new HP and then add one to it?

Looks like an assignments, so here's some pseudocode:

public double getAverageHorsepowerOfYear(int modelYear) {
    sum = 0.0
    count = 0
    for each car:
        if criterion matches:
            add HP to sum
            increment count
    return sum / count
}
tobias_k
  • 74,298
  • 11
  • 102
  • 155
0
  • public double getAverageHorsepowerOfYear(int modelYear){ Iterator it = inventory.iterator(); double totalHP = 0; int count = 0;

       while (it.hasNext()){
           Lamborghini hpList = it.next();
    
           if (hpList.getModelYear() == modelYear) {
               totalHP += hpList.getHorsepower();
              count++;
           }
    
       }
       return (totalHP/count);
    

    }

codetech
  • 1
  • 2
-1

You need to calculate total HP and divide that by number of cars.

public double getAverageHorsepowerOfYear(int modelYear)
{double TotalHP = 0.0;
double avgHP = 0.0;
int count = 0

for(Lamborghini l : inventory){
    if(l.getModelYear() == modelYear){
        TotalHP = TotalHP + l.getHorsepower();
        count++;
    } 
}
avgHP = TotalHP/count;
return avgHP;
}
Maertin
  • 384
  • 1
  • 8