I don't understand constructors. Can anybod help?

The question is:

You are given a classnamed Clock that has one intinstance 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

Dani AI

Generated

A constructor is a special method whose name matches the class, has no return type, and runs when an object is created. A no-argument constructor that sets the hours field to 12 must appear inside the Clock class; a standalone hours = 12; line is not a constructor.

For the three-parameter constructor error shown by ("found: int required: java.lang.Integer"): the class has diff declared as the wrapper type Integer while the constructor parameter is a primitive int. Options that fix the mismatch:

  • make the field primitive int so it accepts an int directly;
  • change the constructor parameter to Integer; or
  • convert the primitive to a wrapper explicitly (or rely on autoboxing if compiling with Java 5+).

Example of explicit conversion:

public Clock(int hours, boolean isTicking, int diff) {
    this.hours = hours;
    this.isTicking = isTicking;
    this.diff = Integer.valueOf(diff); // primitive -> Integer
}

For string checks: compareTo returns a negative number, zero, or a positive number to indicate ordering. Use compareTo(...) == 0 or, more clearly, equals(...) (or equalsIgnoreCase(...)) to test equality rather than testing compareTo > 0 unless ordering is intended.

A few practical notes that expand on 's advice: declare instance fields private, use this. to disambiguate constructor parameters from fields, or adopt a naming convention (underscore or m prefix) to avoid confusion. When using wrapper types beware of null—unboxing a null Integer to int throws NullPointerException. For authoritative references see the Oracle Java tutorial on constructors and on autoboxing, and the String API documentation:

Recommended Answers

All 8 Replies

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

Can you just help me on constructors?

Thanks
-- C++

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.

Thanks.

One more question:

You are given a classnamed 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!

Yep, that would be it.

Opps......this.diff()....should be .......this.diff

i still get more errors

Looking at your code:

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:

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.

My error message is:

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

Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.