• Post Reply Bookmark Topic Watch Topic
  • New Topic
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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

geometry in java

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am learning how to create classes, and I had to create a class with a program to test it that finds the volume and surface area of a sphere, cylinder, and cone.
I am having some problems- for my cone Volume, it is returning everything as 0. For the program, I have
counter = 1;

System.out.println("Cone Volume");

while (counter <= 10)
{
double radius = Math.random();
System.out.println("Radius: " + radius);
double h = Math.random();
System.out.println("H: " + h);
double coneVolume = yourTurnClass.coneVolume(radius,h);
System.out.println("answer: " + coneVolume);
counter++;
}

________

For the class I have:

public static double coneVolume(double radius, double h)
{

answer = (1/3*((Math.PI)*((radius*radius)*(h))));

return answer;
}

_________

Am I messing something up with pi?

Another problem I have is with coneSurface- I have formula:

public static double coneSurface(double radius, double h)
{

answer = (Math.PI*(radius*(Math.sqrt(h*h + radius*radius) + radius*radius)));
return answer;
}

and program

while (counter <= 10)
{
double radius = Math.random();
System.out.println("Radius: " + radius);
double h = Math.random();
System.out.println("H: " + h);
double coneSurface = yourTurnClass.coneSurface(radius,h);
System.out.println("answer: " + coneSurface);
counter++;
}

and my answers are coming out a bit off. Did I miss my formula up, or call something wrong?

My final problem is with the sphere volume- I have class

public static double sphereVolume(double radius)
{

answer = ((4/3)*(Math.PI)*(radius*radius*radius));
return answer;

}


and program

while (counter <= 10)
{
double radius = Math.random();
System.out.println("Radius: " + radius);
double sphereVolume = yourTurnClass.sphereVolume(radius);
System.out.println("answer: " + sphereVolume);
counter++;
}

_______

with answers slightly wrong again. Is there something I am doing wrong that is the same with all three of these?


THANK YOU SO MUCH FOR YOUR HELP!!!
 
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you look at the API for Math.random()?

It generates a double between 0 and 1. Most people multiply it by a number (say 10) to get a random number (say 0 through 10).

Your problems might stem from the numbers being too small to calculate a valid volume, surface, etc....

Just a thought...
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Therese, please Use Code Tags.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the problem is here: answer = (1/3*((Math.PI)*((radius*radius)*(h))));

your '1' is an int literal. your '3' is an int literal. when you have an int divided by an int, the result is...an int. 1 / 3 = 0 in integer division.

Try changing it to "answer = (1.0/3*((Math.PI)*((radius*radius)*(h))));


update: I see that same issue with your "4/3" as well...
 
Janeice DelVecchio
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's almost like Java failed out of elementary division. LOL
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Janeice DelVecchio wrote:It's almost like Java failed out of elementary division. LOL

No, it was given elementary division and stayed with what it was familiar with
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are plenty of times when you WANT the integer division. when combined with the modulus operator, you can do some cool stuff.
 
On top of spaghetti all covered in cheese, there was this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic