944,110 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 2740
  • Java RSS
Sep 20th, 2004
0

How to do constructors

Expand Post »
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
Similar Threads
Reputation Points: 12
Solved Threads: 2
Posting Whiz
Ghost is offline Offline
352 posts
since Aug 2004
Sep 20th, 2004
0

Re: How to do constructors

I figured out the string stuff ( string1.compareTo(string2)>0; )

Can you just help me on constructors?

Thanks
-- C++
Reputation Points: 12
Solved Threads: 2
Posting Whiz
Ghost is offline Offline
352 posts
since Aug 2004
Sep 20th, 2004
0

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.
Reputation Points: 11
Solved Threads: 1
Junior Poster in Training
jerbo is offline Offline
84 posts
since Sep 2004
Sep 20th, 2004
0

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!
Reputation Points: 12
Solved Threads: 2
Posting Whiz
Ghost is offline Offline
352 posts
since Aug 2004
Sep 21st, 2004
0

Re: How to do constructors

Yep, that would be it.
Reputation Points: 11
Solved Threads: 1
Junior Poster in Training
jerbo is offline Offline
84 posts
since Sep 2004
Sep 21st, 2004
0

Re: How to do constructors

Opps......this.diff()....should be .......this.diff
Reputation Points: 11
Solved Threads: 1
Junior Poster in Training
jerbo is offline Offline
84 posts
since Sep 2004
Sep 25th, 2004
0

Re: How to do constructors

i still get more errors
Reputation Points: 12
Solved Threads: 2
Posting Whiz
Ghost is offline Offline
352 posts
since Aug 2004
Sep 27th, 2004
0

Re: How to do constructors

Looking at your code:
Java Syntax (Toggle Plain Text)
  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:
Java Syntax (Toggle Plain Text)
  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.
Reputation Points: 11
Solved Threads: 1
Junior Poster in Training
jerbo is offline Offline
84 posts
since Sep 2004
Oct 9th, 2004
0

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
Reputation Points: 12
Solved Threads: 2
Posting Whiz
Ghost is offline Offline
352 posts
since Aug 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Unsure of where to put classpath for mail.jar
Next Thread in Java Forum Timeline: '[Class] is not abstract..' error when i use 'implements'





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC