Need Help


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: Need Help

  1. #1
    Join Date
    Sep 2004
    Posts
    5

    Need Help

    Guys/Gals I have the core of the program finished but I just need help creating a default constructor. Here are the details and my code will follow.


    Write a program that instantiates a couple of Spheres - one should use the default constructor and one should use the 1-param constructor. Display the volume and surface area of each sphere object.

    Attribute for radius. This should be able to hold decimal values.

    Default constructor. Sets a default radius of 1.

    One-Parameter constructor. Sets the radius to whatever input is passed into this constructor.

    getVolume() method - calculates and returns the volume of the sphere.

    getSurfaceArea() method - calculates and returns the surface area of the sphere.

    //This is my class

    import java.awt.*;

    public class Sphere
    {

    double Radius;

    }

    public void print() {
    System.out.println("Enter the Radius:");// Enters a radius saves it in variable and puts it into formula
    Radius = SavitchIn.readLineDouble();// Stores User Input

    }
    public String getVolume()
    {
    return "Volume: " + 4/3 * Math.PI * Math.pow(Radius, 3);// Takes Radius and puts it into formula that displays the volume
    }

    public String getSurfaceArea()
    {
    return "Surface Area: " + 4 * Math.PI * Math.pow(Radius, 2);//// Takes Radius and puts it into formula that displays the Surface Area
    }
    }

    // This is to test it


    class Testsphere
    {
    public static void main(String[] args)
    {
    Sphere mySphere = new Sphere ();
    mySphere.print();
    System.out.println(mySphere.getSurfaceArea());// Prints out the Surface Area
    System.out.println(mySphere.getVolume());// Print out the Volume

    }
    }


    Any help would be greatly appreciated

  2. #2
    Join Date
    Feb 2004
    Posts
    541
    Well what is it that you're having trouble with? Do you know how to make constructors and what they're for? At the minute your code only has an (implicit) default constructor that does nothing. The compiler adds it in for you when you compile the code. You need to write the two constructors yourself.

    As an asside, the method print() doesn't seem like a good idea. The sphere shouldn't communicate with the user. The main method should communicate with the user and get the radius size, and then should make a Sphere object using that value.

    One last thing, variable names should start with a small letter, not a capital. Capital letters should be used for classes and interfaces only. This makes it easier to tell which are class names and which are variable names.

  3. #3
    Join Date
    Sep 2004
    Posts
    150
    You appear to have an extra close bracket under

    " double Radius; " near the beginning of your class which will probably cause it to not compile.

    For your original question:

    Default contructors look something like this


    public Shere()
    {
    // default settings (if any) for sphere go here
    // notice that it takes no arguments
    // this part can be completely empty for simple programs
    }


    You do not seem to have one with "one" parameter either, which would look something like this:

    public Sphere(double radiusRequested)
    {
    // here you can add your own settings
    // or call Sphere() to load the defaults

    radius = radiusRequested;

    }


    These should go in your class after the "double radius;" line. That is usually a good place to put constructors.

  4. #4
    Join Date
    Sep 2004
    Posts
    5
    Thanks guys, I' am going to try to figure it out to my best ability.

  5. #5
    Join Date
    Sep 2004
    Posts
    5
    Thanks guys, I' am going to try to figure it out to my best ability.

    I' am really getting confused at this point and so far i have this. Can you please state if I' am going in the right direction

    class Sphere

    {
    //decares radius(variable) as double
    double radius;


    // default constructor, set the radius to be 1 automatically
    public Sphere()
    {

    radius = 1;
    }



    // Setting the one parameter constructor

    public Sphere(double radiusRequested)
    {



    radius = radiusRequested;
    }





    }


    **** Now where would I put my code i wrote earlier... I tried adding it in the one parameter constructor but i constantly get errors...

    public void print() {
    System.out.println("Enter the Radius:");
    Radius = SavitchIn.readLineDouble();

    }
    public String getVolume()
    {
    return "Volume: " + 4/3 * Math.PI * Math.pow(Radius, 3);// Takes Radius and puts it into formula that displays the volume
    }

    public String getSurfaceArea()
    {
    return "Surface Area: " + 4 * Math.PI * Math.pow(Radius, 2);//// Takes Radius and puts it into formula that displays the Surface Area
    }

  6. #6
    Join Date
    Feb 2004
    Posts
    541
    All of the other code is for other methods. It doesn't go inside a constructor. It goes outside of it, but inside the class scope (the first and last curly brackets).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center