Ask Question
31 July, 13:39

You are given a class named Clock that has one int instance variable called hours.

Write a constructor for the class Clock that takes one parameter, an int, and gives its value to hours.

+2
Answers (1)
  1. 31 July, 13:59
    0
    public Clock (int hours) {

    this. hours = hours;

    }

    Explanation:

    In Java programming language, Constructors are special methods that are called to initialize the variables of a class when a new object of the class is created with the new keyword. Consider the complete code for the class below;

    public class Clock {

    private int hours;

    public Clock (int hours) {

    this. hours = hours;

    }

    }

    In this example above, an object of this class can created with this statement Clock myclock = new Clock (6); This is a call to the constructor and passes a parameter (6) for hours
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “You are given a class named Clock that has one int instance variable called hours. Write a constructor for the class Clock that takes one ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers