Cone Formula

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Sep 2008
Posts: 35
Reputation: dnmoore is an unknown quantity at this point 
Solved Threads: 0
dnmoore dnmoore is offline Offline
Light Poster

Cone Formula

 
0
  #1
Dec 17th, 2008
have to creat a geosolid class that has 3 sub class i choose sphere cylinder and cone, i got the first two to work but my cone class works except when it calculates the volume it keep getting 0 no matter what number is use for the hieght and radius

<code>
import java.util.Scanner;
import java.text.DecimalFormat;

public class Cone extends GeoSolids
{
double vol, surf_area;
int size = 0;

public Cone(int radi, int h)
{
super(radi, h);

}

public double calcV()
{
vol = 1/3 * Math.PI * height * Math.pow(radius, 2);
return vol;
}

public double calcSurf_A()
{
surf_area = Math.PI * radius * size
+ Math.PI * Math.pow(radius, 2);
return surf_area;
}


public String toString()
{
DecimalFormat fmt = new DecimalFormat("0.##");
return super.toString() + "\nThe volume is " + fmt.format(vol)
+ "\nThe surface area is " + fmt.format(surf_area);
}
}

</code>

cna some please help me fix this problem
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,511
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 522
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Cone Formula

 
0
  #2
Dec 17th, 2008
You just need to specify the 1/3 so that it uses floating point division instead of int division, so change it to 1.0/3.0 or 1/3d
  1. 1/3d * Math.PI * height * Math.pow(radius, 2);
You were getting zero because 1/3 as an int is zero, causing the entire expression to yield zero. Always keep that in mind when mixing integer and floating point primitives in expressions.
Last edited by Ezzaral; Dec 17th, 2008 at 5:39 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 35
Reputation: dnmoore is an unknown quantity at this point 
Solved Threads: 0
dnmoore dnmoore is offline Offline
Light Poster

Re: Cone Formula

 
0
  #3
Dec 17th, 2008
Originally Posted by Ezzaral View Post
You just need to specify the 1/3 so that it uses floating point division instead of int division, so change it to 1.0/3.0 or 1/3d
  1. 1/3d * Math.PI * height * Math.pow(radius, 2);
You were getting zero because 1/3 as an int is zero, causing the entire expression to yield zero. Always keep that in mind when mixing integer and floating point primitives in expressions.


thank you
maybe ur.ll be able ot help me with the next part how would i be able to convert the whole program into a gui the easiest way?
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,511
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 522
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Cone Formula

 
1
  #4
Dec 17th, 2008
Well, I suppose you would start with the Swing tutorial: http://java.sun.com/docs/books/tutor...ing/index.html
and when you have questions, come back and post them along with your code.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 35
Reputation: dnmoore is an unknown quantity at this point 
Solved Threads: 0
dnmoore dnmoore is offline Offline
Light Poster

GeoSolids

 
0
  #5
Dec 17th, 2008
ok i guess i should specify, i know how ot create gui's i was just having a hard time with this one
one more thing
for my parent class GeoSolid we have to create a generic geometric solid
i dont think I did that right may you look at my code and give some tips or pointers on how to fix it ?

<code>
import java.util.Scanner;
import java.text.DecimalFormat;


public class GeoSolids
{
int radius;
int height;

public GeoSolids (int radi, int h)
{
radius = radi;
height = h;
}

public GeoSolids ()
{
radius = 0;
height = 0;
}

public void setRadius(int radi)
{
radius = radi;
}

public double getRadius()
{
return radius;
}

public void setHeight(int h)
{
height = h;
}

public double getHeight()
{
return height;
}

public String toString()
{
DecimalFormat fmt = new DecimalFormat("0.##");
String result = "The radius is " + fmt.format(radius) + "\nThe height is " + fmt.format(height);
return result;

}
}

</code>

****i have attached the complete files for my program just in case you had any questions about why I did some things.
thanks again for all your help
Last edited by dnmoore; Dec 17th, 2008 at 7:17 pm. Reason: different name because it relates to a different problem using mostly the same coding
Attached Files
File Type: zip Final_Project.zip (6.3 KB, 1 views)
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 332
Reputation: quuba is on a distinguished road 
Solved Threads: 54
quuba quuba is offline Offline
Posting Whiz

Re: Cone Formula

 
1
  #6
Dec 17th, 2008
Generics in the Java Programming Language
Gilad Bracha
July 5, 2004
http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 35
Reputation: dnmoore is an unknown quantity at this point 
Solved Threads: 0
dnmoore dnmoore is offline Offline
Light Poster

Re: Cone Formula

 
0
  #7
Dec 17th, 2008
Originally Posted by quuba View Post
Generics in the Java Programming Language
Gilad Bracha
July 5, 2004
http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf


when i meant generic i meant things that all my geo solids have in common thats what im having problems with the most......
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 332
Reputation: quuba is on a distinguished road 
Solved Threads: 54
quuba quuba is offline Offline
Posting Whiz

Re: Cone Formula

 
0
  #8
Dec 17th, 2008
Your Test class, when you using generics
  1. public class Test {
  2.  
  3. public static void main(String[] args) {
  4. GeoSolids circle = new Sphere(5);
  5. System.out.println(circle);
  6.  
  7. /// and
  8.  
  9. GeoSolids[] solid = new GeoSolids[3];
  10. solid[0] = new Sphere(5);
  11. solid[1] = new Cone(5,5);
  12. solid[2] = new Cylinder(5,5);
  13. for (int i = 0; i < 3; i++) {
  14. System.out.println(solid[i]);
  15. }
  16. }
  17. }
complete code of GeoSolids class
  1. public abstract class GeoSolids {
  2.  
  3. public abstract double getVolume();
  4.  
  5. public abstract double getArea();
  6. }
example
  1. public class Sphere extends GeoSolids {
  2.  
  3. int radius;
  4. // konstruktor
  5. public Sphere(int radius) {
  6. this.radius = radius;
  7.  
  8. }
  9.  
  10. //// override abstract methods
  11.  
  12. }
  13. public String toString(){
  14. return ///name of shape, input parameters, volume,area
  15. }
  16. }
you don't need store in shape classes volume and area, always you can get them : getVolume() or getArea().
All code you have wrote . (some changes - and generic program is ready.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 35
Reputation: dnmoore is an unknown quantity at this point 
Solved Threads: 0
dnmoore dnmoore is offline Offline
Light Poster

Re: Cone Formula

 
0
  #9
Dec 17th, 2008
ok so if i get and set the volume and surface area in the GeoSolid class
as an abstract do i still need to put the formulas in the subclasses since the 3 geosolids formulas are diff?
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 332
Reputation: quuba is on a distinguished road 
Solved Threads: 54
quuba quuba is offline Offline
Posting Whiz

Re: Cone Formula

 
0
  #10
Dec 18th, 2008
If functions declared inside GeoSolids-class are abstract ,then
we must implement them in not-abstract subclasses of GeoSolids-class.
Sphere-class is not abstract,
then we must override functions getVolume() and getSurf_Area().
Also in Cone-class and Cylinder-class. For all shape math. formula is different.
I little simplified your classes.
Write the other two classes in the same way from scratch.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 2572 | Replies: 10
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC