Back | Reverse |

Programming II problem
Link | by Artie on 2009-10-01 14:59:21 (edited 2009-10-01 15:00:29)
I'm in programming II and I've recently been given an assignment where I have to draw a square and a right triangle; which I did; and calculate their area and perimeter; this is where I am having some trouble. Any help would be very great full.

If someone has the solution to this problem, you can post it here or email me at: ajantonin@gmail.com

Re: Programming II problem
Link | by gendou on 2009-10-01 22:03:13
Do you know how to calculate the area and perimeter of those two shapes?

int area_of_a_square(int length) { return length * length; }
int perimeter_of_a_square(int length) { return length * 4; }
int area_of_a_triangle(int length, int height) { return length * height / 2; }
int perimeter_of_a_triangle(int length, int height) { return length + height + sqrt(length * length + height * height); }


Re: Programming II problem
Link | by Artie on 2009-10-02 06:58:15
Would I have to create one class for finding the area/perimeter of a square, and one for that of a triangle?

Re: Programming II problem
Link | by gendou on 2009-10-02 07:09:03 (edited 2009-10-02 07:09:27)
You can code it any way you want.
The code above was meant to answer what I felt was your central question: how to calculate the area/perimeter of those shapes.
If you're not sure how to structure your code, forget structure, until you notice you've got to change it.
Or, if your teacher has given you instructions as to code structure, follow them.
An object-oriented approach to your problem might look like this:

virtual class Shape: virtual void draw(); virtual int area(); virtual int perimeter();
class Square implements Shape: Square(int length);
class Triangle implements Shape: Triangle(int length, int width);


Re: Programming II problem
Link | by Artie on 2009-10-03 09:52:42
Thanks for your help Gendou, I got most of it working now! I just have to fix some variables in a separate class for the triangle. You were a great help! :D

Re: Programming II problem
Link | by gendou on 2009-10-03 10:19:06
Glad I could help.


Back | Reverse |

Copyright 2000-2024 Gendou | Terms of Use | Page loaded in 0.0024 seconds at 2024-12-30 11:11:35