We’ve gotten a homework assignment in Java, which relates to inheritance. I don’t have a problem with the programming in itself, but I’m a bit unsure about some of the math, and would like confirmation/corrections from someone a bit more knowledgable.
The assignment starts with a abstract class, GeometricObject, which is extended into three two-dimensional objects. A rectangle, a circle and a triangle. Those three are then extended into a cuboid for the rectangle, a cylinder and a sphere for the circle, and the triangle into a triangular prism.
Each of these three-dimensional objects are hollow, and has a defined thickness and is made of a special metal, so we are to calculate their weight. And herein lies the problem, since I’m a bit unsure as to how I find the ‘inner volume’ on some of them.
- Cuboid: Here I assume that I can just subtract 2 * thickness from the width, height and depth, and then everything looks fine.
- Cylinder: Subtract thickness from the radius making up the base, and 2*thickness from the height
- Sphere: Subtract thickness from the radius
- Prism: This is where I’m a bit stuck. Each object gets passed a baseline, height of the triangle, and the height for the entire prism. How can I use this in order to find the ‘inner prism’ representing the inner volume?
Update: Forgot to mention that when creating the object, we specify the outmost sizes, and the hollow part is inside of this. The other way around is not allowed.
Update again: The triangle is an isosceles triangle.
Update yet again: Mixed up radius and diameter for the circular. Corrected now.
I think you cannot get this result from the data you have (baseline length & triangle height). You have to get other information, like location of the points or the angles at the baseline.
Edit: since the triangle is isosceles:
As AnthonyWJones already pointed out, the inner triangle is similar to the outer triangle. Therefore, the only thing you need is find the ratio between the two.
sketch http://img76.imageshack.us/img76/4164/g2654.png
You can find it easily from the height. Since triangles CQP and ACS are similar
where
Then, you compute
h2and the ratior = (height - h1 - h2)/heightis the ratio between the two triangles. The area of the inner triangle is thenr^2 * area of the outer triangle.