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 5 of 5

Thread: Need help on objects

  1. #1
    Member
    Join Date
    Sep 2012
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need help on objects

    Hello everyone!

    I'm a relatively newcommer to the programming experience, with a bit of knowledge in C++ and beginners Java.

    I need help in an assignment of mine, which requires me to create a circle class and circle object, with a getradius method.

    My teacher has provided a sample code:

    public class Main
    {
    public static void main (String[] args)
    {
    // defines Circle object;
    // call helper(class) method to input the radius
    // instantiate the Circle object;
    // output the circumference using instance method
    // output the area using instance method
    }
    public static __________ getRadius()
    {
    // define a local radius variable
    // read in a value for radius
    // return the value
    }
    }
    -----Circle.java--------------
    public class Circle {

    double radius;

    public Circle(double rad)
    {
    radius = rad;
    }

    public double getRadius()
    {
    return radius;
    }

    public double getDiameter()
    {
    return 2.0*radius;
    }

    public double getCircumference()
    {
    return 2.0*Math.PI*radius;
    }

    public double getArea()
    {
    return Math.PI*radius*radius;
    }

    }

    My question is:

    1) the public class method is the actual circle object?
    2) how do i define a circle object?
    3) how do i instantiate the circle object?
    4) and what are instance methods? How do I output results using an instance method?


  2. #2
    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: Need help on objects

    1) the public class Circle is the class that will represent Circle objects, created by the constructor defined as public Circle(double rad)
    2) you define a circle object pretty much the same way you define any variable. You declare it, and define it. (or all-in-one) An example of such: Circle myObjectOfClassCircle = new Circle(42.0);
    3) To declare a variable, is to say one will exist. Like saying: int i; ...or in the case of your object: Circle myObject; ..This reserves space for your object, but does not actually make an object. Trying to use the variable named myObject at this point will cause a nullPointerException to be thrown. To define the variable, is to give the variable a value. Same thing as instantiate. This is when you bring an object into existance and store it in your variable. myObject = new Circle(34.0);
    4) an instance method is a method that belongs to the associated object, and through said object only. Browse this for more. The other side of the tracks has static methods which you can access by ClassName.methodName(); where instance methods would have to be called ClassName variableOfClassName = new ClassName(); and variableOfClassName.methodName(); where an actual instance(object) of the class is being used.

  3. #3
    Member
    Join Date
    Sep 2012
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help on objects

    So to define the circle object, I would define it by: class Circle = new Circle? I'm very confused, what are the arguments in new circle that you gave? The (42.0)?

    What does it mean to instantiate the circle object?

  4. #4
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Need help on objects

    Quote Originally Posted by alex067 View Post
    ...
    My question is:

    1) the public class method is the actual circle object?
    No, the public class named Circle is a class. All of that code is (presumably) in a file named Circle.java
    A class is not an object. It is kind of a "blueprint" that tells the Java compiler how to create an object and how to access the object's characteristics.


    Quote Originally Posted by alex067 View Post
    2) how do i define a circle object?
    3) how do i instantiate the circle object?
    Somewhere in your code (maybe in the main() function of the class that you are executing with the "java" command) you declare an object of that class and invoke its constructor.

    Since your Circle class constructor takes a double precision argument that will be the radius of the circle, then it can go like this:
    public class Whatever {
        public static void main(String [] args) {
            // A local variable within this main() function that will be
            // used to create the Circle object
            double rad;
     
            // Do "something" here to get a value for rad so that you can use it in the Circle constructor
            // Maybe define a static function named getRadiusFromUser in this class (not the Circle class).
            //
            // For preliminary testing you can just assign a value:
            rad = 1.23456;// Or whatever...
     
           // Then...
     
            // Create a Circle object with this radius:
            Circle circle = new Circle(rad);
            // Now you have a Circle with the value of radius that you gave it.

    Then, to retrieve the value of the radius, you can do something like:
     
            // Use the circle object method to retrieve the value of
            // its radius for the output summary.  (You could
            // use the local variable here, but, in general,
            // it might be better to make sure that the object
            // is what you think it should be.)
            //
            // Use the "dot" notation to access an instance method:
            double radi = circle.getRadius();

    To get the instance method to calculate the circumference:
            // Use the circle method to calculate the circumference
            // from its radius
            double circ = circle.calcCircumference();
    Similarly for the area.

    Finally, print the values of the variables:
            // Now print the summary of radius, circumference and area...
            System.out.printf("Radius        = %f\n", radi);
            System.out.printf("Circumference = %f\n", circ);
            System.out.printf("Area          = %f\n", area);



    Quote Originally Posted by alex067 View Post
    4) and what are instance methods? How do I output results using an instance method?
    They are functions defined in the class that do something with an object of that class.

    You can do it as I showed it above, using the object "dot" notation to store the value in a local variable, or you can just do something like
            System.out.printf("Area          = %f\n", circle.calcArea());

    Output:

    You entered 1.23456
    Radius        = 1.234560
    Circumference = 7.756969
    Area          = 4.788222


    Here's how it goes: With the Circle class an application can create (instantiate) as many Circle objects as you please. Each object has its own radius value (determined by the argument you feed to the constructor). For each object you can use that object's instance methods to calculate that particular object's area and circumference.


    Cheers!

    Z
    Last edited by Zaphod_b; September 10th, 2012 at 08:33 PM.

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Need help on objects

    Please read the forum rules. Duplicate posts are not allowed. Your other thread has been locked.

Similar Threads

  1. [Question] Objects instantiated within objects.
    By Xerosigma in forum Object Oriented Programming
    Replies: 6
    Last Post: April 25th, 2012, 10:53 AM
  2. WORKIN WIT 3D OBJECTS
    By susheela in forum Java Theory & Questions
    Replies: 3
    Last Post: December 12th, 2011, 08:20 AM
  3. Vectors and objects
    By Matty Alan in forum Java Theory & Questions
    Replies: 3
    Last Post: May 10th, 2011, 11:44 AM
  4. New to Objects...
    By Java Neil in forum What's Wrong With My Code?
    Replies: 17
    Last Post: March 24th, 2011, 07:00 AM
  5. Objects
    By chronoz13 in forum Java Theory & Questions
    Replies: 5
    Last Post: January 20th, 2010, 12:50 PM