Need help with writing a constructor!


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Need help with writing a constructor!

  1. #1
    Join Date
    Jun 2005
    Posts
    7

    Question Need help with writing a constructor!

    Hey Guys!

    I'm learning to program in Java and need some help here.

    Problem 1:
    You are given a class named Clock that has three instance variables: One of type int called hours , another of type boolean called isTicking , and the last one of type Integer called diff . Write a constructor for the class Clock that takes three parameters -- an int , a boolean , and another int . The constructor should set the instance variables to the values provided.

    My code:
    public Clock (int time, boolean dog (false), Integer add)
    {
    hours = time;
    isTicking = dog;
    Integer diff = new Integer add;
    }

    I'm getting compiler error messages and I need to know what's wrong. I'm not sure if it's the boolean or Integer that's giving me the error.

    Problem 2:
    A class named Clock has two instance variables: hours (type int ) and isTicking (type boolean) . Write a constructor that takes a reference to an existing Clock object as a parameter and copies that object's instance variables to the object being created.

    Your task: Write a Clock constructor that makes this possible. Just write this constructor -- don't write the whole class or any code outside of the class!

    I don't have a code written up for this one because I don't know what to do.

    Any advice/help are appreciated and thanks in advance.
    -Falcone

  2. #2
    Join Date
    May 2005
    Posts
    6
    Your first constructor shouldn't redeclare diff as an Integer. That should be the only problem, unless you actually have (false) written in. Your second problem is answered like this

    public Clock(Clock existingClock){
    hours = existingClock.hours;
    isTicking = existingClock.isTicking;
    }

    It would be better to use accessor methods in this case, but i just used the direct instance fields for simplicity. Ask if you have any questions.

  3. #3
    Join Date
    Jun 2005
    Posts
    7
    I'm assuming the existingClock is something that you just came up with and that it could have been anything else?

    Also I found out what was wrong with the first code. You were right with not having to redeclare diff as an Integer, but I still got errors when I tried to compile it. Here's the correct code:

    public Clock (int time, boolean dog, int add)
    {
    hours = time;
    isTicking = dog;
    diff = new Integer (add);
    }

    As you can see from this one and the code I had in my first post, I should have putted int add instead of Integer add . Also, the (false) part was actually written into the code by me when it really shouldn't have been.

    Thanks for the help!

  4. #4
    Join Date
    May 2005
    Posts
    6
    exisitingClock is just a variable name I came up with. You can use anything else as long as it fits correct naming standards. I missed that whole integer thing the first time..sorry. You can still pass in an Integer object if you want too and just do diff = add, or you could make diff an int, or you could do it the way you have it. It just depends on what you want. Usually, I use the primitive int's because it is easier to get and assign values to the variables.

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