| | |
Cone Formula
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Sep 2008
Posts: 35
Reputation:
Solved Threads: 0
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
<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
You just need to specify the 1/3 so that it uses floating point division instead of int division, so change it to 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.
1.0/3.0 or 1/3d java Syntax (Toggle Plain Text)
1/3d * Math.PI * height * Math.pow(radius, 2);
Last edited by Ezzaral; Dec 17th, 2008 at 5:39 pm.
•
•
Join Date: Sep 2008
Posts: 35
Reputation:
Solved Threads: 0
•
•
•
•
You just need to specify the 1/3 so that it uses floating point division instead of int division, so change it to1.0/3.0or1/3dYou 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.java Syntax (Toggle Plain Text)
1/3d * Math.PI * height * Math.pow(radius, 2);
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?
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.
and when you have questions, come back and post them along with your code.
•
•
Join Date: Sep 2008
Posts: 35
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Nov 2008
Posts: 332
Reputation:
Solved Threads: 54
Generics in the Java Programming Language
Gilad Bracha
July 5, 2004
http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf
Gilad Bracha
July 5, 2004
http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf
•
•
Join Date: Sep 2008
Posts: 35
Reputation:
Solved Threads: 0
•
•
•
•
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......
•
•
Join Date: Nov 2008
Posts: 332
Reputation:
Solved Threads: 54
Your Test class, when you using generics
complete code of GeoSolids class
example
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.
java Syntax (Toggle Plain Text)
public class Test { public static void main(String[] args) { GeoSolids circle = new Sphere(5); System.out.println(circle); /// and GeoSolids[] solid = new GeoSolids[3]; solid[0] = new Sphere(5); solid[1] = new Cone(5,5); solid[2] = new Cylinder(5,5); for (int i = 0; i < 3; i++) { System.out.println(solid[i]); } } }
java Syntax (Toggle Plain Text)
public abstract class GeoSolids { public abstract double getVolume(); public abstract double getArea(); }
java Syntax (Toggle Plain Text)
public class Sphere extends GeoSolids { int radius; // konstruktor public Sphere(int radius) { this.radius = radius; } //// override abstract methods } public String toString(){ return ///name of shape, input parameters, volume,area } }
All code you have wrote . (some changes - and generic program is ready.
•
•
Join Date: Nov 2008
Posts: 332
Reputation:
Solved Threads: 54
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.
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.
![]() |
Similar Threads
Other Threads in the Java Forum
- Previous Thread: Serialization/Deserialization Error
- Next Thread: help in sqlite database locked problem??
Views: 2572 | Replies: 10
| Thread Tools | Search this Thread |
Tag cloud for Java
android api apple applet application apps arguments array arrays automation awt binary bluetooth businessintelligence busy_handler(null) card chat class classes client code collision component constructor database draw eclipse error event eventlistener exception file fractal game givemetehcodez graphics gui helpwithhomework html ide image input integer j2me jar java javafx javamicroeditionuseofmotionsensor javaprojects jmf jni jpanel jtree julia link linux list loop machine map method methods mobile netbeans newbie nls number object oracle parsing plazmic print problem program programming project recursion scanner screen server set sharepoint size smart sms socket sort sortedmaps sql string swing test textfield threads time transfer tree unlimited webservices windows






