Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

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

  1. #1
    Member
    Join Date
    Aug 2012
    Posts
    67
    Thanks
    4
    Thanked 0 Times in 0 Posts

    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:

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

    public class IceCreamtester 
    {
    	public static void main(String[] args)
    	{
    		IceCreamCone jean = new IceCreamCone(2,1); //says constructor is undefined.
     
     
    	}
     
    }

    Thanks!


  2. #2
    Junior Member
    Join Date
    Sep 2012
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    I think you need a scanner for each variables.

  3. #3
    Member
    Join Date
    Aug 2012
    Posts
    67
    Thanks
    4
    Thanked 0 Times in 0 Posts

    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?

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


    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

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

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

    area = ((Math.PI) * (radius) * (surface) * + (Math.PI) * (radius * radius));
    This looks odd

Similar Threads

  1. which class has a default constructor? (Req. guidance on constructor)
    By DragBall in forum Java Theory & Questions
    Replies: 1
    Last Post: June 27th, 2012, 04:42 PM
  2. [SOLVED] I need help with saving state data.
    By JonLane in forum Collections and Generics
    Replies: 6
    Last Post: February 27th, 2012, 03:48 AM
  3. Creating an array in constructor ... defaults all values?
    By mwebb in forum Object Oriented Programming
    Replies: 2
    Last Post: February 19th, 2012, 04:06 PM
  4. [SOLVED] Class constructor default values
    By srs in forum Java Theory & Questions
    Replies: 3
    Last Post: November 25th, 2010, 09:51 PM
  5. State Variables interaction with outside classes?
    By Ace Coder in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 24th, 2010, 03:52 PM