Win a copy of Spark in Action this week in the Open Source Projects forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Bear Bibeault
• Ron McLeod
• Jeanne Boyarsky
• Paul Clapham
Sheriffs:
• Tim Cooke
• Liutauras Vilda
• Junilu Lacar
Saloon Keepers:
• Tim Moores
• Stephan van Hulst
• Tim Holloway
• fred rosenberger
• salvin francis
Bartenders:
• Piet Souris
• Frits Walraven
• Carey Brown

program to calculate area of hexagon based on size (input)

Greenhorn
Posts: 9
can someone please help me figure out what i'm doing wrong with this program?
i'd like to (1) fix the equation for the hexagon, i keep writing it incorrectly and (2) change this into a method that I can call for the side input

Marshal
Posts: 69737
277
Welcome to the Ranch

What is the formula for area of a hexagon? I presume you mean a regular hexagon, the sort you can draw with one circle and six points with the same pair of compasses.
Before you try that Math.pow invocation, get the computer to print 1/3.

Kai Jennings
Greenhorn
Posts: 9

Campbell Ritchie wrote:Welcome to the Ranch

What is the formula for area of a hexagon? I presume you mean a regular hexagon, the sort you can draw with one circle and six points with the same pair of compasses.
Before you try that Math.pow invocation, get the computer to print 1/3.

Thank you,

The formula for finding the area of a hexagon is Area = (3√3 s2)/ 2 where s is the length of a side of the regular hexagon.

Yes, just a regular hexagon.

author
Posts: 23877
142
• 1

Kai Jennings wrote:
Thank you,

The formula for finding the area of a hexagon is Area = (3√3 s2)/ 2 where s is the length of a side of the regular hexagon.

Yes, just a regular hexagon.

First, what Campbell is trying to tell you is that an integer one divided by an integer three doesn't yield what you want. Or at least, do so, and remain accurate enough to provide an answer you want.

Second, while you got the formula right, you implemented it wrong. So, even after you fix the issue pointed out by Campbell, you have another issue related to the formula implementation.

Henry

Kai Jennings
Greenhorn
Posts: 9
Hmm, made some changes but still not sure what I'm doing wrong. The answer I'm getting now is different but still not right.

Bartender
Posts: 3323
86
Where in your written formula is there a cube root?

Kai Jennings
Greenhorn
Posts: 9

Tony Docherty wrote:Where in your written formula is there a cube root?

Ah, thank you. I'm new to Java and new to math as well. The code I have now works... Going to try to change it so there is a method to call.

Campbell Ritchie
Marshal
Posts: 69737
277
• 1
Not at all happy with it.
• 1: Forget there is such a thing as a float. Use doubles only, please.
• 2: Avoid Math#pow if you can. There is something much better which you will find in the Math class documentation.
• 3: Get as much code out of the main method as possible. Create a RegularHexagon class with getArea and getPerimeter methods.
•
Kai Jennings
Greenhorn
Posts: 9

Campbell Ritchie wrote:Not at all happy with it.

• 1: Forget there is such a thing as a float. Use doubles only, please.
• 2: Avoid Math#pow if you can. There is something much better which you will find in the Math class documentation.
• 3: Get as much code out of the main method as possible. Create a RegularHexagon class with getArea and getPerimeter methods.

• Thanks, I followed your suggestions and now have the following code with a method called that gets the area of the hexagon. Can you please explain more in depth what you mean by a (1) RegularHexagon class, (2) getArea method, and (3) getPerimeter methods?

Bartender
Posts: 2230
63
• 1
Instead of writing Math.pow(a, 2) you might consider a * a.
Well, I would write static final double factor = 3 * Math.sqrt(3) / 2
and then return factor * a * a;
I'm unsure if it would work faster but this would look kind of cleaner to me.

Campbell Ritchie
Marshal
Posts: 69737
277

Kai Jennings wrote: . . .

Thanks, I followed your suggestions

You're welcome
But you have only partially followed what I suggested.

Can you please explain more in depth what you mean by a (1) RegularHexagon class, (2) getArea method, and (3) getPerimeter methods?
. . .

A RegularHexagon class is one which represents a regular hexagon. It would have getArea and getPerimeter methods which return the area and perimeter respectively. It would have fields holding the requisite dimensions.
What more should I explain? I presume you know how to write classes representing real‑life objects?

Greenhorn
Posts: 10
• 1
you have too take care of the throw using input.next or input.nextLine

if(side = 0)
{
input.nextLine; //dicard the incorrect input data for the scanner
System.out.println("Error, division by zero.");
continue labelOne; //place your label where-ever the program will re-begin.
}

else
{
System.out.print(area);
}

three things,

1. I'm not sure if brackets are required for the else statement if there is only one statement,
I always put them anyways.
2. You should put area as it refers to an object as the argument using the toString method
that is implicity used by the respected class, I wouldn't put the calculation as what you
print out.
3. I am a novice java programmer, and programmer in general, so I really want others to
read my post for any possible errors or if I have the wrong idea.

I post this because I'm quite confident that I'm either right or close to correctness on this,

hope I could help in some way.

Campbell Ritchie
Marshal
Posts: 69737
277
• 1

Pawel Pawlowicz wrote: . . . I'm unsure if it would work faster but this would look kind of cleaner to me.

I think x * x probably would execute faster than Math.pow(x, 2) because there is no need to calculate log(x). But maybe there is an optimisation which uses x * x .

 Men call me Jim. Women look past me to this tiny ad: Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton