Java Area Calculation

/edit, I didn't read you question fully so I don't think this is what you were actually asking.

In your example we need to do the following:
(10*10) + (12*12) + (14*14) + (16*16) + (18*18)

We need to loop through our sums B times. Each time through the loop A increases by size C.

Code:
SUM = 0
REPEAT B TIMES
{
  SUM = SUM + (A * A) //add our current square to the total
  A = A + C // increase our square length by the increment
}
RETURN SUM
 
Last edited:
Something along these lines?
Code:
public static double calculateArea(double startLength, int squareCount, double increment)
{
	double totalArea = 0;
	double length = startLength;
	for (int i = 0; i < squareCount; i++, length += increment)
	{
		totalArea += length * length;
	}
	
	return totalArea;
}
 
Last edited:
Welshy said:
Give him the answer why dont you...
Well I was going to edit it and put a more thorough explanation of how it'd be done in but realised the post above had done that.

It didn't seem like a "homework question", so I saw no need to cover up the actual answer :confused:
 
Inquisitor said:
Something along these lines?
Or even:

Code:
public static double calculateArea(double start, double step, int N)
{
     return N*(start*(start+step*(N-1))+step*step*(N-1)*(2*N-1)/6);
}
(not a terribly serious suggestion, but it would be interesting to see the OP try to explain how it works...)
 
Inquisitor said:
It didn't seem like a "homework question", so I saw no need to cover up the actual answer :confused:
It could still be a part of an assignment or something, I always think pseudo code is far more appropriate if people are only asking for pointers.

Sorry if I sounded harsh... :p
 
Inquisitor said:
Something along these lines?
Code:
public static double calculateArea(double startLength, int squareCount, double increment)
{
	double totalArea = 0;
	double length = startLength;
	for (int i = 0; i < squareCount; i++, length += increment)
	{
		totalArea += length * length;
	}
	
	return totalArea;
}

Thanks, I had thought that it would be some sort of loop.

I have been having a go with that one, cant quite get it to work though. the return totalarea part is showing errors in the compiler at the moment. But I will keep trying.

And for the record its not coursework, its an example I have been given to practise on before getting the proper assignment in about 3 weeks time.

This work isnt being marked or assessed its simply practise for me :p (and my god do I need it!!)
 
Nazbit said:
I have been having a go with that one, cant quite get it to work though. the return totalarea part is showing errors in the compiler at the moment. But I will keep trying.
Works fine for me when I copy the method into Eclipse :p
 
Back
Top Bottom