| | |
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 |
911 actionlistener addball addressbook android applet application apps array automation binary bluetooth businessintelligence button card character class client code collision component consumer crashcourse css csv database desktop eclipse ee error fractal free ftp game givemetehcodez graphics gui html image integration j2me japplet java javaarraylist javac javadoc javaee javafx javaprojects jni jpanel julia jvm linked linux loan mac method migrate mobile netbeans objects online oriented phone physics printf problem program programming project projects radio recursion replaydirector reporting researchinmotion rotatetext rsa scanner se server service set sms software sort sql swing test textfield textfields threads time tree trolltech ubuntu update utility windows





