| | |
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
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
•
•
Join Date: Sep 2004
Posts: 84
Reputation:
Solved Threads: 1
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.
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.
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!
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!
•
•
Join Date: Sep 2004
Posts: 84
Reputation:
Solved Threads: 1
Looking at your code:
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:
Hoope this clarifies things. If not, submit what errors you are receiving and the code.
Java Syntax (Toggle Plain Text)
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:
Java Syntax (Toggle Plain Text)
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.
![]() |
Similar Threads
- Why constructors don't have return types (C++)
- Taking address of constructors?? (C++)
- C++ Tic Tac Toe using classes & operator overloading (C++)
- Confusion with constructors (Java)
- what is the effect of using constructors in the program? (Java)
Other Threads in the Java Forum
- Previous Thread: Unsure of where to put classpath for mail.jar
- Next Thread: '[Class] is not abstract..' error when i use 'implements'
| Thread Tools | Search this Thread |
android api applet application apps array arrays automation awt bidirectional binary birt bluetooth businessintelligence busy_handler(null) card class classes client code collision columns component constructor crashcourse database designadrawingapplicationusingjavajslider draw eclipse error errors eventlistener exception expand fractal game givemetehcodez graphics gui guidancer html ide image inetaddress integer intellij j2me java javadoc javafx javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia linux list loop machine map method methods mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle physics plazmic print problem program project radio recursion scanner server set sharepoint smart sms smsspam sort sortedmaps sql string subclass support swing textfield threads tree trolltech unlimited utility webservices windows





