programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Calculating area and perimeter with menu?

Riki Maru
Greenhorn
Posts: 5
Make a Program that can calculate the area and perimeter based on the choice selected.

Example:
1.Square
2.Triangle
3.Circle
Select: 1

1.Area
2.Perimeter
Select: 1

Input side : 5
Result : 25

This is the question. i dont really understand the question but this has to be submitted by today night . If anyone can help me that would be great

Campbell Ritchie
Marshal
Posts: 56546
172
Start with a square class. Give it a constructor and getArea() and getPerimeter() methods. Also a toString method.
Don't use Math#pow to calculate the area.Consider circles and triangles later

Mike. J. Thompson
Bartender
Posts: 689
17
Well the question is asking you to give the user some options, then calculate the result based on those options.

So you need to ask the user to pick a shape, ask the user to pick a measurement (perimeter or area), then ask the user to give the side length of the shape.

So do you know how to get input from the user? I would start simple and just ask the user to pick a shape and then print back their choice to them. You can build from there.

Robert D. Smith
Ranch Hand
Posts: 221
5
This seems fairly straightforward, although your instructions on finding the area of a triangle appear to be incorrect (if I understand it correctly).

My suggestion, to expound on Campbell's advice is to turn off the computer, find a pencil (or a pen depending on your confidence level) and start with figuring out the math, which will help you narrow down what you are going to need in the form of methods, properties, etc. Calculating the area of a square is different than calculating the area of a triangle is different than calculating the area of a circle. The same applies to figuring out the perimeter/circumference.

Going back to my comment on the triangle above. Your example appears to take only a single input for the area calculation, but the math is Area = (base * height) / 2;so you will need more than a single input. Once you have that laid out, the code will fall into place.

Regards,
Robert

Mike. J. Thompson
Bartender
Posts: 689
17
I think this may be working with regular shapes where all sides are equal, but its not explicitly stated. If that's true then the example has correctly calculated the area of a square.

Campbell Ritchie
Marshal
Posts: 56546
172
Robert D. Smith wrote: . . . Area = (base * height) / 2 . . .
You can work that out from sidea × sideb × sin(angleC) ÷ 2 but that requires use of the cosine rule to calculate the angle, if you have three sides. There is another method which seems to be called Heron's formula.
They call s semiperimeter, so you should use the perimeter to calculate it..