How to do constructors

Reply

Join Date: Aug 2004
Posts: 350
Reputation: Ghost is an unknown quantity at this point 
Solved Threads: 2
Ghost's Avatar
Ghost Ghost is offline Offline
Posting Whiz

How to do constructors

 
0
  #1
Sep 20th, 2004
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 350
Reputation: Ghost is an unknown quantity at this point 
Solved Threads: 2
Ghost's Avatar
Ghost Ghost is offline Offline
Posting Whiz

Re: How to do constructors

 
0
  #2
Sep 20th, 2004
I figured out the string stuff ( string1.compareTo(string2)>0; )

Can you just help me on constructors?

Thanks
-- C++
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 84
Reputation: jerbo is an unknown quantity at this point 
Solved Threads: 1
jerbo jerbo is offline Offline
Junior Poster in Training

Re: How to do constructors

 
0
  #3
Sep 20th, 2004
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 350
Reputation: Ghost is an unknown quantity at this point 
Solved Threads: 2
Ghost's Avatar
Ghost Ghost is offline Offline
Posting Whiz

Re: How to do constructors

 
0
  #4
Sep 20th, 2004
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!
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 84
Reputation: jerbo is an unknown quantity at this point 
Solved Threads: 1
jerbo jerbo is offline Offline
Junior Poster in Training

Re: How to do constructors

 
0
  #5
Sep 21st, 2004
Yep, that would be it.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 84
Reputation: jerbo is an unknown quantity at this point 
Solved Threads: 1
jerbo jerbo is offline Offline
Junior Poster in Training

Re: How to do constructors

 
0
  #6
Sep 21st, 2004
Opps......this.diff()....should be .......this.diff
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 350
Reputation: Ghost is an unknown quantity at this point 
Solved Threads: 2
Ghost's Avatar
Ghost Ghost is offline Offline
Posting Whiz

Re: How to do constructors

 
0
  #7
Sep 25th, 2004
i still get more errors
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 84
Reputation: jerbo is an unknown quantity at this point 
Solved Threads: 1
jerbo jerbo is offline Offline
Junior Poster in Training

Re: How to do constructors

 
0
  #8
Sep 27th, 2004
Looking at your code:
  1. public Clock(int hours, boolean isTicking, int diff){
  2. this.hours = hours;
  3. this.isTicking = isTicking;
  4. 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:
  1. public class CLock {
  2. // Private variables to the class
  3. private int hours_;
  4. private isTicking_;
  5. private diff_;
  6.  
  7. // Your constructor
  8. public Clock(int hours, boolean isTicking, int diff){
  9. this.hours_ = hours;
  10. this.isTicking_ = isTicking;
  11. this.diff_ = diff;
  12. } // End of Clock Constructor
  13. } //End of Clock Class

Hoope this clarifies things. If not, submit what errors you are receiving and the code.
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 350
Reputation: Ghost is an unknown quantity at this point 
Solved Threads: 2
Ghost's Avatar
Ghost Ghost is offline Offline
Posting Whiz

Re: How to do constructors

 
0
  #9
Oct 9th, 2004
My error message is:

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

Thanks
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC