Results 1 to 4 of 4
  1. #1
    jean28 is offline Member
    Join Date
    Aug 2012
    Posts
    16
    Rep Power
    0

    Default 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:

    Java 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;
    	}
    }
    Java Code:
    public class IceCreamtester 
    {
    	public static void main(String[] args)
    	{
    		IceCreamCone jean = new IceCreamCone(2,1); //says constructor is undefined.
    		
    	
    	}
    
    }
    Thanks!

  2. #2
    jean28 is offline Member
    Join Date
    Aug 2012
    Posts
    16
    Rep Power
    0

    Default 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?

    Java 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;
    	}
    }

    Java 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");
    	
    	}
    
    }
    22.069106351866907
    Expected value: 10.16
    0.0
    Expected value: 2.09

  3. #3
    SJF
    SJF is offline Senior Member
    Join Date
    Oct 2012
    Posts
    108
    Rep Power
    0

    Default Re: Why can't I state the values of the constructor??

    Java Code:
            IceCreamCone jean = new IceCreamCone(2d , 1d );

  4. #4
    SJF
    SJF is offline Senior Member
    Join Date
    Oct 2012
    Posts
    108
    Rep Power
    0

    Default Re: Why can't I state the values of the constructor??

    Also, look at at your volume code!

    HINT:
    Java Code:
    		double oneThird = 1/3;					//double = int/int  ~ (double)(int/int)
    		System.out.println(oneThird);			// 0.0!
    		oneThird = 1d/3;						//double = double/int ~ double/double
    		System.out.println(oneThird);			// 0.33333333333!

Similar Threads

  1. Replies: 5
    Last Post: 08-11-2012, 09:50 AM
  2. Replies: 2
    Last Post: 10-31-2011, 09:03 PM
  3. Assigning array values to objects in a constructor
    By Dreaming in forum New To Java
    Replies: 8
    Last Post: 10-25-2011, 06:17 PM
  4. Replies: 2
    Last Post: 02-14-2011, 02:27 PM
  5. dynamically populate the city combo box based on the values of state combo +ajax+jsp
    By sandy1000 in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 12-29-2010, 10:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •