168k views
1 vote
A class named Clock has two instance variables: hours (type int) and isTicking (type boolean). Write a constructor that takes a reference to an existing Clock object as a parameter and copies that object's instance variables to the object being created. Your task: Write a Clock constructor that makes this possible. Just write this constructor -- don't write the whole class or any code outside of the class!

User Unclexo
by
3.1k points

1 Answer

3 votes

Answer:

The following are the code for the constructor.

public Clock (Clock c) //define constructor

{

hours = c.hours; // holding the value in the hour variable

isTicking = c.isTicking;// holding the value in the hour variable

}

Step-by-step explanation:

The following are the description of the code.

  • Firstly, we define the constructor 'Clock()' because the constructor name is the same as the class name and declares the object of the class that is 'c' in the parameter of the constructor.
  • Finally, copies the instance variables (which are already declared) of the object to that object that is formed.
User Andy Clement
by
3.3k points