![]() |
| ||
| 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 |
| ||
| Re: How to do constructors I figured out the string stuff ( string1.compareTo(string2)>0; ) Can you just help me on constructors? Thanks -- C++ |
| ||
| 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. |
| ||
| 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! |
| ||
| Re: How to do constructors Yep, that would be it. |
| ||
| Re: How to do constructors Opps......this.diff()....should be .......this.diff |
| ||
| Re: How to do constructors i still get more errors |
| ||
| Re: How to do constructors Looking at your code: public Clock(int hours, boolean isTicking, int 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 {Hoope this clarifies things. If not, submit what errors you are receiving and the code. |
| ||
| 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