Why can't I state the values of the constructor??
I am trying to write a code where I state the height and radius and then calculates the surface area and volume of a code.
These are the formulas:
Surface = sqrt(r^2 + s^2)
Surface area = pi * r * s + pi * r^2
Volume = (1/3) * pi * r^2 * h
I think I wrote the class correctly, but I get a few errors that say the I am not using the instance variables height and radius. Then when I try to state a variable in the tester, it says that the constructor is undefined.
Here are the classes:
Code :
public class IceCreamCone
{
private double height; // Says value height is not used.
private double radius; // Says value radius is not used.
private double area;
private double volume;
// Receives parameters; calculates surface area and volume.
public void IceCreamCone(double h, double r)
{
height = h;
radius = r;
double s = Math.sqrt((r * r) + (h * h));
area = (Math.PI) * (r) * (s) * + (Math.PI) * (r * r);
volume = (1 / 3) * (Math.PI) * (r * r) * (h);
}
// Returns calculated area.
public double getSurfaceArea()
{
return area;
}
// Returns calculated volume.
public double getVolume()
{
return volume;
}
}
Code :
public class IceCreamtester
{
public static void main(String[] args)
{
IceCreamCone jean = new IceCreamCone(2,1); //says constructor is undefined.
}
}
Thanks!
Re: Why can't I state the values of the constructor??
I think you need a scanner for each variables.
Re: Why can't I state the values of the constructor??
I fixed a few things and now I can print the results, but now the results are wrong. What is wrong?
Code :
public class IceCreamCone
{
private double height;
private double radius;
private double area;
private double volume;
private double surface;
// Receives parameters; calculates surface.
public IceCreamCone(double h, double r)
{
height = h;
radius = r;
surface = Math.sqrt((radius * radius) + (height * height));
}
// Returns calculated area.
public double getSurfaceArea()
{
area = ((Math.PI) * (radius) * (surface) * + (Math.PI) * (radius * radius));
return area;
}
// Returns calculated volume.
public double getVolume()
{
volume = ((1 / 3) * (Math.PI) * (radius * radius) * (height));
return volume;
}
}
Code :
public class IceCreamtester
{
public static void main(String[] args)
{
IceCreamCone jean = new IceCreamCone(2,1); //says constructor is undefined.
System.out.println(jean.getSurfaceArea());
System.out.println("Expected value: 10.16");
System.out.println(jean.getVolume());
System.out.println("Expected value: 2.09");
}
}
Quote:
22.069106351866907
Expected value: 10.16
0.0
Expected value: 2.09
Re: Why can't I state the values of the constructor??
Quote:
area = ((Math.PI) * (radius) * (surface) * + (Math.PI) * (radius * radius));
This looks odd