Help with some methods !! Please..
Hi !!
My University teacher gave me an exercize where he askes to calculate the minimum,medium and maximum cubic capacities in an ArrayList<Cars> carList .
I didn't find problems to make a new method for searching the minum and maximum cubic capacities,but I could not think a way to make the method for the Average cubic capacity value.
For the min and max searches I wrote down :
public Car searchForMinCubicCapacityValue()
{
Car car= carList.get(0);
for(Car aCar : carList)
{
if(aCar.getCubicCapacity() <= car.getCubicCapacity())
minimumCar = aCar; (where minimumCar is an Instance Variable)
}
return minimumCar;
}
The same is for the method searchForMaxCubicCapacityValue() but I put >= indeed and I have used the Instance Variable maximumCar.
How could I write down the method searchForAverageCubicCapacityValue() ?
I really need your help please !...
I have to know for next week test ! In this test I'll have to write down a class with these same methods and others !
Thanks in advance !
Those are strange method names. But going along with your theory that "search for" should be implemented as "calculate"... Do you know how to calculate an average? If you had a bunch of numbers, how would you calculate the average of those numbers? Forget about Java for now, how would you do it with pencil and paper?
Do you know how to calculate an average by hand? Describe that in very small, simple, detailed steps. Then translate those steps to Java.
jverda at 2007-7-29 17:51:22 >

Sorry ! I'll change the name of my 3 methods !
I would calculate by hand an average just adding up the cubic capacities and then dividing the result of this addition by the total number of cars.
But I have tried to write down this one in the method for an Average cubic capacity but I get the same result of the maximum cubic capacity.But I know that there is an average value in my ArrayList !
I think this thing happens because the result of the division is a number that doesn't exist in my ArrayList...but maybe my thought is wrong...
Sorry !
You think to try adding a method that goes through your array adds the numbers up and divides by how many times you added? just a thought
Well I don' t think that this thought is good and for this reason I asked you to help me in this topic courteously !
> Sorry ! I'll change the name of my 3 methods !
> I would calculate by hand an average just adding up
> the cubic capacities and then dividing the result of
> this addition by the total number of cars.
Right.
> But I have tried to write down this one in the method
> for an Average cubic capacity but I get the same
> result of the maximum cubic capacity.
Well, then the Java you wrote doesn't do what you described above. If you show the code, someone can help you find your error.
When you post code, please use[code] and [/code] tags as described in Formatting tips on the message entry page. (http://forum.java.sun.com/help.jspa?sec=formatting) It makes it much easier to read.
> But I know that
> there is an average value in my ArrayList !
Of course there is. Any collection of values has an average.
> I think this thing happens because the result of the
> division is a number that doesn't exist in my
> ArrayList.
The average may very well not be a numbre in the list. If the list contains only 0 and 100, the average is 50, which is not in the list.
jverda at 2007-7-29 17:51:23 >

> Well I don' t think that this thought is good and for
> this reason I asked you to help me in this topic
> courteously !
What? ok it's not very efficent but it should work just turn your whole arraylist into an array by using the toArray() cmd then do a for loop go though it and add the values together and then divide by the total using .size()
> just turn your whole arraylist into an array by using
> the toArray()
No, that would be pointless.
jverda at 2007-7-29 17:51:23 >

> No, that would be pointless.
ya i kinda noticed that... but i wasn't sure if you can add values in an arraylist
For the method calculateAverageCubicCapacity() I wrote down :
public Car calculateAverageCubicCapacity()
{
Car car = carList.get(0);
averageCar = car;
double sum = 0;
for(Car aCar : carList)
sum = sum + aCar.getCubicCapacity();
// sum contains the total capacity of all cars.
for(Car aCar : carList)
{
if(aCar.getCubicCapacity() <= (sum / carList.size()));
averageCar = aCar;
}
return averageCar ;
}
Well I didn't think to turn the ArrayList into an array.
But I would want to try with the ArrayList indeed !
> > No, that would be pointless.
>
> ya i kinda noticed that... but i wasn't sure if you
> can add values in an arraylist
You don't do anything "in" the list. Whether it's a list or an array, you find the average the same way--you iterate over the values, keeping a running total as you go, and then divide by the number of elements.
jverda at 2007-7-29 17:51:23 >

>for(Car aCar : carList)
> {
> if(aCar.getCubicCapacity() <= (sum /
> um / carList.size()));
>averageCar = aCar;
>
>}
What's that supposed to be for?
First, it's pointless. Second, because of the semi-colon after the if, the averageCar = aCar line will *always* execute.
jverda at 2007-7-29 17:51:23 >

and if your returning a value its prob going to be a double so why set averageCar = car?
Ok !
Thanks to all you :D !!
You have cleared up my mind about this method !