HI
problem:
Write the definition of a class Clock . The class 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 . You should also write a constructor that takes three parameters -- an int , a boolean , and another int . The constructor should set the instance variables to the values provided.


my code:

public class Clock (int time, boolean dog, int add)
{
hours = time;
isTicking = dog;
diff = new Integer (add);
}

I dont know what is wrong with this code, there was a similar question to this one except " you are given a class Clock..." and my code was same as one above except removing "class"

thanks!! really appreciated This is my first Java course this is really frustrating :D
thanks!

Recommended Answers

All 3 Replies

That's a (unholy) mix of a class definition with a constructor definition.

Example:

public class ClassName {
  Type var;
  public Classname (Type arg) {
    this.var = arg;
  }
}

There's also something wrong with your idea of declaring an initializing.
Variables always need to be declared before you can use them. This simply means: say what it is.
Type name;
When you do it like that, the pc knows you want to use a variable AND it knows what kind (type) it is in order to free enough memory.
Now your variable name is declared.

Initializing is giving a first value to the variable. This is not always necessary.

Black Box

> This is my first Java course this is really frustrating
It would be better if you did a bit of research / reading before jumping right into coding. Just trying out various things at random wouldn't push you any close to attaining enlightenment.

Read your textbook, try to understand what the error message says, look at a sample declaration of a constructor and see what really makes your constructor different from a working one. Analyzing your problems and approaching them in a well planned manner would be much more beneficial.

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.