I'm new on java is there any problem with my coding about float? because it became 0.0 and I didn't understand float much can you explain it? It happen on getFloat
This program to calculate cone volume
public class Cone_Volume
{
private int ri, hi, ivol;
private float rf, hf, fvol;
public Cone_Volume()
{
}
public Cone_Volume(int ri, int hi)
{
this.ri = ri;
this.hi = hi;
}
public Cone_Volume(float rf, float hf)
{
this.rf = rf;
this.hf = hf;
}
public int getInt()
{
int ivol = (int)((3.142f*(ri*ri)*hi)/3);
return ivol;
}
public float getFloat()
{
float fvol = (float)((3.142f*(ri*ri)*hi)/3);
return fvol;
}
public void Volume_I(int ri, int hi)
{
this.ri = ri;
this.hi = hi;
}
public void Volume_F(float rf, float hf)
{
this.rf = rf;
this.hf = hf;
}
}
this program is for main method for Cone_Volume
public class TestConeVol
{
public static void main(String args[])
{
Cone_Volume cone1 = new Cone_Volume (5,10);
Cone_Volume cone2 = new Cone_Volume (3.5f,7.9f);
Cone_Volume cone3 = new Cone_Volume ();
Cone_Volume cone4 = new Cone_Volume ();
cone3.Volume_I (10,30);
cone4.Volume_F (9.4f,5.8f);
System.out.println("Volume of cone one : " + cone1.getInt());
System.out.println("Volume of cone two : " + cone2.getFloat());
System.out.println("Volume of cone three : " + cone3.getInt());
System.out.println("Volume of cone four : " + cone4.getFloat());
}
}
Output:
Volume of cone one : 261
Volume of cone two : 0.0
Volume of cone three : 3141
Volume of cone four : 0.0
In the method getFloat
you mistakenly used ri
and hi
instead of rf
and hf
(respectively).
The important lesson here is to use meaningful names ;)