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

Thread: Java constructor/object

  1. #1
    Member
    Join Date
    Dec 2012
    Posts
    127
    My Mood
    Angelic
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Java constructor/object

    Could someone help me figure out why is the print out = 0.0? Thanks

     
    public class AnnualFuelUse
    {
        //Variables
        private int  distanceDay;
        private double amountAYear , distanceAnnual ;
     
        //Constructor
        AnnualFuelUse(int distance20Day, double timePerYear)
        {
            distanceDay = distance20Day;
            amountAYear = timePerYear;
            distanceAnnual= 0.00 ;
        }
     
        //Distance annual projection
        public void annualDis()
        {
        distanceAnnual = (distanceDay + amountAYear);
        }
        //Return distance annual
        public double getAnnualDis()
        {
            return distanceAnnual;
        }
    }

    public class AnnualFuelUseTester
    {
        public static void main(String[]args)
        {
         AnnualFuelUse annualProject = new AnnualFuelUse(1110,18.25);
     
         System.out.println(annualProject.getAnnualDis());
    }
    }


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Java constructor/object

    So, what did you expect or intend it to print?

    I notice that getAnnualDis() - the thing that is printed - returns distanceAnnual and that there are exactly two places where this variable is given a value: the constructor and the annualDis() method. Use System.out.println() to see when or if the value of distanceAnnual changes.

    public class AnnualFuelUse
    {
        //Variables
        private int  distanceDay;
        private double amountAYear , distanceAnnual ;
     
        //Constructor
        AnnualFuelUse(int distance20Day, double timePerYear)
        {
            distanceDay = distance20Day;
            amountAYear = timePerYear;
            System.out.println("Constructor setting distanceAnnual to 0.0");
            distanceAnnual= 0.00 ;
        }
     
        //Distance annual projection
        public void annualDis()
        {
            System.out.println("annualDis setting distanceAnnual to " + (distanceDay + amountAYear));
            distanceAnnual = (distanceDay + amountAYear);
        }
        //Return distance annual
        public double getAnnualDis()
        {
            return distanceAnnual;
        }
    }

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

    Default Re: Java constructor/object

    What variable's value is being printed? Where does that variable get assigned a value?
    Is the code that assigns the variable a value executed before its value is printed?
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Member
    Join Date
    Dec 2012
    Posts
    127
    My Mood
    Angelic
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: Java constructor/object

    @pbrockway2, wouldn't the value of distanceAnnual change to what ever (distanceDay + amountAYear) is and then return that value? Could you explain why it return 0 ? Also, how can I make it display System.out.println("annualDis setting distanceAnnual to " + (distanceDay + amountAYear)); ? Thanks

    ** Is it because I haven't invoke the annualDis method?

    --- Update ---

    Quote Originally Posted by Norm View Post
    What variable's value is being printed? Where does that variable get assigned a value?
    Is the code that assigns the variable a value executed before its value is printed?
    The variable value that I'm trying to print is distanceAnnual in the getAnnualDis() method. This variable get assigned 0.00 in the constructor then it is assign the value of distanceDay + amountAYeear in the AnnualDis() method. - I think I haven't invoke the method where distanceAnnual is assigned a new value that is why.

    --- Update ---

    I got it now. Thanks you so much!

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

    Default Re: Java constructor/object

    haven't invoke the method where distanceAnnual is assigned a new value
    You need to call that method to have some value put into the variable before printing it.
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    maple1100 (January 21st, 2013)

  7. #6
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Java constructor/object

    wouldn't the value of distanceAnnual change to what ever (distanceDay + amountAYear) is and then return that value?
    It isn't a question of "wouldn't...?", it's a question of "does it ... or not?". In other words the task at this point is to see what is going on. The code I posted - which is just yours with a couple of lines added - should answer this question. Just add those two lines, compile and run.

    At that point you should be able to answer Norm's three questions.

    Could you explain why it return 0 ?
    Well, it returns zero because that's the value of distanceAnnual. Run the (ammended) code to see why. Or, if it's still not clear, describe what it does output.

    [Edit] Massively slow Well done! Yes, you have to call a method for it to do its work.

  8. The Following User Says Thank You to pbrockway2 For This Useful Post:

    maple1100 (January 21st, 2013)

Similar Threads

  1. Method calculate 2 object using constructor with parameter
    By DavidXCode in forum Object Oriented Programming
    Replies: 2
    Last Post: November 21st, 2012, 09:31 PM
  2. 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
  3. Java Constructor
    By BDuquense in forum Object Oriented Programming
    Replies: 1
    Last Post: June 6th, 2012, 07:43 PM
  4. Reading from ResultSet to Object and from object Object Array
    By anmaston in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 7th, 2011, 06:11 AM
  5. Object array with constructor
    By thedolphin13 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 8th, 2010, 11:02 AM