DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   Java (http://www.daniweb.com/forums/forum9.html)
-   -   How to do constructors (http://www.daniweb.com/forums/thread11105.html)

Ghost Sep 20th, 2004 1:45 am
How to do constructors
 
I don't understand constructors. Can anybod help?

The question is:

You are givenÂ* a classÂ*namedÂ* Clock that has one intÂ*instance variableÂ* called hours . Write a constructorÂ* with no parametersÂ* for the classÂ* Clock . The constructorÂ* should set hours to 12 .

I think the answer is:

hours=12;

but I'm worng. Please help.

P.S. One more question:

How do you compare Strings. I think you do:

compareTo(string1,string2)>1;

Am i correct? :rolleyes:

Thanks

Ghost Sep 20th, 2004 3:23 am
Re: How to do constructors
 
I figured out the string stuff ( string1.compareTo(string2)>0; )

Can you just help me on constructors?

Thanks
-- C++

jerbo Sep 20th, 2004 4:41 pm
Re: How to do constructors
 
A constructor in essance is a method (with the same name as the Class) that is executed when an instance of the class is created.

For Example:

public class foo {

public int x;

// Constructor
public foo() {
// Do something like
this.x = 45;
}

}

So when you enter in your application:

foo x = new foo() // the constructor foo() is executed.

Ghost Sep 20th, 2004 9:44 pm
Re: How to do constructors
 
Thanks.

One more question:

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.

I think the answer is:

public Clock(int hours, boolean isTicking, int diff){
this.hours = hours;
this.isTicking = isTicking;
this.diff() = diff;
}


Thanks!

jerbo Sep 21st, 2004 10:10 am
Re: How to do constructors
 
Yep, that would be it.

jerbo Sep 21st, 2004 10:10 am
Re: How to do constructors
 
Opps......this.diff()....should be .......this.diff

Ghost Sep 25th, 2004 12:16 pm
Re: How to do constructors
 
i still get more errors

jerbo Sep 27th, 2004 11:26 am
Re: How to do constructors
 
Looking at your code:
public Clock(int hours, boolean isTicking, int diff){
    this.hours = hours;
    this.isTicking = isTicking;
    this.diff = diff;

You should have hours, isTicking, and diff defined as class variables too. I did not see that in your code snippet.

To prevent confusion between class variables, and local (method) variables, I add an underscore to my private variables, so that I don't get them confused......ie:
public class CLock {
  // Private variables to the class
  private int hours_;
  private isTicking_;
  private diff_;

    // Your constructor
    public Clock(int hours, boolean isTicking, int diff){
        this.hours_ = hours;
        this.isTicking_ = isTicking;
        this.diff_ = diff;
    } // End of Clock Constructor
} //End of Clock Class

Hoope this clarifies things. If not, submit what errors you are receiving and the code.

Ghost Oct 9th, 2004 3:05 am
Re: How to do constructors
 
My error message is:

Clock.java:11: incompatible types
found : int
required: java.lang.Integer
this.diff=diff;
^
1 error

Thanks


All times are GMT -4. The time now is 6:34 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC