• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Kai Jennings
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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



 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where in your written formula is there a cube root?
 
Kai Jennings
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 56529
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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?

     
    Paweł Baczyński
    Bartender
    Posts: 2077
    44
    Firefox Browser IntelliJ IDE Java Linux Spring
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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: 56529
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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?
     
    Jeff Karr
    Greenhorn
    Posts: 10
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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: 56529
    172
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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 .
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!