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: Help me I am trying to get this code to work.

  1. #1
    Member
    Join Date
    Dec 2012
    Posts
    31
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Help me I am trying to get this code to work.

    Here is what my compiler is telling me: GeometryCalculator.java:20: error: constructor Sphere in class Sphere cannot be applied to given types;
    Sphere sp = new Sphere(r);
    ^
    required: no arguments
    found: double
    reason: actual and formal argument lists differ in length


    import java.util.Scanner;
     
    /**
       This is a test driver for the Sphere, Cylinder, and Cone class
       This approach is more object-oriented than the previous one because
       it clearly separates the duties of each class.
    */
    public class GeometryCalculator
    {
       public static void main(String[] args)
       {
          Scanner in = new Scanner(System.in);
     
          System.out.println("Please enter the radius: ");
          double r = in.nextDouble();
     
          System.out.println("Please enter the height: ");
          double h = in.nextDouble();
     
          Sphere sp = new Sphere(r);
          double v = sp.getVolume();
          double s = sp.getSurfaceArea();
     
          System.out.println("The volume of the sphere is: " + v);
          System.out.println("The surface area of the sphere is: " + s);
     
          Cylinder cy = new Cylinder(r, h);
          v = cy.getVolume();
          s = cy.getSurfaceArea();
     
          System.out.println("The volume of the cylinder is: " + v);
          System.out.println("The surface area of the cylinder is: " + s);
     
          Cone co = new Cone(r, h);
          v = co.getVolume();
          s = co.getSurfaceArea();
     
          System.out.println("The volume of the cone is: " + v);
          System.out.println("The surface area of the cone is: " + s);
       }
    }



    /**
       This class provides methods to compute the volume and surface area of a sphere.
       @author:
       @version:
    */
    public class Sphere
    {
     
       /**
          Constructs a Sphere object with input: radius.
          @param aRadius the radius
       */
    	 Sphereobject sphere = new Sphereobject();
     
     
     
     
       /**
          Computes the volume of a sphere.
          @return volume of sphere
        */
    	 public static double  Computevolume(double r){
     
    	 double Volume = 4/3*Math.PI*r * Math.pow(r,2);
     
    	 return Volume;
    }
     
       /**
          Computes the surface area of a sphere.
          @return surface area of a sphere
        */
     
    	public static double computeSurfaceArea(double r){
     
       double SurfaceArea = 4*Math.PI* Math.pow(r,2);
     
    	return SurfaceArea;
     
     
    	}
    		private double r;
    }


    public class Sphereobject
    {
    public static void main(String[] args)
    {
     
     
    	Sphere Sphere1 = new Sphere ();
    	}
    	}


    --- Update ---

    I left some comments so it would be easier to understand what I had to do but now I am completely confused as to what I have to do to fix this.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help me I am trying to get this code to work.

    Sphere sp = new Sphere(r);
    ^
    required: no arguments
    found: double
    reason: actual and formal argument lists differ in length
    The compiler can not find a constructor for the Sphere class that takes a double.
    Either remove the double arg (r) passed to the constructor
    or add a constructor to the Sphere class that takes a double as arg
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Dec 2012
    Posts
    31
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Help me I am trying to get this code to work.

    How would I do the latter? just put in (double r)?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help me I am trying to get this code to work.

    Take a look at the tutorial: Providing Constructors for Your Classes (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. actionListner code doesn't work ????
    By sciences in forum AWT / Java Swing
    Replies: 4
    Last Post: February 27th, 2012, 06:46 AM
  2. HI, could someone please tell me why my code doesnt work?
    By joelmeler in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 3rd, 2011, 01:37 AM
  3. Timer code does not work!
    By mariapatrawala in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 10th, 2010, 10:03 AM
  4. WHY this code dont work?
    By sibbe in forum Java Theory & Questions
    Replies: 7
    Last Post: December 9th, 2010, 10:47 AM
  5. please tell me why this code does not work
    By amr in forum Java Theory & Questions
    Replies: 9
    Last Post: December 6th, 2010, 06:46 PM